-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathmap.go
57 lines (48 loc) · 1.26 KB
/
map.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package conv
import "fmt"
// YAMLMapToJSONMap changes the nested map[interface{}]interface{} to map[string]interface{} recursively
// This is needed to convert YAML deserializer result into a JSON compatible result
func YAMLMapToJSONMap(m map[string]interface{}) map[string]interface{} {
return fixVal(m).(map[string]interface{})
}
func fixVal(m interface{}) interface{} {
switch m := m.(type) {
case map[interface{}]interface{}:
return fixMap(m)
case map[string]interface{}:
for k, v := range m {
m[k] = fixVal(v)
}
return m
case []interface{}:
return fixArray(m)
}
return m
}
func fixMap(in map[interface{}]interface{}) map[string]interface{} {
if in == nil {
return nil
}
out := make(map[string]interface{})
for k, v := range in {
sk, ok := k.(string)
if !ok {
sk = fmt.Sprint(k)
}
out[sk] = fixVal(v)
}
return out
}
func fixArray(in []interface{}) []interface{} {
if in == nil {
return nil
}
out := make([]interface{}, len(in))
for i, v := range in {
out[i] = fixVal(v)
}
return out
}