Skip to content

Commit

Permalink
fix: embedded should be nil if not exists (#6219)
Browse files Browse the repository at this point in the history
  • Loading branch information
a631807682 committed Apr 11, 2023
1 parent b444011 commit f0360dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 0 additions & 10 deletions schema/field.go
Expand Up @@ -580,8 +580,6 @@ func (field *Field) setupValuerAndSetter() {
case **bool:
if data != nil && *data != nil {
field.ReflectValueOf(ctx, value).SetBool(**data)
} else {
field.ReflectValueOf(ctx, value).SetBool(false)
}
case bool:
field.ReflectValueOf(ctx, value).SetBool(data)
Expand All @@ -601,8 +599,6 @@ func (field *Field) setupValuerAndSetter() {
case **int64:
if data != nil && *data != nil {
field.ReflectValueOf(ctx, value).SetInt(**data)
} else {
field.ReflectValueOf(ctx, value).SetInt(0)
}
case int64:
field.ReflectValueOf(ctx, value).SetInt(data)
Expand Down Expand Up @@ -667,8 +663,6 @@ func (field *Field) setupValuerAndSetter() {
case **uint64:
if data != nil && *data != nil {
field.ReflectValueOf(ctx, value).SetUint(**data)
} else {
field.ReflectValueOf(ctx, value).SetUint(0)
}
case uint64:
field.ReflectValueOf(ctx, value).SetUint(data)
Expand Down Expand Up @@ -721,8 +715,6 @@ func (field *Field) setupValuerAndSetter() {
case **float64:
if data != nil && *data != nil {
field.ReflectValueOf(ctx, value).SetFloat(**data)
} else {
field.ReflectValueOf(ctx, value).SetFloat(0)
}
case float64:
field.ReflectValueOf(ctx, value).SetFloat(data)
Expand Down Expand Up @@ -767,8 +759,6 @@ func (field *Field) setupValuerAndSetter() {
case **string:
if data != nil && *data != nil {
field.ReflectValueOf(ctx, value).SetString(**data)
} else {
field.ReflectValueOf(ctx, value).SetString("")
}
case string:
field.ReflectValueOf(ctx, value).SetString(data)
Expand Down
11 changes: 11 additions & 0 deletions tests/embedded_struct_test.go
Expand Up @@ -103,9 +103,16 @@ func TestEmbeddedPointerTypeStruct(t *testing.T) {
URL string
}

type Author struct {
ID string
Name string
Email string
}

type HNPost struct {
*BasePost
Upvotes int32
*Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct
}

DB.Migrator().DropTable(&HNPost{})
Expand All @@ -123,6 +130,10 @@ func TestEmbeddedPointerTypeStruct(t *testing.T) {
if hnPost.Title != "embedded_pointer_type" {
t.Errorf("Should find correct value for embedded pointer type")
}

if hnPost.Author != nil {
t.Errorf("Expected to get back a nil Author but got: %v", hnPost.Author)
}
}

type Content struct {
Expand Down

0 comments on commit f0360dc

Please sign in to comment.