Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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)
Expand Down
32 changes: 32 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,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},
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the casting that this library does implies some conversion, but how can both true and false be the same as struct{}{}?

{[]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{}
Expand Down
30 changes: 30 additions & 0 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,35 @@ 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)
}

// ToStringMapIntE casts an interface to a map[string]int{} type.
func ToStringMapIntE(i interface{}) (map[string]int, error) {
var m = map[string]int{}
Expand Down Expand Up @@ -1069,6 +1098,7 @@ func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
}
return m, nil

}

// ToSliceE casts an interface to a []interface{} type.
Expand Down