github.com/fasmat/pop@v4.13.1+incompatible/callbacks.go (about) 1 package pop 2 3 import ( 4 "reflect" 5 6 "golang.org/x/sync/errgroup" 7 ) 8 9 // AfterFindable callback will be called after a record, or records, 10 // has been retrieved from the database. 11 type AfterFindable interface { 12 AfterFind(*Connection) error 13 } 14 15 func (m *Model) afterFind(c *Connection) error { 16 if x, ok := m.Value.(AfterFindable); ok { 17 if err := x.AfterFind(c); err != nil { 18 return err 19 } 20 } 21 22 // if the "model" is a slice/array we want 23 // to loop through each of the elements in the collection 24 // and call AfterFind on them if they exist. 25 rv := reflect.Indirect(reflect.ValueOf(m.Value)) 26 kind := rv.Kind() 27 if kind != reflect.Slice && kind != reflect.Array { 28 return nil 29 } 30 31 wg := &errgroup.Group{} 32 for i := 0; i < rv.Len(); i++ { 33 func(i int) { 34 wg.Go(func() error { 35 y := rv.Index(i) 36 y = y.Addr() 37 if x, ok := y.Interface().(AfterFindable); ok { 38 return x.AfterFind(c) 39 } 40 return nil 41 }) 42 }(i) 43 } 44 45 return wg.Wait() 46 } 47 48 // BeforeSaveable callback will be called before a record is 49 // either created or updated in the database. 50 type BeforeSaveable interface { 51 BeforeSave(*Connection) error 52 } 53 54 func (m *Model) beforeSave(c *Connection) error { 55 if x, ok := m.Value.(BeforeSaveable); ok { 56 return x.BeforeSave(c) 57 } 58 return nil 59 } 60 61 // BeforeCreateable callback will be called before a record is 62 // created in the database. 63 type BeforeCreateable interface { 64 BeforeCreate(*Connection) error 65 } 66 67 func (m *Model) beforeCreate(c *Connection) error { 68 if x, ok := m.Value.(BeforeCreateable); ok { 69 return x.BeforeCreate(c) 70 } 71 return nil 72 } 73 74 // BeforeUpdateable callback will be called before a record is 75 // updated in the database. 76 type BeforeUpdateable interface { 77 BeforeUpdate(*Connection) error 78 } 79 80 func (m *Model) beforeUpdate(c *Connection) error { 81 if x, ok := m.Value.(BeforeUpdateable); ok { 82 return x.BeforeUpdate(c) 83 } 84 return nil 85 } 86 87 // BeforeDestroyable callback will be called before a record is 88 // destroyed in the database. 89 type BeforeDestroyable interface { 90 BeforeDestroy(*Connection) error 91 } 92 93 func (m *Model) beforeDestroy(c *Connection) error { 94 if x, ok := m.Value.(BeforeDestroyable); ok { 95 return x.BeforeDestroy(c) 96 } 97 return nil 98 } 99 100 // BeforeValidateable callback will be called before a record is 101 // validated during 102 // ValidateAndCreate, ValidateAndUpdate, or ValidateAndSave 103 type BeforeValidateable interface { 104 BeforeValidate(*Connection) error 105 } 106 107 func (m *Model) beforeValidate(c *Connection) error { 108 if x, ok := m.Value.(BeforeValidateable); ok { 109 return x.BeforeValidate(c) 110 } 111 return nil 112 } 113 114 // AfterDestroyable callback will be called after a record is 115 // destroyed in the database. 116 type AfterDestroyable interface { 117 AfterDestroy(*Connection) error 118 } 119 120 func (m *Model) afterDestroy(c *Connection) error { 121 if x, ok := m.Value.(AfterDestroyable); ok { 122 return x.AfterDestroy(c) 123 } 124 return nil 125 } 126 127 // AfterUpdateable callback will be called after a record is 128 // updated in the database. 129 type AfterUpdateable interface { 130 AfterUpdate(*Connection) error 131 } 132 133 func (m *Model) afterUpdate(c *Connection) error { 134 if x, ok := m.Value.(AfterUpdateable); ok { 135 return x.AfterUpdate(c) 136 } 137 return nil 138 } 139 140 // AfterCreateable callback will be called after a record is 141 // created in the database. 142 type AfterCreateable interface { 143 AfterCreate(*Connection) error 144 } 145 146 func (m *Model) afterCreate(c *Connection) error { 147 if x, ok := m.Value.(AfterCreateable); ok { 148 return x.AfterCreate(c) 149 } 150 return nil 151 } 152 153 // AfterSaveable callback will be called after a record is 154 // either created or updated in the database. 155 type AfterSaveable interface { 156 AfterSave(*Connection) error 157 } 158 159 func (m *Model) afterSave(c *Connection) error { 160 if x, ok := m.Value.(AfterSaveable); ok { 161 return x.AfterSave(c) 162 } 163 return nil 164 }