Skip to content

Commit

Permalink
Merge pull request #113 from pashagolub/102-scan-panics-for-sqlnulltime
Browse files Browse the repository at this point in the history
[-] fix `Scan()` panic if nullable `sql` types used, fixes #102
  • Loading branch information
pashagolub authored Nov 2, 2022
2 parents 30a71a2 + 697eba4 commit 4a470aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
16 changes: 3 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
linters:
enable:
- gocyclo
- golint
- misspell
disable:
- structcheck # too many false positives
- misspell
- revive

linters-settings:
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 20

issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
exclude:
- SA5008 # ignore staticcheck for go-flags
min-complexity: 20
4 changes: 2 additions & 2 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (rs *rowSets) Scan(dest ...interface{}) error {
continue
}
val := reflect.ValueOf(col)
if _, ok := dest[i].(*interface{}); ok || destVal.Elem().Kind() == val.Kind() {
if _, ok := dest[i].(*interface{}); ok || val.Type().AssignableTo(destVal.Elem().Type()) {
if destElem := destVal.Elem(); destElem.CanSet() {
destElem.Set(val)
} else {
return fmt.Errorf("Cannot set destination value for column %s", string(r.defs[i].Name))
return fmt.Errorf("Cannot set destination value for column %s", string(r.defs[i].Name))
}
} else {
// Try to use Scanner interface
Expand Down
33 changes: 33 additions & 0 deletions sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pgxmock_test

import (
"context"
"database/sql"
"testing"
"time"

pgxmock "github.com/pashagolub/pgxmock/v2"
)

func TestScanTime(t *testing.T) {
mock, err := pgxmock.NewPool()
if err != nil {
panic(err)
}

now, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z07:00")

mock.ExpectQuery(`SELECT now()`).
WillReturnRows(
mock.NewRows([]string{"stamp"}).
AddRow(now))

var value sql.NullTime
err = mock.QueryRow(context.Background(), `SELECT now()`).Scan(&value)
if err != nil {
t.Error(err)
}
if value.Time != now {
t.Errorf("want %v, got %v", now, value.Time)
}
}

0 comments on commit 4a470aa

Please sign in to comment.