mirror of https://github.com/spf13/cast.git
Add more integer to boolean conversions
Signed-off-by: Yveshield <yveshield@gmail.com>
This commit is contained in:
parent
2b0eb0f724
commit
557a6a2fb7
28
cast_test.go
28
cast_test.go
|
@ -796,6 +796,18 @@ func TestToBoolE(t *testing.T) {
|
|||
iserr bool
|
||||
}{
|
||||
{0, false, false},
|
||||
{int64(0), false, false},
|
||||
{int32(0), false, false},
|
||||
{int16(0), false, false},
|
||||
{int8(0), false, false},
|
||||
{uint(0), false, false},
|
||||
{uint64(0), false, false},
|
||||
{uint32(0), false, false},
|
||||
{uint16(0), false, false},
|
||||
{uint8(0), false, false},
|
||||
{float64(0), false, false},
|
||||
{float32(0), false, false},
|
||||
{time.Duration(0), false, false},
|
||||
{jf, false, false},
|
||||
{nil, false, false},
|
||||
{"false", false, false},
|
||||
|
@ -811,10 +823,26 @@ func TestToBoolE(t *testing.T) {
|
|||
{"t", true, false},
|
||||
{"T", true, false},
|
||||
{1, true, false},
|
||||
{int64(1), true, false},
|
||||
{int32(1), true, false},
|
||||
{int16(1), true, false},
|
||||
{int8(1), true, false},
|
||||
{uint(1), true, false},
|
||||
{uint64(1), true, false},
|
||||
{uint32(1), true, false},
|
||||
{uint16(1), true, false},
|
||||
{uint8(1), true, false},
|
||||
{float64(1), true, false},
|
||||
{float32(1), true, false},
|
||||
{time.Duration(1), true, false},
|
||||
{jt, true, false},
|
||||
{je, true, false},
|
||||
{true, true, false},
|
||||
{-1, true, false},
|
||||
{int64(-1), true, false},
|
||||
{int32(-1), true, false},
|
||||
{int16(-1), true, false},
|
||||
{int8(-1), true, false},
|
||||
|
||||
// errors
|
||||
{"test", false, true},
|
||||
|
|
29
caste.go
29
caste.go
|
@ -98,10 +98,31 @@ func ToBoolE(i interface{}) (bool, error) {
|
|||
case nil:
|
||||
return false, nil
|
||||
case int:
|
||||
if i.(int) != 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
return b != 0, nil
|
||||
case int64:
|
||||
return b != 0, nil
|
||||
case int32:
|
||||
return b != 0, nil
|
||||
case int16:
|
||||
return b != 0, nil
|
||||
case int8:
|
||||
return b != 0, nil
|
||||
case uint:
|
||||
return b != 0, nil
|
||||
case uint64:
|
||||
return b != 0, nil
|
||||
case uint32:
|
||||
return b != 0, nil
|
||||
case uint16:
|
||||
return b != 0, nil
|
||||
case uint8:
|
||||
return b != 0, nil
|
||||
case float64:
|
||||
return b != 0, nil
|
||||
case float32:
|
||||
return b != 0, nil
|
||||
case time.Duration:
|
||||
return b != 0, nil
|
||||
case string:
|
||||
return strconv.ParseBool(i.(string))
|
||||
case json.Number:
|
||||
|
|
Loading…
Reference in New Issue