mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
a16ca2ec53
Whereas package "configs" deals with the static structure of the configuration language, this new package "lang" deals with the dynamic aspects such as expression evaluation. So far this mainly consists of populating a hcl.EvalContext that contains the values necessary to evaluate a block or an expression. There is also special handling here for dynamic block generation using the HCL "dynblock" extension, which is exposed in the public interface (rather than hiding it as an implementation detail of EvalBlock) so that the caller can then extract proper source locations for any result values using the expanded body. This also includes the beginnings of a replacement for the function table handling that currently lives in the old "config" package, but most of the functions are not yet ported and so this will expand in subsequent commits.
63 lines
2.1 KiB
Go
63 lines
2.1 KiB
Go
package lang
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/hashicorp/hcl2/hcldec"
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/config/configschema"
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
)
|
|
|
|
// 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 References(traversals []hcl.Traversal) ([]*addrs.Reference, tfdiags.Diagnostics) {
|
|
if len(traversals) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
var diags tfdiags.Diagnostics
|
|
refs := make([]*addrs.Reference, 0, len(traversals))
|
|
|
|
for _, traversal := range traversals {
|
|
ref, refDiags := addrs.ParseRef(traversal)
|
|
diags = diags.Append(refDiags)
|
|
if ref == nil {
|
|
continue
|
|
}
|
|
refs = append(refs, ref)
|
|
}
|
|
|
|
return refs, diags
|
|
}
|
|
|
|
// 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 ReferencesInBlock(body hcl.Body, schema *configschema.Block) ([]*addrs.Reference, tfdiags.Diagnostics) {
|
|
spec := schema.DecoderSpec()
|
|
traversals := hcldec.Variables(body, spec)
|
|
return References(traversals)
|
|
}
|
|
|
|
// ReferencesInExpr is a helper wrapper around References that first searches
|
|
// the given expression for traversals, before converting those traversals
|
|
// to references.
|
|
func ReferencesInExpr(expr hcl.Expression) ([]*addrs.Reference, tfdiags.Diagnostics) {
|
|
traversals := expr.Variables()
|
|
return References(traversals)
|
|
}
|