Offset issue resolved for scanning results back into struct (#5227)

This commit is contained in:
Hasan 2022-04-07 21:56:41 +06:00 committed by GitHub
parent 38a24606da
commit 81c4024232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -196,7 +196,7 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
for idx, column := range columns {
if field := sch.LookUpField(column); field != nil && field.Readable {
if curIndex, ok := selectedColumnsMap[column]; ok {
for fieldIndex, selectField := range sch.Fields[curIndex:] {
for fieldIndex, selectField := range sch.Fields[curIndex+1:] {
if selectField.DBName == column && selectField.Readable {
selectedColumnsMap[column] = curIndex + fieldIndex + 1
fields[idx] = selectField

View File

@ -184,11 +184,34 @@ func TestScanToEmbedded(t *testing.T) {
t.Errorf("Failed to run join query, got error: %v", err)
}
personMatched := false
addressMatched := false
for _, info := range personAddressInfoList {
if info.Person != nil {
if info.Person.ID == person1.ID && info.Person.Name != person1.Name {
if info.Person == nil {
t.Fatalf("Failed, expected not nil, got person nil")
}
if info.Address == nil {
t.Fatalf("Failed, expected not nil, got address nil")
}
if info.Person.ID == person1.ID {
personMatched = true
if info.Person.Name != person1.Name {
t.Errorf("Failed, expected %v, got %v", person1.Name, info.Person.Name)
}
}
if info.Address.ID == address1.ID {
addressMatched = true
if info.Address.Name != address1.Name {
t.Errorf("Failed, expected %v, got %v", address1.Name, info.Address.Name)
}
}
}
if !personMatched {
t.Errorf("Failed, no person matched")
}
if !addressMatched {
t.Errorf("Failed, no address matched")
}
}