mirror of https://github.com/go-gorm/gorm.git
feat: Optimal value type acquisition for v (#4278)
This commit is contained in:
parent
d7911300f8
commit
5555b010dc
|
@ -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()))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue