From 34e27c038a4418ef2fb345bfe8fe907dab6620fa Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 27 Jan 2018 09:03:44 -0800 Subject: 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. --- ext/dynblock/expr_wrap.go | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) (limited to 'ext') 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 } -- cgit v1.2.3