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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package flatten
import (
"fmt"
"github.com/icinga/icingadb/pkg/types"
"strconv"
)
// Flatten creates flat, one-dimensional maps from arbitrarily nested values, e.g. JSON.
func Flatten(value interface{}, prefix string) map[string]types.String {
var flatten func(string, interface{})
flattened := make(map[string]types.String)
flatten = func(key string, value interface{}) {
switch value := value.(type) {
case map[string]interface{}:
if len(value) == 0 {
flattened[key] = types.String{}
break
}
for k, v := range value {
flatten(key+"."+k, v)
}
case []interface{}:
if len(value) == 0 {
flattened[key] = types.String{}
break
}
for i, v := range value {
flatten(key+"["+strconv.Itoa(i)+"]", v)
}
case nil:
flattened[key] = types.MakeString("null")
case float64:
flattened[key] = types.MakeString(strconv.FormatFloat(value, 'f', -1, 64))
default:
flattened[key] = types.MakeString(fmt.Sprintf("%v", value))
}
}
flatten(prefix, value)
return flattened
}
|