diff --git a/scanner/scanner.go b/scanner/scanner.go index 192b8d1..858d6f2 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -264,10 +264,21 @@ func bind(result map[string]interface{}, target interface{}) (resp error) { if !valuei.CanSet() { continue } + tagName, ok := lookUpTagName(fieldTypeI) if !ok || "" == tagName { + switch fieldTypeI.Type.Kind() { + //如果是结构体并且未设置tag,递归调用bind + case reflect.Struct, reflect.Ptr: + err := bind(result, valuei.Addr().Interface()) + if nil != err { + return err + } + } + continue } + mapValue, ok := result[tagName] if !ok || mapValue == nil { continue diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 8846b6c..cac39a6 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -5,12 +5,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/stretchr/testify/require" "math" "reflect" "testing" "time" + "github.com/stretchr/testify/require" + "github.com/DATA-DOG/go-sqlmock" ) @@ -1177,15 +1178,28 @@ func TestUnmarshalByte(t *testing.T) { func TestScanClose(t *testing.T) { rows := &fakeRows{ - columns: []string{"foo", "bar"}, + columns: []string{"foo", "bar", "foo1", "bar1"}, dataset: [][]interface{}{ - []interface{}{1, 2}, + []interface{}{1, 2, 3, 4}, }, } - var testObj = struct { + + type S1 struct { + Foo1 int `ddb:"foo1"` + } + + type S2 struct { + Bar1 int `ddb:"bar1"` + } + + type SS struct { + S1 + *S2 Foo int `ddb:"foo"` Bar int `ddb:"bar"` - }{} + } + + var testObj = SS{} should := require.New(t) err := ScanClose(rows, &testObj) e, ok := err.(CloseErr) @@ -1193,6 +1207,8 @@ func TestScanClose(t *testing.T) { should.Equal(errCloseForTest.Error(), e.Error()) should.Equal(1, testObj.Foo) should.Equal(2, testObj.Bar) + should.Equal(3, testObj.Foo1) + should.Equal(4, testObj.Bar1) } func TestErrClose(t *testing.T) {