Skip to content

Commit

Permalink
Fix for #122
Browse files Browse the repository at this point in the history
* [FIX] Scanner errors on pointers to primitive values #122
  • Loading branch information
doug-martin committed Aug 3, 2019
1 parent 0ba9f96 commit 5868061
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
6 changes: 5 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## v8.2.2

* [FIX] Scanner errors on pointers to primitive values [#122](https://github.com/doug-martin/goqu/issues/122)

## v8.2.1

* [FIX] Return an error when an empty identifier is encountered [#115](https://github.com/doug-martin/goqu/issues/118)
* [FIX] Return an error when an empty identifier is encountered [#115](https://github.com/doug-martin/goqu/issues/115)

## v8.2.0

Expand Down
2 changes: 0 additions & 2 deletions exec/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ func (q *scanner) scanIntoRecord(columns []string, cm util.ColumnMap) (record ex
switch {
case !ok:
return record, unableToFindFieldError(col)
case util.IsPointer(data.GoType.Kind()):
scans[i] = reflect.New(data.GoType.Elem()).Interface()
default:
scans[i] = reflect.New(data.GoType).Interface()
}
Expand Down
26 changes: 19 additions & 7 deletions exec/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -103,24 +104,35 @@ func (cet *crudExecTest) TestScanStructs_withUntaggedFields() {

func (cet *crudExecTest) TestScanStructs_withPointerFields() {
type StructWithPointerFields struct {
Address *string
Name *string
Str *string
Time *time.Time
Bool *bool
Int *int64
Float *float64
}
db, mock, err := sqlmock.New()
cet.NoError(err)

now := time.Now()
str1, str2 := "str1", "str2"
t := true
var i1, i2 int64 = 1, 2
var f1, f2 float64 = 1.1, 2.1
mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))
WillReturnRows(sqlmock.NewRows([]string{"str", "time", "bool", "int", "float"}).
AddRow(str1, now, true, i1, f1).
AddRow(str2, now, true, i2, f2).
AddRow(nil, nil, nil, nil, nil),
)

e := newQueryExecutor(db, nil, `SELECT * FROM "items"`)

var items []StructWithPointerFields
cet.NoError(e.ScanStructs(&items))
cet.Equal([]StructWithPointerFields{
{Address: &testAddr1, Name: &testName1},
{Address: &testAddr2, Name: &testName2},
{Str: &str1, Time: &now, Bool: &t, Int: &i1, Float: &f1},
{Str: &str2, Time: &now, Bool: &t, Int: &i2, Float: &f2},
{},
}, items)
}

Expand Down
8 changes: 2 additions & 6 deletions internal/util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,9 @@ func assignRowData(row reflect.Value, rd rowData, cm ColumnMap) {
for name, data := range cm {
src, ok := rd[name]
if ok {
srcVal := reflect.ValueOf(src)
f := row.FieldByIndex(data.FieldIndex)
if f.Kind() == reflect.Ptr {
f.Set(srcVal)
} else {
f.Set(reflect.Indirect(srcVal))
}
srcVal := reflect.ValueOf(src)
f.Set(reflect.Indirect(srcVal))
}
}
}
Expand Down

0 comments on commit 5868061

Please sign in to comment.