Skip to content

Commit

Permalink
chore: copy expressions while evaluating.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Oct 23, 2024
1 parent 63c22da commit fc11dc3
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 189 deletions.
13 changes: 1 addition & 12 deletions generate/genhcl/genhcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,7 @@ func loadGenHCLBlocks(root *config.Root, st *config.Stack, cfgdir project.Path)
func copyBody(dest *hclwrite.Body, src *hclsyntax.Body, eval hcl.Evaluator) error {
attrs := ast.SortRawAttributes(ast.AsHCLAttributes(src.Attributes))
for _, attr := range attrs {
// a generate_hcl.content block must be partially evaluated multiple
// times then the updates nodes should not be persisted.
expr := &ast.CloneExpression{
Expression: attr.Expr.(hclsyntax.Expression),
}

newexpr, _, err := eval.PartialEval(expr)
newexpr, _, err := eval.PartialEval(attr.Expr)
if err != nil {
return errors.E(err, attr.Expr.Range())
}
Expand Down Expand Up @@ -708,11 +702,6 @@ func getDynamicBlockAttrs(block *hclsyntax.Block) (dynBlockAttributes, error) {
switch name {
case "attributes":
dynAttrs.attributes = attr
if _, ok := attr.Expr.(*ast.CloneExpression); !ok {
dynAttrs.attributes.Expr = &ast.CloneExpression{
Expression: attr.Expr,
}
}

case "for_each":
dynAttrs.foreach = attr
Expand Down
11 changes: 0 additions & 11 deletions hcl/ast/expr_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ import (

"github.com/terramate-io/hcl/v2"
"github.com/terramate-io/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

// CloneExpression is an expression wrapper that
type CloneExpression struct {
hclsyntax.Expression
}

// Value evaluates the wrapped expression.
func (clone *CloneExpression) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
return clone.Expression.Value(ctx)
}

// CloneExpr clones the given expression.
func CloneExpr(expr hclsyntax.Expression) hclsyntax.Expression {
if expr == nil {
Expand Down
Loading

0 comments on commit fc11dc3

Please sign in to comment.