mirror of https://github.com/go-gorm/gorm.git
Fix nested embedded field with pointer, close #3071
This commit is contained in:
parent
32bd6b3e8f
commit
e77e7bb842
|
@ -397,11 +397,11 @@ func (field *Field) setupValuerAndSetter() {
|
||||||
default:
|
default:
|
||||||
field.ReflectValueOf = func(value reflect.Value) reflect.Value {
|
field.ReflectValueOf = func(value reflect.Value) reflect.Value {
|
||||||
v := reflect.Indirect(value)
|
v := reflect.Indirect(value)
|
||||||
for _, idx := range field.StructField.Index {
|
for idx, fieldIdx := range field.StructField.Index {
|
||||||
if idx >= 0 {
|
if fieldIdx >= 0 {
|
||||||
v = v.Field(idx)
|
v = v.Field(fieldIdx)
|
||||||
} else {
|
} else {
|
||||||
v = v.Field(-idx - 1)
|
v = v.Field(-fieldIdx - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.Kind() == reflect.Ptr {
|
if v.Kind() == reflect.Ptr {
|
||||||
|
@ -436,7 +436,9 @@ func (field *Field) setupValuerAndSetter() {
|
||||||
fieldValue := field.ReflectValueOf(value)
|
fieldValue := field.ReflectValueOf(value)
|
||||||
|
|
||||||
if reflectV.Type().AssignableTo(field.FieldType.Elem()) {
|
if reflectV.Type().AssignableTo(field.FieldType.Elem()) {
|
||||||
if fieldValue.IsNil() {
|
if !fieldValue.IsValid() {
|
||||||
|
fieldValue = reflect.New(field.FieldType.Elem())
|
||||||
|
} else if fieldValue.IsNil() {
|
||||||
fieldValue.Set(reflect.New(field.FieldType.Elem()))
|
fieldValue.Set(reflect.New(field.FieldType.Elem()))
|
||||||
}
|
}
|
||||||
fieldValue.Elem().Set(reflectV)
|
fieldValue.Elem().Set(reflectV)
|
||||||
|
|
|
@ -10,10 +10,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmbeddedStruct(t *testing.T) {
|
func TestEmbeddedStruct(t *testing.T) {
|
||||||
|
type ReadOnly struct {
|
||||||
|
ReadOnly *bool
|
||||||
|
}
|
||||||
|
|
||||||
type BasePost struct {
|
type BasePost struct {
|
||||||
Id int64
|
Id int64
|
||||||
Title string
|
Title string
|
||||||
URL string
|
URL string
|
||||||
|
ReadOnly
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author struct {
|
type Author struct {
|
||||||
|
|
Loading…
Reference in New Issue