forked from beme/abide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
32 lines (29 loc) · 940 Bytes
/
json.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 abide
// updateKeyValuesInMap updates every instance of a key within an arbitrary
// `map[string]interface{}` with the given value.
func updateKeyValuesInMap(key string, value interface{}, m map[string]interface{}) map[string]interface{} {
return updateMap(key, value, m)
}
// Recursively update the map to update the specified key.
func updateMap(key string, value interface{}, m map[string]interface{}) map[string]interface{} {
for k, v := range m {
switch s := v.(type) {
// If slice, iterate through each entry and call updateMap
// only if it's a map[string]interface{}.
case []interface{}:
for i := range s {
switch s[i].(type) {
case map[string]interface{}:
v.([]interface{})[i] = updateMap(key, value, v.([]interface{})[i].(map[string]interface{}))
}
}
case map[string]interface{}:
m[k] = updateMap(key, value, s)
default:
if k == key {
m[k] = value
}
}
}
return m
}