diff --git a/scanner/scanner.go b/scanner/scanner.go index 192b8d1..45e4d39 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -32,6 +32,8 @@ type Rows interface { Next() bool Scan(dest ...interface{}) error + + Err() error } const ( @@ -329,6 +331,10 @@ func resolveDataFromRows(rows Rows) ([]map[string]interface{}, error) { } result = append(result, mp) } + err = rows.Err() + if nil != err { + return nil, err + } return result, nil } diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 8846b6c..ea0cceb 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -1,6 +1,7 @@ package scanner import ( + "context" "database/sql" "encoding/json" "errors" @@ -987,7 +988,9 @@ type fakeRows struct { idx int } +var gCtx context.Context = context.Background() var errCloseForTest = errors.New("just for test") +var errCancelForTest = errors.New("context canceled") func (r *fakeRows) Close() error { return errCloseForTest @@ -1020,6 +1023,14 @@ func (r *fakeRows) Scan(dt ...interface{}) (err error) { return nil } +func (r *fakeRows) Err() error { + err := gCtx.Err() + if err != nil { + return err + } + return nil +} + func TestScanNotSettable(t *testing.T) { should := require.New(t) err := Scan(&fakeRows{}, nil) @@ -1076,6 +1087,31 @@ func TestScanMock(t *testing.T) { should.Equal(24, boys[1].Age) } +func TestScanCtxErr(t *testing.T) { + should := require.New(t) + scannn := &fakeRows{ + columns: []string{"name", "age"}, + dataset: [][]interface{}{ + {"deen", 23}, + {"caibirdme", 24}, + }, + } + type curdBoy struct { + Name string `ddb:"name"` + Age int `ddb:"age"` + } + var boys []curdBoy + defaultTag := DefaultTagName + userDefinedTagName = &defaultTag + + var cancle context.CancelFunc + gCtx, cancle = context.WithCancel(gCtx) + cancle() + err := Scan(scannn, &boys) + should.Equal(errCancelForTest.Error(), err.Error()) + gCtx = context.Background() +} + func TestScanEmpty(t *testing.T) { should := require.New(t) scannn := &fakeRows{}