forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.go
More file actions
54 lines (44 loc) · 1.08 KB
/
Copy pathhook.go
File metadata and controls
54 lines (44 loc) · 1.08 KB
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 (
BeforeExec = iota
AfterExec
BeforeDo
AfterDo
)
type Hook struct {
For string //
// Exec 事务外
BeforeExec func(ctx context.Context, n *Node, method string) error
AfterExec func(ctx context.Context, n *Node, method string) error
// Do 事务内
BeforeDo func(ctx context.Context, n *Node, method string) error
AfterDo 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.TableName]...)
for _, hook := range hooks {
var handler func(ctx context.Context, n *Node, method string) error
switch hookAt {
case BeforeExec:
handler = hook.BeforeExec
case AfterExec:
handler = hook.AfterExec
case BeforeDo:
handler = hook.BeforeDo
case AfterDo:
handler = hook.AfterDo
}
if handler != nil {
err := handler(ctx, node, method)
if err != nil {
return err
}
}
}
return nil
}