-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.go
53 lines (42 loc) · 1.22 KB
/
functions.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
package goql
import (
"fmt"
"sync"
)
// Function is the functions in the system, it should check for arguments count and return error on
// wrong arguments count, but for type, it should try to cast
// TODO : check for function arguments on prepare
type Function interface {
// Execute is called on each row
Execute(...Getter) (Getter, error)
}
var (
functions = make(map[string]Function)
fnLock = &sync.RWMutex{}
)
// RegisterFunction is entry point for registering a function into system, the name must be unique
func RegisterFunction(name string, fn Function) {
fnLock.Lock()
defer fnLock.Unlock()
if _, ok := functions[name]; ok {
panic(fmt.Sprintf("function with name '%s' is already registered", name))
}
functions[name] = fn
}
// hasFunction return if the function is available
func hasFunction(name string) bool {
fnLock.RLock()
defer fnLock.RUnlock()
_, ok := functions[name]
return ok
}
// executeFunction is a helper to execute function by its name
func executeFunction(name string, value ...Getter) (Getter, error) {
fnLock.RLock()
defer fnLock.RUnlock()
fn, ok := functions[name]
if !ok {
return nil, fmt.Errorf("function with name '%s' is not registered", name)
}
return fn.Execute(value...)
}