hashicorp / hashicorp/hcl

How can I add newlines between keys in generated Object values?

Open
#481 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

scenario: generating hundreds of raw hcl files via a script, for consumption by terraform

minimal code:

```
import (
"io/ioutil"

"github.com/hashicorp/hcl2/hclwrite"
"github.com/zclconf/go-cty/cty"
)

func main() {
outputPath := "./output/example.hcl"

file := hclwrite.NewEmptyFile()
rootBody := file.Body()

topBlock := rootBody.AppendNewBlock("topBlock", nil)
nextBlock := topBlock.Body().AppendNewBlock("nextBlock", nil)

nextBlock.Body().SetAttributeValue("object", cty.ObjectVal(map[string]cty.Value{
"object_key_01": cty.NumberIntVal(1),
"object_key_02": cty.StringVal("object-key-value-02"),
"object_key_03": cty.StringVal("object-key-value-03"),
"object_key_04": cty.StringVal("object-key-value-04"),
"object_key_05": cty.StringVal("object-key-value-05"),
"object_key_06": cty.StringVal("object-key-value-06"),
"object_key_07": cty.StringVal("object-key-value-07"),
"object_key_08": cty.StringVal("object-key-value-08"),
"object_key_09": cty.StringVal("object-key-value-09"),
"object_key_10": cty.StringVal("object-key-value-10"),
}))

outBytes := hclwrite.Format(file.Bytes())

ioutil.WriteFile(outputPath, outBytes, 0644)
}
```

the following output is what I actually get, whether I use `hclwrite.Format` or not:

```
topBlock {
nextBlock {
object = { object_key_01 = 1, object_key_02 = "object-key-value-02", object_key_03 = "object-key-value-03", object_key_04 = "object-key-value-04", object_key_05 = "object-key-value-05", object_key_06 = "object-key-value-06", object_key_07 = "object-key-value-07", object_key_08 = "object-key-value-08", object_key_09 = "object-key-value-09", object_key_10 = "object-key-value-10" }
}
}
```

But I'm expecting (for readability purposes):

```
topBlock {
nextBlock {
object = {
object_key_01 = 1,
object_key_02 = "object-key-value-02",
object_key_03 = "object-key-value-03",
object_key_04 = "object-key-value-04",
object_key_05 = "object-key-value-05",
object_key_06 = "object-key-value-06",
object_key_07 = "object-key-value-07",
object_key_08 = "object-key-value-08",
object_key_09 = "object-key-value-09",
object_key_10 = "object-key-value-10"
}
}
}
```

[Docs and examples](https://github.com/hashicorp/hcl/blob/main/hclwrite/examples_test.go#L51) seem to suggest that my expected output should be possible, but I haven't been able to work out how -- can anyone give me some pointers?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.