-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyaml.go
268 lines (248 loc) · 6.73 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package nflex
import (
"regexp"
"strconv"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type parsedYAML struct {
root *yaml.Node
// nodes store maps as alternating key/value in an array
// cache turns that into a map
cache map[*yaml.Node]map[string]*yaml.Node
debugID int
pathToHere []string
}
func UnmarshalYAML(data []byte) (Source, error) {
var node yaml.Node
err := yaml.Unmarshal(data, &node)
if err != nil {
return parsedYAML{}, errors.Wrap(err, "yaml")
}
p := parsedYAML{
root: &node,
cache: make(map[*yaml.Node]map[string]*yaml.Node),
debugID: debugID(),
}
debug("nflex/UnmarshalYAML", p.debugID, p.debugKeys)
return p, nil
}
func (p parsedYAML) Exists(keys ...string) bool {
n, err := p.lookup(p.root, keys)
if err != nil || n == nil {
return false
}
return true
}
func (p parsedYAML) Recurse(keys ...string) Source {
if len(keys) == 0 {
debug("nflex/yaml Recurse()", id(p), "-> self", p.debugKeys)
return p
}
n, err := p.lookup(p.root, keys)
if err != nil || n == nil {
debug("nflex/yaml Recurse(", keys, ")", id(p), "-> nil")
return nil
}
np := parsedYAML{
root: n.root,
cache: p.cache,
pathToHere: combine(p.pathToHere, keys),
debugID: debugID(),
}
debug("nflex/yaml Recurse(", keys, ")", id(p), "->", id(np), np.debugKeys)
return np
}
func (p parsedYAML) GetBool(keys ...string) (bool, error) {
n, err := p.lookupScalar(keys)
if err != nil {
return false, err
}
b, err := strconv.ParseBool(n.Value)
if err != nil {
return false, errors.Wrapf(ErrWrongType, "Lookup %v, parse error: %s", combine(p.pathToHere, keys), err)
}
return b, nil
}
func (p parsedYAML) GetInt(keys ...string) (int64, error) {
n, err := p.lookupScalar(keys)
if err != nil {
return 0, err
}
i, err := strconv.ParseInt(n.Value, 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrWrongType, "Lookup %v, parse error: %s", combine(p.pathToHere, keys), err)
}
return i, nil
}
func (p parsedYAML) GetUInt(keys ...string) (uint64, error) {
n, err := p.lookupScalar(keys)
if err != nil {
return 0, err
}
i, err := strconv.ParseUint(n.Value, 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrWrongType, "Lookup %v, parse error: %s", combine(p.pathToHere, keys), err)
}
return i, nil
}
func (p parsedYAML) GetFloat(keys ...string) (float64, error) {
n, err := p.lookupScalar(keys)
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(n.Value, 64)
if err != nil {
return 0, errors.Wrapf(ErrWrongType, "Lookup %v, parse error: %s", combine(p.pathToHere, keys), err)
}
return f, nil
}
func (p parsedYAML) GetString(keys ...string) (string, error) {
n, err := p.lookupScalar(keys)
if err != nil {
return "", err
}
return n.Value, nil
}
var boolRE = regexp.MustCompile(`^(?:true|True|TRUE|false|False|FALSE|t|T|f|F)$`)
var intRE = regexp.MustCompile(`^\d+$`)
var floatRE = regexp.MustCompile(`^(?:(?:\.\d+(?:[eE][-+]?\d+)?)|(?:\d+(?:\.\d+(?:[eE][-+]?\d+)?)?))$`)
func (p parsedYAML) Type(keys ...string) NodeType {
n, err := p.lookup(p.root, keys)
if err != nil {
return Undefined
}
if n == nil {
return Nil
}
switch n.root.Kind {
case yaml.DocumentNode, yaml.MappingNode:
return Map
case yaml.SequenceNode:
return Slice
case yaml.ScalarNode:
if boolRE.MatchString(n.root.Value) {
return Bool
}
if intRE.MatchString(n.root.Value) {
return Int
}
if floatRE.MatchString(n.root.Value) {
return Float
}
return String
default:
return Undefined // this shouldn't happen
}
}
func (p parsedYAML) Len(keys ...string) (int, error) {
n, err := p.lookup(p.root, keys)
if err != nil {
return 0, errors.Wrapf(ErrDoesNotExist, "Could not get %v: %s", combine(p.pathToHere, keys), err)
}
if n == nil {
return 0, errors.Wrapf(ErrDoesNotExist, "Could not get %v", combine(p.pathToHere, keys))
}
if n.root.Kind != yaml.SequenceNode {
return 0, errors.Wrapf(ErrWrongType, "Len %s is a %d", combine(p.pathToHere, keys), n.root.Kind)
}
return len(n.root.Content), nil
}
func (p parsedYAML) Keys(keys ...string) ([]string, error) {
n, err := p.lookup(p.root, keys)
if err != nil {
return nil, errors.Wrapf(ErrDoesNotExist, "Could not get %v: %s", combine(p.pathToHere, keys), err)
}
if n == nil {
return nil, errors.Wrapf(ErrDoesNotExist, "Could not get %v", combine(p.pathToHere, keys))
}
root := n.root
if root.Kind == yaml.DocumentNode {
if len(root.Content) == 0 {
// empty document, move right along folks
return nil, nil
}
root = root.Content[0]
}
if root.Kind != yaml.MappingNode {
return nil, errors.Wrapf(ErrWrongType, "Keys %s is a %d", combine(p.pathToHere, keys), n.root.Kind)
}
ret := make([]string, len(root.Content)/2)
for i := 0; i < len(root.Content); i += 2 {
ret[i/2] = root.Content[i].Value
}
return ret, nil
}
func (p parsedYAML) lookupScalar(keys []string) (*yaml.Node, error) {
n, err := p.lookup(p.root, keys)
if err != nil {
return nil, errors.Wrapf(ErrDoesNotExist, "Could not get %v: %s", combine(p.pathToHere, keys), err)
}
if n == nil {
return nil, errors.Wrapf(ErrDoesNotExist, "Could not get %v", combine(p.pathToHere, keys))
}
if n.root.Kind != yaml.ScalarNode {
return nil, errors.Wrapf(ErrWrongType, "Lookup %v is a %d", combine(p.pathToHere, keys), n.root.Kind)
}
return n.root, nil
}
var isNumberRE = regexp.MustCompile(`^[0-9]+$`)
func (p parsedYAML) lookup(n *yaml.Node, keys []string) (*parsedYAML, error) {
original := keys
for len(keys) > 0 {
key := keys[0]
if n == nil {
return nil, nil
}
switch n.Kind {
case yaml.DocumentNode:
if len(n.Content) == 0 {
// empty document, move right along folks
return nil, nil
}
n = n.Content[0]
continue
case yaml.SequenceNode:
if !isNumberRE.MatchString(key) {
return nil, errors.Errorf("cannot use '%s' as an array index", combine(p.pathToHere, original))
}
i, err := strconv.ParseInt(key, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "parse array index")
}
if i >= int64(len(n.Content)) {
return nil, nil
}
n = n.Content[i]
case yaml.MappingNode:
if _, ok := p.cache[n]; !ok {
cache := make(map[string]*yaml.Node)
p.cache[n] = cache
if len(n.Content)%2 != 0 {
return nil, errors.Errorf("mapping node %s/%s has non-even content", n.Tag, n.Anchor)
}
for i := 0; i < len(n.Content); i += 2 {
cache[n.Content[i].Value] = n.Content[i+1]
}
}
n = p.cache[n][key]
case yaml.ScalarNode:
return nil, errors.Errorf("Cannot index through scalar with '%s'", combine(p.pathToHere, original))
case yaml.AliasNode:
n = n.Alias
continue
}
keys = keys[1:]
}
if n == nil {
return nil, nil
}
return &parsedYAML{
root: n,
cache: p.cache,
pathToHere: combine(p.pathToHere, original),
}, nil
}
func (p parsedYAML) debugKeys() string {
return debugKeys(p)
}