diff --git a/scanner/scanner.go b/scanner/scanner.go index 9bca8ea..9c00374 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -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) } @@ -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) diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index d1832a7..364d2d7 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -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"`