-
Notifications
You must be signed in to change notification settings - Fork 136
/
api.go
322 lines (254 loc) · 8.56 KB
/
api.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
package overlay
import (
"fmt"
"reflect"
"github.com/k14s/ytt/pkg/template/core"
"github.com/k14s/ytt/pkg/yamlmeta"
"github.com/k14s/ytt/pkg/yamltemplate"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
var (
API = starlark.StringDict{
"overlay": &starlarkstruct.Module{
Name: "overlay",
Members: starlark.StringDict{
"apply": starlark.NewBuiltin("overlay.apply", core.ErrWrapper(overlayModule{}.Apply)),
"index": starlark.NewBuiltin("overlay.index", core.ErrWrapper(overlayModule{}.Index)),
"all": starlark.NewBuiltin("overlay.all", core.ErrWrapper(overlayModule{}.All)),
"map_key": starlark.NewBuiltin("overlay.map_key", core.ErrWrapper(overlayModule{}.MapKey)),
"subset": starlark.NewBuiltin("overlay.subset", core.ErrWrapper(overlayModule{}.Subset)),
},
},
}
)
type overlayModule struct{}
func (b overlayModule) Apply(
thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() == 0 {
return starlark.None, fmt.Errorf("expected exactly at least argument")
}
typedVals := core.NewStarlarkValue(args).AsInterface().([]interface{})
var result interface{} = typedVals[0]
for _, right := range typedVals[1:] {
var err error
result, err = OverlayOp{Left: result, Right: right, Thread: thread}.Apply() // left is modified
if err != nil {
return starlark.None, err
}
}
return yamltemplate.NewStarlarkFragment(result), nil
}
func (b overlayModule) Index(
thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
expectedIdx64, err := core.NewStarlarkValue(args.Index(0)).AsInt64()
if err != nil {
return starlark.None, err
}
matchFunc := func(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 3 {
return starlark.None, fmt.Errorf("expected exactly 3 arguments")
}
idx64, err := core.NewStarlarkValue(args.Index(0)).AsInt64()
if err != nil {
return starlark.None, err
}
if expectedIdx64 == idx64 {
return starlark.Bool(true), nil
}
return starlark.Bool(false), nil
}
return starlark.NewBuiltin("overlay.index_matcher", core.ErrWrapper(matchFunc)), nil
}
func (b overlayModule) All(
thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 3 {
return starlark.None, fmt.Errorf("expected exactly 3 arguments")
}
return starlark.Bool(true), nil
}
func (b overlayModule) MapKey(
thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
keyName, err := core.NewStarlarkValue(args.Index(0)).AsString()
if err != nil {
return starlark.None, err
}
matchFunc := func(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 3 {
return starlark.None, fmt.Errorf("expected exactly 3 arguments")
}
oldVal := core.NewStarlarkValue(args.Index(1)).AsInterface()
newVal := core.NewStarlarkValue(args.Index(2)).AsInterface()
result, err := b.compareByMapKey(keyName, oldVal, newVal)
if err != nil {
return nil, err
}
return starlark.Bool(result), nil
}
return starlark.NewBuiltin("overlay.map_key_matcher", core.ErrWrapper(matchFunc)), nil
}
func (b overlayModule) compareByMapKey(keyName string, oldVal, newVal interface{}) (bool, error) {
oldKeyVal, err := b.pullOutMapValue(keyName, oldVal)
if err != nil {
return false, err
}
newKeyVal, err := b.pullOutMapValue(keyName, newVal)
if err != nil {
return false, err
}
result, _ := b.compareLeafs(oldKeyVal, newKeyVal)
return result, nil
}
func (b overlayModule) pullOutMapValue(keyName string, newVal interface{}) (interface{}, error) {
typedArrayItem, ok := newVal.(*yamlmeta.ArrayItem)
if !ok {
return starlark.None, fmt.Errorf("Expected new item to be arrayitem, but was %T", newVal)
}
typedMap, ok := typedArrayItem.Value.(*yamlmeta.Map)
if !ok {
return starlark.None, fmt.Errorf("Expected arrayitem to contain map, but was %T", typedArrayItem.Value)
}
for _, item := range typedMap.Items {
if reflect.DeepEqual(item.Key, keyName) {
return item.Value, nil
}
}
return starlark.None, fmt.Errorf("Expected to find mapitem with key %s, but did not", keyName)
}
func (b overlayModule) Subset(
thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
expectedArg := args.Index(0)
matchFunc := func(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 3 {
return starlark.None, fmt.Errorf("expected exactly 3 arguments")
}
leftVal := core.NewStarlarkValue(args.Index(1)).AsInterface()
expectedVal := core.NewStarlarkValue(expectedArg).AsInterface()
actualObj := yamlmeta.NewASTFromInterface(leftVal)
expectedObj := yamlmeta.NewASTFromInterface(expectedVal)
if _, ok := actualObj.(*yamlmeta.ArrayItem); ok {
expectedObj = &yamlmeta.ArrayItem{Value: expectedObj}
}
if _, ok := actualObj.(*yamlmeta.Document); ok {
expectedObj = &yamlmeta.Document{Value: expectedObj}
}
result, _ := b.compareSubset(actualObj, expectedObj)
return starlark.Bool(result), nil
}
return starlark.NewBuiltin("overlay.subset_matcher", core.ErrWrapper(matchFunc)), nil
}
func (b *overlayModule) compareSubset(left, right interface{}) (bool, string) {
switch typedRight := right.(type) {
case *yamlmeta.DocumentSet:
panic("Unexpected docset")
case *yamlmeta.Document:
typedLeft, isDoc := left.(*yamlmeta.Document)
if !isDoc {
return false, fmt.Sprintf("Expected doc, but was %T", left)
}
return b.compareSubset(typedLeft.Value, typedRight.Value)
case *yamlmeta.Map:
typedLeft, isMap := left.(*yamlmeta.Map)
if !isMap {
return false, fmt.Sprintf("Expected map, but was %T", left)
}
for _, rightItem := range typedRight.Items {
matched := false
for _, leftItem := range typedLeft.Items {
if reflect.DeepEqual(leftItem.Key, rightItem.Key) {
result, explain := b.compareSubset(leftItem, rightItem)
if !result {
return false, explain
}
matched = true
}
}
if !matched {
return false, "Expected at least one map item to match by key"
}
}
return true, ""
case *yamlmeta.MapItem:
typedLeft, isMapItem := left.(*yamlmeta.MapItem)
if !isMapItem {
return false, fmt.Sprintf("Expected mapitem, but was %T", left)
}
return b.compareSubset(typedLeft.Value, typedRight.Value)
case *yamlmeta.Array:
typedLeft, isArray := left.(*yamlmeta.Array)
if !isArray {
return false, fmt.Sprintf("Expected array, but was %T", left)
}
for i, item := range typedRight.Items {
if i >= len(typedLeft.Items) {
return false, "Expected to have matching number of array items"
}
result, explain := b.compareSubset(typedLeft.Items[i].Value, item.Value)
if !result {
return false, explain
}
}
return true, ""
case *yamlmeta.ArrayItem:
typedLeft, isArrayItem := left.(*yamlmeta.ArrayItem)
if !isArrayItem {
return false, fmt.Sprintf("Expected arrayitem, but was %T", left)
}
return b.compareSubset(typedLeft.Value, typedRight.Value)
default:
return b.compareLeafs(left, right)
}
}
func (b *overlayModule) compareLeafs(left, right interface{}) (bool, string) {
if reflect.DeepEqual(left, right) {
return true, ""
}
if result, _ := b.compareAsInt64s(left, right); result {
return true, ""
}
return false, fmt.Sprintf("Expected leaf values to match %T %T", left, right)
}
func (b *overlayModule) compareAsInt64s(left, right interface{}) (bool, string) {
leftVal, ok := b.upcastToInt64(left)
if !ok {
return false, "Left obj is upcastable to int64"
}
rightVal, ok := b.upcastToInt64(right)
if !ok {
return false, "Right obj is upcastable to int64"
}
return leftVal == rightVal, "Left and right numbers are not equal"
}
func (b *overlayModule) upcastToInt64(val interface{}) (int64, bool) {
switch typedVal := val.(type) {
case int:
return int64(typedVal), true
case int16:
return int64(typedVal), true
case int32:
return int64(typedVal), true
case int64:
return int64(typedVal), true
case int8:
return int64(typedVal), true
default:
return 0, false
}
}