forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.go
133 lines (110 loc) · 2.5 KB
/
callbacks.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
package pop
import (
"reflect"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
type afterFindable interface {
AfterFind(*Connection) error
}
func (m *Model) afterFind(c *Connection) error {
if x, ok := m.Value.(afterFindable); ok {
if err := x.AfterFind(c); err != nil {
return errors.WithStack(err)
}
}
// if the "model" is a slice/array we want
// to loop through each of the elements in the collection
// and call AfterFind on them if they exist.
rv := reflect.Indirect(reflect.ValueOf(m.Value))
kind := rv.Kind()
if kind != reflect.Slice && kind != reflect.Array {
return nil
}
wg := &errgroup.Group{}
for i := 0; i < rv.Len(); i++ {
func(i int) {
wg.Go(func() error {
y := rv.Index(i)
y = y.Addr()
if x, ok := y.Interface().(afterFindable); ok {
return x.AfterFind(c)
}
return nil
})
}(i)
}
return wg.Wait()
}
type beforeSaveable interface {
BeforeSave(*Connection) error
}
func (m *Model) beforeSave(c *Connection) error {
if x, ok := m.Value.(beforeSaveable); ok {
return x.BeforeSave(c)
}
return nil
}
type beforeCreateable interface {
BeforeCreate(*Connection) error
}
func (m *Model) beforeCreate(c *Connection) error {
if x, ok := m.Value.(beforeCreateable); ok {
return x.BeforeCreate(c)
}
return nil
}
type beforeUpdateable interface {
BeforeUpdate(*Connection) error
}
func (m *Model) beforeUpdate(c *Connection) error {
if x, ok := m.Value.(beforeUpdateable); ok {
return x.BeforeUpdate(c)
}
return nil
}
type beforeDestroyable interface {
BeforeDestroy(*Connection) error
}
func (m *Model) beforeDestroy(c *Connection) error {
if x, ok := m.Value.(beforeDestroyable); ok {
return x.BeforeDestroy(c)
}
return nil
}
type afterDestroyable interface {
AfterDestroy(*Connection) error
}
func (m *Model) afterDestroy(c *Connection) error {
if x, ok := m.Value.(afterDestroyable); ok {
return x.AfterDestroy(c)
}
return nil
}
type afterUpdateable interface {
AfterUpdate(*Connection) error
}
func (m *Model) afterUpdate(c *Connection) error {
if x, ok := m.Value.(afterUpdateable); ok {
return x.AfterUpdate(c)
}
return nil
}
type afterCreateable interface {
AfterCreate(*Connection) error
}
func (m *Model) afterCreate(c *Connection) error {
if x, ok := m.Value.(afterCreateable); ok {
return x.AfterCreate(c)
}
return nil
}
type afterSaveable interface {
AfterSave(*Connection) error
}
func (m *Model) afterSave(c *Connection) error {
if x, ok := m.Value.(afterSaveable); ok {
return x.AfterSave(c)
}
return nil
}