github.com/terraform-linters/tflint@v0.51.2-0.20240520175844-3750771571b6/terraform/tfhcl/expr_wrap.go (about)

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