forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml_path.go
376 lines (347 loc) · 11.2 KB
/
yaml_path.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package docs
import (
"errors"
"fmt"
"strconv"
"gopkg.in/yaml.v3"
)
func removeFieldFromMapping(name string, node *yaml.Node) error {
var newContent []*yaml.Node
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value != name {
newContent = append(newContent, node.Content[i], node.Content[i+1])
}
}
node.Content = newContent
return nil
}
func getFieldFromMapping(name string, createMissing bool, node *yaml.Node) (*yaml.Node, error) {
node.Kind = yaml.MappingNode
node.Style = yaml.LiteralStyle
var foundNode *yaml.Node
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == name {
foundNode = node.Content[i+1]
break
}
}
if foundNode == nil {
if !createMissing {
return nil, fmt.Errorf("%v: key not found in mapping", name)
}
var keyNode yaml.Node
if err := keyNode.Encode(name); err != nil {
return nil, fmt.Errorf("%v: failed to encode key: %w", name, err)
}
node.Content = append(node.Content, &keyNode)
foundNode = &yaml.Node{}
node.Content = append(node.Content, foundNode)
}
return foundNode, nil
}
func removeIndexFromMapping(name string, node *yaml.Node) error {
removeIndex, err := strconv.Atoi(name)
if err != nil {
return err
}
if len(node.Content) <= removeIndex {
return fmt.Errorf("invalid delete index: %v, length: %v", removeIndex, len(node.Content))
}
newContent := make([]*yaml.Node, 0, len(node.Content)-1)
for i := 0; i < len(node.Content); i++ {
if i != removeIndex {
newContent = append(newContent, node.Content[i])
}
}
node.Content = newContent
return nil
}
func getIndexFromSequence(name string, allowAppend bool, node *yaml.Node) (*yaml.Node, error) {
node.Kind = yaml.SequenceNode
node.Style = yaml.LiteralStyle
var foundNode *yaml.Node
if name != "-" {
index, err := strconv.Atoi(name)
if err != nil {
return nil, fmt.Errorf("%v: failed to parse path segment as array index: %w", name, err)
}
if len(node.Content) <= index {
return nil, fmt.Errorf("%v: target index greater than array length", name)
}
foundNode = node.Content[index]
} else {
if !allowAppend {
return nil, fmt.Errorf("%v: append directive not allowed", name)
}
foundNode = &yaml.Node{}
node.Content = append(node.Content, foundNode)
}
return foundNode, nil
}
// SetYAMLPath sets the value of a node within a YAML document identified by a
// path to a value.
func (f FieldSpecs) SetYAMLPath(docsProvider Provider, root, value *yaml.Node, path ...string) error {
root = unwrapDocumentNode(root)
value = unwrapDocumentNode(value)
var foundSpec FieldSpec
for _, spec := range f {
if spec.Name == path[0] {
foundSpec = spec
break
}
}
if foundSpec.Name == "" {
return fmt.Errorf("%v: field not recognised", path[0])
}
// Check for delete
if len(path) == 1 && value == nil {
return removeFieldFromMapping(path[0], root)
}
foundNode, err := getFieldFromMapping(path[0], true, root)
if err != nil {
return err
}
if err := foundSpec.SetYAMLPath(docsProvider, foundNode, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
}
func setYAMLPathCore(docsProvider Provider, coreType Type, root, value *yaml.Node, path ...string) error {
foundNode, err := getFieldFromMapping(path[0], true, root)
if err != nil {
return err
}
if f, exists := ReservedFieldsByType(coreType)[path[0]]; exists {
if err = f.SetYAMLPath(docsProvider, foundNode, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
}
cSpec, exists := docsProvider.GetDocs(path[0], coreType)
if !exists {
return fmt.Errorf("%v: field not recognised", path[0])
}
if err = cSpec.Config.SetYAMLPath(docsProvider, foundNode, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
}
// SetYAMLPath sets the value of a node within a YAML document identified by a
// path to a value.
func (f FieldSpec) SetYAMLPath(docsProvider Provider, root, value *yaml.Node, path ...string) error {
root = unwrapDocumentNode(root)
value = unwrapDocumentNode(value)
switch f.Kind {
case Kind2DArray:
if len(path) == 0 {
if value.Kind == yaml.SequenceNode {
*root = *value
} else {
root.Kind = yaml.SequenceNode
root.Style = yaml.LiteralStyle
root.Content = []*yaml.Node{{
Kind: yaml.SequenceNode,
Style: yaml.LiteralStyle,
Content: []*yaml.Node{value},
}}
}
return nil
}
if len(path) == 1 && value == nil {
return removeIndexFromMapping(path[0], root)
}
target, err := getIndexFromSequence(path[0], true, root)
if err != nil {
return err
}
if err = f.Array().SetYAMLPath(docsProvider, target, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
case KindArray:
if len(path) == 0 {
if value.Kind == yaml.SequenceNode {
*root = *value
} else {
root.Kind = yaml.SequenceNode
root.Style = yaml.LiteralStyle
root.Content = []*yaml.Node{value}
}
return nil
}
if len(path) == 1 && value == nil {
return removeIndexFromMapping(path[0], root)
}
target, err := getIndexFromSequence(path[0], true, root)
if err != nil {
return err
}
if err = f.Scalar().SetYAMLPath(docsProvider, target, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
case KindMap:
if len(path) == 0 {
return errors.New("cannot set map directly")
}
if len(path) == 1 && value == nil {
return removeFieldFromMapping(path[0], root)
}
target, err := getFieldFromMapping(path[0], true, root)
if err != nil {
return err
}
if err = f.Scalar().SetYAMLPath(docsProvider, target, value, path[1:]...); err != nil {
return fmt.Errorf("%v.%w", path[0], err)
}
return nil
}
if len(path) == 0 {
*root = *value
return nil
}
if len(path) == 1 && value == nil {
return removeFieldFromMapping(path[0], root)
}
if coreType, isCore := f.Type.IsCoreComponent(); isCore {
if len(path) == 0 {
return fmt.Errorf("(%v): cannot set core type directly", coreType)
}
return setYAMLPathCore(docsProvider, coreType, root, value, path...)
}
if len(f.Children) > 0 {
return f.Children.SetYAMLPath(docsProvider, root, value, path...)
}
return fmt.Errorf("%v: field not recognised", path[0])
}
//------------------------------------------------------------------------------
// GetDocsForPath attempts to find the documentation for a given node of a
// config identified by a path.
func (f FieldSpecs) GetDocsForPath(docsProvider Provider, path ...string) (FieldSpec, error) {
target := path[0]
for _, spec := range f {
if spec.Name == target {
return spec.GetDocsForPath(docsProvider, path[1:]...)
}
}
return FieldSpec{}, fmt.Errorf("%v: field not recognised", path[0])
}
func getDocsForPathCore(docsProvider Provider, coreType Type, path ...string) (FieldSpec, error) {
if f, exists := ReservedFieldsByType(coreType)[path[0]]; exists {
return f.GetDocsForPath(docsProvider, path[1:]...)
}
cSpec, exists := docsProvider.GetDocs(path[0], coreType)
if !exists {
return FieldSpec{}, fmt.Errorf("%v: field not recognised", path[0])
}
return cSpec.Config.GetDocsForPath(docsProvider, path[1:]...)
}
// GetDocsForPath attempts to find the documentation for a given node of a
// config identified by a path.
func (f FieldSpec) GetDocsForPath(docsProvider Provider, path ...string) (FieldSpec, error) {
if len(path) == 0 {
return f, nil
}
switch f.Kind {
case Kind2DArray:
return f.Array().GetDocsForPath(docsProvider, path[1:]...)
case KindArray, KindMap:
return f.Scalar().GetDocsForPath(docsProvider, path[1:]...)
}
if coreType, isCore := f.Type.IsCoreComponent(); isCore {
return getDocsForPathCore(docsProvider, coreType, path...)
}
if len(f.Children) > 0 {
return f.Children.GetDocsForPath(docsProvider, path...)
}
return FieldSpec{}, fmt.Errorf("%v: field not recognised", path[0])
}
// GetYAMLPath attempts to obtain a specific value within a YAML tree by
// following a sequence of path identifiers.
func GetYAMLPath(root *yaml.Node, path ...string) (*yaml.Node, error) {
root = unwrapDocumentNode(root)
if len(path) == 0 {
return root, nil
}
if root.Kind == yaml.SequenceNode {
newRoot, err := getIndexFromSequence(path[0], false, root)
if err != nil {
return nil, err
}
if newRoot, err = GetYAMLPath(newRoot, path[1:]...); err != nil {
return nil, fmt.Errorf("%v.%w", path[0], err)
}
return newRoot, nil
}
newRoot, err := getFieldFromMapping(path[0], false, root)
if err != nil {
return nil, err
}
if newRoot, err = GetYAMLPath(newRoot, path[1:]...); err != nil {
return nil, fmt.Errorf("%v.%w", path[0], err)
}
return newRoot, nil
}
//------------------------------------------------------------------------------
// YAMLLabelsToPaths walks a YAML tree using a field spec as a reference point.
// When a component of the YAML tree has a label field it is added to the
// provided labelsToPaths map with the path to the component.
func (f FieldSpecs) YAMLLabelsToPaths(docsProvider Provider, node *yaml.Node, labelsToPaths map[string][]string, path []string) {
node = unwrapDocumentNode(node)
fieldMap := map[string]FieldSpec{}
for _, spec := range f {
fieldMap[spec.Name] = spec
}
for i := 0; i < len(node.Content)-1; i += 2 {
key := node.Content[i].Value
if spec, exists := fieldMap[key]; exists {
spec.YAMLLabelsToPaths(docsProvider, node.Content[i+1], labelsToPaths, append(path, key))
}
}
}
// YAMLLabelsToPaths walks a YAML tree using a field spec as a reference point.
// When a component of the YAML tree has a label field it is added to the
// provided labelsToPaths map with the path to the component.
func (f FieldSpec) YAMLLabelsToPaths(docsProvider Provider, node *yaml.Node, labelsToPaths map[string][]string, path []string) {
node = unwrapDocumentNode(node)
switch f.Kind {
case Kind2DArray:
nextSpec := f.Array()
for i, child := range node.Content {
nextSpec.YAMLLabelsToPaths(docsProvider, child, labelsToPaths, append(path, strconv.Itoa(i)))
}
case KindArray:
nextSpec := f.Scalar()
for i, child := range node.Content {
nextSpec.YAMLLabelsToPaths(docsProvider, child, labelsToPaths, append(path, strconv.Itoa(i)))
}
case KindMap:
nextSpec := f.Scalar()
for i, child := range node.Content {
nextSpec.YAMLLabelsToPaths(docsProvider, child, labelsToPaths, append(path, strconv.Itoa(i)))
}
for i := 0; i < len(node.Content)-1; i += 2 {
key := node.Content[i].Value
nextSpec.YAMLLabelsToPaths(docsProvider, node.Content[i+1], labelsToPaths, append(path, key))
}
default:
if coreType, isCore := f.Type.IsCoreComponent(); isCore {
coreFields := FieldSpecs{}
for _, f := range ReservedFieldsByType(coreType) {
coreFields = append(coreFields, f)
}
if inferred, cSpec, err := GetInferenceCandidateFromYAML(docsProvider, coreType, node); err == nil {
conf := cSpec.Config
conf.Name = inferred
coreFields = append(coreFields, conf)
}
coreFields.YAMLLabelsToPaths(docsProvider, node, labelsToPaths, path)
} else if len(f.Children) > 0 {
f.Children.YAMLLabelsToPaths(docsProvider, node, labelsToPaths, path)
} else if f.Name == labelField.Name && f.Description == labelField.Description {
pathCopy := make([]string, len(path)-1)
copy(pathCopy, path[:len(path)-1])
labelsToPaths[node.Value] = pathCopy // Add path to the parent node
}
}
}