-
Notifications
You must be signed in to change notification settings - Fork 137
/
data_values.go
97 lines (80 loc) · 2.21 KB
/
data_values.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
package yttlibrary
import (
"fmt"
"github.com/k14s/ytt/pkg/structmeta"
"github.com/k14s/ytt/pkg/yamlmeta"
"github.com/k14s/ytt/pkg/yamltemplate"
)
const (
AnnotationDataValues structmeta.AnnotationName = "data/values"
)
type DataValues struct {
DocSet *yamlmeta.DocumentSet
MetasOpts yamltemplate.MetasOpts
}
func (v DataValues) Extract() ([]*yamlmeta.Document, []*yamlmeta.Document, error) {
err := v.checkNonDocs(v.DocSet)
if err != nil {
return nil, nil, err
}
valuesDocs, nonValuesDocs, err := v.extract(v.DocSet)
if err != nil {
return nil, nil, err
}
return valuesDocs, nonValuesDocs, nil
}
func (v DataValues) extract(docSet *yamlmeta.DocumentSet) ([]*yamlmeta.Document, []*yamlmeta.Document, error) {
var valuesDocs []*yamlmeta.Document
var nonValuesDocs []*yamlmeta.Document
for _, doc := range docSet.Items {
var hasMatchingAnn bool
for _, meta := range doc.GetMetas() {
// TODO potentially use template.NewAnnotations(doc).Has(yttoverlay.AnnotationMatch)
// however if doc was not processed by the template, it wont have any annotations set
structMeta, err := yamltemplate.NewStructMetaFromMeta(meta, v.MetasOpts)
if err != nil {
return nil, nil, err
}
for _, ann := range structMeta.Annotations {
if ann.Name == AnnotationDataValues {
hasMatchingAnn = true
}
}
}
if hasMatchingAnn {
valuesDocs = append(valuesDocs, doc)
} else {
nonValuesDocs = append(nonValuesDocs, doc)
}
}
return valuesDocs, nonValuesDocs, nil
}
func (v DataValues) checkNonDocs(val interface{}) error {
node, ok := val.(yamlmeta.Node)
if !ok {
return nil
}
for _, meta := range node.GetMetas() {
structMeta, err := yamltemplate.NewStructMetaFromMeta(meta, v.MetasOpts)
if err != nil {
return err
}
for _, ann := range structMeta.Annotations {
if ann.Name == AnnotationDataValues {
// TODO check for annotation emptiness
_, isDoc := node.(*yamlmeta.Document)
if !isDoc {
errMsg := "Expected YAML document to be annotated with %s but was %T"
return fmt.Errorf(errMsg, AnnotationDataValues, node)
}
}
}
}
for _, childVal := range node.GetValues() {
err := v.checkNonDocs(childVal)
if err != nil {
return err
}
}
return nil
}