feat: Optimal value type acquisition for v (#4278)

This commit is contained in:
heige 2021-04-13 09:41:30 +08:00 committed by GitHub
parent d7911300f8
commit 5555b010dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -479,17 +479,19 @@ func (field *Field) setupValuerAndSetter() {
field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem()) field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem())
} else { } else {
reflectV := reflect.ValueOf(v) reflectV := reflect.ValueOf(v)
// Optimal value type acquisition for v
reflectValType := reflectV.Type()
if reflectV.Type().AssignableTo(field.FieldType) { if reflectValType.AssignableTo(field.FieldType) {
field.ReflectValueOf(value).Set(reflectV) field.ReflectValueOf(value).Set(reflectV)
return return
} else if reflectV.Type().ConvertibleTo(field.FieldType) { } else if reflectValType.ConvertibleTo(field.FieldType) {
field.ReflectValueOf(value).Set(reflectV.Convert(field.FieldType)) field.ReflectValueOf(value).Set(reflectV.Convert(field.FieldType))
return return
} else if field.FieldType.Kind() == reflect.Ptr { } else if field.FieldType.Kind() == reflect.Ptr {
fieldValue := field.ReflectValueOf(value) fieldValue := field.ReflectValueOf(value)
if reflectV.Type().AssignableTo(field.FieldType.Elem()) { if reflectValType.AssignableTo(field.FieldType.Elem()) {
if !fieldValue.IsValid() { if !fieldValue.IsValid() {
fieldValue = reflect.New(field.FieldType.Elem()) fieldValue = reflect.New(field.FieldType.Elem())
} else if fieldValue.IsNil() { } else if fieldValue.IsNil() {
@ -497,7 +499,7 @@ func (field *Field) setupValuerAndSetter() {
} }
fieldValue.Elem().Set(reflectV) fieldValue.Elem().Set(reflectV)
return return
} else if reflectV.Type().ConvertibleTo(field.FieldType.Elem()) { } else if reflectValType.ConvertibleTo(field.FieldType.Elem()) {
if fieldValue.IsNil() { if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.FieldType.Elem())) fieldValue.Set(reflect.New(field.FieldType.Elem()))
} }