Skip to content

Commit 998f60f

Browse files
committed
support scan boolean type
1 parent 0a22272 commit 998f60f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

scanner/scanner.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ func convert(mapValue interface{}, valuei reflect.Value, wrapErr convertErrWrapp
375375
valuei.SetInt(mapValue.(int64))
376376
} else if isUintSeriesType(vit.Kind()) {
377377
valuei.SetUint(uint64(mapValue.(int64)))
378+
} else if vit.Kind() == reflect.Bool {
379+
v := mapValue.(int64)
380+
if v > 0 {
381+
valuei.SetBool(true)
382+
} else {
383+
valuei.SetBool(false)
384+
}
378385
} else {
379386
return wrapErr(mvt, vit)
380387
}
@@ -426,6 +433,16 @@ func handleConvertSlice(mapValue interface{}, mvt, vit reflect.Type, valuei *ref
426433
return wrapErr(mvt, vit)
427434
}
428435
valuei.SetFloat(floatVal)
436+
case vitKind == reflect.Bool:
437+
intVal, err := strconv.ParseInt(mapValueStr, 10, 64)
438+
if nil != err {
439+
return wrapErr(mvt, vit)
440+
}
441+
if intVal > 0 {
442+
valuei.SetBool(true)
443+
} else {
444+
valuei.SetBool(false)
445+
}
429446
default:
430447
if _, ok := valuei.Interface().(ByteUnmarshaler); ok {
431448
return byteUnmarshal(mapValueSlice, valuei, wrapErr)

scanner/scanner_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,70 @@ func Test_Slice_2_Float(t *testing.T) {
603603
}
604604
}
605605

606+
func Test_int64_2_bool(t *testing.T) {
607+
type user struct {
608+
Name string `ddb:"name"`
609+
IsGirl bool `ddb:"ig"`
610+
}
611+
var testData = []struct {
612+
in map[string]interface{}
613+
out user
614+
err error
615+
}{
616+
{
617+
in: map[string]interface{}{
618+
"name": "foo",
619+
"ig": int64(1),
620+
},
621+
out: user{
622+
Name: "foo",
623+
IsGirl: true,
624+
},
625+
},
626+
{
627+
in: map[string]interface{}{
628+
"name": "bar",
629+
"ig": []uint8("10"),
630+
},
631+
out: user{
632+
Name: "bar",
633+
IsGirl: true,
634+
},
635+
},
636+
{
637+
in: map[string]interface{}{
638+
"name": "bar",
639+
"ig": int64(0),
640+
},
641+
out: user{
642+
Name: "bar",
643+
IsGirl: false,
644+
},
645+
},
646+
{
647+
in: map[string]interface{}{
648+
"name": "bar",
649+
"ig": []byte("-1"),
650+
},
651+
out: user{
652+
Name: "bar",
653+
IsGirl: false,
654+
},
655+
},
656+
}
657+
ass := assert.New(t)
658+
for _, tc := range testData {
659+
var u user
660+
err := bind(tc.in, &u)
661+
if tc.err == nil {
662+
ass.NoError(err)
663+
} else {
664+
ass.Error(err)
665+
}
666+
ass.Equal(tc.out, u)
667+
}
668+
}
669+
606670
func Test_uint8_2_any(t *testing.T) {
607671
type user struct {
608672
Name string `ddb:"name"`

0 commit comments

Comments
 (0)