forked from mirror/cast
Add ToStringMapBool
ToStringMapBool is useful for a map of flags in a configuration file.
This commit is contained in:
parent
770890fb15
commit
bbe2d4b35d
5
cast.go
5
cast.go
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
@ -53,7 +55,7 @@ func TestToBool(t *testing.T) {
|
|||
assert.Equal(t, ToBool("F"), false)
|
||||
assert.Equal(t, ToBool(false), false)
|
||||
assert.Equal(t, ToBool("foo"), false)
|
||||
|
||||
|
||||
assert.Equal(t, ToBool("true"), true)
|
||||
assert.Equal(t, ToBool("TRUE"), true)
|
||||
assert.Equal(t, ToBool("True"), true)
|
||||
|
|
24
caste.go
24
caste.go
|
@ -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))
|
||||
|
||||
|
|
Loading…
Reference in New Issue