diff options
| author | Martin Atkins <mart@degeneration.co.uk> | 2018-01-27 09:03:44 -0800 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2018-01-27 09:10:18 -0800 |
| commit | 34e27c038a4418ef2fb345bfe8fe907dab6620fa (patch) | |
| tree | 2578599de9e5679cb0a17168996882db101f309b /ext | |
| parent | 60b539d5d7ee37ca2cc69e1b42594c80217671cb (diff) | |
hcl: UnwrapExpression and UnwrapExpressionUntil
A pattern has emerged of wrapping Expression instances with other
Expressions in order to subtly modify their behavior. A key example of
this is in ext/dynblock, where wrap an expression in order to introduce
our additional iteration variable for expressions in dynamic blocks.
Rather than having each wrapper expression implement wrapping
implementations for our various syntax-level-analysis functions (like
ExprList and AbsTraversalForExpr), instead we define a standard mechanism
to unwrap expressions back to the lowest-level object -- usually an AST
node -- and then use this in all of our analyses that look at the
expression's structure rather than its value.
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/dynblock/expr_wrap.go | 26 |
1 files changed, 4 insertions, 22 deletions
diff --git a/ext/dynblock/expr_wrap.go b/ext/dynblock/expr_wrap.go index f29d5a6..6916fc1 100644 --- a/ext/dynblock/expr_wrap.go +++ b/ext/dynblock/expr_wrap.go @@ -35,26 +35,8 @@ func (e exprWrap) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { return e.Expression.Value(extCtx) } -// Passthrough implementation for hcl.ExprList -func (e exprWrap) ExprList() []hcl.Expression { - type exprList interface { - ExprList() []hcl.Expression - } - - if el, supported := e.Expression.(exprList); supported { - return el.ExprList() - } - return nil -} - -// Passthrough implementation for hcl.AbsTraversalForExpr and hcl.RelTraversalForExpr -func (e exprWrap) AsTraversal() hcl.Traversal { - type asTraversal interface { - AsTraversal() hcl.Traversal - } - - if at, supported := e.Expression.(asTraversal); supported { - return at.AsTraversal() - } - return nil +// UnwrapExpression returns the expression being wrapped by this instance. +// This allows the original expression to be recovered by hcl.UnwrapExpression. +func (e exprWrap) UnwrapExpression() hcl.Expression { + return e.Expression } |
