-
Notifications
You must be signed in to change notification settings - Fork 136
/
api.go
203 lines (156 loc) · 5.69 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
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, _ := Comparison{}.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, _ := Comparison{}.Compare(actualObj, expectedObj)
return starlark.Bool(result), nil
}
return starlark.NewBuiltin("overlay.subset_matcher", core.ErrWrapper(matchFunc)), nil
}