optimize setupValuerAndSetter func

This commit is contained in:
daheige 2021-07-14 22:03:17 +08:00
parent 74752018dc
commit a70254609d
1 changed files with 8 additions and 7 deletions

View File

@ -490,21 +490,22 @@ func (field *Field) setupValuerAndSetter() {
return return
} else if field.FieldType.Kind() == reflect.Ptr { } else if field.FieldType.Kind() == reflect.Ptr {
fieldValue := field.ReflectValueOf(value) fieldValue := field.ReflectValueOf(value)
fieldType := field.FieldType.Elem()
if reflectValType.AssignableTo(field.FieldType.Elem()) { if reflectValType.AssignableTo(fieldType) {
if !fieldValue.IsValid() { if !fieldValue.IsValid() {
fieldValue = reflect.New(field.FieldType.Elem()) fieldValue = reflect.New(fieldType)
} else if fieldValue.IsNil() { } else if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.FieldType.Elem())) fieldValue.Set(reflect.New(fieldType))
} }
fieldValue.Elem().Set(reflectV) fieldValue.Elem().Set(reflectV)
return return
} else if reflectValType.ConvertibleTo(field.FieldType.Elem()) { } else if reflectValType.ConvertibleTo(fieldType) {
if fieldValue.IsNil() { 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 return
} }
} }
@ -520,7 +521,7 @@ func (field *Field) setupValuerAndSetter() {
err = setter(value, v) err = setter(value, v)
} }
} else { } 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)
} }
} }