Skip to content

Commit

Permalink
Merge pull request #372 from hashicorp/format-objects-maps-on-multipl…
Browse files Browse the repository at this point in the history
…e-lines

hclwrite: Generate multi-line objects and maps
  • Loading branch information
alisdair committed May 6, 2020
2 parents 7098ede + 926e53e commit 60bf88b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
10 changes: 7 additions & 3 deletions hclwrite/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ func Example_generateFromScratch() {
// Output:
// string = "foo"
//
// object = { bar = 5, baz = true, foo = "foo" }
// bool = false
// path = env.PATH
// object = {
// bar = 5
// baz = true
// foo = "foo"
// }
// bool = false
// path = env.PATH
//
// foo {
// hello = "world"
Expand Down
16 changes: 10 additions & 6 deletions hclwrite/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ func appendTokensForValue(val cty.Value, toks Tokens) Tokens {
Type: hclsyntax.TokenOBrace,
Bytes: []byte{'{'},
})
if val.LengthInt() > 0 {
toks = append(toks, &Token{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
})
}

i := 0
for it := val.ElementIterator(); it.Next(); {
if i > 0 {
toks = append(toks, &Token{
Type: hclsyntax.TokenComma,
Bytes: []byte{','},
})
}
eKey, eVal := it.Element()
if hclsyntax.ValidIdentifier(eKey.AsString()) {
toks = append(toks, &Token{
Expand All @@ -142,6 +142,10 @@ func appendTokensForValue(val cty.Value, toks Tokens) Tokens {
Bytes: []byte{'='},
})
toks = appendTokensForValue(eVal, toks)
toks = append(toks, &Token{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
})
i++
}

Expand Down
51 changes: 36 additions & 15 deletions hclwrite/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,14 @@ func TestTokensForValue(t *testing.T) {
Type: hclsyntax.TokenOBrace,
Bytes: []byte(`{`),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(`foo`),
SpacesBefore: 1,
SpacesBefore: 2,
},
{
Type: hclsyntax.TokenEqual,
Expand All @@ -356,9 +360,12 @@ func TestTokensForValue(t *testing.T) {
SpacesBefore: 1,
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
SpacesBefore: 1,
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
},
},
},
Expand All @@ -372,10 +379,14 @@ func TestTokensForValue(t *testing.T) {
Type: hclsyntax.TokenOBrace,
Bytes: []byte(`{`),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(`bar`),
SpacesBefore: 1,
SpacesBefore: 2,
},
{
Type: hclsyntax.TokenEqual,
Expand All @@ -388,13 +399,13 @@ func TestTokensForValue(t *testing.T) {
SpacesBefore: 1,
},
{
Type: hclsyntax.TokenComma,
Bytes: []byte(`,`),
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(`foo`),
SpacesBefore: 1,
SpacesBefore: 2,
},
{
Type: hclsyntax.TokenEqual,
Expand All @@ -407,9 +418,12 @@ func TestTokensForValue(t *testing.T) {
SpacesBefore: 1,
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
SpacesBefore: 1,
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
},
},
},
Expand All @@ -422,10 +436,14 @@ func TestTokensForValue(t *testing.T) {
Type: hclsyntax.TokenOBrace,
Bytes: []byte(`{`),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenOQuote,
Bytes: []byte(`"`),
SpacesBefore: 1,
SpacesBefore: 2,
},
{
Type: hclsyntax.TokenQuotedLit,
Expand All @@ -446,9 +464,12 @@ func TestTokensForValue(t *testing.T) {
SpacesBefore: 1,
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
SpacesBefore: 1,
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
},
},
},
Expand Down

0 comments on commit 60bf88b

Please sign in to comment.