package convx import ( "fmt" "strconv" ) // ToBool converts v to bool. func ToBool(v interface{}) (bool, error) { switch v := v.(type) { case bool: return v, nil case string: if v, err := strconv.ParseBool(v); err == nil { return v, nil } else { return false, err } case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: i, err := ToInt(v) if err != nil { return false, err } return i > 0, nil default: return false, fmt.Errorf("unsupported type %T", v) } }