Skip to content

Commit

Permalink
TestStringifyKeysOnMapRecursivelyMovesThroughSlices
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Mar 19, 2018
1 parent 44f2180 commit f0aaf95
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion util.go
Expand Up @@ -11,7 +11,17 @@ func StringifyKeys(m map[interface{}]interface{}) (out map[string]interface{}) {
out = map[string]interface{}{}

for k, v := range m {
if x, ok := v.(map[interface{}]interface{}); ok {
switch x := v.(type) {
case []interface{}:
newSlice := []interface{}{}
for _, sliceEntry := range x {
if subMap, ok := sliceEntry.(map[interface{}]interface{}); ok {
sliceEntry = StringifyKeys(subMap)
}
newSlice = append(newSlice, sliceEntry)
}
v = newSlice
case map[interface{}]interface{}:
v = StringifyKeys(x)
}

Expand Down
28 changes: 28 additions & 0 deletions util_test.go
Expand Up @@ -53,3 +53,31 @@ func TestStringifyKeysOnMapWithEntriesRecursively(t *testing.T) {
t.Errorf("Expected:\n %#+v\nGot:\n %#+v", expected, result)
}
}

func TestStringifyKeysOnMapRecursivelyMovesThroughSlices(t *testing.T) {
m := map[interface{}]interface{}{
"foo": []interface{}{
map[interface{}]interface{}{
"foo": "bar",
"baz": 123,
},
123,
},
"baz": 123,
}
result := phpserialize.StringifyKeys(m)
expected := map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"foo": "bar",
"baz": 123,
},
123,
},
"baz": 123,
}

if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected:\n %#+v\nGot:\n %#+v", expected, result)
}
}

0 comments on commit f0aaf95

Please sign in to comment.