From 99f1223ff64ed0c6a1dd9c850474f5fecba6e0a4 Mon Sep 17 00:00:00 2001 From: spf13 Date: Sat, 26 Apr 2014 16:56:25 -0600 Subject: [PATCH] Adding ToSlice --- cast.go | 5 +++++ caste.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cast.go b/cast.go index 4bd3ba3..dee6787 100644 --- a/cast.go +++ b/cast.go @@ -42,6 +42,11 @@ func ToStringMap(i interface{}) map[string]interface{} { return v } +func ToSlice(i interface{}) []interface{} { + v, _ := ToSliceE(i) + return v +} + func ToStringSlice(i interface{}) []string { v, _ := ToStringSliceE(i) return v diff --git a/caste.go b/caste.go index 78c9483..78c6a9e 100644 --- a/caste.go +++ b/caste.go @@ -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) } +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) { jww.DEBUG.Println("ToStringSliceE called on type:", reflect.TypeOf(i))