From a70254609dbfbd12539e7216e415b734d1b09115 Mon Sep 17 00:00:00 2001 From: daheige Date: Wed, 14 Jul 2021 22:03:17 +0800 Subject: [PATCH] optimize setupValuerAndSetter func --- schema/field.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/schema/field.go b/schema/field.go index 9efaa44a..ce0e3c13 100644 --- a/schema/field.go +++ b/schema/field.go @@ -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) } }