HCL -> json equivalency example in readme is wrong?
The readme says:
variable "ami" { description = "the AMI to use" }This would be equivalent to the following json:
{ "variable": { "ami": { "description": "the AMI to use" } } }
However, based my usage of a parser (which admittedly might be out of date, though I built it with the latest version of this library) and the described functionality bellow:
... As an alternative, lists of objects can be created with repeated blocks, using this structure:
service { key = "value" } service { key = "value" }
The actual equivalency appears to be:
variable "ami" {
description = "the AMI to use"
}
{
"variable": [
{
"ami": [
{
"description": "the AMI to use"
}
]
}
]
}
Are my assumptions correct? Is my parser wrong? At the very least the readme is unclear about how the lists of objects can be created with repeated blocks deals with only having one declaration.
This is the workaround I currently use, because I need the extra [] to be gone:
flattenDataStructure(&objs)
data, e = json.Marshal(objs)
// Workaround for: https://github.com/hashicorp/hcl/issues/237, https://github.com/kvz/json2hcl/issues/3
func flattenDataStructure(data *map[string]interface{}) {
for k, v := range *data {
vListOfMap, ok := v.([]map[string]interface{})
if ok {
if len(vListOfMap) == 1 {
(*data)[k] = vListOfMap[0]
}
}
}
for _, v := range *data {
vMap, ok := v.(map[string]interface{})
if ok {
flattenDataStructure(&vMap)
}
}
}
It's not nice but at least for me it gets the job done.