2017-09-11 23:00:31 +00:00
|
|
|
package hcldec
|
2017-06-04 00:34:32 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-06-04 00:34:32 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Decode interprets the given body using the given specification and returns
|
|
|
|
// the resulting value. If the given body is not valid per the spec, error
|
|
|
|
// diagnostics are returned and the returned value is likely to be incomplete.
|
|
|
|
//
|
|
|
|
// The ctx argument may be nil, in which case any references to variables or
|
|
|
|
// functions will produce error diagnostics.
|
2017-09-11 23:40:37 +00:00
|
|
|
func Decode(body hcl.Body, spec Spec, ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
|
2017-06-04 00:34:32 +00:00
|
|
|
val, _, diags := decode(body, nil, ctx, spec, false)
|
|
|
|
return val, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartialDecode is like Decode except that it permits "leftover" items in
|
|
|
|
// the top-level body, which are returned as a new body to allow for
|
|
|
|
// further processing.
|
|
|
|
//
|
|
|
|
// Any descendent block bodies are _not_ decoded partially and thus must
|
|
|
|
// be fully described by the given specification.
|
2017-09-11 23:40:37 +00:00
|
|
|
func PartialDecode(body hcl.Body, spec Spec, ctx *hcl.EvalContext) (cty.Value, hcl.Body, hcl.Diagnostics) {
|
2017-06-04 00:34:32 +00:00
|
|
|
return decode(body, nil, ctx, spec, true)
|
|
|
|
}
|
2017-06-04 15:13:36 +00:00
|
|
|
|
2017-10-03 23:27:34 +00:00
|
|
|
// ImpliedType returns the value type that should result from decoding the
|
|
|
|
// given spec.
|
|
|
|
func ImpliedType(spec Spec) cty.Type {
|
|
|
|
return impliedType(spec)
|
|
|
|
}
|
|
|
|
|
2017-06-04 15:13:36 +00:00
|
|
|
// SourceRange interprets the given body using the given specification and
|
|
|
|
// then returns the source range of the value that would be used to
|
|
|
|
// fulfill the spec.
|
|
|
|
//
|
|
|
|
// This can be used if application-level validation detects value errors, to
|
|
|
|
// obtain a reasonable SourceRange to use for generated diagnostics. It works
|
|
|
|
// best when applied to specific body items (e.g. using AttrSpec, BlockSpec, ...)
|
|
|
|
// as opposed to entire bodies using ObjectSpec, TupleSpec. The result will
|
|
|
|
// be less useful the broader the specification, so e.g. a spec that returns
|
|
|
|
// the entirety of all of the blocks of a given type is likely to be
|
|
|
|
// _particularly_ arbitrary and useless.
|
|
|
|
//
|
|
|
|
// If the given body is not valid per the given spec, the result is best-effort
|
|
|
|
// and may not actually be something ideal. It's expected that an application
|
|
|
|
// will already have used Decode or PartialDecode earlier and thus had an
|
|
|
|
// opportunity to detect and report spec violations.
|
2017-09-11 23:40:37 +00:00
|
|
|
func SourceRange(body hcl.Body, spec Spec) hcl.Range {
|
2017-06-04 15:13:36 +00:00
|
|
|
return sourceRange(body, nil, spec)
|
|
|
|
}
|
2018-01-22 02:08:43 +00:00
|
|
|
|
|
|
|
// ChildBlockTypes returns a map of all of the child block types declared
|
|
|
|
// by the given spec, with block type names as keys and the associated
|
|
|
|
// nested body specs as values.
|
|
|
|
func ChildBlockTypes(spec Spec) map[string]Spec {
|
|
|
|
ret := map[string]Spec{}
|
|
|
|
|
|
|
|
// visitSameBodyChildren walks through the spec structure, calling
|
|
|
|
// the given callback for each descendent spec encountered. We are
|
|
|
|
// interested in the specs that reference attributes and blocks.
|
|
|
|
var visit visitFunc
|
|
|
|
visit = func(s Spec) {
|
|
|
|
if bs, ok := s.(blockSpec); ok {
|
|
|
|
for _, blockS := range bs.blockHeaderSchemata() {
|
2018-05-22 22:06:42 +00:00
|
|
|
nested := bs.nestedSpec()
|
|
|
|
if nested != nil { // nil can be returned to dynamically opt out of this interface
|
|
|
|
ret[blockS.Type] = nested
|
|
|
|
}
|
2018-01-22 02:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.visitSameBodyChildren(visit)
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(spec)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|