| 1 |
package callbacks |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
|
| 6 |
"gorm.io/gorm" |
| 7 |
) |
| 8 |
|
| 9 |
func callMethod(db *gorm.DB, fc func(value interface{}, tx *gorm.DB) bool) { |
| 10 |
tx := db.Session(&gorm.Session{NewDB: true}) |
| 11 |
if called := fc(db.Statement.ReflectValue.Interface(), tx); !called { |
| 12 |
switch db.Statement.ReflectValue.Kind() { |
| 13 |
case reflect.Slice, reflect.Array: |
| 14 |
db.Statement.CurDestIndex = 0 |
| 15 |
for i := 0; i < db.Statement.ReflectValue.Len(); i++ { |
| 16 |
if value := reflect.Indirect(db.Statement.ReflectValue.Index(i)); value.CanAddr() { |
| 17 |
fc(value.Addr().Interface(), tx) |
| 18 |
} else { |
| 19 |
db.AddError(gorm.ErrInvalidValue) |
| 20 |
return |
| 21 |
} |
| 22 |
db.Statement.CurDestIndex++ |
| 23 |
} |
| 24 |
case reflect.Struct: |
| 25 |
if db.Statement.ReflectValue.CanAddr() { |
| 26 |
fc(db.Statement.ReflectValue.Addr().Interface(), tx) |
| 27 |
} else { |
| 28 |
db.AddError(gorm.ErrInvalidValue) |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
} |