/
registry.go
85 lines (70 loc) · 1.64 KB
/
registry.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
82
83
84
85
package function
import (
"fmt"
"strings"
"sync"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
var log = logger.GetLogger("function-registry")
type Function interface {
GetName() string
GetCategory() string
}
var (
functionsMu sync.Mutex
functions = make(map[string]Function)
)
func Registry(f Function) {
functionsMu.Lock()
defer functionsMu.Unlock()
if f == nil {
log.Errorf("Cannot rregistry nil function")
return
}
log.Debugf("Registry function name %s tag %s", f.GetName(), f.GetCategory())
var registeName string
if f.GetCategory() != "" && len(strings.TrimSpace(f.GetCategory())) > 0 {
registeName = f.GetCategory() + "." + f.GetName()
} else {
registeName = f.GetName()
}
functions[strings.ToLower(registeName)] = f
}
func GetFunction(name string) (Function, error) {
name = strings.ToLower(name)
f, ok := functions[name]
if ok {
return f, nil
}
for k, _ := range functions {
log.Debugf("function %s", k)
}
return nil, fmt.Errorf("No function %s found", name)
}
func GetFunctionByTag(name string, tag string) (Function, error) {
regName := strings.ToLower(getRegisteName(name, tag))
f, ok := functions[regName]
if ok {
return f, nil
}
for k, _ := range functions {
log.Debugf("function %s", k)
}
return nil, fmt.Errorf("No function name %s tag %s found", name, tag)
}
func ListAllFunctions() []string {
var keys []string
for k, _ := range functions {
keys = append(keys, k)
}
return keys
}
func getRegisteName(name, tag string) string {
var registeName string
if tag != "" && len(strings.TrimSpace(tag)) > 0 {
registeName = tag + "." + name
} else {
registeName = name
}
return registeName
}