adding ToStringMapStringSlice method

This commit is contained in:
jackspirou 2015-07-30 15:13:54 -05:00 committed by spf13
parent 893a4154d1
commit 7ed57e5c41
2 changed files with 44 additions and 0 deletions

View File

@ -42,6 +42,11 @@ func ToStringMapString(i interface{}) map[string]string {
return v
}
func ToStringMapStringSlice(i interface{}) map[string][]string {
v, _ := ToStringMapStringSliceE(i)
return v
}
func ToStringMapBool(i interface{}) map[string]bool {
v, _ := ToStringMapBoolE(i)
return v

View File

@ -231,6 +231,45 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
return m, fmt.Errorf("Unable to Cast %#v to map[string]string", i)
}
func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
jww.DEBUG.Println("ToStringMapStringSliceE called on type:", reflect.TypeOf(i))
var m = map[string][]string{}
switch v := i.(type) {
case map[interface{}]interface{}:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[interface{}][]interface{}:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[string][]interface{}:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[string]interface{}:
for k, val := range v {
m[ToString(k)] = []string{ToString(val)}
}
return m, nil
case map[string][]string:
return v, nil
case map[string]string:
for k, val := range v {
m[ToString(k)] = []string{val}
}
default:
fmt.Printf("unexpected type %T", v)
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)
}
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
jww.DEBUG.Println("ToStringMapBoolE called on type:", reflect.TypeOf(i))