mirror of https://github.com/go-gorm/gorm.git
Refact fix invalid zero value for Preload
This commit is contained in:
parent
861c477a33
commit
c6a22c5096
16
preload.go
16
preload.go
|
@ -9,14 +9,11 @@ import (
|
|||
)
|
||||
|
||||
func getRealValue(value reflect.Value, columns []string) (results []interface{}) {
|
||||
for _, column := range columns {
|
||||
pointedValue := reflect.Indirect(value)
|
||||
// If v is a nil pointer, Indirect returns a zero Value!
|
||||
// If value is a nil pointer, Indirect returns a zero Value!
|
||||
// Therefor we need to check for a zero value,
|
||||
// as FieldByName could panic
|
||||
if !pointedValue.IsValid() {
|
||||
continue
|
||||
}
|
||||
if pointedValue := reflect.Indirect(value); pointedValue.IsValid() {
|
||||
for _, column := range columns {
|
||||
if pointedValue.FieldByName(column).IsValid() {
|
||||
result := pointedValue.FieldByName(column).Interface()
|
||||
if r, ok := result.(driver.Valuer); ok {
|
||||
|
@ -25,6 +22,7 @@ func getRealValue(value reflect.Value, columns []string) (results []interface{})
|
|||
results = append(results, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -297,12 +295,6 @@ func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface
|
|||
}
|
||||
}
|
||||
} else {
|
||||
// If v is a nil pointer, Indirect returns a zero Value!
|
||||
// Therefor we need to check for a zero value,
|
||||
// as FieldByName could panic
|
||||
if !scope.IndirectValue().IsValid() {
|
||||
return
|
||||
}
|
||||
object := scope.IndirectValue()
|
||||
source := getRealValue(object, associationForeignStructFieldNames)
|
||||
field := object.FieldByName(field.Name)
|
||||
|
|
Loading…
Reference in New Issue