Adding ToSlice

This commit is contained in:
spf13 2014-04-26 16:56:25 -06:00
parent d578378de0
commit 99f1223ff6
2 changed files with 29 additions and 0 deletions

View File

@ -42,6 +42,11 @@ func ToStringMap(i interface{}) map[string]interface{} {
return v return v
} }
func ToSlice(i interface{}) []interface{} {
v, _ := ToSliceE(i)
return v
}
func ToStringSlice(i interface{}) []string { func ToStringSlice(i interface{}) []string {
v, _ := ToStringSliceE(i) v, _ := ToStringSliceE(i)
return v return v

View File

@ -179,6 +179,30 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
return m, fmt.Errorf("Unable to Cast %#v to map[string]interface{}", i) return m, fmt.Errorf("Unable to Cast %#v to map[string]interface{}", i)
} }
func ToSliceE(i interface{}) ([]interface{}, error) {
jww.DEBUG.Println("ToSliceE called on type:", reflect.TypeOf(i))
var s []interface{}
switch v := i.(type) {
case []interface{}:
fmt.Println("here")
for _, u := range v {
s = append(s, u)
}
return s, nil
case []map[string]interface{}:
for _, u := range v {
s = append(s, u)
}
return s, nil
default:
return s, fmt.Errorf("Unable to Cast %#v of type %v to []interface{}", i, reflect.TypeOf(i))
}
return s, fmt.Errorf("Unable to Cast %#v to []interface{}", i)
}
func ToStringSliceE(i interface{}) ([]string, error) { func ToStringSliceE(i interface{}) ([]string, error) {
jww.DEBUG.Println("ToStringSliceE called on type:", reflect.TypeOf(i)) jww.DEBUG.Println("ToStringSliceE called on type:", reflect.TypeOf(i))