34e27c038a
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.
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package dynblock
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
type exprWrap struct {
|
|
hcl.Expression
|
|
i *iteration
|
|
}
|
|
|
|
func (e exprWrap) Variables() []hcl.Traversal {
|
|
raw := e.Expression.Variables()
|
|
ret := make([]hcl.Traversal, 0, len(raw))
|
|
|
|
// Filter out traversals that refer to our iterator name or any
|
|
// iterator we've inherited; we're going to provide those in
|
|
// our Value wrapper, so the caller doesn't need to know about them.
|
|
for _, traversal := range raw {
|
|
rootName := traversal.RootName()
|
|
if rootName == e.i.IteratorName {
|
|
continue
|
|
}
|
|
if _, inherited := e.i.Inherited[rootName]; inherited {
|
|
continue
|
|
}
|
|
ret = append(ret, traversal)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (e exprWrap) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
|
|
extCtx := e.i.EvalContext(ctx)
|
|
return e.Expression.Value(extCtx)
|
|
}
|
|
|
|
// 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
|
|
}
|