forked from IGLOU-EU/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
81 lines (70 loc) · 2.29 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package formula
import (
"sort"
"sync"
"baliance.com/gooxml"
)
// SupportedFunctions returns a list of supported functions.
func SupportedFunctions() []string {
ret := []string{}
for k := range registered {
ret = append(ret, k)
}
for k := range registeredComplex {
ret = append(ret, k)
}
sort.Strings(ret)
return ret
}
// Function is a standard function whose result only depends on its arguments.
type Function func(args []Result) Result
// FunctionComplex is a function whose result depends on its arguments and the
// context that it's in. As an example, INDIRECT is a complex function so that
// INDIRECT("A1") which returns the value of the "A1" cell in a sheet can use
// the context to reach into the sheet and pull out required values.
type FunctionComplex func(ctx Context, ev Evaluator, args []Result) Result
var regLock sync.Mutex
var registered = map[string]Function{}
var registeredComplex = map[string]FunctionComplex{}
// RegisterFunction registers a standard function.
func RegisterFunction(name string, fn Function) {
regLock.Lock()
defer regLock.Unlock()
if _, ok := registered[name]; ok {
gooxml.Log("duplicate registration of function %s", name)
}
registered[name] = fn
}
// RegisterFunctionComplex registers a standard function.
func RegisterFunctionComplex(name string, fn FunctionComplex) {
regLock.Lock()
defer regLock.Unlock()
if _, ok := registeredComplex[name]; ok {
gooxml.Log("duplicate registration of function %s", name)
}
registeredComplex[name] = fn
}
// LookupFunction looks up and returns a standard function or nil.
func LookupFunction(name string) Function {
regLock.Lock()
defer regLock.Unlock()
if fn, ok := registered[name]; ok {
return fn
}
return nil
}
// LookupFunctionComplex looks up and returns a complex function or nil.
func LookupFunctionComplex(name string) FunctionComplex {
regLock.Lock()
defer regLock.Unlock()
if fn, ok := registeredComplex[name]; ok {
return fn
}
return nil
}