-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhook.go
59 lines (51 loc) · 1.16 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
55
56
57
58
59
package cosy
import "gorm.io/gorm"
func (c *Ctx[T]) GormScope(hook func(tx *gorm.DB) *gorm.DB) *Ctx[T] {
c.gormScopes = append(c.gormScopes, hook)
return c
}
func (c *Ctx[T]) beforeExecuteHook() (abort bool) {
if len(c.beforeExecuteHookFunc) > 0 {
for _, v := range c.beforeExecuteHookFunc {
v(c)
if c.abort {
return true
}
}
}
return
}
func (c *Ctx[T]) beforeDecodeHook() (abort bool) {
if len(c.beforeDecodeHookFunc) > 0 {
for _, v := range c.beforeDecodeHookFunc {
v(c)
if c.abort {
return true
}
}
}
return
}
func (c *Ctx[T]) executedHook() (abort bool) {
if len(c.executedHookFunc) > 0 {
for _, v := range c.executedHookFunc {
v(c)
if c.abort {
return true
}
}
}
return
}
func (c *Ctx[T]) BeforeDecodeHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
c.beforeDecodeHookFunc = append(c.beforeDecodeHookFunc, hook...)
return c
}
func (c *Ctx[T]) BeforeExecuteHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
c.beforeExecuteHookFunc = append(c.beforeExecuteHookFunc, hook...)
return c
}
func (c *Ctx[T]) ExecutedHook(hook ...func(ctx *Ctx[T])) *Ctx[T] {
c.executedHookFunc = append(c.executedHookFunc, hook...)
return c
}