-
Notifications
You must be signed in to change notification settings - Fork 136
/
document_match_annotation.go
105 lines (87 loc) · 2.67 KB
/
document_match_annotation.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
package overlay
import (
"fmt"
"github.com/k14s/ytt/pkg/template"
tplcore "github.com/k14s/ytt/pkg/template/core"
"github.com/k14s/ytt/pkg/yamlmeta"
"github.com/k14s/ytt/pkg/yamltemplate"
"go.starlark.net/starlark"
)
type DocumentMatchAnnotation struct {
newDoc *yamlmeta.Document
thread *starlark.Thread
matcher *starlark.Value
expects MatchAnnotationExpectsKwarg
}
func NewDocumentMatchAnnotation(newDoc *yamlmeta.Document, thread *starlark.Thread) (DocumentMatchAnnotation, error) {
annotation := DocumentMatchAnnotation{
newDoc: newDoc,
thread: thread,
expects: MatchAnnotationExpectsKwarg{thread: thread},
}
anns := template.NewAnnotations(newDoc)
kwargs := anns.Kwargs(AnnotationMatch)
if len(kwargs) == 0 {
return annotation, fmt.Errorf("Expected '%s' annotation to have "+
"at least one keyword argument (by=..., expects=...)", AnnotationMatch)
}
for _, kwarg := range kwargs {
kwargName := string(kwarg[0].(starlark.String))
switch kwargName {
case "by":
annotation.matcher = &kwarg[1]
case "expects":
annotation.expects.expects = &kwarg[1]
case "missing_ok":
annotation.expects.missingOK = &kwarg[1]
default:
return annotation, fmt.Errorf(
"Unknown '%s' annotation keyword argument '%s'", AnnotationMatch, kwargName)
}
}
return annotation, nil
}
func (a DocumentMatchAnnotation) IndexTuples(leftDocSets []*yamlmeta.DocumentSet) ([][]int, error) {
idxs, err := a.MatchNodes(leftDocSets)
if err != nil {
return nil, err
}
return idxs, a.expects.Check(len(idxs))
}
func (a DocumentMatchAnnotation) MatchNodes(leftDocSets []*yamlmeta.DocumentSet) ([][]int, error) {
if a.matcher == nil {
return nil, fmt.Errorf("Expected '%s' annotation "+
"keyword argument 'by' to be specified", AnnotationMatch)
}
switch typedVal := (*a.matcher).(type) {
case starlark.Callable:
var leftIdxs [][]int
var combinedIdx int
for i, leftDocSet := range leftDocSets {
for j, item := range leftDocSet.Items {
matcherArgs := starlark.Tuple{
starlark.MakeInt(combinedIdx),
yamltemplate.NewStarlarkFragment(item),
yamltemplate.NewStarlarkFragment(a.newDoc),
}
// TODO check thread correctness
result, err := starlark.Call(a.thread, *a.matcher, matcherArgs, []starlark.Tuple{})
if err != nil {
return nil, err
}
resultBool, err := tplcore.NewStarlarkValue(result).AsBool()
if err != nil {
return nil, err
}
if resultBool {
leftIdxs = append(leftIdxs, []int{i, j})
}
combinedIdx += 1
}
}
return leftIdxs, nil
default:
return nil, fmt.Errorf("Expected '%s' annotation keyword argument 'by'"+
" to be function, but was %T", AnnotationMatch, typedVal)
}
}