-
Notifications
You must be signed in to change notification settings - Fork 4
/
yaml.go
102 lines (93 loc) · 2 KB
/
yaml.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package limacharlie
import (
"fmt"
)
func (d *Dict) UnmarshalYAML(unmarshal func(interface{}) error) error {
out := map[string]interface{}{}
if err := unmarshal(&out); err != nil {
return err
}
if err := unmarshalCleanYAMLMap(out); err != nil {
return err
}
*d = out
return nil
}
func (l *List) UnmarshalYAML(unmarshal func(interface{}) error) error {
out := []interface{}{}
if err := unmarshal(&out); err != nil {
return err
}
if err := unmarshalCleanYAMLList(out); err != nil {
return err
}
*l = out
return nil
}
func unmarshalCleanYAMLMap(out map[string]interface{}) error {
for k, v := range out {
switch val := v.(type) {
case map[interface{}]interface{}:
n, err := yamlMapToJsonMap(val)
if err != nil {
return err
}
out[k] = n
if err := unmarshalCleanYAMLMap(n); err != nil {
return err
}
case []interface{}:
if err := unmarshalCleanYAMLList(val); err != nil {
return err
}
default:
var err error
out[k], err = unmarshalCleanYAMLElement(val)
if err != nil {
return err
}
}
}
return nil
}
func yamlMapToJsonMap(m map[interface{}]interface{}) (map[string]interface{}, error) {
out := map[string]interface{}{}
for k, v := range m {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("unsupported key type: %T", k)
}
out[s] = v
}
return out, nil
}
func unmarshalCleanYAMLList(out []interface{}) error {
for i, v := range out {
switch val := v.(type) {
case map[interface{}]interface{}:
n, err := yamlMapToJsonMap(val)
if err != nil {
return err
}
out[i] = n
if err := unmarshalCleanYAMLMap(n); err != nil {
return err
}
case []interface{}:
if err := unmarshalCleanYAMLList(val); err != nil {
return err
}
default:
var err error
out[i], err = unmarshalCleanYAMLElement(val)
if err != nil {
return err
}
}
}
return nil
}
func unmarshalCleanYAMLElement(out interface{}) (interface{}, error) {
// YAML handles integers vs floats properly already.
return out, nil
}