-
Notifications
You must be signed in to change notification settings - Fork 16
/
expression.go
316 lines (273 loc) · 7.47 KB
/
expression.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
package dynaml
import (
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/mandelsoft/spiff/features"
"github.com/mandelsoft/spiff/yaml"
)
type Status interface {
error
Issue(fmt string, args ...interface{}) (issue yaml.Issue, localError bool, failed bool)
HasError() bool
}
type SourceProvider interface {
SourceName() string
}
type ExecCache interface {
Lock()
Unlock()
Get(key string) []byte
Set(key string, content []byte)
Clear()
}
type State interface {
GetTempName(data []byte) (string, error)
GetFileContent(file string, cached bool) ([]byte, error)
GetEncryptionKey() string
OSAccessAllowed() bool
FileAccessAllowed() bool
FileSystem() vfs.VFS
GetRegistry() Registry
GetFeatures() features.FeatureFlags
GetExecCache() ExecCache
InterpolationEnabled() bool
ControlEnabled() bool
SetTag(name string, node yaml.Node, path []string, scope TagScope) error
GetTag(name string) *Tag
GetTags(name string) []*TagInfo
}
type Binding interface {
SourceProvider
GetStaticBinding() map[string]yaml.Node
GetRootBinding() map[string]yaml.Node
FindFromRoot([]string) (yaml.Node, bool)
FindReference([]string) (yaml.Node, bool)
FindInStubs([]string) (yaml.Node, bool)
WithScope(step map[string]yaml.Node) Binding
WithListScope(step []yaml.Node) Binding // only for root element
WithLocalScope(step map[string]yaml.Node) Binding
WithPath(step string) Binding
WithSource(source string) Binding
WithNewRoot() Binding
RedirectOverwrite(path []string) Binding
Outer() Binding
Path() []string
StubPath() []string
NoMerge() bool
GetState() State
GetFeatures() features.FeatureFlags
GetTempName(data []byte) (string, error)
GetFileContent(file string, cached bool) ([]byte, error)
Flow(source yaml.Node, shouldOverride bool) (yaml.Node, Status)
Cascade(outer Binding, template yaml.Node, partial bool, templates ...yaml.Node) (yaml.Node, error)
}
type Cleanup interface {
Cleanup() error
}
type EvaluationInfo struct {
RedirectPath []string
Replace bool
Merged bool
Preferred bool
KeyName string
Source string
LocalError bool
Failed bool
Undefined bool
Raw bool
Issue yaml.Issue
Cleanups []Cleanup
yaml.NodeFlags
}
type EvaluationError struct {
resolved bool
EvaluationInfo
ok bool
}
func (e EvaluationError) Error() string {
return e.Issue.Issue
}
func RaiseEvaluationError(resolved bool, info EvaluationInfo, ok bool) {
panic(EvaluationError{resolved, info, ok})
}
func RaiseEvaluationErrorf(format string, args ...interface{}) {
info := DefaultInfo()
info.SetError(format, args...)
panic(EvaluationError{true, info, false})
}
func CatchEvaluationError(result *interface{}, info *EvaluationInfo, ok *bool, msgfmt string, args ...interface{}) {
err := recover()
if err != nil {
if eerr, my := err.(EvaluationError); my {
*result = nil
*info = eerr.EvaluationInfo
if msgfmt != "" {
(*info).SetError(msgfmt, args...)
(*info).Issue.Sequence = true
if eerr.Issue.Issue != "" {
(*info).Issue.Nested = []yaml.Issue{eerr.Issue}
}
}
*ok = eerr.ok
} else {
panic(err)
}
}
}
func (e EvaluationInfo) SourceName() string {
return e.Source
}
func DefaultInfo() EvaluationInfo {
return EvaluationInfo{nil, false, false,
false, "", "",
false, false, false, false,
yaml.Issue{}, nil, 0}
}
type Expression interface {
Evaluate(Binding, bool) (interface{}, EvaluationInfo, bool)
}
type StaticallyScopedValue interface {
StaticResolver() Binding
SetStaticResolver(binding Binding) StaticallyScopedValue
}
func (i *EvaluationInfo) Cleanup() error {
var err error
for _, c := range i.Cleanups {
e := c.Cleanup()
if e != nil {
err = e
}
}
i.Cleanups = nil
return err
}
func (i *EvaluationInfo) DenyOSOperation(name string) (interface{}, EvaluationInfo, bool) {
return i.Error("%s: no OS operations supported in this execution environment", name)
}
func (i *EvaluationInfo) Error(msgfmt interface{}, args ...interface{}) (interface{}, EvaluationInfo, bool) {
i.SetError(msgfmt, args...)
return nil, *i, false
}
func (i *EvaluationInfo) AnnotateError(err EvaluationInfo, msgfmt interface{}, args ...interface{}) (interface{}, EvaluationInfo, bool) {
i.SetError(msgfmt, args...)
if err.Issue.Issue != "" {
i.Issue.Nested = append(i.Issue.Nested, err.Issue)
}
return nil, *i, false
}
func (i *EvaluationInfo) SetError(msgfmt interface{}, args ...interface{}) {
i.LocalError = true
switch f := msgfmt.(type) {
case string:
i.Issue = yaml.NewIssue(f, args...)
default:
i.Issue = yaml.NewIssue("%s", msgfmt)
}
}
func (i *EvaluationInfo) PropagateError(value interface{}, state Status, msgfmt string, args ...interface{}) (interface{}, EvaluationInfo, bool) {
i.Issue, i.LocalError, i.Failed = state.Issue(msgfmt, args...)
if i.LocalError {
value = nil
}
return value, *i, false //!i.LocalError
}
func (i EvaluationInfo) CleanError() EvaluationInfo {
i.Issue = yaml.Issue{}
i.LocalError = false
i.Failed = false
i.Undefined = false
return i
}
func (i EvaluationInfo) GetError() string {
return i.Issue.Issue
}
func (i EvaluationInfo) Join(o EvaluationInfo) EvaluationInfo {
if o.RedirectPath != nil {
i.RedirectPath = o.RedirectPath
}
i.Replace = o.Replace // replace only by directly using the merge node
i.Preferred = i.Preferred || o.Preferred
i.Merged = i.Merged || o.Merged
if o.KeyName != "" {
i.KeyName = o.KeyName
}
if o.Issue.Issue != "" {
i.Issue = o.Issue
}
if o.LocalError {
i.LocalError = true
}
if o.Failed {
i.Failed = true
}
if o.Undefined {
i.Undefined = true
}
i.NodeFlags |= o.NodeFlags
i.Cleanups = append(i.Cleanups, o.Cleanups...)
return i
}
func ResolveExpressionOrPushEvaluation(e *Expression, resolved *bool, info *EvaluationInfo, binding Binding, locally bool) (interface{}, EvaluationInfo, bool) {
val, infoe, ok := (*e).Evaluate(binding, locally)
if info != nil {
infoe = (*info).Join(infoe)
}
if !ok {
return nil, infoe, false
}
if v, ok := val.(Expression); ok {
*e = KeepArgWrapper(v, *e)
*resolved = false
return nil, infoe, true
} else {
return val, infoe, true
}
}
func ResolveIntegerExpressionOrPushEvaluation(e *Expression, resolved *bool, info *EvaluationInfo, binding Binding, locally bool) (int64, EvaluationInfo, bool) {
value, infoe, ok := ResolveExpressionOrPushEvaluation(e, resolved, info, binding, locally)
if value == nil {
return 0, infoe, ok
}
i, ok := value.(int64)
if ok {
return i, infoe, true
} else {
infoe.Issue = yaml.NewIssue("integer operand required")
return 0, infoe, false
}
}
func ResolveExpressionListOrPushEvaluation(list *[]Expression, resolved *bool, info *EvaluationInfo, binding Binding, locally bool) ([]interface{}, EvaluationInfo, bool) {
values := make([]interface{}, len(*list))
pushed := make([]Expression, len(*list))
infoe := EvaluationInfo{}
expand := false
ok := true
copy(pushed, *list)
for i, _ := range pushed {
values[i], infoe, ok = ResolveExpressionOrPushEvaluation(&pushed[i], resolved, info, binding, locally)
info = &infoe
expand = expand || IsListExpansion(pushed[i])
if !ok {
return nil, infoe, false
}
}
if expand {
vlist := []interface{}{}
for i, v := range values {
if IsListExpansion(pushed[i]) {
list, ok := v.([]yaml.Node)
if !ok {
_, infoe, ok := infoe.Error("argument expansion required list argument")
return nil, infoe, ok
}
for _, e := range list {
vlist = append(vlist, e.Value())
}
} else {
vlist = append(vlist, v)
}
}
values = vlist
}
*list = pushed
return values, infoe, true
}