-
-
Save cpuspellcaster/7a72b0f32c462dde7c72da8111fa5e4a to your computer and use it in GitHub Desktop.
hcl2 - hclwrite printing maps over multiple lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
"github.com/hashicorp/hcl/v2/hclsyntax" | |
"github.com/hashicorp/hcl/v2/hclwrite" | |
"github.com/zclconf/go-cty/cty" | |
"github.com/zclconf/go-cty/cty/gocty" | |
) | |
func main() { | |
tags := map[string]string{ | |
"foo": "bar", | |
"f.o/o": "bar", | |
} | |
tagsType, _ := gocty.ImpliedType(tags) | |
tagsVal, _ := gocty.ToCtyValue(tags, tagsType) | |
f := hclwrite.NewEmptyFile() | |
rootBody := f.Body() | |
// First attempt using SetAttributeValue on the entire map value | |
res1Block := rootBody.AppendNewBlock("resource", []string{"aws_elb", "foo1"}) | |
res1Body := res1Block.Body() | |
res1Body.SetAttributeValue("tags", tagsVal) | |
// Second attempt using a child block and SetAttributeValue per key-value pair | |
res2Block := rootBody.AppendNewBlock("resource", []string{"aws_elb", "foo2"}) | |
res2Body := res2Block.Body() | |
tagBlock := res2Body.AppendNewBlock("tags", nil) | |
tagBody := tagBlock.Body() | |
tagsVal.ForEachElement(func(key cty.Value, value cty.Value) bool { | |
tagBody.SetAttributeValue(key.AsString(), value) | |
return false | |
}) | |
// Third attempt using SetAttributeRaw | |
res3Block := rootBody.AppendNewBlock("resource", []string{"aws_elb", "foo3"}) | |
res3Body := res3Block.Body() | |
writeMap(res3Body, "tags", tagsVal.AsValueMap()) | |
bytes := hclwrite.Format(f.Bytes()) | |
fmt.Printf("%s\n", bytes) | |
} | |
func writeMap(body *hclwrite.Body, key string, values map[string]cty.Value) { | |
tokens := hclwrite.Tokens{ | |
{Type: hclsyntax.TokenOBrace, Bytes: []byte("{"), SpacesBefore: 1}, | |
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")}, | |
} | |
keys := make([]string, 0, len(values)) | |
for k := range values { | |
keys = append(keys, k) | |
} | |
sort.Strings(keys) | |
for _, k := range keys { | |
v := values[k] | |
tokens = append(tokens, []*hclwrite.Token{ | |
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}}, | |
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(k)}, | |
{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}}, | |
{Type: hclsyntax.TokenEqual, Bytes: []byte("=")}, | |
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}}, | |
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte(v.AsString())}, | |
{Type: hclsyntax.TokenCQuote, Bytes: []byte{'"'}}, | |
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")}, | |
}...) | |
} | |
tokens = append(tokens, | |
&hclwrite.Token{Type: hclsyntax.TokenCBrace, Bytes: []byte("}")}, | |
) | |
body.SetAttributeRaw(key, tokens) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment