42351b1d15
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. |
||
---|---|---|
.. | ||
decode_test.go | ||
decode.go | ||
doc.go | ||
encode_test.go | ||
encode.go | ||
schema_test.go | ||
schema.go | ||
types.go |