-
Notifications
You must be signed in to change notification settings - Fork 786
/
yaml.go
851 lines (762 loc) · 23.6 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
package docs
import (
"fmt"
"gopkg.in/yaml.v3"
)
// FieldsFromYAML walks the children of a YAML node and returns a list of fields
// extracted from it. This can be used in order to infer a field spec for a
// parsed component.
func FieldsFromYAML(node *yaml.Node) FieldSpecs {
node = unwrapDocumentNode(node)
var fields FieldSpecs
for i := 0; i < len(node.Content)-1; i += 2 {
fields = append(fields, FieldFromYAML(node.Content[i].Value, node.Content[i+1]))
}
return fields
}
// FieldFromYAML infers a field spec from a YAML node. This mechanism has many
// limitations and should only be used for pre-hydrating field specs for old
// components with struct based config.
func FieldFromYAML(name string, node *yaml.Node) FieldSpec {
node = unwrapDocumentNode(node)
field := newField(name, "")
switch node.Kind {
case yaml.MappingNode:
field = field.WithChildren(FieldsFromYAML(node)...)
field.Type = FieldTypeObject
if len(field.Children) == 0 {
var defaultI any = map[string]any{}
field.Default = &defaultI
}
case yaml.SequenceNode:
field.Kind = KindArray
field.Type = FieldTypeUnknown
if len(node.Content) > 0 {
tmpField := FieldFromYAML("", node.Content[0])
field.Type = tmpField.Type
field.Children = tmpField.Children
switch field.Type {
case FieldTypeString:
var defaultArray []string
_ = node.Decode(&defaultArray)
var defaultI any = defaultArray
field.Default = &defaultI
case FieldTypeInt:
var defaultArray []int64
_ = node.Decode(&defaultArray)
var defaultI any = defaultArray
field.Default = &defaultI
}
} else {
var defaultI any = []any{}
field.Default = &defaultI
}
case yaml.ScalarNode:
switch node.Tag {
case "!!bool":
field.Type = FieldTypeBool
var defaultBool bool
_ = node.Decode(&defaultBool)
var defaultI any = defaultBool
field.Default = &defaultI
case "!!int":
field.Type = FieldTypeInt
var defaultInt int64
_ = node.Decode(&defaultInt)
var defaultI any = defaultInt
field.Default = &defaultI
case "!!float":
field.Type = FieldTypeFloat
var defaultFloat float64
_ = node.Decode(&defaultFloat)
var defaultI any = defaultFloat
field.Default = &defaultI
default:
field.Type = FieldTypeString
var defaultStr string
_ = node.Decode(&defaultStr)
var defaultI any = defaultStr
field.Default = &defaultI
}
}
return field
}
// GetInferenceCandidateFromYAML checks a yaml node config structure for a
// component and returns either the inferred type name or an error if one cannot
// be inferred.
func GetInferenceCandidateFromYAML(docProv Provider, t Type, node *yaml.Node) (string, ComponentSpec, error) {
node = unwrapDocumentNode(node)
if node.Kind != yaml.MappingNode {
return "", ComponentSpec{}, fmt.Errorf("invalid type %v, expected object", node.Kind)
}
var keys []string
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == "type" {
tStr := node.Content[i+1].Value
spec, exists := docProv.GetDocs(tStr, t)
if !exists {
return "", ComponentSpec{}, fmt.Errorf("%v type '%v' was not recognised", string(t), tStr)
}
return tStr, spec, nil
}
keys = append(keys, node.Content[i].Value)
}
return getInferenceCandidateFromList(docProv, t, keys)
}
// GetPluginConfigYAML extracts a plugin configuration node from a component
// config. This exists because there are two styles of plugin config.
func GetPluginConfigYAML(name string, node *yaml.Node) (yaml.Node, error) {
node = unwrapDocumentNode(node)
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == name {
return *node.Content[i+1], nil
}
}
pluginStruct := struct {
Plugin yaml.Node `yaml:"plugin"`
}{}
if err := node.Decode(&pluginStruct); err != nil {
return yaml.Node{}, err
}
return pluginStruct.Plugin, nil
}
//------------------------------------------------------------------------------
func (f FieldSpec) shouldOmitYAML(parentFields FieldSpecs, fieldNode, parentNode *yaml.Node) (why string, shouldOmit bool) {
conf := ToValueConfig{
Passive: true,
FallbackToInterface: true,
}
if f.omitWhenFn == nil {
return
}
field, err := f.YAMLToValue(fieldNode, conf)
if err != nil {
// If we weren't able to infer a value type then it's assumed
// that we'll capture this type error elsewhere.
return
}
parent, err := parentFields.YAMLToMap(parentNode, conf)
if err != nil {
// If we weren't able to infer a value type then it's assumed
// that we'll capture this type error elsewhere.
return
}
return f.omitWhenFn(field, parent)
}
// SanitiseYAML takes a yaml.Node and a config spec and sorts the fields of the
// node according to the spec. Also optionally removes the `type` field from
// this and all nested components.
func SanitiseYAML(cType Type, node *yaml.Node, conf SanitiseConfig) error {
node = unwrapDocumentNode(node)
newNodes := []*yaml.Node{}
var name string
var keys []string
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == "label" {
if _, omit := labelField.shouldOmitYAML(nil, node.Content[i+1], node); !omit {
newNodes = append(newNodes, node.Content[i], node.Content[i+1])
}
break
}
}
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == "type" {
if name = node.Content[i+1].Value; name == "" {
continue
}
if !conf.RemoveTypeField {
newNodes = append(newNodes, node.Content[i], node.Content[i+1])
}
break
} else {
keys = append(keys, node.Content[i].Value)
}
}
if name == "" {
if len(node.Content) == 0 {
return nil
}
var err error
if name, _, err = getInferenceCandidateFromList(conf.DocsProvider, cType, keys); err != nil {
return err
}
}
cSpec, exists := conf.DocsProvider.GetDocs(name, cType)
if !exists {
return fmt.Errorf("failed to obtain docs for %v type %v", cType, name)
}
nameFound := false
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == "plugin" && cSpec.Plugin {
node.Content[i].Value = name
}
if node.Content[i].Value != name {
continue
}
nameFound = true
if err := cSpec.Config.SanitiseYAML(node.Content[i+1], conf); err != nil {
return err
}
newNodes = append(newNodes, node.Content[i], node.Content[i+1])
break
}
// If the type field was omitted but we didn't see a config under the name
// then we need to add an empty object.
if !nameFound && conf.RemoveTypeField {
var keyNode yaml.Node
if err := keyNode.Encode(name); err != nil {
return err
}
bodyNode, err := cSpec.Config.ToYAML(conf.ForExample)
if err != nil {
return err
}
if err := cSpec.Config.SanitiseYAML(bodyNode, conf); err != nil {
return err
}
newNodes = append(newNodes, &keyNode, bodyNode)
}
reservedFields := ReservedFieldsByType(cType)
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == name || node.Content[i].Value == "type" || node.Content[i].Value == "label" {
continue
}
if spec, exists := reservedFields[node.Content[i].Value]; exists {
if _, omit := spec.shouldOmitYAML(nil, node.Content[i+1], node); omit {
continue
}
if err := spec.SanitiseYAML(node.Content[i+1], conf); err != nil {
return err
}
newNodes = append(newNodes, node.Content[i], node.Content[i+1])
}
}
node.Content = newNodes
return nil
}
// SanitiseYAML attempts to reduce a parsed config (as a *yaml.Node) down into a
// minimal representation without changing the behaviour of the config. The
// fields of the result will also be sorted according to the field spec.
func (f FieldSpec) SanitiseYAML(node *yaml.Node, conf SanitiseConfig) error {
node = unwrapDocumentNode(node)
if coreType, isCore := f.Type.IsCoreComponent(); isCore {
switch f.Kind {
case Kind2DArray:
for i := 0; i < len(node.Content); i++ {
for j := 0; j < len(node.Content[i].Content); j++ {
if err := SanitiseYAML(coreType, node.Content[i].Content[j], conf); err != nil {
return err
}
}
}
case KindArray:
for i := 0; i < len(node.Content); i++ {
if err := SanitiseYAML(coreType, node.Content[i], conf); err != nil {
return err
}
}
case KindMap:
for i := 0; i < len(node.Content)-1; i += 2 {
if err := SanitiseYAML(coreType, node.Content[i+1], conf); err != nil {
return err
}
}
default:
if err := SanitiseYAML(coreType, node, conf); err != nil {
return err
}
}
} else if len(f.Children) > 0 {
switch f.Kind {
case Kind2DArray:
for i := 0; i < len(node.Content); i++ {
for j := 0; j < len(node.Content[i].Content); j++ {
if err := f.Children.SanitiseYAML(node.Content[i].Content[j], conf); err != nil {
return err
}
}
}
case KindArray:
for i := 0; i < len(node.Content); i++ {
if err := f.Children.SanitiseYAML(node.Content[i], conf); err != nil {
return err
}
}
case KindMap:
for i := 0; i < len(node.Content)-1; i += 2 {
if err := f.Children.SanitiseYAML(node.Content[i+1], conf); err != nil {
return err
}
}
default:
if err := f.Children.SanitiseYAML(node, conf); err != nil {
return err
}
}
}
return nil
}
// SanitiseYAML attempts to reduce a parsed config (as a *yaml.Node) down into a
// minimal representation without changing the behaviour of the config. The
// fields of the result will also be sorted according to the field spec.
func (f FieldSpecs) SanitiseYAML(node *yaml.Node, conf SanitiseConfig) error {
node = unwrapDocumentNode(node)
nodeKeys := map[string]*yaml.Node{}
for i := 0; i < len(node.Content)-1; i += 2 {
nodeKeys[node.Content[i].Value] = node.Content[i+1]
}
// Following the order of our field specs, extract each field.
newNodes := []*yaml.Node{}
for _, field := range f {
if field.IsDeprecated && conf.RemoveDeprecated {
continue
}
if conf.Filter.shouldDrop(field) {
continue
}
value, exists := nodeKeys[field.Name]
if !exists {
continue
}
if _, omit := field.shouldOmitYAML(f, value, node); omit {
continue
}
if err := field.SanitiseYAML(value, conf); err != nil {
return err
}
var keyNode yaml.Node
if err := keyNode.Encode(field.Name); err != nil {
return err
}
newNodes = append(newNodes, &keyNode, value)
}
node.Content = newNodes
return nil
}
//------------------------------------------------------------------------------
func lintYAMLFromOmit(parentSpec FieldSpecs, lintTargetSpec FieldSpec, parent, node *yaml.Node) []Lint {
why, shouldOmit := lintTargetSpec.shouldOmitYAML(parentSpec, node, parent)
if shouldOmit {
return []Lint{NewLintError(node.Line, LintShouldOmit, why)}
}
return nil
}
func customLintFromYAML(ctx LintContext, spec FieldSpec, node *yaml.Node) []Lint {
lintFn := spec.getLintFunc()
if lintFn == nil {
return nil
}
fieldValue, err := spec.YAMLToValue(node, ToValueConfig{
Passive: true,
FallbackToInterface: true,
})
if err != nil {
// If we weren't able to infer a value type then it's assumed
// that we'll capture this type error elsewhere.
return []Lint{}
}
line := node.Line
if node.Style == yaml.LiteralStyle {
line++
}
lints := lintFn(ctx, line, node.Column, fieldValue)
return lints
}
// LintYAML takes a yaml.Node and a config spec and returns a list of linting
// errors found in the config.
func LintYAML(ctx LintContext, cType Type, node *yaml.Node) []Lint {
node = unwrapDocumentNode(node)
var lints []Lint
var name string
var keys []string
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == "type" {
name = node.Content[i+1].Value
break
} else {
keys = append(keys, node.Content[i].Value)
}
}
if name == "" {
if len(node.Content) == 0 {
return nil
}
var err error
if name, _, err = getInferenceCandidateFromList(ctx.DocsProvider, cType, keys); err != nil {
lints = append(lints, NewLintWarning(node.Line, LintComponentMissing, "unable to infer component type"))
return lints
}
}
cSpec, exists := ctx.DocsProvider.GetDocs(name, cType)
if !exists {
lints = append(lints, NewLintWarning(node.Line, LintComponentNotFound, fmt.Sprintf("failed to obtain docs for %v type %v", cType, name)))
return lints
}
if ctx.RejectDeprecated && cSpec.Status == StatusDeprecated {
lints = append(lints, NewLintError(node.Line, LintDeprecated, fmt.Sprintf("component %v is deprecated", cSpec.Name)))
}
nameFound := false
for i := 0; i < len(node.Content)-1; i += 2 {
if node.Content[i].Value == name {
nameFound = true
lints = append(lints, cSpec.Config.LintYAML(ctx, node.Content[i+1])...)
break
}
}
reservedFields := ReservedFieldsByType(cType)
_, canLabel := reservedFields["label"]
hasLabel := false
for i := 0; i < len(node.Content)-1; i += 2 {
key := node.Content[i].Value
if key == name || key == "type" {
continue
}
if key == "plugin" {
if nameFound || !cSpec.Plugin {
lints = append(lints, NewLintError(node.Content[i].Line, LintShouldOmit, "plugin object is ineffective"))
} else {
lints = append(lints, cSpec.Config.LintYAML(ctx, node.Content[i+1])...)
}
}
spec, exists := reservedFields[key]
hasLabel = hasLabel || (key == "label")
if exists {
lints = append(lints, lintYAMLFromOmit(cSpec.Config.Children, spec, node, node.Content[i+1])...)
lints = append(lints, spec.LintYAML(ctx, node.Content[i+1])...)
} else {
lints = append(lints, NewLintError(
node.Content[i].Line,
LintUnknown,
fmt.Sprintf("field %v is invalid when the component type is %v (%v)", node.Content[i].Value, name, cType),
))
}
}
if ctx.RequireLabels && canLabel && !hasLabel {
lints = append(lints, NewLintError(node.Line, LintMissingLabel, fmt.Sprintf("label is required for %s", cSpec.Name)))
}
return lints
}
// LintYAML returns a list of linting errors found by checking a field
// definition against a yaml node.
func (f FieldSpec) LintYAML(ctx LintContext, node *yaml.Node) []Lint {
node = unwrapDocumentNode(node)
var lints []Lint
if ctx.RejectDeprecated && f.IsDeprecated {
lints = append(lints, NewLintError(node.Line, LintDeprecated, fmt.Sprintf("field %v is deprecated", f.Name)))
}
// Execute custom linters, if the kind is non-scalar this means we execute
// the linter from the perspective of both the scalar and higher level types
// and it's up to the linting implementation to distinguish between them.
lints = append(lints, customLintFromYAML(ctx, f, node)...)
// Check basic kind matches, and execute custom linters
switch f.Kind {
case Kind2DArray:
if node.Kind != yaml.SequenceNode {
lints = append(lints, NewLintError(node.Line, LintExpectedArray, "expected array value"))
return lints
}
for i := 0; i < len(node.Content); i++ {
lints = append(lints, f.Array().LintYAML(ctx, node.Content[i])...)
}
return lints
case KindArray:
if node.Kind != yaml.SequenceNode {
lints = append(lints, NewLintError(node.Line, LintExpectedArray, "expected array value"))
return lints
}
for i := 0; i < len(node.Content); i++ {
lints = append(lints, f.Scalar().LintYAML(ctx, node.Content[i])...)
}
return lints
case KindMap:
if node.Kind != yaml.MappingNode {
lints = append(lints, NewLintError(node.Line, LintExpectedObject, "expected object value"))
return lints
}
for i := 0; i < len(node.Content)-1; i += 2 {
lints = append(lints, f.Scalar().LintYAML(ctx, node.Content[i+1])...)
}
return lints
}
// If we're a core type then execute component specific linting
if coreType, isCore := f.Type.IsCoreComponent(); isCore {
return append(lints, LintYAML(ctx, coreType, node)...)
}
// If the field has children then lint the child fields
if len(f.Children) > 0 {
return append(lints, f.Children.LintYAML(ctx, node)...)
}
// Otherwise we're a leaf node, so do basic type checking
switch f.Type {
// TODO: Do proper checking for bool and number types.
case FieldTypeBool, FieldTypeString, FieldTypeInt, FieldTypeFloat:
if node.Kind == yaml.MappingNode || node.Kind == yaml.SequenceNode {
lints = append(lints, NewLintError(node.Line, LintExpectedScalar, fmt.Sprintf("expected %v value", f.Type)))
}
case FieldTypeObject:
if node.Kind != yaml.MappingNode && node.Kind != yaml.AliasNode {
lints = append(lints, NewLintError(node.Line, LintExpectedObject, "expected object value"))
}
}
return lints
}
// LintYAML walks a yaml node and returns a list of linting errors found.
func (f FieldSpecs) LintYAML(ctx LintContext, node *yaml.Node) []Lint {
node = unwrapDocumentNode(node)
var lints []Lint
if node.Kind != yaml.MappingNode {
if node.Kind == yaml.AliasNode {
// TODO: Actually lint through aliases
return nil
}
lints = append(lints, NewLintError(node.Line, LintExpectedObject, "expected object value"))
return lints
}
specNames := map[string]FieldSpec{}
for _, field := range f {
specNames[field.Name] = field
}
for i := 0; i < len(node.Content)-1; i += 2 {
spec, exists := specNames[node.Content[i].Value]
if !exists {
if node.Content[i+1].Kind != yaml.AliasNode {
lints = append(lints, NewLintError(node.Content[i].Line, LintUnknown, fmt.Sprintf("field %v not recognised", node.Content[i].Value)))
}
continue
}
lints = append(lints, lintYAMLFromOmit(f, spec, node, node.Content[i+1])...)
lints = append(lints, spec.LintYAML(ctx, node.Content[i+1])...)
delete(specNames, node.Content[i].Value)
}
for name, remaining := range specNames {
_, isCore := remaining.Type.IsCoreComponent()
if remaining.needsDefault() &&
remaining.Default == nil &&
!isCore &&
remaining.Kind == KindScalar &&
len(remaining.Children) == 0 {
lints = append(lints, NewLintError(node.Line, LintMissing, fmt.Sprintf("field %v is required", name)))
}
}
return lints
}
//------------------------------------------------------------------------------
// ToYAML creates a YAML node from a field spec. If a default value has been
// specified then it is used. Otherwise, a zero value is generated. If recurse
// is enabled and the field has children then all children will also have values
// generated.
func (f FieldSpec) ToYAML(recurse bool) (*yaml.Node, error) {
var node yaml.Node
if f.Default != nil {
if err := node.Encode(*f.Default); err != nil {
return nil, err
}
return &node, nil
}
if f.Kind == KindArray || f.Kind == Kind2DArray {
s := []any{}
if err := node.Encode(s); err != nil {
return nil, err
}
} else if f.Kind == KindMap || len(f.Children) > 0 {
if len(f.Children) > 0 && recurse {
return f.Children.ToYAML()
}
s := map[string]any{}
if err := node.Encode(s); err != nil {
return nil, err
}
} else {
switch f.Type {
case FieldTypeString:
if err := node.Encode(""); err != nil {
return nil, err
}
case FieldTypeInt:
if err := node.Encode(0); err != nil {
return nil, err
}
case FieldTypeFloat:
if err := node.Encode(0.0); err != nil {
return nil, err
}
case FieldTypeBool:
if err := node.Encode(false); err != nil {
return nil, err
}
default:
if err := node.Encode(nil); err != nil {
return nil, err
}
}
}
return &node, nil
}
// ToYAML creates a YAML node from a list of field specs. If a default value has
// been specified for a given field then it is used. Otherwise, a zero value is
// generated.
func (f FieldSpecs) ToYAML() (*yaml.Node, error) {
var node yaml.Node
node.Kind = yaml.MappingNode
for _, spec := range f {
var keyNode yaml.Node
if err := keyNode.Encode(spec.Name); err != nil {
return nil, err
}
valueNode, err := spec.ToYAML(true)
if err != nil {
return nil, err
}
node.Content = append(node.Content, &keyNode, valueNode)
}
return &node, nil
}
// ToValueConfig describes custom options for how documentation fields should be
// used to convert a parsed node to a value type.
type ToValueConfig struct {
// Whether an problem in the config node detected during conversion
// should return a "best attempt" structure rather than an error.
Passive bool
// When a field spec is for a non-scalar type (a component) fall back to
// decoding it into an interface, otherwise the raw yaml.Node is
// returned in its place.
FallbackToInterface bool
}
// YAMLToValue converts a yaml node into a generic value by referencing the
// expected type.
func (f FieldSpec) YAMLToValue(node *yaml.Node, conf ToValueConfig) (any, error) {
node = unwrapDocumentNode(node)
switch f.Kind {
case Kind2DArray:
if !conf.Passive && node.Kind != yaml.SequenceNode {
return nil, fmt.Errorf("line %v: expected array value, got %v", node.Line, node.Kind)
}
subSpec := f.Array()
var s []any
for i := 0; i < len(node.Content); i++ {
v, err := subSpec.YAMLToValue(node.Content[i], conf)
if err != nil {
return nil, err
}
s = append(s, v)
}
return s, nil
case KindArray:
if !conf.Passive && node.Kind != yaml.SequenceNode {
return nil, fmt.Errorf("line %v: expected array value, got %v", node.Line, node.Kind)
}
subSpec := f.Scalar()
var s []any
for i := 0; i < len(node.Content); i++ {
v, err := subSpec.YAMLToValue(node.Content[i], conf)
if err != nil {
return nil, err
}
s = append(s, v)
}
return s, nil
case KindMap:
if !conf.Passive && node.Kind != yaml.MappingNode {
return nil, fmt.Errorf("line %v: expected map value, got %v", node.Line, node.Kind)
}
subSpec := f.Scalar()
m := map[string]any{}
for i := 0; i < len(node.Content)-1; i += 2 {
var err error
if m[node.Content[i].Value], err = subSpec.YAMLToValue(node.Content[i+1], conf); err != nil {
return nil, err
}
}
return m, nil
}
switch f.Type {
case FieldTypeString:
var s string
if err := node.Decode(&s); err != nil {
return nil, err
}
return s, nil
case FieldTypeInt:
var i int
if err := node.Decode(&i); err != nil {
return nil, err
}
return i, nil
case FieldTypeFloat:
var f float64
if err := node.Decode(&f); err != nil {
return nil, err
}
return f, nil
case FieldTypeBool:
var b bool
if err := node.Decode(&b); err != nil {
return nil, err
}
return b, nil
case FieldTypeUnknown:
var i any
if err := node.Decode(&i); err != nil {
return nil, err
}
return i, nil
case FieldTypeObject:
return f.Children.YAMLToMap(node, conf)
}
if conf.FallbackToInterface {
// We don't know what the field actually is (likely a component
// type), so if we we can either decode into a generic interface
// or return the raw node itself.
var v any
if err := node.Decode(&v); err != nil {
return nil, err
}
return v, nil
}
return node, nil
}
// YAMLToMap converts a yaml node into a generic map structure by referencing
// expected fields, adding default values to the map when the node does not
// contain them.
func (f FieldSpecs) YAMLToMap(node *yaml.Node, conf ToValueConfig) (map[string]any, error) {
node = unwrapDocumentNode(node)
pendingFieldsMap := map[string]FieldSpec{}
for _, field := range f {
pendingFieldsMap[field.Name] = field
}
resultMap := map[string]any{}
for i := 0; i < len(node.Content)-1; i += 2 {
fieldName := node.Content[i].Value
if f, exists := pendingFieldsMap[fieldName]; exists {
delete(pendingFieldsMap, f.Name)
var err error
if resultMap[fieldName], err = f.YAMLToValue(node.Content[i+1], conf); err != nil {
return nil, fmt.Errorf("field '%v': %w", fieldName, err)
}
} else {
var v any
if err := node.Content[i+1].Decode(&v); err != nil {
return nil, err
}
resultMap[fieldName] = v
}
}
for k, v := range pendingFieldsMap {
defValue, err := getDefault(k, v)
if err != nil {
if v.needsDefault() && !conf.Passive {
return nil, err
}
continue
}
resultMap[k] = defValue
}
return resultMap, nil
}
//------------------------------------------------------------------------------
func unwrapDocumentNode(node *yaml.Node) *yaml.Node {
if node != nil && node.Kind == yaml.DocumentNode && len(node.Content) > 0 {
return node.Content[0]
}
return node
}