-
Notifications
You must be signed in to change notification settings - Fork 39
/
util.go
32 lines (28 loc) · 913 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package phpserialize
// StringifyKeys recursively converts a map into a more sensible map with
// strings as keys.
//
// map[interface{}]interface{} is used as an unmarshalling format because PHP
// serialise() permits keys of associative arrays to be non-string. However, in
// reality this is rarely the case and so strings for keys are much more
// compatible with external code.
func StringifyKeys(m map[interface{}]interface{}) (out map[string]interface{}) {
out = map[string]interface{}{}
for k, v := range m {
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)
}
out[k.(string)] = v
}
return
}