Commit Graph

17 Commits

Author SHA1 Message Date
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
Chris Marchesi
939ae58199
gohcl: document optional attribute
This documents the "optional" attribute, which allows you to specify an
attribute that is optional when parsing.
2020-02-13 18:51:40 -08: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
1b9738a196 gohcl: EncodeIntoBody and EncodeAsBlock functions
These are wrappers around the lower-level hclwrite package that are able
to reverse a subset of the behavior of the Decode functions to populate
an hclwrite DOM.

They are not fully symmetrical with DecodeBody because that function can
leave certain parts of the configuration in its opaque form for later
decoding, and these encode functions don't have enough information to
repack that abstract/opaque form into new source code.

In practice we expect that callers using complex techniques like partial
decoding will also use more complex techniques with the hclwrite API
directly, since they will need to coordinate partial _encoding_ of data
that has been portioned off into separate structures, which gohcl is not
equipped to do itself.
2018-11-04 01:16:51 +00:00
Martin Atkins
75cceef4f0 gohcl: don't reflect.DeepEqual number values in tests
big.Float is not DeepEqual-friendly because it contains a precision value
that can make two numerically-equal values appear as non-equal.

Since the number decoding isn't the point of these tests, instead we just
swap out for cty.Bool values which _are_ compatible with
reflect.DeepEqual, since they are just wrappers around the native bool
type.
2018-02-27 07:53:20 -08:00
Nicholas Jackson
23fc060132 gohcl: allow optional attributes to be specified via struct tag
Previously we required optional attributes to be specified as pointers so that we could represent the empty vs. absent distinction.

For applications that don't need to make that distinction, representing "optional" as a struct tag is more convenient.
2018-02-17 10:36:04 -08:00
Martin Atkins
1f3c1e1b14 gohcl: update remaining reference to "zcl" 2018-01-27 11:03:05 -08: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
bdf1e7c6e6 gohcl: rename struct tag prefix from "zcl:" to "hcl:" 2017-09-11 17:29:56 -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