terraform: github.com/hashicorp/terraform/lang Index | Files | Directories

package lang

import "github.com/hashicorp/terraform/lang"

Package lang deals with the runtime aspects of Terraform's configuration language, with concerns such as expression evaluation. It is closely related to sibling package "configs", which is responsible for configuration parsing and static validation.

Index

Package Files

data.go doc.go eval.go functions.go references.go scope.go

func References Uses

func References(traversals []hcl.Traversal) ([]*addrs.Reference, tfdiags.Diagnostics)

References finds all of the references in the given set of traversals, returning diagnostics if any of the traversals cannot be interpreted as a reference.

This function does not do any de-duplication of references, since references have source location information embedded in them and so any invalid references that are duplicated should have errors reported for each occurence.

If the returned diagnostics contains errors then the result may be incomplete or invalid. Otherwise, the returned slice has one reference per given traversal, though it is not guaranteed that the references will appear in the same order as the given traversals.

func ReferencesInBlock Uses

func ReferencesInBlock(body hcl.Body, schema *configschema.Block) ([]*addrs.Reference, tfdiags.Diagnostics)

ReferencesInBlock is a helper wrapper around References that first searches the given body for traversals, before converting those traversals to references.

A block schema must be provided so that this function can determine where in the body variables are expected.

func ReferencesInExpr Uses

func ReferencesInExpr(expr hcl.Expression) ([]*addrs.Reference, tfdiags.Diagnostics)

ReferencesInExpr is a helper wrapper around References that first searches the given expression for traversals, before converting those traversals to references.

type Data Uses

type Data interface {
    StaticValidateReferences(refs []*addrs.Reference, self addrs.Referenceable) tfdiags.Diagnostics

    GetCountAttr(addrs.CountAttr, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetForEachAttr(addrs.ForEachAttr, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetResource(addrs.Resource, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetLocalValue(addrs.LocalValue, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetModuleInstance(addrs.ModuleCallInstance, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetModuleInstanceOutput(addrs.ModuleCallOutput, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetPathAttr(addrs.PathAttr, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetTerraformAttr(addrs.TerraformAttr, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
    GetInputVariable(addrs.InputVariable, tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics)
}

Data is an interface whose implementations can provide cty.Value representations of objects identified by referenceable addresses from the addrs package.

This interface will grow each time a new type of reference is added, and so implementations outside of the Terraform codebases are not advised.

Each method returns a suitable value and optionally some diagnostics. If the returned diagnostics contains errors then the type of the returned value is used to construct an unknown value of the same type which is then used in place of the requested object so that type checking can still proceed. In cases where it's not possible to even determine a suitable result type, cty.DynamicVal is returned along with errors describing the problem.

type Scope Uses

type Scope struct {
    // Data is used to resolve references in expressions.
    Data Data

    // SelfAddr is the address that the "self" object should be an alias of,
    // or nil if the "self" object should not be available at all.
    SelfAddr addrs.Referenceable

    // BaseDir is the base directory used by any interpolation functions that
    // accept filesystem paths as arguments.
    BaseDir string

    // PureOnly can be set to true to request that any non-pure functions
    // produce unknown value results rather than actually executing. This is
    // important during a plan phase to avoid generating results that could
    // then differ during apply.
    PureOnly bool
    // contains filtered or unexported fields
}

Scope is the main type in this package, allowing dynamic evaluation of blocks and expressions based on some contextual information that informs which variables and functions will be available.

func (*Scope) EvalBlock Uses

func (s *Scope) EvalBlock(body hcl.Body, schema *configschema.Block) (cty.Value, tfdiags.Diagnostics)

EvalBlock evaluates the given body using the given block schema and returns a cty object value representing its contents. The type of the result conforms to the implied type of the given schema.

This function does not automatically expand "dynamic" blocks within the body. If that is desired, first call the ExpandBlock method to obtain an expanded body to pass to this method.

If the returned diagnostics contains errors then the result may be incomplete or invalid.

func (*Scope) EvalContext Uses

func (s *Scope) EvalContext(refs []*addrs.Reference) (*hcl.EvalContext, tfdiags.Diagnostics)

EvalContext constructs a HCL expression evaluation context whose variable scope contains sufficient values to satisfy the given set of references.

Most callers should prefer to use the evaluation helper methods that this type offers, but this is here for less common situations where the caller will handle the evaluation calls itself.

func (*Scope) EvalExpr Uses

func (s *Scope) EvalExpr(expr hcl.Expression, wantType cty.Type) (cty.Value, tfdiags.Diagnostics)

EvalExpr evaluates a single expression in the receiving context and returns the resulting value. The value will be converted to the given type before it is returned if possible, or else an error diagnostic will be produced describing the conversion error.

Pass an expected type of cty.DynamicPseudoType to skip automatic conversion and just obtain the returned value directly.

If the returned diagnostics contains errors then the result may be incomplete, but will always be of the requested type.

func (*Scope) EvalReference Uses

func (s *Scope) EvalReference(ref *addrs.Reference, wantType cty.Type) (cty.Value, tfdiags.Diagnostics)

EvalReference evaluates the given reference in the receiving scope and returns the resulting value. The value will be converted to the given type before it is returned if possible, or else an error diagnostic will be produced describing the conversion error.

Pass an expected type of cty.DynamicPseudoType to skip automatic conversion and just obtain the returned value directly.

If the returned diagnostics contains errors then the result may be incomplete, but will always be of the requested type.

func (*Scope) ExpandBlock Uses

func (s *Scope) ExpandBlock(body hcl.Body, schema *configschema.Block) (hcl.Body, tfdiags.Diagnostics)

ExpandBlock expands any "dynamic" blocks present in the given body. The result is a body with those blocks expanded, ready to be evaluated with EvalBlock.

If the returned diagnostics contains errors then the result may be incomplete or invalid.

func (*Scope) Functions Uses

func (s *Scope) Functions() map[string]function.Function

Functions returns the set of functions that should be used to when evaluating expressions in the receiving scope.

Directories

PathSynopsis
blocktoattrPackage blocktoattr includes some helper functions that can perform preprocessing on a HCL body where a configschema.Block schema is available in order to allow list and set attributes defined in the schema to be optionally written by the user as block syntax.
funcs

Package lang imports 17 packages (graph) and is imported by 18 packages. Updated 2019-10-10. Refresh now. Tools for package owners.