-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
55 lines (46 loc) · 1.14 KB
/
handler.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
package engine
import (
"context"
"fmt"
"github.com/woocoos/workflow/pkg/spec/vars"
"sync"
)
type (
HandlerFunc func(ctx context.Context, input vars.Mapping) (vars.Mapping, error)
)
var (
handlerFuncMap = make(map[string]HandlerFunc)
mu sync.RWMutex
)
func init() {
RegistryGlobalHandler("test", func(ctx context.Context, input vars.Mapping) (vars.Mapping, error) {
return nil, nil
})
}
type HandlerFuncMap struct {
id string
}
func NewHandlerFuncMap(id string) *HandlerFuncMap {
return &HandlerFuncMap{id: id}
}
func (h *HandlerFuncMap) RegistryHandler(name string, fun HandlerFunc) {
mu.Lock()
defer mu.Unlock()
handlerFuncMap[h.id+"."+name] = fun
}
func (h *HandlerFuncMap) RunHandler(ctx context.Context, name string, input vars.Mapping) (vars.Mapping, error) {
mu.RLock()
defer mu.RUnlock()
if fun, ok := handlerFuncMap[h.id+"."+name]; ok {
return fun(ctx, input)
}
if fun, ok := handlerFuncMap[name]; ok {
return fun(ctx, input)
}
return nil, fmt.Errorf("not support service task type: %s", name)
}
func RegistryGlobalHandler(name string, fun HandlerFunc) {
mu.Lock()
defer mu.Unlock()
handlerFuncMap[name] = fun
}