skip preloading nil fields
This commit is contained in:
parent
041cd3dd31
commit
2061f6a012
37
scope.go
37
scope.go
@ -1217,21 +1217,50 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r
|
|||||||
var result []interface{}
|
var result []interface{}
|
||||||
var object = indirect(indirectValue.Index(i))
|
var object = indirect(indirectValue.Index(i))
|
||||||
for _, column := range columns {
|
for _, column := range columns {
|
||||||
result = append(result, object.FieldByName(column).Interface())
|
field := object.FieldByName(column)
|
||||||
|
if isNil(field) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, field.Interface())
|
||||||
|
}
|
||||||
|
if len(result) > 0 {
|
||||||
|
results = append(results, result)
|
||||||
}
|
}
|
||||||
results = append(results, result)
|
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
var result []interface{}
|
var result []interface{}
|
||||||
for _, column := range columns {
|
for _, column := range columns {
|
||||||
result = append(result, indirectValue.FieldByName(column).Interface())
|
field := indirectValue.FieldByName(column)
|
||||||
|
if isNil(field) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, field.Interface())
|
||||||
|
}
|
||||||
|
if len(result) > 0 {
|
||||||
|
results = append(results, result)
|
||||||
}
|
}
|
||||||
results = append(results, result)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isNil(v reflect.Value) bool {
|
||||||
|
if v.Kind() == reflect.Ptr && v.IsNil() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
switch v := v.Interface().(type) {
|
||||||
|
case sql.NullBool:
|
||||||
|
return !v.Valid
|
||||||
|
case sql.NullFloat64:
|
||||||
|
return !v.Valid
|
||||||
|
case sql.NullInt64:
|
||||||
|
return !v.Valid
|
||||||
|
case sql.NullString:
|
||||||
|
return !v.Valid
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (scope *Scope) getColumnAsScope(column string) *Scope {
|
func (scope *Scope) getColumnAsScope(column string) *Scope {
|
||||||
indirectScopeValue := scope.IndirectValue()
|
indirectScopeValue := scope.IndirectValue()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user