-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathcompile.go
444 lines (386 loc) · 11.6 KB
/
compile.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package template
import (
"errors"
"fmt"
"sort"
"github.com/wallix/awless/template/env"
"github.com/wallix/awless/template/internal/ast"
"github.com/wallix/awless/template/params"
)
type Mode []compileFunc
var (
TestCompileMode = []compileFunc{
injectCommandsInNodesPass,
failOnDeclarationWithNoResultPass,
processAndValidateParamsPass,
checkInvalidReferenceDeclarationsPass,
resolveHolesPass,
resolveMissingHolesPass,
removeOptionalHolesPass,
resolveAliasPass,
inlineVariableValuePass,
resolveParamsAndExtractRefsPass,
}
PreRevertCompileMode = []compileFunc{
resolveParamsAndExtractRefsPass,
}
NewRunnerCompileMode = []compileFunc{
injectCommandsInNodesPass,
failOnDeclarationWithNoResultPass,
processAndValidateParamsPass,
checkInvalidReferenceDeclarationsPass,
resolveHolesPass,
resolveMissingHolesPass,
removeOptionalHolesPass,
resolveAliasPass,
inlineVariableValuePass,
failOnUnresolvedHolesPass,
failOnUnresolvedAliasPass,
resolveParamsAndExtractRefsPass,
convertParamsPass,
validateCommandsPass,
}
)
func Compile(tpl *Template, cenv env.Compiling, mode ...Mode) (*Template, env.Compiling, error) {
var pass *multiPass
if len(mode) > 0 {
pass = newMultiPass(mode[0]...)
} else {
pass = newMultiPass(NewRunnerCompileMode...)
}
return pass.compile(tpl, cenv)
}
type compileFunc func(*Template, env.Compiling) (*Template, env.Compiling, error)
// Leeloo Dallas
type multiPass struct {
passes []compileFunc
}
func newMultiPass(passes ...compileFunc) *multiPass {
return &multiPass{passes: passes}
}
func (p *multiPass) compile(tpl *Template, cenv env.Compiling) (newTpl *Template, newEnv env.Compiling, err error) {
newTpl, newEnv = tpl, cenv
for _, pass := range p.passes {
newTpl, newEnv, err = pass(newTpl, newEnv)
if err != nil {
return
}
}
return
}
func injectCommandsInNodesPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
if cenv.LookupCommandFunc() == nil {
return tpl, cenv, fmt.Errorf("command lookuper is undefined")
}
for _, node := range tpl.CommandNodesIterator() {
key := fmt.Sprintf("%s%s", node.Action, node.Entity)
cmd, ok := cenv.LookupCommandFunc()(key).(ast.Command)
if !ok {
return tpl, cenv, fmt.Errorf("%s: casting: %v is not a command", key, cmd)
}
if cmd == nil {
return tpl, cenv, fmt.Errorf("command for '%s' is nil", key)
}
node.Command = cmd
}
return tpl, cenv, nil
}
func failOnDeclarationWithNoResultPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
failOnDeclarationWithNoResult := func(node *ast.DeclarationNode) error {
cmdNode, ok := node.Expr.(*ast.CommandNode)
if !ok {
return nil
}
type ER interface {
ExtractResult(interface{}) string
}
if _, ok := cmdNode.Command.(ER); !ok {
return cmdErr(cmdNode, "command does not return a result, cannot assign to a variable")
}
return nil
}
for _, dcl := range tpl.declarationNodesIterator() {
if err := failOnDeclarationWithNoResult(dcl); err != nil {
return tpl, cenv, err
}
}
return tpl, cenv, nil
}
func processAndValidateParamsPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
normalizeMissingRequiredParamsAsHoleAndValidate := func(node *ast.CommandNode) error {
rule := node.ParamsSpec().Rule()
missingRequired := rule.Missing(node.Keys())
for _, e := range missingRequired {
normalized := fmt.Sprintf("%s.%s", node.Entity, e)
node.ParamNodes[e] = ast.NewHoleNode(normalized)
}
if err := params.Run(rule, node.Keys()); err != nil {
return cmdErr(node, err)
}
_, optionals, suggested := params.List(rule)
switch cenv.ParamsMode() {
case env.REQUIRED_PARAMS_ONLY:
return nil
case env.REQUIRED_AND_SUGGESTED_PARAMS:
suggested = excludeFromSlice(suggested, node.Keys())
case env.ALL_PARAMS:
suggested = excludeFromSlice(optionals, node.Keys())
}
for _, e := range suggested {
key := fmt.Sprintf("%s.%s", node.Entity, e)
node.ParamNodes[e] = ast.NewOptionalHoleNode(key)
}
return nil
}
err := tpl.visitCommandNodesE(normalizeMissingRequiredParamsAsHoleAndValidate)
return tpl, cenv, err
}
func resolveParamsAndExtractRefsPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
for _, node := range tpl.CommandNodesIterator() {
for k, param := range node.ParamNodes {
switch paramNode := param.(type) {
case ast.InterfaceNode:
node.ParamNodes[k] = paramNode.Value()
case ast.RefNode:
node.Refs[k] = paramNode
delete(node.ParamNodes, k)
case ast.ListNode:
var hasRef bool
var arr []interface{}
for _, elem := range paramNode.Elems() {
switch e := elem.(type) {
case ast.InterfaceNode:
arr = append(arr, e.Value())
case ast.RefNode:
hasRef = true
arr = append(arr, e)
case ast.ConcatenationNode:
arr = append(arr, e.Concat())
case ast.HoleNode, ast.AliasNode, ast.ListNode:
return tpl, cenv, fmt.Errorf("%s: unresolved value in list of type %T", k, e)
default:
arr = append(arr, e)
}
}
if hasRef {
node.Refs[k] = ast.NewListNode(arr)
delete(node.ParamNodes, k)
} else {
node.ParamNodes[k] = arr
}
case ast.ConcatenationNode:
node.ParamNodes[k] = paramNode.Concat()
case ast.HoleNode, ast.AliasNode:
return tpl, cenv, fmt.Errorf("%s: unresolved value of type %T", k, paramNode)
}
}
}
return tpl, cenv, nil
}
func convertParamsPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
convert := func(node *ast.CommandNode) error {
for _, reducer := range node.ParamsSpec().Reducers() {
params := make(map[string]interface{})
for k, v := range node.ParamNodes {
params[k] = v
}
for k, v := range node.Refs {
params[k] = v
}
out, err := reducer.Reduce(params)
if err != nil {
return cmdErr(node, err)
}
for _, k := range reducer.Keys() {
delete(node.ParamNodes, k)
delete(node.Refs, k)
}
for k, v := range out {
switch v.(type) {
case ast.ListNode, ast.RefNode, ast.ConcatenationNode:
node.Refs[k] = v
default:
node.ParamNodes[k] = v
}
}
}
return nil
}
err := tpl.visitCommandNodesE(convert)
return tpl, cenv, err
}
func validateCommandsPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
collectValidationErrs := func(node *ast.CommandNode) error {
if err := params.Validate(node.ParamsSpec().Validators(), node.ParamNodes); err != nil {
return cmdErr(node, err)
}
return nil
}
err := tpl.visitCommandNodesE(collectValidationErrs)
return tpl, cenv, err
}
func checkInvalidReferenceDeclarationsPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
return tpl, cenv, ast.VerifyRefs(tpl.AST)
}
func inlineVariableValuePass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
newTpl := &Template{ID: tpl.ID, AST: tpl.AST.Clone()}
newTpl.Statements = []*ast.Statement{}
for i, st := range tpl.Statements {
decl, isDecl := st.Node.(*ast.DeclarationNode)
if isDecl {
if right, isRightExpr := decl.Expr.(*ast.RightExpressionNode); isRightExpr {
if res := right.Result(); res != nil {
cenv.Push(env.RESOLVED_VARS, map[string]interface{}{decl.Ident: res})
}
ast.ProcessRefs(
&ast.AST{Statements: tpl.Statements[i+1:]},
map[string]interface{}{decl.Ident: right.Node()},
)
continue
}
}
newTpl.Statements = append(newTpl.Statements, st)
}
return newTpl, cenv, nil
}
func resolveHolesPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
processed := ast.ProcessHoles(tpl.AST, cenv.Get(env.FILLERS))
cenv.Push(env.PROCESSED_FILLERS, processed)
return tpl, cenv, nil
}
func resolveMissingHolesPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
uniqueHoles := ast.CollectUniqueHoles(tpl.AST)
var sortedHoles []ast.HoleNode
for hole := range uniqueHoles {
sortedHoles = append(sortedHoles, hole)
}
sort.Slice(sortedHoles, func(i, j int) bool {
a := sortedHoles[i]
b := sortedHoles[j]
if a.IsOptional() == b.IsOptional() {
return a.Hole() < b.Hole()
} else {
if a.IsOptional() {
return false
}
return true
}
})
for _, hole := range sortedHoles {
k := hole.Hole()
if cenv.MissingHolesFunc() != nil {
actual := cenv.MissingHolesFunc()(k, uniqueHoles[hole], hole.IsOptional())
if actual == "" && hole.IsOptional() {
continue
}
params, err := ParseParams(fmt.Sprintf("%s=%s", k, actual))
if err != nil {
if params, err = ParseParams(fmt.Sprintf("%s=%s", k, ast.Quote(actual))); err != nil {
return tpl, cenv, err
}
}
cenv.Push(env.FILLERS, map[string]interface{}{k: params[k]})
}
}
processed := ast.ProcessHoles(tpl.AST, cenv.Get(env.FILLERS))
cenv.Push(env.PROCESSED_FILLERS, processed)
return tpl, cenv, nil
}
func removeOptionalHolesPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
ast.RemoveOptionalHoles(tpl.AST)
return tpl, cenv, nil
}
func resolveAliasPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
var emptyResolv []string
resolvAliasFunc := func(action, entity string, key string) func(string) (string, bool) {
return func(alias string) (string, bool) {
if cenv.AliasFunc() == nil {
return "", false
}
normalized := fmt.Sprintf("%s.%s.%s", action, entity, key)
actual := cenv.AliasFunc()(normalized, alias)
if actual == "" {
emptyResolv = append(emptyResolv, alias)
return "", false
} else {
cenv.Log().ExtraVerbosef("alias: resolved '%s' to '%s' for key %s", alias, actual, key)
return actual, true
}
}
}
ast.ProcessAliases(tpl.AST, resolvAliasFunc)
switch len(emptyResolv) {
case 0:
break
case 1:
return tpl, cenv, fmt.Errorf("cannot resolve alias \"%s\". Not found in locally synced data.", emptyResolv[0])
default:
return tpl, cenv, fmt.Errorf("cannot resolve aliases: %q. Not found in locally synced data.", emptyResolv)
}
return tpl, cenv, nil
}
func failOnUnresolvedHolesPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
uniqueUnresolved := make(map[string]struct{})
for _, hole := range ast.CollectHoles(tpl.AST) {
uniqueUnresolved[hole.String()] = struct{}{}
}
var unresolved []string
for k := range uniqueUnresolved {
unresolved = append(unresolved, k)
}
if len(unresolved) > 0 {
sort.Strings(unresolved)
return tpl, cenv, fmt.Errorf("template contains unresolved holes: %v", unresolved)
}
return tpl, cenv, nil
}
func failOnUnresolvedAliasPass(tpl *Template, cenv env.Compiling) (*Template, env.Compiling, error) {
uniqueUnresolved := make(map[string]struct{})
for _, alias := range ast.CollectAliases(tpl.AST) {
uniqueUnresolved[alias.String()] = struct{}{}
}
var unresolved []string
for k := range uniqueUnresolved {
unresolved = append(unresolved, k)
}
if len(unresolved) > 0 {
sort.Strings(unresolved)
return tpl, cenv, fmt.Errorf("template contains unresolved alias: %v", unresolved)
}
return tpl, cenv, nil
}
func cmdErr(cmd *ast.CommandNode, i interface{}, a ...interface{}) error {
var prefix string
if cmd != nil {
prefix = fmt.Sprintf("%s %s: ", cmd.Action, cmd.Entity)
}
var msg string
switch ii := i.(type) {
case nil:
return nil
case string:
msg = ii
case error:
msg = ii.Error()
}
if len(a) == 0 {
return errors.New(prefix + msg)
}
return fmt.Errorf("%s"+msg, append([]interface{}{prefix}, a...)...)
}
func contains(arr []string, s string) bool {
for _, v := range arr {
if v == s {
return true
}
}
return false
}
func excludeFromSlice(in []string, exclude []string) (out []string) {
for _, v := range in {
if !contains(exclude, v) {
out = append(out, v)
}
}
return out
}