hcl icon indicating copy to clipboard operation
hcl copied to clipboard

Add support for squash in hcl tags

Open jocgir opened this issue 4 years ago • 1 comments

In hcl v1, it was possible to specify squash on embedded structures as it is with json (done by default) or yaml (with inline) or mitchellh/mapstructure (with squash). That functionality disappeared with v2.

The alternatives we found relied on the remain tag which is not really intended to handle that situation. Moreover, there can be only one remain tag on a structure.

The implementation support that label could be specified anywhere in the hierarchy and the field order is maintained.

With the current implementation, it is possible to specify the same name tag to many fields. The result is that the latest override the previous one silently. At the end, the desired field is not set. We changed the code to support having multiple fields with the same tag. The new result is that all fields will get populated with the desired associated element.

Instead of handling just an index position within the structure, we changed the index to be a field path. That allows us to retrieve the right information when the struct contains squashed embedded structs.

Finally, we added standard marshaling functions Marshal and Unmarshal into hclsimple package. This allows to interact with the library in a same way we do with json or yaml.

This is an alternative to #337 and also solve the issue #348.

jocgir avatar Feb 13 '21 15:02 jocgir

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Sep 09 '21 09:09 hashicorp-cla