mirror of https://github.com/spf13/cast.git
Add ToStringMapStruct()
This commit is contained in:
parent
acbeb36b90
commit
60fe4eb110
6
cast.go
6
cast.go
|
@ -128,6 +128,12 @@ func ToStringMap(i interface{}) map[string]interface{} {
|
|||
return v
|
||||
}
|
||||
|
||||
// ToStringMapStruct casts an interface to a map[string]struct{} type.
|
||||
func ToStringMapStruct(i interface{}) map[string]struct{} {
|
||||
v, _ := ToStringMapStructE(i)
|
||||
return v
|
||||
}
|
||||
|
||||
// ToSlice casts an interface to a []interface{} type.
|
||||
func ToSlice(i interface{}) []interface{} {
|
||||
v, _ := ToSliceE(i)
|
||||
|
|
32
cast_test.go
32
cast_test.go
|
@ -774,6 +774,38 @@ func TestToStringMapE(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestToStringMapStructE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expect map[string]struct{}
|
||||
iserr bool
|
||||
}{
|
||||
{map[string]interface{}{"v1": true, "v2": false}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false},
|
||||
{[]string{"v1", "v2"}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false},
|
||||
{[]interface{}{"v1", "v2"}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false},
|
||||
{map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false},
|
||||
// errors
|
||||
{nil, nil, true},
|
||||
{testing.T{}, nil, true},
|
||||
}
|
||||
for i, test := range tests {
|
||||
errmsg := fmt.Sprintf("i = %d", i) // assert helper message
|
||||
|
||||
v, err := ToStringMapStructE(test.input)
|
||||
if test.iserr {
|
||||
assert.Error(t, err, errmsg)
|
||||
continue
|
||||
}
|
||||
|
||||
assert.NoError(t, err, errmsg)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
|
||||
// Non-E test
|
||||
v = ToStringMapStruct(test.input)
|
||||
assert.Equal(t, test.expect, v, errmsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToStringMapBoolE(t *testing.T) {
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
|
|
30
caste.go
30
caste.go
|
@ -977,6 +977,36 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// ToStringMapStructE casts an interface to a map[string]struct{} type.
|
||||
func ToStringMapStructE(i interface{}) (map[string]struct{}, error) {
|
||||
var m = map[string]struct{}{}
|
||||
|
||||
fmt.Println("type: ", reflect.TypeOf(i))
|
||||
switch v := i.(type) {
|
||||
case map[string]interface{}:
|
||||
for k := range v {
|
||||
m[k] = struct{}{}
|
||||
}
|
||||
return m, nil
|
||||
case []string:
|
||||
fmt.Println("[]string")
|
||||
for _, s := range v {
|
||||
m[s] = struct{}{}
|
||||
}
|
||||
return m, nil
|
||||
case []interface{}:
|
||||
for _, k := range v {
|
||||
m[ToString(k)] = struct{}{}
|
||||
}
|
||||
return m, nil
|
||||
case map[string]struct{}:
|
||||
return v, nil
|
||||
default:
|
||||
fmt.Println("cast err")
|
||||
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
|
||||
}
|
||||
}
|
||||
|
||||
// ToSliceE casts an interface to a []interface{} type.
|
||||
func ToSliceE(i interface{}) ([]interface{}, error) {
|
||||
var s []interface{}
|
||||
|
|
Loading…
Reference in New Issue