Refactor isBlank method

This commit is contained in:
Jinzhu 2013-12-04 13:07:28 +08:00
parent 204df61a8b
commit fb65095376
1 changed files with 1 additions and 28 deletions

View File

@ -6,7 +6,6 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var toSnakeMap = map[string]string{} var toSnakeMap = map[string]string{}
@ -87,31 +86,5 @@ func setFieldValue(field reflect.Value, value interface{}) bool {
} }
func isBlank(value reflect.Value) bool { func isBlank(value reflect.Value) bool {
switch value.Kind() { return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
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
} }