Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: embedded should be nil if not exists #6219

Merged
merged 1 commit into from Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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