Skip to content

Commit

Permalink
Add test case for embedded value selects (#5901)
Browse files Browse the repository at this point in the history
* Add test case for embedded value selects

* Revert recycle struct optimisation to avoid pointer overwrites
  • Loading branch information
emcfarlane authored Dec 19, 2022
1 parent d9525d4 commit 4ec73c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 3 additions & 9 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func (db *DB) scanIntoStruct(rows Rows, reflectValue reflect.Value, values []int

db.RowsAffected++
db.AddError(rows.Scan(values...))

joinedSchemaMap := make(map[*schema.Field]interface{})
for idx, field := range fields {
if field == nil {
Expand Down Expand Up @@ -241,9 +240,8 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
switch reflectValue.Kind() {
case reflect.Slice, reflect.Array:
var (
elem reflect.Value
recyclableStruct = reflect.New(reflectValueType)
isArrayKind = reflectValue.Kind() == reflect.Array
elem reflect.Value
isArrayKind = reflectValue.Kind() == reflect.Array
)

if !update || reflectValue.Len() == 0 {
Expand Down Expand Up @@ -275,11 +273,7 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
}
}
} else {
if isPtr && db.RowsAffected > 0 {
elem = reflect.New(reflectValueType)
} else {
elem = recyclableStruct
}
elem = reflect.New(reflectValueType)
}

db.scanIntoStruct(rows, elem, values, fields, joinFields)
Expand Down
18 changes: 16 additions & 2 deletions tests/embedded_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestEmbeddedStruct(t *testing.T) {

type EngadgetPost struct {
BasePost BasePost `gorm:"Embedded"`
Author Author `gorm:"Embedded;EmbeddedPrefix:author_"` // Embedded struct
Author *Author `gorm:"Embedded;EmbeddedPrefix:author_"` // Embedded struct
ImageUrl string
}

Expand Down Expand Up @@ -74,13 +74,27 @@ func TestEmbeddedStruct(t *testing.T) {
t.Errorf("embedded struct's value should be scanned correctly")
}

DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_news"}})
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_news"}, Author: &Author{Name: "Edward"}})
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_article"}, Author: &Author{Name: "George"}})
var egNews EngadgetPost
if err := DB.First(&egNews, "title = ?", "engadget_news").Error; err != nil {
t.Errorf("no error should happen when query with embedded struct, but got %v", err)
} else if egNews.BasePost.Title != "engadget_news" {
t.Errorf("embedded struct's value should be scanned correctly")
}

var egPosts []EngadgetPost
if err := DB.Order("author_name asc").Find(&egPosts).Error; err != nil {
t.Fatalf("no error should happen when query with embedded struct, but got %v", err)
}
expectAuthors := []string{"Edward", "George"}
for i, post := range egPosts {
t.Log(i, post.Author)
if want := expectAuthors[i]; post.Author.Name != want {
t.Errorf("expected author %s got %s", want, post.Author.Name)
}
}

}

func TestEmbeddedPointerTypeStruct(t *testing.T) {
Expand Down

0 comments on commit 4ec73c9

Please sign in to comment.