github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/hclext/expression.go (about)

     1  package hclext
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/zclconf/go-cty/cty"
     6  )
     7  
     8  // BoundExpr represents an expression whose a value is bound.
     9  // This is a wrapper for any expression, typically satisfying
    10  // an interface to behave like the wrapped expression.
    11  //
    12  // The difference is that when resolving a value with `Value()`,
    13  // instead of resolving the variables with EvalContext,
    14  // the bound value is returned directly.
    15  type BoundExpr struct {
    16  	Val cty.Value
    17  
    18  	original hcl.Expression
    19  }
    20  
    21  var _ hcl.Expression = (*BoundExpr)(nil)
    22  
    23  // BindValue binds the passed value to an expression.
    24  // This returns the bound expression.
    25  func BindValue(val cty.Value, expr hcl.Expression) hcl.Expression {
    26  	return &BoundExpr{original: expr, Val: val}
    27  }
    28  
    29  // Value returns the bound value.
    30  func (e BoundExpr) Value(*hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
    31  	return e.Val, nil
    32  }
    33  
    34  // Variables delegates to the wrapped expression.
    35  func (e BoundExpr) Variables() []hcl.Traversal {
    36  	return e.original.Variables()
    37  }
    38  
    39  // Range delegates to the wrapped expression.
    40  func (e BoundExpr) Range() hcl.Range {
    41  	return e.original.Range()
    42  }
    43  
    44  // StartRange delegates to the wrapped expression.
    45  func (e BoundExpr) StartRange() hcl.Range {
    46  	return e.original.StartRange()
    47  }
    48  
    49  // UnwrapExpression returns the original expression.
    50  // This satisfies the hcl.unwrapExpression interface.
    51  func (e BoundExpr) UnwrapExpression() hcl.Expression {
    52  	return e.original
    53  }