github.com/hashicorp/hcl/v2@v2.20.0/ext/dynblock/expr_wrap.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package dynblock
     5  
     6  import (
     7  	"github.com/hashicorp/hcl/v2"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  type exprWrap struct {
    12  	hcl.Expression
    13  	i *iteration
    14  }
    15  
    16  func (e exprWrap) Variables() []hcl.Traversal {
    17  	raw := e.Expression.Variables()
    18  	ret := make([]hcl.Traversal, 0, len(raw))
    19  
    20  	// Filter out traversals that refer to our iterator name or any
    21  	// iterator we've inherited; we're going to provide those in
    22  	// our Value wrapper, so the caller doesn't need to know about them.
    23  	for _, traversal := range raw {
    24  		rootName := traversal.RootName()
    25  		if rootName == e.i.IteratorName {
    26  			continue
    27  		}
    28  		if _, inherited := e.i.Inherited[rootName]; inherited {
    29  			continue
    30  		}
    31  		ret = append(ret, traversal)
    32  	}
    33  	return ret
    34  }
    35  
    36  func (e exprWrap) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
    37  	extCtx := e.i.EvalContext(ctx)
    38  	return e.Expression.Value(extCtx)
    39  }
    40  
    41  // UnwrapExpression returns the expression being wrapped by this instance.
    42  // This allows the original expression to be recovered by hcl.UnwrapExpression.
    43  func (e exprWrap) UnwrapExpression() hcl.Expression {
    44  	return e.Expression
    45  }