Merge pull request #153 from yveshield/more-int-to-bool

Add more integer to boolean conversions
This commit is contained in:
Márk Sági-Kazár 2023-02-05 21:45:11 +01:00 committed by GitHub
commit ba0a5b63c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 4 deletions

View File

@ -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},

View File

@ -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: