-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhook.go
54 lines (44 loc) · 1.18 KB
/
hook.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
package action
import "context"
const (
BeforeNodeExec = iota
AfterNodeExec
BeforeExecutorDo
AfterExecutorDo
)
type Hook struct {
For string //
// Exec 事务外
BeforeNodeExec func(ctx context.Context, n *Node, method string) error
AfterNodeExec func(ctx context.Context, n *Node, method string) error
// Do 事务内
BeforeExecutorDo func(ctx context.Context, n *Node, method string) error
AfterExecutorDo func(ctx context.Context, n *Node, method string) error
}
var hooksMap = map[string][]Hook{}
func RegHook(h Hook) {
hooksMap[h.For] = append(hooksMap[h.For], h)
}
func EmitHook(ctx context.Context, hookAt int, node *Node, method string) error {
hooks := append(hooksMap["*"], hooksMap[node.AccessName]...)
for _, hook := range hooks {
var handler func(ctx context.Context, n *Node, method string) error
switch hookAt {
case BeforeNodeExec:
handler = hook.BeforeNodeExec
case AfterNodeExec:
handler = hook.AfterNodeExec
case BeforeExecutorDo:
handler = hook.BeforeExecutorDo
case AfterExecutorDo:
handler = hook.AfterExecutorDo
}
if handler != nil {
err := handler(ctx, node, method)
if err != nil {
return err
}
}
}
return nil
}