forked from mirror/gorm
Add test case for embedded value selects (#5901)
* Add test case for embedded value selects * Revert recycle struct optimisation to avoid pointer overwrites
This commit is contained in:
parent
d9525d4da4
commit
4ec73c9bf4
6
scan.go
6
scan.go
|
@ -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 {
|
||||
|
@ -242,7 +241,6 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
|
|||
case reflect.Slice, reflect.Array:
|
||||
var (
|
||||
elem reflect.Value
|
||||
recyclableStruct = reflect.New(reflectValueType)
|
||||
isArrayKind = reflectValue.Kind() == reflect.Array
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
db.scanIntoStruct(rows, elem, values, fields, joinFields)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue