forked from geofffranks/spruce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.go
528 lines (448 loc) · 11.3 KB
/
operator.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package spruce
import (
"fmt"
"os"
"regexp"
"strconv"
"github.com/geofffranks/yaml"
"github.com/starkandwayne/goutils/ansi"
. "github.com/geofffranks/spruce/log"
"github.com/starkandwayne/goutils/tree"
)
// Action ...
type Action int
const (
// Replace ...
Replace Action = iota
// Inject ...
Inject
)
// OperatorPhase ...
type OperatorPhase int
const (
// MergePhase ...
MergePhase OperatorPhase = iota
// EvalPhase ...
EvalPhase
// ParamPhase ...
ParamPhase
)
// Response ...
type Response struct {
Type Action
Value interface{}
}
// Operator ...
type Operator interface {
// setup whatever global/static state needed -- see (( static_ips ... ))
Setup() error
// evaluate the tree and determine what should be done to satisfy caller
Run(ev *Evaluator, args []*Expr) (*Response, error)
// returns a set of implicit / inherent dependencies used by Run()
Dependencies(ev *Evaluator, args []*Expr, locs []*tree.Cursor, auto []*tree.Cursor) []*tree.Cursor
// what phase does this operator run during?
Phase() OperatorPhase
}
// OpRegistry ...
var OpRegistry map[string]Operator
// OperatorFor ...
func OperatorFor(name string) Operator {
if op, ok := OpRegistry[name]; ok {
return op
}
return NullOperator{Missing: name}
}
// RegisterOp ...
func RegisterOp(name string, op Operator) {
if OpRegistry == nil {
OpRegistry = map[string]Operator{}
}
OpRegistry[name] = op
}
// SetupOperators ...
func SetupOperators(phase OperatorPhase) error {
errors := MultiError{Errors: []error{}}
for _, op := range OpRegistry {
if op.Phase() == phase {
if err := op.Setup(); err != nil {
errors.Append(err)
}
}
}
if len(errors.Errors) > 0 {
return errors
}
return nil
}
// ExprType ...
type ExprType int
const (
// Reference ...
Reference ExprType = iota
// Literal ...
Literal
// LogicalOr ...
LogicalOr
EnvVar
)
// Expr ...
type Expr struct {
Type ExprType
Reference *tree.Cursor
Literal interface{}
Name string
Left *Expr
Right *Expr
}
func (e *Expr) String() string {
switch e.Type {
case Literal:
if e.Literal == nil {
return "nil"
}
if _, ok := e.Literal.(string); ok {
return fmt.Sprintf(`"%s"`, e.Literal)
}
return fmt.Sprintf("%v", e.Literal)
case EnvVar:
return fmt.Sprintf("$%s", e.Name)
case Reference:
return e.Reference.String()
case LogicalOr:
return fmt.Sprintf("%s || %s", e.Left, e.Right)
default:
return "<!! unknown !!>"
}
}
// Reduce ...
func (e *Expr) Reduce() (*Expr, error) {
var reduce func(*Expr) (*Expr, *Expr, bool)
reduce = func(e *Expr) (*Expr, *Expr, bool) {
switch e.Type {
case Literal:
return e, e, false
case EnvVar:
return e, nil, false
case Reference:
return e, nil, false
case LogicalOr:
l, short, _ := reduce(e.Left)
if short != nil {
return l, short, true
}
r, short, more := reduce(e.Right)
return &Expr{
Type: LogicalOr,
Left: l,
Right: r,
}, short, more
}
return nil, nil, false
}
reduced, short, more := reduce(e)
if more && short != nil {
return reduced, NewWarningError(eContextAll, "@R{literal} @c{%v} @R{short-circuits expression (}@c{%s}@R{)}", short, e)
}
return reduced, nil
}
// Resolve ...
func (e *Expr) Resolve(tree map[interface{}]interface{}) (*Expr, error) {
switch e.Type {
case Literal:
return e, nil
case EnvVar:
v := os.Getenv(e.Name)
if v == "" {
return nil, ansi.Errorf("@R{Environment variable} @c{$%s} @R{is not set}", e.Name)
}
var val interface{}
err := yaml.Unmarshal([]byte(v), &val)
_, isString := val.(string)
if isString || err != nil {
return &Expr{Type: Literal, Literal: v}, nil
}
return &Expr{Type: Literal, Literal: val}, nil
case Reference:
if _, err := e.Reference.Resolve(tree); err != nil {
return nil, ansi.Errorf("@R{Unable to resolve `}@c{%s}@R{`: %s}", e.Reference, err)
}
return e, nil
case LogicalOr:
if o, err := e.Left.Resolve(tree); err == nil {
return o, nil
}
return e.Right.Resolve(tree)
}
return nil, ansi.Errorf("@R{unknown expression operand type (}@c{%d}@R{)}", e.Type)
}
// Evaluate ...
func (e *Expr) Evaluate(tree map[interface{}]interface{}) (interface{}, error) {
final, err := e.Resolve(tree)
if err != nil {
return nil, err
}
switch final.Type {
case Literal:
return final.Literal, nil
case EnvVar:
return os.Getenv(final.Name), nil
case Reference:
return final.Reference.Resolve(tree)
case LogicalOr:
return nil, fmt.Errorf("expression resolved to a logical OR operation (which shouldn't happen)")
}
return nil, fmt.Errorf("unknown operand type")
}
// Dependencies ...
func (e *Expr) Dependencies(ev *Evaluator, locs []*tree.Cursor) []*tree.Cursor {
l := []*tree.Cursor{}
canonicalize := func(c *tree.Cursor) {
cc := c.Copy()
for cc.Depth() > 0 {
if _, err := cc.Canonical(ev.Tree); err == nil {
break
}
cc.Pop()
}
if cc.Depth() > 0 {
canon, _ := cc.Canonical(ev.Tree)
l = append(l, canon)
}
}
switch e.Type {
case Reference:
canonicalize(e.Reference)
case LogicalOr:
for _, c := range e.Left.Dependencies(ev, locs) {
canonicalize(c)
}
for _, c := range e.Right.Dependencies(ev, locs) {
canonicalize(c)
}
}
return l
}
// Opcall ...
type Opcall struct {
src string
where *tree.Cursor
canonical *tree.Cursor
op Operator
args []*Expr
}
// ParseOpcall ...
func ParseOpcall(phase OperatorPhase, src string) (*Opcall, error) {
split := func(src string) []string {
list := make([]string, 0, 0)
buf := ""
escaped := false
quoted := false
for _, c := range src {
if escaped {
switch c {
case 'n':
buf += "\n"
case 'r':
buf += "\r"
case 't':
buf += "\t"
default:
buf += string(c)
}
escaped = false
continue
}
if c == '\\' {
escaped = true
continue
}
if c == ' ' || c == '\t' || c == ',' {
if quoted {
buf += string(c)
continue
} else {
if buf != "" {
list = append(list, buf)
buf = ""
}
if c == ',' {
list = append(list, ",")
}
}
continue
}
if c == '"' {
buf += string(c)
quoted = !quoted
continue
}
buf += string(c)
}
if buf != "" {
list = append(list, buf)
}
return list
}
argify := func(src string) (args []*Expr, err error) {
qstring := regexp.MustCompile(`(?s)^"(.*)"$`)
integer := regexp.MustCompile(`^[+-]?\d+(\.\d+)?$`)
float := regexp.MustCompile(`^[+-]?\d*\.\d+$`)
envvar := regexp.MustCompile(`^\$[a-zA-Z_][a-zA-Z0-9_.]*$`)
var final []*Expr
var left, op *Expr
pop := func() {
if left != nil {
final = append(final, left)
left = nil
}
}
push := func(e *Expr) {
TRACE("expr: pushing data expression `%s' onto stack", e)
TRACE("expr: start: left=`%s', op=`%s'", left, op)
defer func() { TRACE("expr: end: left=`%s', op=`%s'\n", left, op) }()
if left == nil {
left = e
return
}
if op == nil {
pop()
left = e
return
}
op.Left = left
op.Right = e
left = op
op = nil
}
TRACE("expr: parsing `%s'", src)
for i, arg := range split(src) {
switch {
case arg == ",":
DEBUG(" #%d: literal comma found; treating what we've seen so far as a complete expression", i)
pop()
case envvar.MatchString(arg):
DEBUG(" #%d: parsed as unquoted environment variable reference '%s'", i, arg)
push(&Expr{Type: EnvVar, Name: arg[1:]})
case qstring.MatchString(arg):
m := qstring.FindStringSubmatch(arg)
DEBUG(" #%d: parsed as quoted string literal '%s'", i, m[1])
push(&Expr{Type: Literal, Literal: m[1]})
case float.MatchString(arg):
DEBUG(" #%d: parsed as unquoted floating point literal '%s'", i, arg)
v, err := strconv.ParseFloat(arg, 64)
if err != nil {
DEBUG(" #%d: %s is not parsable as a floating point number: %s", i, arg, err)
return args, err
}
push(&Expr{Type: Literal, Literal: v})
case integer.MatchString(arg):
DEBUG(" #%d: parsed as unquoted integer literal '%s'", i, arg)
v, err := strconv.ParseInt(arg, 10, 64)
if err == nil {
push(&Expr{Type: Literal, Literal: v})
break
}
DEBUG(" #%d: %s is not parsable as an integer, falling back to parsing as float: %s", i, arg, err)
f, err := strconv.ParseFloat(arg, 64)
push(&Expr{Type: Literal, Literal: f})
if err != nil {
panic("Could not actually parse as an int or a float. Need to fix regexp?")
}
case arg == "||":
DEBUG(" #%d: parsed logical-or operator, '||'", i)
if left == nil || op != nil {
return args, fmt.Errorf(`syntax error near: %s`, src)
}
TRACE("expr: pushing || expr-op onto the stack")
op = &Expr{Type: LogicalOr}
case arg == "nil" || arg == "null" || arg == "~" || arg == "Nil" || arg == "Null" || arg == "NIL" || arg == "NULL":
DEBUG(" #%d: parsed the nil value token '%s'", i, arg)
push(&Expr{Type: Literal, Literal: nil})
case arg == "false" || arg == "False" || arg == "FALSE":
DEBUG(" #%d: parsed the false value token '%s'", i, arg)
push(&Expr{Type: Literal, Literal: false})
case arg == "true" || arg == "True" || arg == "TRUE":
DEBUG(" #%d: parsed the true value token '%s'", i, arg)
push(&Expr{Type: Literal, Literal: true})
default:
c, err := tree.ParseCursor(arg)
if err != nil {
DEBUG(" #%d: %s is a malformed reference: %s", i, arg, err)
return args, err
}
DEBUG(" #%d: parsed as a reference to $.%s", i, c)
push(&Expr{Type: Reference, Reference: c})
}
}
pop()
if left != nil || op != nil {
return nil, fmt.Errorf(`syntax error near: %s`, src)
}
DEBUG("")
for _, e := range final {
TRACE("expr: pushing expression `%v' onto the operand list", e)
reduced, err := e.Reduce()
if err != nil {
if warning, isWarning := err.(WarningError); isWarning {
warning.Warn()
} else {
fmt.Fprintf(os.Stdout, "warning: %s\n", err)
}
}
args = append(args, reduced)
}
return args, nil
}
op := &Opcall{src: src}
for _, pattern := range []string{
`^\Q((\E\s*([a-zA-Z][a-zA-Z0-9_-]*)(?:\s*\((.*)\))?\s*\Q))\E$`, // (( op(x,y,z) ))
`^\Q((\E\s*([a-zA-Z][a-zA-Z0-9_-]*)(?:\s+(.*))?\s*\Q))\E$`, // (( op x y z ))
} {
re := regexp.MustCompile(pattern)
if !re.MatchString(src) {
continue
}
m := re.FindStringSubmatch(src)
DEBUG("parsing `%s': looks like a (( %s ... )) operator\n arguments:", src, m[1])
op.op = OperatorFor(m[1])
if _, ok := op.op.(NullOperator); ok && len(m[2]) == 0 {
DEBUG("skipping `%s': not a real operator -- might be a BOSH variable?", src)
continue
}
if op.op.Phase() != phase {
DEBUG(" - skipping (( %s ... )) operation; it belongs to a different phase", m[1])
return nil, nil
}
args, err := argify(m[2])
if err != nil {
return nil, err
}
if len(args) == 0 {
DEBUG(" (none)")
}
op.args = args
return op, nil
}
return nil, nil
}
// Dependencies ...
func (op *Opcall) Dependencies(ev *Evaluator, locs []*tree.Cursor) []*tree.Cursor {
l := []*tree.Cursor{}
for _, arg := range op.args {
for _, c := range arg.Dependencies(ev, locs) {
l = append(l, c)
}
}
return op.op.Dependencies(ev, op.args, locs, l)
}
// Run ...
func (op *Opcall) Run(ev *Evaluator) (*Response, error) {
was := ev.Here
ev.Here = op.where
r, err := op.op.Run(ev, op.args)
ev.Here = was
if err != nil {
return nil, ansi.Errorf("@m{$.%s}: @R{%s}", op.where, err)
}
return r, nil
}