From 5555b010dc2617b07dc4a444a130506b1f7e6e56 Mon Sep 17 00:00:00 2001 From: heige Date: Tue, 13 Apr 2021 09:41:30 +0800 Subject: [PATCH] feat: Optimal value type acquisition for v (#4278) --- schema/field.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/schema/field.go b/schema/field.go index 1881ad1a..5dbc96f1 100644 --- a/schema/field.go +++ b/schema/field.go @@ -479,17 +479,19 @@ func (field *Field) setupValuerAndSetter() { field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem()) } else { 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) return - } else if reflectV.Type().ConvertibleTo(field.FieldType) { + } else if reflectValType.ConvertibleTo(field.FieldType) { field.ReflectValueOf(value).Set(reflectV.Convert(field.FieldType)) return } else if field.FieldType.Kind() == reflect.Ptr { fieldValue := field.ReflectValueOf(value) - if reflectV.Type().AssignableTo(field.FieldType.Elem()) { + if reflectValType.AssignableTo(field.FieldType.Elem()) { if !fieldValue.IsValid() { fieldValue = reflect.New(field.FieldType.Elem()) } else if fieldValue.IsNil() { @@ -497,7 +499,7 @@ func (field *Field) setupValuerAndSetter() { } fieldValue.Elem().Set(reflectV) return - } else if reflectV.Type().ConvertibleTo(field.FieldType.Elem()) { + } else if reflectValType.ConvertibleTo(field.FieldType.Elem()) { if fieldValue.IsNil() { fieldValue.Set(reflect.New(field.FieldType.Elem())) }