mirror of https://github.com/go-gorm/gorm.git
Simplify set field value
This commit is contained in:
parent
40c16f3c29
commit
b78645279c
27
utils.go
27
utils.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -123,25 +122,19 @@ func fileWithLineNum() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFieldValue(field reflect.Value, value interface{}) bool {
|
func setFieldValue(field reflect.Value, value interface{}) (result bool) {
|
||||||
|
result = false
|
||||||
if field.IsValid() && field.CanAddr() {
|
if field.IsValid() && field.CanAddr() {
|
||||||
switch field.Kind() {
|
result = true
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
|
||||||
if str, ok := value.(string); ok {
|
scanner.Scan(value)
|
||||||
value, _ = strconv.Atoi(str)
|
} else if reflect.TypeOf(value).ConvertibleTo(field.Type()) {
|
||||||
}
|
field.Set(reflect.ValueOf(value).Convert(field.Type()))
|
||||||
field.SetInt(reflect.ValueOf(value).Int())
|
} else {
|
||||||
default:
|
result = false
|
||||||
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
|
|
||||||
scanner.Scan(value)
|
|
||||||
} else {
|
|
||||||
field.Set(reflect.ValueOf(value))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isBlank(value reflect.Value) bool {
|
func isBlank(value reflect.Value) bool {
|
||||||
|
|
Loading…
Reference in New Issue