Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error result when transform digital type (except int) to bool #137

Closed
tiyee opened this issue Apr 25, 2022 · 0 comments · Fixed by #153
Closed

Return error result when transform digital type (except int) to bool #137

tiyee opened this issue Apr 25, 2022 · 0 comments · Fixed by #153

Comments

@tiyee
Copy link

tiyee commented Apr 25, 2022

As title said, when you transform int16, int64 or other digital type (not int) to bool, return an error result.

func try()  {
	var i int64 = 1
	fmt.Println(cast.ToBool(i)) // false
}

because cast only assert int type.

// ToBoolE casts an interface to a bool type.
func ToBoolE(i interface{}) (bool, error) {
	i = indirect(i)

	switch b := i.(type) {
	case bool:
		return b, nil
	case nil:
		return false, nil
	case int:
		if i.(int) != 0 {
			return true, nil
		}
		return false, nil
	case string:
		return strconv.ParseBool(i.(string))
	default:
		return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant