Skip to content

Commit

Permalink
feat: support empty tag (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: jieshuang <[email protected]>
  • Loading branch information
nlimpid and jieshuang authored Mar 20, 2023
1 parent becd38a commit dbb2872
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
25 changes: 13 additions & 12 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ByteUnmarshaler interface {
UnmarshalByte(data []byte) error
}

//Rows defines methods that scanner needs, which database/sql.Rows already implements
// Rows defines methods that scanner needs, which database/sql.Rows already implements
type Rows interface {
Close() error

Expand All @@ -41,7 +41,7 @@ const (
)

var (
userDefinedTagName string
userDefinedTagName *string
//ErrTargetNotSettable means the second param of Bind is not settable
ErrTargetNotSettable = errors.New("[scanner]: target is not settable! a pointer is required")
//ErrNilRows means the first param can't be a nil
Expand All @@ -52,15 +52,15 @@ var (
ErrEmptyResult = errors.New(`[scanner]: empty result`)
)

//SetTagName can be set only once
// SetTagName can be set only once
func SetTagName(name string) {
if userDefinedTagName != "" {
if userDefinedTagName != nil {
return
}
userDefinedTagName = name
userDefinedTagName = &name
}

//ScanErr will be returned if an underlying type couldn't be AssignableTo type of target field
// ScanErr will be returned if an underlying type couldn't be AssignableTo type of target field
type ScanErr struct {
structName, fieldName string
from, to reflect.Type
Expand Down Expand Up @@ -202,7 +202,7 @@ func ScanClose(rows Rows, target interface{}) error {
return err
}

//caller must guarantee to pass a &slice as the second param
// caller must guarantee to pass a &slice as the second param
func bindSlice(arr []map[string]interface{}, target interface{}) error {
targetObj := reflect.ValueOf(target)
if !targetObj.Elem().CanSet() {
Expand Down Expand Up @@ -333,11 +333,12 @@ func resolveDataFromRows(rows Rows) ([]map[string]interface{}, error) {
}

func lookUpTagName(typeObj reflect.StructField) (string, bool) {
var tName string
if "" != userDefinedTagName {
tName = userDefinedTagName
} else {
tName = DefaultTagName
tName := DefaultTagName
if userDefinedTagName != nil {
tName = *userDefinedTagName
}
if tName == "" {
return typeObj.Name, true
}
name, ok := typeObj.Tag.Lookup(tName)
if !ok {
Expand Down
37 changes: 30 additions & 7 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,15 +948,37 @@ func Test_sql_scanner_with_pointer(t *testing.T) {
}
}

func TestBindWithEmptyTag(t *testing.T) {
emptyStr := ""
userDefinedTagName = &emptyStr
type Person struct {
Name string
Age int
}
var p Person
name := "deen"
age := 23
var mp = map[string]interface{}{
"Name": name,
"Age": age,
}
err := bind(mp, &p)
should := require.New(t)
should.NoError(err)
should.Equal(name, p.Name)
should.Equal(age, p.Age)
}

func TestTagSetOnlyOnce(t *testing.T) {
userDefinedTagName = "a"
a := "a"
userDefinedTagName = &a
SetTagName("foo")
require.Equal(t, "a", userDefinedTagName)
userDefinedTagName = ""
require.Equal(t, "a", *userDefinedTagName)
userDefinedTagName = nil
SetTagName("foo")
require.Equal(t, "foo", userDefinedTagName)
require.Equal(t, "foo", *userDefinedTagName)
// restore default tag
userDefinedTagName = DefaultTagName
userDefinedTagName = nil
}

type fakeRows struct {
Expand Down Expand Up @@ -1044,7 +1066,8 @@ func TestScanMock(t *testing.T) {
Age int `ddb:"age"`
}
var boys []curdBoy
userDefinedTagName = "ddb"
defaultTag := DefaultTagName
userDefinedTagName = &defaultTag
err := Scan(scannn, &boys)
should.NoError(err)
should.Equal("deen", boys[0].Name)
Expand All @@ -1061,7 +1084,7 @@ func TestScanEmpty(t *testing.T) {
Age int `ddb:"age"`
}
var boys []curdBoy
userDefinedTagName = "ddb"
*userDefinedTagName = "ddb"
err := Scan(scannn, &boys)
should.NoError(err)
should.Equal(0, len(boys))
Expand Down

0 comments on commit dbb2872

Please sign in to comment.