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:
|
||||
field.ReflectValueOf = func(value reflect.Value) reflect.Value {
|
||||
v := reflect.Indirect(value)
|
||||
for _, idx := range field.StructField.Index {
|
||||
if idx >= 0 {
|
||||
v = v.Field(idx)
|
||||
for idx, fieldIdx := range field.StructField.Index {
|
||||
if fieldIdx >= 0 {
|
||||
v = v.Field(fieldIdx)
|
||||
} else {
|
||||
v = v.Field(-idx - 1)
|
||||
v = v.Field(-fieldIdx - 1)
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Ptr {
|
||||
|
@ -436,7 +436,9 @@ func (field *Field) setupValuerAndSetter() {
|
|||
fieldValue := field.ReflectValueOf(value)
|
||||
|
||||
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.Elem().Set(reflectV)
|
||||
|
|
|
@ -10,10 +10,15 @@ import (
|
|||
)
|
||||
|
||||
func TestEmbeddedStruct(t *testing.T) {
|
||||
type ReadOnly struct {
|
||||
ReadOnly *bool
|
||||
}
|
||||
|
||||
type BasePost struct {
|
||||
Id int64
|
||||
Title string
|
||||
URL string
|
||||
ReadOnly
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
|
|
Loading…
Reference in New Issue