mirror of https://github.com/spf13/cast.git
adding unit tests for ToStringMapString and ToStringMapStringSlice methods
This commit is contained in:
parent
d2e80cab41
commit
9b6ca94623
35
cast_test.go
35
cast_test.go
|
@ -73,8 +73,43 @@ func TestErrorToString(t *testing.T) {
|
||||||
func TestMaps(t *testing.T) {
|
func TestMaps(t *testing.T) {
|
||||||
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
|
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
|
||||||
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
|
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
|
||||||
|
|
||||||
|
// ToStringMapString inputs/outputs
|
||||||
|
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||||
|
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||||
|
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||||
|
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||||
|
|
||||||
|
// ToStringMapStringSlice inputs/outputs
|
||||||
|
var stringMapStringSlice = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
||||||
|
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
||||||
|
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": []string{"value", "1"}, "key 2": []string{"value", "2"}, "key 3": []string{"value", "3"}}
|
||||||
|
var interfaceMapStringSlice = map[interface{}][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
||||||
|
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
||||||
|
|
||||||
|
var stringMapStringSliceMultiple = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
||||||
|
var stringMapStringSliceSingle = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}, "key 3": []string{"value 3"}}
|
||||||
|
|
||||||
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
||||||
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
||||||
|
|
||||||
|
// ToStringMapString tests
|
||||||
|
assert.Equal(t, ToStringMapString(stringMapString), stringMapString)
|
||||||
|
assert.Equal(t, ToStringMapString(stringMapInterface), stringMapString)
|
||||||
|
assert.Equal(t, ToStringMapString(interfaceMapString), stringMapString)
|
||||||
|
assert.Equal(t, ToStringMapString(interfaceMapInterface), stringMapString)
|
||||||
|
|
||||||
|
// ToStringMapStringSlice tests
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapStringSlice), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapInterfaceSlice), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapString), stringMapStringSliceSingle)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(stringMapInterface), stringMapStringSliceSingle)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(interfaceMapStringSlice), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterfaceSlice), stringMapStringSlice)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(interfaceMapString), stringMapStringSingleSliceFieldsResult)
|
||||||
|
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterface), stringMapStringSingleSliceFieldsResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSlices(t *testing.T) {
|
func TestSlices(t *testing.T) {
|
||||||
|
|
77
caste.go
77
caste.go
|
@ -211,18 +211,23 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
|
||||||
var m = map[string]string{}
|
var m = map[string]string{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[string]string:
|
||||||
for k, val := range v {
|
return v, nil
|
||||||
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
|
return m, nil
|
||||||
case map[string]string:
|
case map[interface{}]string:
|
||||||
return v, nil
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = ToString(val)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
case map[interface{}]interface{}:
|
||||||
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = ToString(val)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
default:
|
default:
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -235,7 +240,28 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
|
||||||
var m = map[string][]string{}
|
var m = map[string][]string{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[string][]string:
|
||||||
|
return v, nil
|
||||||
|
case map[string][]interface{}:
|
||||||
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = ToStringSlice(val)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
case map[string]string:
|
||||||
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = []string{val}
|
||||||
|
}
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = []string{ToString(val)}
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
case map[interface{}][]string:
|
||||||
|
for k, val := range v {
|
||||||
|
m[ToString(k)] = ToStringSlice(val)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
case map[interface{}]string:
|
||||||
for k, val := range v {
|
for k, val := range v {
|
||||||
m[ToString(k)] = ToStringSlice(val)
|
m[ToString(k)] = ToStringSlice(val)
|
||||||
}
|
}
|
||||||
|
@ -245,27 +271,20 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
|
||||||
m[ToString(k)] = ToStringSlice(val)
|
m[ToString(k)] = ToStringSlice(val)
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
case map[string][]interface{}:
|
case map[interface{}]interface{}:
|
||||||
for k, val := range v {
|
for k, val := range v {
|
||||||
m[ToString(k)] = ToStringSlice(val)
|
key, err := ToStringE(k)
|
||||||
|
if err != nil {
|
||||||
|
return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
|
||||||
|
}
|
||||||
|
value, err := ToStringSliceE(val)
|
||||||
|
if err != nil {
|
||||||
|
return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
|
||||||
|
}
|
||||||
|
m[key] = value
|
||||||
}
|
}
|
||||||
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)
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
|
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
|
||||||
|
@ -351,7 +370,11 @@ func ToStringSliceE(i interface{}) ([]string, error) {
|
||||||
case string:
|
case string:
|
||||||
return strings.Fields(v), nil
|
return strings.Fields(v), nil
|
||||||
default:
|
default:
|
||||||
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
|
str, err := ToStringE(v)
|
||||||
|
if err != nil {
|
||||||
|
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
|
||||||
|
}
|
||||||
|
return []string{str}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
|
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
|
||||||
|
|
Loading…
Reference in New Issue