forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.go
164 lines (137 loc) · 3.86 KB
/
callback.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
package gorm
type callback struct {
creates []*func()
updates []*func()
deletes []*func()
queries []*func()
processors []*callback_processor
}
type callback_processor struct {
name string
before string
after string
replace bool
typ string
processor *func()
callback *callback
}
func (c *callback) addProcessor(typ string) *callback_processor {
cp := &callback_processor{typ: typ, callback: c}
c.processors = append(c.processors, cp)
return cp
}
func (c *callback) Create() *callback_processor {
return c.addProcessor("create")
}
func (c *callback) Update() *callback_processor {
return c.addProcessor("update")
}
func (c *callback) Delete() *callback_processor {
return c.addProcessor("delete")
}
func (c *callback) Query() *callback_processor {
return c.addProcessor("query")
}
func (cp *callback_processor) Before(name string) *callback_processor {
cp.before = name
return cp
}
func (cp *callback_processor) After(name string) *callback_processor {
cp.after = name
return cp
}
func (cp *callback_processor) Register(name string, fc func()) {
cp.name = name
cp.processor = &fc
cp.callback.sort()
}
func (cp *callback_processor) Remove(name string) {
cp.Replace(name, func() {})
}
func (cp *callback_processor) Replace(name string, fc func()) {
cp.name = name
cp.processor = &fc
cp.replace = true
cp.callback.sort()
}
func getIndex(strs []string, str string) int {
for index, value := range strs {
if str == value {
return index
}
}
return -1
}
func sortProcessors(cps []*callback_processor) []*func() {
var sortCallbackProcessor func(c *callback_processor, force bool)
var names, sortedNames = []string{}, []string{}
for _, cp := range cps {
names = append(names, cp.name)
}
sortCallbackProcessor = func(c *callback_processor, force bool) {
if getIndex(sortedNames, c.name) > -1 {
return
}
if len(c.before) > 0 {
if index := getIndex(sortedNames, c.before); index > -1 {
sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
} else if index := getIndex(names, c.before); index > -1 {
sortedNames = append(sortedNames, c.name)
sortCallbackProcessor(cps[index], true)
} else {
sortedNames = append(sortedNames, c.name)
}
}
if len(c.after) > 0 {
if index := getIndex(sortedNames, c.after); index > -1 {
sortedNames = append(sortedNames[:index+1], append([]string{c.name}, sortedNames[index+1:]...)...)
} else if index := getIndex(names, c.after); index > -1 {
cp := cps[index]
if len(cp.before) == 0 {
cp.before = c.name
}
sortCallbackProcessor(cp, true)
} else {
sortedNames = append(sortedNames, c.name)
}
}
if getIndex(sortedNames, c.name) == -1 && force {
sortedNames = append(sortedNames, c.name)
}
}
for _, cp := range cps {
sortCallbackProcessor(cp, false)
}
var funcs = []*func(){}
var sortedFuncs = []*func(){}
for _, name := range sortedNames {
index := getIndex(names, name)
sortedFuncs = append(sortedFuncs, cps[index].processor)
}
for _, cp := range cps {
if sindex := getIndex(sortedNames, cp.name); sindex == -1 {
funcs = append(funcs, cp.processor)
}
}
return append(sortedFuncs, funcs...)
}
func (c *callback) sort() {
creates, updates, deletes, queries := []*callback_processor{}, []*callback_processor{}, []*callback_processor{}, []*callback_processor{}
for _, processor := range c.processors {
switch processor.typ {
case "create":
creates = append(creates, processor)
case "update":
updates = append(updates, processor)
case "delete":
deletes = append(deletes, processor)
case "query":
queries = append(queries, processor)
}
}
c.creates = sortProcessors(creates)
c.updates = sortProcessors(updates)
c.deletes = sortProcessors(deletes)
c.queries = sortProcessors(queries)
}
var DefaultCallback = &callback{processors: []*callback_processor{}}