From 6dc583869b5aef690650f3e3e62d6a80c5de99ea Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 17 Jul 2020 12:02:00 +0800 Subject: [PATCH] Don't use value's first field to guess data type for struct implements GormDataTypeInterface --- schema/field.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/schema/field.go b/schema/field.go index 2c43229b..bc3dbc62 100644 --- a/schema/field.go +++ b/schema/field.go @@ -105,28 +105,30 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { // if field is valuer, used its value or first fields as data type valuer, isValuer := fieldValue.Interface().(driver.Valuer) if isValuer { - var overrideFieldValue bool - if v, err := valuer.Value(); v != nil && err == nil { - overrideFieldValue = true - fieldValue = reflect.ValueOf(v) - } + if _, ok := fieldValue.Interface().(GormDataTypeInterface); !ok { + var overrideFieldValue bool + if v, err := valuer.Value(); v != nil && err == nil { + overrideFieldValue = true + fieldValue = reflect.ValueOf(v) + } - if field.IndirectFieldType.Kind() == reflect.Struct { - for i := 0; i < field.IndirectFieldType.NumField(); i++ { - if !overrideFieldValue { - newFieldType := field.IndirectFieldType.Field(i).Type - for newFieldType.Kind() == reflect.Ptr { - newFieldType = newFieldType.Elem() + if field.IndirectFieldType.Kind() == reflect.Struct { + for i := 0; i < field.IndirectFieldType.NumField(); i++ { + if !overrideFieldValue { + newFieldType := field.IndirectFieldType.Field(i).Type + for newFieldType.Kind() == reflect.Ptr { + newFieldType = newFieldType.Elem() + } + + fieldValue = reflect.New(newFieldType) + overrideFieldValue = true } - fieldValue = reflect.New(newFieldType) - overrideFieldValue = true - } - - // copy tag settings from valuer - for key, value := range ParseTagSetting(field.IndirectFieldType.Field(i).Tag.Get("gorm"), ";") { - if _, ok := field.TagSettings[key]; !ok { - field.TagSettings[key] = value + // copy tag settings from valuer + for key, value := range ParseTagSetting(field.IndirectFieldType.Field(i).Tag.Get("gorm"), ";") { + if _, ok := field.TagSettings[key]; !ok { + field.TagSettings[key] = value + } } } }