github.com/hashicorp/hcl/v2@v2.20.0/static_expr.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hcl
     5  
     6  import (
     7  	"github.com/zclconf/go-cty/cty"
     8  )
     9  
    10  type staticExpr struct {
    11  	val cty.Value
    12  	rng Range
    13  }
    14  
    15  // StaticExpr returns an Expression that always evaluates to the given value.
    16  //
    17  // This is useful to substitute default values for expressions that are
    18  // not explicitly given in configuration and thus would otherwise have no
    19  // Expression to return.
    20  //
    21  // Since expressions are expected to have a source range, the caller must
    22  // provide one. Ideally this should be a real source range, but it can
    23  // be a synthetic one (with an empty-string filename) if no suitable range
    24  // is available.
    25  func StaticExpr(val cty.Value, rng Range) Expression {
    26  	return staticExpr{val, rng}
    27  }
    28  
    29  func (e staticExpr) Value(ctx *EvalContext) (cty.Value, Diagnostics) {
    30  	return e.val, nil
    31  }
    32  
    33  func (e staticExpr) Variables() []Traversal {
    34  	return nil
    35  }
    36  
    37  func (e staticExpr) Range() Range {
    38  	return e.rng
    39  }
    40  
    41  func (e staticExpr) StartRange() Range {
    42  	return e.rng
    43  }