Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScanStruct support for structs with Interface types #399

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions exec/query_executor_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,52 @@ func (qes *queryExecutorSuite) TestScanStruct_taggedStructs() {
}, item)
}

func (qes *queryExecutorSuite) TestScanStruct_structWithInterfaceItemImplementingScanner() {
type TestStruct struct {
Value sql.Scanner
}

db, mock, err := sqlmock.New()
qes.NoError(err)

mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"value"}).
AddRow(testName1),
)

mock.ExpectQuery(`SELECT \* FROM "items"`).
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"value"}).
AddRow(1234),
)

e := newQueryExecutor(db, nil, `SELECT * FROM "items"`)
var item TestStruct
item.Value = &sql.NullString{}

found, err := e.ScanStruct(&item)
qes.NoError(err)
qes.True(found)
qes.Equal(TestStruct{
Value: &sql.NullString{
String: testName1,
Valid: true,
},
}, item)

item.Value = &sql.NullInt32{}
found, err = e.ScanStruct(&item)
qes.NoError(err)
qes.True(found)
qes.Equal(TestStruct{
Value: &sql.NullInt32{
Int32: 1234,
Valid: true,
},
}, item)
}

func (qes *queryExecutorSuite) TestScanVals() {
db, mock, err := sqlmock.New()
qes.NoError(err)
Expand Down
19 changes: 13 additions & 6 deletions internal/util/column_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,30 @@ type (
ColumnMap map[string]ColumnData
)

func newColumnMap(t reflect.Type, fieldIndex []int, prefixes []string) ColumnMap {
func newColumnMap(val reflect.Value, t reflect.Type, fieldIndex []int, prefixes []string) ColumnMap {
cm, n := ColumnMap{}, t.NumField()
var subColMaps []ColumnMap
for i := 0; i < n; i++ {
f := t.Field(i)
if f.Anonymous && (f.Type.Kind() == reflect.Struct || f.Type.Kind() == reflect.Ptr) {
goquTag := tag.New("db", f.Tag)
if !goquTag.Contains("-") {
subColMaps = append(subColMaps, getStructColumnMap(&f, fieldIndex, goquTag.Values(), prefixes))
subColMaps = append(subColMaps, getStructColumnMap(val.Field(i), &f, fieldIndex, goquTag.Values(), prefixes))
}
} else if f.PkgPath == "" {
dbTag := tag.New("db", f.Tag)
// if PkgPath is empty then it is an exported field
columnName := getColumnName(&f, dbTag)
if !shouldIgnoreField(dbTag) {
if f.Type.Kind() == reflect.Interface { // && !f.IsNil() {
v := val.Field(i)
if !v.IsNil() {
concreteType := v.Elem().Type()
f.Type = concreteType
}
}
if !implementsScanner(f.Type) {
subCm := getStructColumnMap(&f, fieldIndex, []string{columnName}, prefixes)
subCm := getStructColumnMap(val.Field(i), &f, fieldIndex, []string{columnName}, prefixes)
if len(subCm) != 0 {
subColMaps = append(subColMaps, subCm)
continue
Expand Down Expand Up @@ -100,14 +107,14 @@ func newColumnData(f *reflect.StructField, columnName string, fieldIndex []int,
}
}

func getStructColumnMap(f *reflect.StructField, fieldIndex []int, fieldNames, prefixes []string) ColumnMap {
func getStructColumnMap(val reflect.Value, f *reflect.StructField, fieldIndex []int, fieldNames, prefixes []string) ColumnMap {
subFieldIndexes := concatFieldIndexes(fieldIndex, f.Index)
subPrefixes := prefixes
subPrefixes = append(subPrefixes, fieldNames...)
if f.Type.Kind() == reflect.Ptr {
return newColumnMap(f.Type.Elem(), subFieldIndexes, subPrefixes)
return newColumnMap(val, f.Type.Elem(), subFieldIndexes, subPrefixes)
}
return newColumnMap(f.Type, subFieldIndexes, subPrefixes)
return newColumnMap(val, f.Type, subFieldIndexes, subPrefixes)
}

func getColumnName(f *reflect.StructField, dbTag tag.Options) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func GetColumnMap(i interface{}) (ColumnMap, error) {
structMapCacheLock.Lock()
defer structMapCacheLock.Unlock()
if _, ok := structMapCache[t]; !ok {
structMapCache[t] = newColumnMap(t, []int{}, []string{})
structMapCache[t] = newColumnMap(val, t, []int{}, []string{})
}
return structMapCache[t], nil
}