Revert recycle struct optimisation to avoid pointer overwrites

This commit is contained in:
Edward McFarlane 2022-12-05 12:44:21 +00:00
parent a58159e00e
commit bb86abb588
2 changed files with 5 additions and 11 deletions

12
scan.go
View File

@ -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 {
@ -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 {
@ -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)

View File

@ -89,9 +89,9 @@ func TestEmbeddedStruct(t *testing.T) {
}
expectAuthors := []string{"Edward", "George"}
for i, post := range egPosts {
t.Log(i, post)
t.Log(i, post.Author)
if want := expectAuthors[i]; post.Author.Name != want {
t.Errorf("expected author %s got %s", post.Author.Name, want)
t.Errorf("expected author %s got %s", want, post.Author.Name)
}
}