Documentation ¶
Overview ¶
Package customdecode contains a HCL extension that allows, in certain contexts, expression evaluation to be overridden by custom static analysis.
This mechanism is only supported in certain specific contexts where expressions are decoded with a specific target type in mind. For more information, see the documentation on CustomExpressionDecoder.
Index ¶
Constants ¶
const CustomExpressionDecoder = customDecoderImpl(1)
CustomExpressionDecoder is a value intended to be used as a cty capsule type ExtensionData key for capsule types whose values are to be obtained by static analysis of an expression rather than normal evaluation of that expression.
When a cooperating capsule type is asked for ExtensionData with this key, it must return a non-nil CustomExpressionDecoderFunc value.
This mechanism is not universally supported; instead, it's handled in a few specific places where expressions are evaluated with the intent of producing a cty.Value of a type given by the calling application.
Specifically, this currently works for type constraints given in hcldec.AttrSpec and hcldec.BlockAttrsSpec, and it works for arguments to function calls in the HCL native syntax. HCL extensions implemented outside of the main HCL module may also implement this; consult their own documentation for details.
Variables ¶
var ExpressionClosureType cty.Type
ExpressionClosureType is a cty capsule type that carries hcl.Expression values along with their original evaluation contexts.
This is similar to ExpressionType except that during custom decoding it also captures the hcl.EvalContext that was provided, allowing callers to evaluate the expression later in the same context where it would originally have been evaluated, or a context derived from that one.
var ExpressionType cty.Type
ExpressionType is a cty capsule type that carries hcl.Expression values.
This type implements custom decoding in the most general way possible: it just captures whatever expression is given to it, with no further processing whatsoever. It could therefore be useful in situations where an application must defer processing of the expression content until a later step.
ExpressionType only captures the expression, not the evaluation context it was destined to be evaluated in. That means this type can be fine for situations where the recipient of the value only intends to do static analysis, but ExpressionClosureType is more appropriate in situations where the recipient will eventually evaluate the given expression.
Functions ¶
func ExpressionClosureVal ¶
func ExpressionClosureVal(closure *ExpressionClosure) cty.Value
ExpressionClosureVal returns a new cty value of type ExpressionClosureType, wrapping the given expression closure.
func ExpressionFromVal ¶
ExpressionFromVal returns the expression encapsulated in the given value, or panics if the value is not a known value of ExpressionType.
func ExpressionVal ¶
ExpressionVal returns a new cty value of type ExpressionType, wrapping the given expression.
Types ¶
type CustomExpressionDecoderFunc ¶
type CustomExpressionDecoderFunc func(expr hcl.Expression, ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
CustomExpressionDecoderFunc is the type of value that must be returned by a capsule type handling the key CustomExpressionDecoder in its ExtensionData implementation.
If no error diagnostics are returned, the result value MUST be of the capsule type that the decoder function was derived from. If the returned error diagnostics prevent producing a value at all, return cty.NilVal.
func CustomExpressionDecoderForType ¶
func CustomExpressionDecoderForType(ty cty.Type) CustomExpressionDecoderFunc
CustomExpressionDecoderForType takes any cty type and returns its custom expression decoder implementation if it has one. If it is not a capsule type or it does not implement a custom expression decoder, this function returns nil.
type ExpressionClosure ¶
type ExpressionClosure struct { Expression hcl.Expression EvalContext *hcl.EvalContext }
ExpressionClosure is the type encapsulated in ExpressionClosureType
func ExpressionClosureFromVal ¶
func ExpressionClosureFromVal(v cty.Value) *ExpressionClosure
ExpressionClosureFromVal returns the expression closure encapsulated in the given value, or panics if the value is not a known value of ExpressionClosureType.
The caller MUST NOT modify the returned closure or the EvalContext inside it. To derive a new EvalContext, either create a child context or make a copy.
func (*ExpressionClosure) Value ¶
func (c *ExpressionClosure) Value() (cty.Value, hcl.Diagnostics)
Value evaluates the closure's expression using the closure's EvalContext, returning the result.