Skip to content

Commit

Permalink
Merge pull request #123 from doug-martin/issue122-fix
Browse files Browse the repository at this point in the history
Fix for #122
  • Loading branch information
doug-martin authored Aug 3, 2019
2 parents 0ba9f96 + fb3f85f commit 2137511
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 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
45 changes: 27 additions & 18 deletions internal/util/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,20 +487,21 @@ func (rt *reflectTest) TestAssignStructVals_withStructWithPointerVals() {
var ts TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns := &sql.NullString{String: "null_str1", Valid: true}
data := []map[string]interface{}{
{
"str": "string",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str", Valid: true},
"valuer": &ns,
},
}
AssignStructVals(&ts, data, cm)
assert.Equal(t, ts, TestStruct{
Str: "string",
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str", Valid: true},
Valuer: ns,
})
}

Expand All @@ -519,20 +520,21 @@ func (rt *reflectTest) TestAssignStructVals_withStructWithEmbeddedStruct() {
var ts TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns := &sql.NullString{String: "null_str1", Valid: true}
data := []map[string]interface{}{
{
"str": "string",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str", Valid: true},
"valuer": &ns,
},
}
AssignStructVals(&ts, data, cm)
assert.Equal(t, ts, TestStruct{
EmbeddedStruct: EmbeddedStruct{Str: "string"},
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str", Valid: true},
Valuer: ns,
})
}

Expand All @@ -551,20 +553,21 @@ func (rt *reflectTest) TestAssignStructVals_withStructWithEmbeddedStructPointer(
var ts TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns := &sql.NullString{String: "null_str1", Valid: true}
data := []map[string]interface{}{
{
"str": "string",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str", Valid: true},
"valuer": &ns,
},
}
AssignStructVals(&ts, data, cm)
assert.Equal(t, ts, TestStruct{
EmbeddedStruct: &EmbeddedStruct{Str: "string"},
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str", Valid: true},
Valuer: ns,
})
}

Expand Down Expand Up @@ -666,18 +669,20 @@ func (rt *reflectTest) TestAssignStructVals_withSliceOfStructsWithPointerVals()
var ts []TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns1 := &sql.NullString{String: "null_str1", Valid: true}
ns2 := &sql.NullString{String: "null_str2", Valid: true}
data := []map[string]interface{}{
{
"str": "string1",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str1", Valid: true},
"valuer": &ns1,
},
{
"str": "string2",
"int": int64(20),
"bool": false,
"valuer": &sql.NullString{String: "null_str2", Valid: true},
"valuer": &ns2,
},
}
AssignStructVals(&ts, data, cm)
Expand All @@ -686,13 +691,13 @@ func (rt *reflectTest) TestAssignStructVals_withSliceOfStructsWithPointerVals()
Str: "string1",
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str1", Valid: true},
Valuer: ns1,
},
{
Str: "string2",
Int: 20,
Bool: false,
Valuer: &sql.NullString{String: "null_str2", Valid: true},
Valuer: ns2,
},
})
}
Expand All @@ -712,18 +717,20 @@ func (rt *reflectTest) TestAssignStructVals_withSliceofStructsWithEmbeddedStruct
var ts []TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns1 := &sql.NullString{String: "null_str1", Valid: true}
ns2 := &sql.NullString{String: "null_str2", Valid: true}
data := []map[string]interface{}{
{
"str": "string1",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str1", Valid: true},
"valuer": &ns1,
},
{
"str": "string2",
"int": int64(20),
"bool": false,
"valuer": &sql.NullString{String: "null_str2", Valid: true},
"valuer": &ns2,
},
}
AssignStructVals(&ts, data, cm)
Expand All @@ -732,13 +739,13 @@ func (rt *reflectTest) TestAssignStructVals_withSliceofStructsWithEmbeddedStruct
EmbeddedStruct: EmbeddedStruct{Str: "string1"},
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str1", Valid: true},
Valuer: ns1,
},
{
EmbeddedStruct: EmbeddedStruct{Str: "string2"},
Int: 20,
Bool: false,
Valuer: &sql.NullString{String: "null_str2", Valid: true},
Valuer: ns2,
},
})
}
Expand All @@ -758,18 +765,20 @@ func (rt *reflectTest) TestAssignStructVals_withSliceofStructsWithEmbeddedStruct
var ts []TestStruct
cm, err := GetColumnMap(&ts)
assert.NoError(t, err)
ns1 := &sql.NullString{String: "null_str1", Valid: true}
ns2 := &sql.NullString{String: "null_str2", Valid: true}
data := []map[string]interface{}{
{
"str": "string1",
"int": int64(10),
"bool": true,
"valuer": &sql.NullString{String: "null_str1", Valid: true},
"valuer": &ns1,
},
{
"str": "string2",
"int": int64(20),
"bool": false,
"valuer": &sql.NullString{String: "null_str2", Valid: true},
"valuer": &ns2,
},
}
AssignStructVals(&ts, data, cm)
Expand All @@ -778,13 +787,13 @@ func (rt *reflectTest) TestAssignStructVals_withSliceofStructsWithEmbeddedStruct
EmbeddedStruct: &EmbeddedStruct{Str: "string1"},
Int: 10,
Bool: true,
Valuer: &sql.NullString{String: "null_str1", Valid: true},
Valuer: ns1,
},
{
EmbeddedStruct: &EmbeddedStruct{Str: "string2"},
Int: 20,
Bool: false,
Valuer: &sql.NullString{String: "null_str2", Valid: true},
Valuer: ns2,
},
})
}
Expand Down

0 comments on commit 2137511

Please sign in to comment.