Commit Graph

10 Commits

Author SHA1 Message Date
RouxAntoine b54debed5c
feat: Support composite struct with nested keyword 2021-03-08 00:56:12 +01:00
Alisdair McDiarmid 7e77010ee3
Merge pull request #335 from kinvolk/fix-slice-value-decoding
gohcl/decode.go: fix decoding value slices
2020-11-19 11:35:55 -05:00
Mitchell Hashimoto 38280c6c69
gohcl: add "body" struct tag to capture entire block body
Structs can specify the "body" struct tag to capture the entire block
body. This does NOT act as "remain" where leftover fields are
non-erroneous. If you want leftover fields to be allowed, a "remain"
field must ALSO be present.

This is used to capture the full block body that was decoded for the
block. This is useful if you want to ever redecode something differently
(maybe with a different EvalContext) or partially decode something but
redecode the entire value.
2020-10-22 16:56:33 -07:00
Mateusz Gozdek e3e49cc099
gohcl/decode.go: fix decoding value slices
Currently, if user tries to decode such scenario, it results in
"slice index out of range".

Fixes regression introduced in 42351b1d15.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
2020-01-15 13:45:37 +01:00
Mateusz Gozdek 42351b1d15 gohcl: retain nested blocks while decoding
Currently, if nonzero struct is passed to the DecodeBody function,
decoding process will keep already initialized top-level fields values or
overwrite them, if they are specified in HCL. This behaviour is useful,
as it allows to have some default values for top-level fields.

However, if the field is a type block or slice (multiple blocks), then the
entire block is overwritten, which erases the existing values. Because of
that, setting default values in nested structs is not possible.

With this commit, decode functions will check if the value is
nil and only then set them to empty struct, which allows for appending
to existing structs.

In case of a slice, either new empty element will be added, or existing
element will be used for setting new value (so values will be merged).

Also, to keep the same behavior as json.Unmarshal, if retained list
have more elements than new list, additional elements will be removed
and existing elements will be merged. This allows to have default values
also for positional elements.

Behavior added by this patch is the same as in json.Unmarshal and
yaml.Unmarshal, which both retain nested structs during unmarshaling
process, so I believe this is an expected behavior from user
perspective.
2019-10-10 09:15:24 -07: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 f44382c4fa gohcl: Decode missing hcl.Expression as a cty Null
Rather than writing a nil hcl.Expression, as a special case we'll deal
with this within the cty type system, which we assume is what the caller
wants if they are decoding into a hcl.Expression rather than a native Go
type.
2017-09-20 16:23:23 -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