-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutor.go
190 lines (165 loc) · 3.95 KB
/
executor.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ski
import (
"context"
"fmt"
"regexp"
"strings"
"sync"
)
type (
// Executor accept the argument and output result.
// when the parameter is a slice, it needs to be wrapped with Iterator.
Executor interface {
Exec(context.Context, any) (any, error)
}
// NewExecutor to create a new Executor
NewExecutor func(Arguments) (Executor, error)
)
// Arguments slice of Executor
type Arguments []Executor
// Get index of Executor, if index out of range, return nil
func (a Arguments) Get(i int) Executor {
if i < 0 || i >= len(a) {
return nil
}
return a[i]
}
// GetString index of string, return empty string if index out of range or type is not string
func (a Arguments) GetString(i int) string {
e := a.Get(i)
switch t := e.(type) {
case String:
return string(t)
case fmt.Stringer:
return t.String()
case _raw:
s, ok := t.any.(string)
if !ok {
return ""
}
return s
default:
return ""
}
}
var reName = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
// Register registers the NewExecutor with the given name.
// Valid name characters: a-zA-Z0-9_
func Register(name string, fn NewExecutor) {
if fn == nil {
panic("ski: NewExecutor is nil")
}
before, method, _ := strings.Cut(name, ".")
if !reName.MatchString(before) {
panic(fmt.Sprintf("ski: invalid name %q", name))
}
if method != "" && !reName.MatchString(method) {
panic(fmt.Sprintf("ski: invalid name %q", name))
}
executors.Lock()
defer executors.Unlock()
entries, ok := executors.registry[before]
if !ok {
entries = make(NewExecutors)
executors.registry[before] = entries
}
entries[method] = fn
}
// NewExecutors map of NewExecutor
type NewExecutors map[string]NewExecutor
// Registers register the NewExecutors.
// Valid name characters: a-zA-Z0-9_
func Registers(e NewExecutors) {
executors.Lock()
defer executors.Unlock()
for name, fn := range e {
if fn == nil {
panic("ski: NewExecutor is nil")
}
before, method, _ := strings.Cut(name, ".")
if !reName.MatchString(before) {
panic(fmt.Sprintf("ski: invalid name %q", name))
}
if method != "" && !reName.MatchString(method) {
panic(fmt.Sprintf("ski: invalid name %q", name))
}
entries, ok := executors.registry[before]
if !ok {
entries = make(NewExecutors)
executors.registry[before] = entries
}
entries[method] = fn
}
}
// GetExecutor returns a NewExecutor with the given name
func GetExecutor(name string) (NewExecutor, bool) {
executors.RLock()
defer executors.RUnlock()
name, method, _ := strings.Cut(name, ".")
entries, ok := executors.registry[name]
if !ok {
return nil, false
}
e, ok := entries[method]
return e, ok
}
// GetExecutors returns the all NewExecutor with the given name
func GetExecutors(name string) (map[string]NewExecutor, bool) {
executors.RLock()
defer executors.RUnlock()
name, _, _ = strings.Cut(name, ".")
entries, ok := executors.registry[name]
if !ok {
return nil, false
}
ret := make(map[string]NewExecutor, len(entries))
for method, e := range entries {
ret[method] = e
}
return ret, true
}
// RemoveExecutor removes Executor with the given names
func RemoveExecutor(names ...string) {
if len(names) == 0 {
return
}
executors.Lock()
defer executors.Unlock()
for _, name := range names {
name, method, _ := strings.Cut(name, ".")
entries, ok := executors.registry[name]
if !ok {
continue
}
if method == "" {
delete(executors.registry, name)
continue
}
delete(entries, method)
if len(entries) == 0 {
delete(executors.registry, name)
}
}
}
// AllExecutors returns the all NewExecutor
func AllExecutors() map[string]NewExecutor {
executors.RLock()
defer executors.RUnlock()
ret := make(map[string]NewExecutor)
for name, entries := range executors.registry {
for method, e := range entries {
if method == "" {
ret[name] = e
} else {
ret[name+"."+method] = e
}
}
}
return ret
}
var executors = struct {
sync.RWMutex
registry map[string]NewExecutors
}{
registry: make(map[string]NewExecutors),
}