From 81c4024232c35c3d49907f3ae77c2857a1dd7f63 Mon Sep 17 00:00:00 2001 From: Hasan Date: Thu, 7 Apr 2022 21:56:41 +0600 Subject: [PATCH] Offset issue resolved for scanning results back into struct (#5227) --- scan.go | 2 +- tests/scan_test.go | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/scan.go b/scan.go index c8da13da..2ce6bd28 100644 --- a/scan.go +++ b/scan.go @@ -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 diff --git a/tests/scan_test.go b/tests/scan_test.go index ec1e652f..425c0a29 100644 --- a/tests/scan_test.go +++ b/tests/scan_test.go @@ -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") } }