hcl/gohcl
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
..
decode.go gohcl: retain nested blocks while decoding 2019-10-10 09:15:24 -07:00
decode_test.go gohcl: retain nested blocks while decoding 2019-10-10 09:15:24 -07:00
doc.go gohcl: EncodeIntoBody and EncodeAsBlock functions 2018-11-04 01:16:51 +00:00
encode.go Change module path to github.com/hashicorp/hcl/v2 2019-09-09 15:46:40 -07:00
encode_test.go Change module path to github.com/hashicorp/hcl/v2 2019-09-09 15:46:40 -07:00
schema.go Unfold the "hcl" directory up into the root 2019-09-09 16:08:19 -07:00
schema_test.go Unfold the "hcl" directory up into the root 2019-09-09 16:08:19 -07:00
types.go Unfold the "hcl" directory up into the root 2019-09-09 16:08:19 -07:00