Skip to content

Commit

Permalink
[+] allow to Scan() destination of *interface{} type, fixes #73 (#74
Browse files Browse the repository at this point in the history
)

* [+] allow to `Scan()` destination of `*interface{}` type, fixes #73
  • Loading branch information
pashagolub authored May 19, 2022
1 parent 147cfc2 commit d1c9fe2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func (rs *rowSets) Scan(dest ...interface{}) error {
continue
}
val := reflect.ValueOf(col)
if destVal.Elem().Kind() == val.Kind() {
if _, ok := dest[i].(*interface{}); ok || destVal.Elem().Kind() == val.Kind() {
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
19 changes: 19 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import (
"github.com/jackc/pgconn"
)

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

mock.ExpectQuery(`SELECT 123`).
WillReturnRows(
mock.NewRows([]string{"id"}).
AddRow(int64(123))) // Value which should be scanned in *interface{}

var value interface{}
err = mock.QueryRow(context.Background(), `SELECT 123`).Scan(&value)
if err != nil || value.(int64) != 123 {
t.Error(err)
}

}

func TestExplicitTypeCasting(t *testing.T) {
mock, err := NewPool()
if err != nil {
Expand Down

0 comments on commit d1c9fe2

Please sign in to comment.