Skip to content

Commit

Permalink
support scan boolean type
Browse files Browse the repository at this point in the history
  • Loading branch information
caibirdme committed May 14, 2019
1 parent 0a22272 commit 998f60f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ func convert(mapValue interface{}, valuei reflect.Value, wrapErr convertErrWrapp
valuei.SetInt(mapValue.(int64))
} else if isUintSeriesType(vit.Kind()) {
valuei.SetUint(uint64(mapValue.(int64)))
} else if vit.Kind() == reflect.Bool {
v := mapValue.(int64)
if v > 0 {
valuei.SetBool(true)
} else {
valuei.SetBool(false)
}
} else {
return wrapErr(mvt, vit)
}
Expand Down Expand Up @@ -426,6 +433,16 @@ func handleConvertSlice(mapValue interface{}, mvt, vit reflect.Type, valuei *ref
return wrapErr(mvt, vit)
}
valuei.SetFloat(floatVal)
case vitKind == reflect.Bool:
intVal, err := strconv.ParseInt(mapValueStr, 10, 64)
if nil != err {
return wrapErr(mvt, vit)
}
if intVal > 0 {
valuei.SetBool(true)
} else {
valuei.SetBool(false)
}
default:
if _, ok := valuei.Interface().(ByteUnmarshaler); ok {
return byteUnmarshal(mapValueSlice, valuei, wrapErr)
Expand Down
64 changes: 64 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,70 @@ func Test_Slice_2_Float(t *testing.T) {
}
}

func Test_int64_2_bool(t *testing.T) {
type user struct {
Name string `ddb:"name"`
IsGirl bool `ddb:"ig"`
}
var testData = []struct {
in map[string]interface{}
out user
err error
}{
{
in: map[string]interface{}{
"name": "foo",
"ig": int64(1),
},
out: user{
Name: "foo",
IsGirl: true,
},
},
{
in: map[string]interface{}{
"name": "bar",
"ig": []uint8("10"),
},
out: user{
Name: "bar",
IsGirl: true,
},
},
{
in: map[string]interface{}{
"name": "bar",
"ig": int64(0),
},
out: user{
Name: "bar",
IsGirl: false,
},
},
{
in: map[string]interface{}{
"name": "bar",
"ig": []byte("-1"),
},
out: user{
Name: "bar",
IsGirl: false,
},
},
}
ass := assert.New(t)
for _, tc := range testData {
var u user
err := bind(tc.in, &u)
if tc.err == nil {
ass.NoError(err)
} else {
ass.Error(err)
}
ass.Equal(tc.out, u)
}
}

func Test_uint8_2_any(t *testing.T) {
type user struct {
Name string `ddb:"name"`
Expand Down

0 comments on commit 998f60f

Please sign in to comment.