Commit Graph

22 Commits

Author SHA1 Message Date
Chris Marchesi 9c4784b144
hcldec: add ValidateSpec
This adds ValidateSpec, a new decoder Spec that allows one to add
custom validations to work with values at decode-time.

The validation is run on the value after the wrapped spec is applied to
the expression in question. Diagnostics are expected to be returned,
with the author having flexibility over whether or not they want to
specify a range; if one is not supplied, the range of the wrapped
expression is used.
2020-06-04 14:25:45 -07:00
Martin Atkins d8ae04bc78 ext/customdecode: Custom expression decoding extension
Most of the time, the standard expression decoding built in to HCL is
sufficient. Sometimes though, it's useful to be able to customize the
decoding of certain arguments where the application intends to use them
in a very specific way, such as in static analysis.

This extension is an approximate analog of gohcl's support for decoding
into an hcl.Expression, allowing hcldec-based applications and
applications with custom functions to similarly capture and manipulate
the physical expressions used in arguments, rather than their values.

This includes one example use-case: the typeexpr extension now includes
a cty.Function called ConvertFunc that takes a type expression as its
second argument. A type expression is not evaluatable in the usual sense,
but thanks to cty capsule types we _can_ produce a cty.Value from one
and then make use of it inside the function implementation, without
exposing this custom type to the broader language:

    convert(["foo"], set(string))

This mechanism is intentionally restricted only to "argument-like"
locations where there is a specific type we are attempting to decode into.
For now, that's hcldec AttrSpec/BlockAttrsSpec -- analogous to gohcl
decoding into hcl.Expression -- and in arguments to functions.
2019-12-17 07:51:07 -08:00
Martin Atkins c22ad7a9a6 hcldec: Improved context for expression diagnostics
The two cases where we decode attribute values should include the
expression and EvalContext in any diagnostics they generate so that the
calling application can give hints about the types and values of variables
that are used within the expression.

This also includes some adjustments to the returned source ranges so that
both cases are consistent with one another and so that both indicate
the entire expression as the Subject and include the attribute name in
the Context. Including the whole expression in the range ensures that
when there's a problem inside a tuple or object constructor, for example,
we'll show the line containing the problem and not just the opening [
or { symbol.
2019-12-11 07:25:33 -08:00
Martin Atkins 6c4344623b Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
2019-09-09 16:08:19 -07:00
Martin Atkins 3327dee567 Change module path to github.com/hashicorp/hcl/v2
This is in preparation for the first v2 release from the main HCL
repository.
2019-09-09 15:46:40 -07:00
Martin Atkins ed8144cda1 hcldec: BlockTupleSpec and BlockObjectSpec
When nested attributes are of type cty.DynamicPseudoType, a block spec
that is backed by a cty collection is annoying to use because it requires
all of the blocks to have homogenous types for such attributes.

These new specs are similar to BlockListSpec and BlockMapSpec
respectively, but permit each nested block result to have its own distinct
type.

In return for this flexibility, we lose the ability to predict the exact
type of the result: these specs must just indicate their type as being
cty.DynamicPseudoType themselves, since we need to know how many blocks
there are and what types are inside them before we can know our final
result type.
2018-08-22 12:31:30 -07:00
Martin Atkins b82170e941 hcldec: Handle or forbid cty.DynamicPseudoType attributes in nested blocks
Our BlockList, BlockSet, and BlockMap specs all produce cty collection
values, which require all elements to have a homogeneous type. If the
nested spec contained an attribute of type cty.DynamicPseudoType, that
would create the risk of each element having a different type, which would
previously have caused decoding to panic.

Now we either handle this during decode (BlockList, BlockSet) or forbid
it outright (BlockMap) to prevent that crash. BlockMap could _potentially_
also handle this during decode, but that would require a more significant
reorganization of its implementation than I want to take on right now,
and decoding dynamically-typed values inside collections is an edge case
anyway.
2018-08-22 11:52:40 -07:00
Martin Atkins bb724af7fd hcldec: BlockAttrsSpec spec type
This is the hcldec interface to Body.JustAttributes, producing a map whose
keys are the child attribute names and whose values are the results of
evaluating those expressions.

We can't just expose a JustAttributes-style spec directly here because
it's not really compatible with how hcldec thinks about things, but we
can expose a spec that decodes a specific child block because that can
then compose properly with other specs at the same level without
interfering with their operation.

The primary use for this is to allow the use of the block syntax to define
a map:

    dynamic_stuff {
      foo = "bar"
    }

JustAttributes is normally used in static analysis situations such as
enumerating the contents of a block to decide what to include in the
final EvalContext. That's not really possible with the hcldec model
because both structural decoding and expression evaluation happen
together. Therefore the use of this is pretty limited: it's useful if you
want to be compatible with an existing format based on legacy HCL where a
map was conventionally defined using block syntax, relying on the fact
that HCL did not make a strong distinction between attribute and block
syntax.
2018-08-09 16:53:16 -07:00
Martin Atkins 36446359d2 hcldec: Variables must visit deeply-nested specifications
Previously this implementation was doing only one level of recursion in
its walk, which gave the appearance of working until the
transform/container-type specs (DefaultSpec, TransformSpec, ...) were
introduced, creating the possibility of "same body children" being more
than one level away from the initial spec.

It's still correct to only process the schema and content once, because
ImpliedSchema is already collecting all of the requirements from the
"same body children", and so our content object will include everything
that the nested specs should need to analyze needed variables.
2018-05-24 12:11:53 -07:00
Martin Atkins bbbd0ef30d hcldec: Fix DefaultSpec to allow attribute and block specs
Previously it was not implementing the two optional interfaces required
for this, and so decoding would fail for any AttrSpec or block spec nested
inside.

Now it passes through attribute requirements from both the primary and
default, and passes block requirements only from the primary, thus
allowing either fallback between two attributes, fallback from an
attribute to a constant, or fallback from a block to a constant. Other
permutations are also possible, but not very important.
2018-05-22 15:06:42 -07:00
Martin Atkins 1ba92ee170 cmd/hcldec: "transform" spec type
This new spec type allows evaluating an arbitrary expression on the
result of a nested spec, for situations where the a value must be
transformed in some way.
2018-02-04 09:59:20 -08:00
Martin Atkins 130b3c5105 hcldec: New function ChildBlockTypes
This function returns a map describing all of the child block types
declared inside a spec. This can be used for recursive decoding of bodies
using the low-level HCL API, though in most cases callers should just use
Decode which does recursive decoding of an entire nested structure in
a single call.
2018-01-27 09:10:18 -08:00
Martin Atkins 44bad6dbf5 hcldec: ImpliedType function
This function returns the type of value that should be returned when
decoding the given spec. As well as being generally useful to the caller
for book-keeping purposes, this also allows us to return correct type
information when we are returning null and empty values, where before we
were leaning a little too much on cty.DynamicPseudoType.
2017-10-03 16:27:34 -07:00
Martin Atkins 0d6247f4cf hcldec: BlockLabelSpec decoding
A BlockLabelSpec can be placed in the nested spec structure of one of the
block specs to require and obtain labels on that block.

This is a more generic methodology than BlockMapSpec since it allows the
result to be a list or set with the labels inside the values, rather than
forcing all the label tuples to be unique and losing the ordering by
collapsing into a map structure.
2017-10-03 15:59:20 -07:00
Martin Atkins a6dff4e9f9 hcldec: BlockSetSpec and BlockMapSpec decode implementations 2017-10-03 14:44:54 -07:00
Martin Atkins cbdf66e80a hcldec: register DefaultSpec with gob 2017-10-03 14:43:54 -07:00
Martin Atkins 2892561eed hcldec: Fix MinItems/MaxItems diagnostics messages
These were not previously consistent with the usual style.
2017-10-03 12:17:47 -07:00
Martin Atkins cc215b6afe hcldec: ImpliedSchema must visit deep specs
Previously, due to a bug, it was only visiting the first two levels of
specifications, missing anything referenced at lower levels.
2017-10-03 12:17:47 -07:00
Martin Atkins a4ee7188ad hcldec: New DefaultSpec specification
This is a wrapper that allows a default value to be applied if a primary
spec results in a null value.
2017-10-03 11:57:53 -07:00
Martin Atkins 46b20d40af Update doc comments and readmes for zcl -> HCL. 2017-09-11 16:56:31 -07:00
Martin Atkins 708abb8c97 Move the zcl package and its two parsing subpackages to "hcl" names
This is a super-invasive update since the "zcl" package in particular
is referenced all over.

There are probably still a few zcl references hanging around in comments,
etc but this takes care of most of it.
2017-09-11 16:40:37 -07:00
Martin Atkins 0dc3a6015c Rename the ancillary packages from "zcl" to "hcl".
The main "zcl" package requires a bit more care because of how many
callers it has and because of its two subpackages, so we'll take care
of that one separately.
2017-09-11 16:00:31 -07:00