-
Notifications
You must be signed in to change notification settings - Fork 136
/
array_item_match_annotation.go
125 lines (102 loc) · 3.06 KB
/
array_item_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 ArrayItemMatchAnnotation struct {
newItem *yamlmeta.ArrayItem
thread *starlark.Thread
matcher *starlark.Value
expects MatchAnnotationExpectsKwarg
}
func NewArrayItemMatchAnnotation(newItem *yamlmeta.ArrayItem,
defaults MatchChildDefaultsAnnotation,
thread *starlark.Thread) (ArrayItemMatchAnnotation, error) {
annotation := ArrayItemMatchAnnotation{
newItem: newItem,
thread: thread,
expects: MatchAnnotationExpectsKwarg{thread: thread},
}
anns := template.NewAnnotations(newItem)
if !anns.Has(AnnotationMatch) {
return annotation, fmt.Errorf(
"Expected array item to have '%s' annotation", AnnotationMatch)
}
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)
}
}
annotation.expects.FillInDefaults(defaults)
return annotation, nil
}
func (a ArrayItemMatchAnnotation) Indexes(leftArray *yamlmeta.Array) ([]int, error) {
idxs, err := a.MatchNodes(leftArray)
if err != nil {
return nil, err
}
return idxs, a.expects.Check(len(idxs))
}
func (a ArrayItemMatchAnnotation) MatchNodes(leftArray *yamlmeta.Array) ([]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.String:
var leftIdxs []int
for i, item := range leftArray.Items {
result, err := overlayModule{}.compareByMapKey(string(typedVal), item, a.newItem)
if err != nil {
return nil, err
}
if result {
leftIdxs = append(leftIdxs, i)
}
}
return leftIdxs, nil
case starlark.Callable:
var leftIdxs []int
for i, item := range leftArray.Items {
matcherArgs := starlark.Tuple{
starlark.MakeInt(i),
yamltemplate.NewStarlarkFragment(item),
yamltemplate.NewStarlarkFragment(a.newItem),
}
// 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, i)
}
}
return leftIdxs, nil
default:
return nil, fmt.Errorf("Expected '%s' annotation keyword argument 'by'"+
" to be either string (for map key) or function, but was %T", AnnotationMatch, typedVal)
}
}