Merge pull request #4530 from daheige/optimize-setupValuerAndSetter

optimize setupValuerAndSetter func
This commit is contained in:
Jason Lee 2021-07-28 18:51:09 +08:00 committed by GitHub
commit 3e15478534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 7 deletions

View File

@ -490,21 +490,22 @@ func (field *Field) setupValuerAndSetter() {
return
} else if field.FieldType.Kind() == reflect.Ptr {
fieldValue := field.ReflectValueOf(value)
fieldType := field.FieldType.Elem()
if reflectValType.AssignableTo(field.FieldType.Elem()) {
if reflectValType.AssignableTo(fieldType) {
if !fieldValue.IsValid() {
fieldValue = reflect.New(field.FieldType.Elem())
fieldValue = reflect.New(fieldType)
} else if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.FieldType.Elem()))
fieldValue.Set(reflect.New(fieldType))
}
fieldValue.Elem().Set(reflectV)
return
} else if reflectValType.ConvertibleTo(field.FieldType.Elem()) {
} else if reflectValType.ConvertibleTo(fieldType) {
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.FieldType.Elem()))
fieldValue.Set(reflect.New(fieldType))
}
fieldValue.Elem().Set(reflectV.Convert(field.FieldType.Elem()))
fieldValue.Elem().Set(reflectV.Convert(fieldType))
return
}
}
@ -520,7 +521,7 @@ func (field *Field) setupValuerAndSetter() {
err = setter(value, v)
}
} else {
return fmt.Errorf("failed to set value %+v to field %v", v, field.Name)
return fmt.Errorf("failed to set value %+v to field %s", v, field.Name)
}
}