Caste now returns error instead of bool.

This commit is contained in:
spf13 2014-04-24 10:21:33 -06:00
parent b252e9f21d
commit d578378de0
1 changed files with 75 additions and 72 deletions

147
caste.go
View File

@ -8,140 +8,136 @@ package cast
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"strconv" "strconv"
"time" "time"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
func ToTimeE(i interface{}) (tim time.Time, ok bool) { func ToTimeE(i interface{}) (tim time.Time, err error) {
jww.DEBUG.Println("ToTimeE called on type:", reflect.TypeOf(i))
switch s := i.(type) { switch s := i.(type) {
case time.Time: case time.Time:
return s, true return s, nil
case string: case string:
d, e := StringToDate(s) d, e := StringToDate(s)
if e == nil { if e == nil {
return d, true return d, nil
} }
return time.Time{}, fmt.Errorf("Could not parse Date/Time format: %v\n", e)
jww.ERROR.Println("Could not parse Date/Time format:", e)
return time.Time{}, false
default: default:
jww.ERROR.Printf("Unable to Cast %#v to Time", i) return time.Time{}, fmt.Errorf("Unable to Cast %#v to Time\n", i)
return time.Time{}, false
} }
return time.Time{}, false
} }
func ToBoolE(i interface{}) (bool, bool) { func ToBoolE(i interface{}) (bool, error) {
jww.DEBUG.Println("ToBoolE called on type:", reflect.TypeOf(i))
switch b := i.(type) { switch b := i.(type) {
case bool: case bool:
return b, true return b, nil
case nil: case nil:
return false, true return false, nil
case int: case int:
if i.(int) > 0 { if i.(int) > 0 {
return true, true return true, nil
} }
return false, true return false, nil
default: default:
return false, false return false, fmt.Errorf("Unable to Cast %#v to bool", i)
jww.ERROR.Printf("Unable to Cast %#v to bool", i)
} }
return false, false
} }
func ToFloat64E(i interface{}) (float64, bool) { func ToFloat64E(i interface{}) (float64, error) {
jww.DEBUG.Println("ToFloat64E called on type:", reflect.TypeOf(i))
switch s := i.(type) { switch s := i.(type) {
case float64: case float64:
return s, true return s, nil
case float32: case float32:
return float64(s), true return float64(s), nil
case int64: case int64:
return float64(s), true return float64(s), nil
case int32: case int32:
return float64(s), true return float64(s), nil
case int16: case int16:
return float64(s), true return float64(s), nil
case int8: case int8:
return float64(s), true return float64(s), nil
case int: case int:
return float64(s), true return float64(s), nil
case string: case string:
v, err := strconv.ParseFloat(s, 64) v, err := strconv.ParseFloat(s, 64)
if err == nil { if err == nil {
return float64(v), true return float64(v), nil
} else { } else {
jww.ERROR.Printf("Unable to Cast %#v to float", i) return 0.0, fmt.Errorf("Unable to Cast %#v to float", i)
jww.ERROR.Println(err)
} }
default: default:
jww.ERROR.Printf("Unable to Cast %#v to float", i) return 0.0, fmt.Errorf("Unable to Cast %#v to float", i)
} }
return 0.0, false
} }
func ToIntE(i interface{}) (int, bool) { func ToIntE(i interface{}) (int, error) {
jww.DEBUG.Println("ToIntE called on type:", reflect.TypeOf(i))
switch s := i.(type) { switch s := i.(type) {
case int: case int:
return s, true return s, nil
case int64: case int64:
return int(s), true return int(s), nil
case int32: case int32:
return int(s), true return int(s), nil
case int16: case int16:
return int(s), true return int(s), nil
case int8: case int8:
return int(s), true return int(s), nil
case string: case string:
v, err := strconv.ParseInt(s, 0, 0) v, err := strconv.ParseInt(s, 0, 0)
if err == nil { if err == nil {
return int(v), true return int(v), nil
} else { } else {
jww.ERROR.Printf("Unable to Cast %#v to int", i) return 0, fmt.Errorf("Unable to Cast %#v to int", i)
jww.ERROR.Println(err)
} }
case float64: case float64:
return int(s), true return int(s), nil
case bool: case bool:
if bool(s) { if bool(s) {
return 1, true return 1, nil
} else { } else {
return 0, true return 0, nil
} }
case nil: case nil:
return 0, true return 0, nil
default: default:
jww.ERROR.Printf("Unable to Cast %#v to int", i) return 0, fmt.Errorf("Unable to Cast %#v to int", i)
} }
return 0, false
} }
func ToStringE(i interface{}) (string, bool) { func ToStringE(i interface{}) (string, error) {
jww.DEBUG.Println("ToStringE called on type:", reflect.TypeOf(i))
switch s := i.(type) { switch s := i.(type) {
case string: case string:
return s, true return s, nil
case float64: case float64:
return strconv.FormatFloat(i.(float64), 'f', -1, 64), true return strconv.FormatFloat(i.(float64), 'f', -1, 64), nil
case int: case int:
return strconv.FormatInt(int64(i.(int)), 10), true return strconv.FormatInt(int64(i.(int)), 10), nil
case []byte: case []byte:
return string(s), true return string(s), nil
case nil: case nil:
return "", true return "", nil
default: default:
jww.ERROR.Printf("Unable to Cast %#v to string", i) return "", fmt.Errorf("Unable to Cast %#v to string", i)
} }
return "", false
} }
func ToStringMapStringE(i interface{}) (map[string]string, bool) { func ToStringMapStringE(i interface{}) (map[string]string, error) {
jww.DEBUG.Println("ToStringMapStringE called on type:", reflect.TypeOf(i))
var m = map[string]string{} var m = map[string]string{}
switch v := i.(type) { switch v := i.(type) {
@ -149,20 +145,23 @@ func ToStringMapStringE(i interface{}) (map[string]string, bool) {
for k, val := range v { for k, val := range v {
m[ToString(k)] = ToString(val) m[ToString(k)] = ToString(val)
} }
return m, nil
case map[string]interface{}: case map[string]interface{}:
for k, val := range v { for k, val := range v {
m[ToString(k)] = ToString(val) m[ToString(k)] = ToString(val)
} }
return m, nil
case map[string]string: case map[string]string:
return v, true return v, nil
default: default:
return m, false return m, fmt.Errorf("Unable to Cast %#v to map[string]string", i)
} }
return m, fmt.Errorf("Unable to Cast %#v to map[string]string", i)
return m, true
} }
func ToStringMapE(i interface{}) (map[string]interface{}, bool) { func ToStringMapE(i interface{}) (map[string]interface{}, error) {
jww.DEBUG.Println("ToStringMapE called on type:", reflect.TypeOf(i))
var m = map[string]interface{}{} var m = map[string]interface{}{}
switch v := i.(type) { switch v := i.(type) {
@ -170,16 +169,19 @@ func ToStringMapE(i interface{}) (map[string]interface{}, bool) {
for k, val := range v { for k, val := range v {
m[ToString(k)] = val m[ToString(k)] = val
} }
return m, nil
case map[string]interface{}: case map[string]interface{}:
return v, true return v, nil
default: default:
return m, false return m, fmt.Errorf("Unable to Cast %#v to map[string]interface{}", i)
} }
return m, true return m, fmt.Errorf("Unable to Cast %#v to map[string]interface{}", i)
} }
func ToStringSliceE(i interface{}) ([]string, bool) { func ToStringSliceE(i interface{}) ([]string, error) {
jww.DEBUG.Println("ToStringSliceE called on type:", reflect.TypeOf(i))
var a []string var a []string
switch v := i.(type) { switch v := i.(type) {
@ -187,13 +189,14 @@ func ToStringSliceE(i interface{}) ([]string, bool) {
for _, u := range v { for _, u := range v {
a = append(a, ToString(u)) a = append(a, ToString(u))
} }
return a, nil
case []string: case []string:
return v, true return v, nil
default: default:
return a, false return a, fmt.Errorf("Unable to Cast %#v to []string", i)
} }
return a, true return a, fmt.Errorf("Unable to Cast %#v to []string", i)
} }
func StringToDate(s string) (time.Time, error) { func StringToDate(s string) (time.Time, error) {