Merge pull request #4 from bjornerik/tostringmapbool

This commit is contained in:
Steve Francia 2014-12-03 08:47:58 +01:00
commit c8f8b1be37
3 changed files with 32 additions and 1 deletions

View File

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

View File

@ -40,7 +40,9 @@ func TestToString(t *testing.T) {
func TestMaps(t *testing.T) {
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
}
func TestToBool(t *testing.T) {

View File

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