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

Respect structs which implement sql.Scanner #81

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func initFieldTag(sliceItem reflect.Value, fieldTagMap *map[string]reflect.Value
}
}

var sqlScannerType = reflect.TypeOf((*sql.Scanner)(nil)).Elem()

func structPointers(sliceItem reflect.Value, cols []string, strict bool) []interface{} {
pointers := make([]interface{}, 0, len(cols))
fieldTag := make(map[string]reflect.Value, len(cols))
Expand All @@ -179,6 +181,13 @@ func structPointers(sliceItem reflect.Value, cols []string, strict bool) []inter
fieldVal = reflect.ValueOf(nil)
} else {
fieldVal = sliceItem.FieldByName(ScannerMapper(colName))
if fieldVal == (reflect.Value{}) {
if sliceItem.Addr().Type().Implements(sqlScannerType) {
// probably this is a custom struct that implements sql.Scanner.
// do our best and don't set "nothing" as a pointer
fieldVal = sliceItem
}
}
}
}
if !fieldVal.IsValid() || !fieldVal.CanSet() {
Expand Down
72 changes: 72 additions & 0 deletions sql_scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package scan_test

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/require"

"github.com/blockloop/scan/v2"
)

func TestCustomScanner(t *testing.T) {
db := mustDB(t.Name(), `
CREATE TABLE test
(
id int PRIMARY KEY,
data int NOT NULL
)`,
`INSERT INTO test (id, data) VALUES (1, 123), (2, 234)`,
)
t.Cleanup(func() { db.Close() })

const (
selectOneQuery = `SELECT data FROM test WHERE id = 1`
selectAllQuery = `SELECT data FROM test`
)

t.Run("scan.Row must work", func(t *testing.T) {
var data customScanner
rows, err := db.Query(selectOneQuery)
require.NoError(t, err)

err = scan.Row(&data, rows)
require.NoError(t, err)
require.Equal(t, customScanner{v: 123}, data)
})

t.Run("scan.Rows must work", func(t *testing.T) {
var data []customScanner
rows, err := db.Query(selectAllQuery)
require.NoError(t, err)

err = scan.Rows(&data, rows)
require.NoError(t, err)
require.ElementsMatch(t, []customScanner{{v: 123}, {v: 234}}, data)
})

}

type customScanner struct {
v int64
}

func (c *customScanner) Scan(src interface{}) error {
switch v := src.(type) {
case int64:
*c = customScanner{v: v}
case []byte: // for ramsql
n, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
return fmt.Errorf("parse int: %w", err)
}
*c = customScanner{v: n}
case nil:
return nil
default:
return fmt.Errorf("unsupported type %T", src)
}

return nil
}
Loading