forked from mirror/gorm
Refactor isBlank method
This commit is contained in:
parent
204df61a8b
commit
fb65095376
29
utils.go
29
utils.go
|
@ -6,7 +6,6 @@ import (
|
|||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var toSnakeMap = map[string]string{}
|
||||
|
@ -87,31 +86,5 @@ func setFieldValue(field reflect.Value, value interface{}) bool {
|
|||
}
|
||||
|
||||
func isBlank(value reflect.Value) bool {
|
||||
switch value.Kind() {
|
||||
case reflect.Int, reflect.Int64, reflect.Int32:
|
||||
return value.Int() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return value.Float() == 0
|
||||
case reflect.String:
|
||||
return value.String() == ""
|
||||
case reflect.Slice:
|
||||
return value.Len() == 0
|
||||
case reflect.Struct:
|
||||
time_value, is_time := value.Interface().(time.Time)
|
||||
if is_time {
|
||||
return time_value.IsZero()
|
||||
} else {
|
||||
_, is_scanner := reflect.New(value.Type()).Interface().(sql.Scanner)
|
||||
if is_scanner {
|
||||
return !value.FieldByName("Valid").Interface().(bool)
|
||||
} else {
|
||||
m := &Model{data: value.Interface()}
|
||||
fields := m.columnsHasValue("other")
|
||||
if len(fields) == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue