Skip to content

Commit

Permalink
Merge pull request #153 from yveshield/more-int-to-bool
Browse files Browse the repository at this point in the history
Add more integer to boolean conversions
  • Loading branch information
sagikazarmark authored Feb 5, 2023
2 parents 7ef7a18 + 557a6a2 commit ba0a5b6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
28 changes: 28 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand Down
29 changes: 25 additions & 4 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit ba0a5b6

Please sign in to comment.