diff --git a/schema/field.go b/schema/field.go index 8c793f93..826680c5 100644 --- a/schema/field.go +++ b/schema/field.go @@ -465,24 +465,33 @@ func (field *Field) setupValuerAndSetter() { } // ValueOf returns field's value and if it is zero - field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) { - v = reflect.Indirect(v) - for _, fieldIdx := range field.StructField.Index { - if fieldIdx >= 0 { - v = v.Field(fieldIdx) - } else { - v = v.Field(-fieldIdx - 1) - - if !v.IsNil() { - v = v.Elem() + fieldIndex := field.StructField.Index[0] + switch { + case len(field.StructField.Index) == 1 && fieldIndex > 0: + field.ValueOf = func(ctx context.Context, value reflect.Value) (interface{}, bool) { + fieldValue := reflect.Indirect(value).Field(fieldIndex) + return fieldValue.Interface(), fieldValue.IsZero() + } + default: + field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) { + v = reflect.Indirect(v) + for _, fieldIdx := range field.StructField.Index { + if fieldIdx >= 0 { + v = v.Field(fieldIdx) } else { - return nil, true + v = v.Field(-fieldIdx - 1) + + if !v.IsNil() { + v = v.Elem() + } else { + return nil, true + } } } - } - fv, zero := v.Interface(), v.IsZero() - return fv, zero + fv, zero := v.Interface(), v.IsZero() + return fv, zero + } } if field.Serializer != nil { @@ -509,24 +518,31 @@ func (field *Field) setupValuerAndSetter() { } // ReflectValueOf returns field's reflect value - field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value { - v = reflect.Indirect(v) - for idx, fieldIdx := range field.StructField.Index { - if fieldIdx >= 0 { - v = v.Field(fieldIdx) - } else { - v = v.Field(-fieldIdx - 1) + switch { + case len(field.StructField.Index) == 1 && fieldIndex > 0: + field.ReflectValueOf = func(ctx context.Context, value reflect.Value) reflect.Value { + return reflect.Indirect(value).Field(fieldIndex) + } + default: + field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value { + v = reflect.Indirect(v) + for idx, fieldIdx := range field.StructField.Index { + if fieldIdx >= 0 { + v = v.Field(fieldIdx) + } else { + v = v.Field(-fieldIdx - 1) - if v.IsNil() { - v.Set(reflect.New(v.Type().Elem())) - } + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) + } - if idx < len(field.StructField.Index)-1 { - v = v.Elem() + if idx < len(field.StructField.Index)-1 { + v = v.Elem() + } } } + return v } - return v } fallbackSetter := func(ctx context.Context, value reflect.Value, v interface{}, setter func(context.Context, reflect.Value, interface{}) error) (err error) { diff --git a/tests/go.mod b/tests/go.mod index cefe6f96..9e3453b7 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -3,6 +3,7 @@ module gorm.io/gorm/tests go 1.14 require ( + github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/google/uuid v1.3.0 github.com/jackc/pgx/v4 v4.15.0 // indirect github.com/jinzhu/now v1.1.4