forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.go
184 lines (157 loc) · 4.17 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package pop
import (
"reflect"
"golang.org/x/sync/errgroup"
)
// AfterFindable callback will be called after a record, or records,
// has been retrieved from the database.
type AfterFindable interface {
AfterFind(*Connection) error
}
// AfterEagerFindable callback will be called after a record, or records,
// has been retrieved from the database and their associations have been
// eagerly loaded.
type AfterEagerFindable interface {
AfterEagerFind(*Connection) error
}
func (m *Model) afterFind(c *Connection, eager bool) error {
if eager {
if x, ok := m.Value.(AfterEagerFindable); ok {
if err := x.AfterEagerFind(c); err != nil {
return err
}
}
} else {
if x, ok := m.Value.(AfterFindable); ok {
if err := x.AfterFind(c); err != nil {
return 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++ {
elem := rv.Index(i).Addr()
if eager {
if x, ok := elem.Interface().(AfterEagerFindable); ok {
wg.Go(func() error {
return x.AfterEagerFind(c)
})
}
} else {
if x, ok := elem.Interface().(AfterFindable); ok {
wg.Go(func() error {
return x.AfterFind(c)
})
}
}
}
return wg.Wait()
}
// BeforeSaveable callback will be called before a record is
// either created or updated in the database.
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
}
// BeforeCreateable callback will be called before a record is
// created in the database.
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
}
// BeforeUpdateable callback will be called before a record is
// updated in the database.
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
}
// BeforeDestroyable callback will be called before a record is
// destroyed in the database.
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
}
// BeforeValidateable callback will be called before a record is
// validated during
// ValidateAndCreate, ValidateAndUpdate, or ValidateAndSave
type BeforeValidateable interface {
BeforeValidate(*Connection) error
}
func (m *Model) beforeValidate(c *Connection) error {
if x, ok := m.Value.(BeforeValidateable); ok {
return x.BeforeValidate(c)
}
return nil
}
// AfterDestroyable callback will be called after a record is
// destroyed in the database.
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
}
// AfterUpdateable callback will be called after a record is
// updated in the database.
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
}
// AfterCreateable callback will be called after a record is
// created in the database.
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
}
// AfterSaveable callback will be called after a record is
// either created or updated in the database.
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
}