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

add scan to sub struct field #139

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -1177,22 +1178,37 @@ 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)
should.True(ok)
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) {
Expand Down