Skip to content

Commit

Permalink
Fix comma placement when formatting multi-line string values inside l…
Browse files Browse the repository at this point in the history
…ists (#52)
  • Loading branch information
jrhouston authored Oct 21, 2022
1 parent cfc0208 commit 32389ba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
14 changes: 11 additions & 3 deletions contrib/hashicorp/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func FormatValue(v cty.Value, indent int, stripKeyQuotes bool) string {
return fmt.Sprintf("%#v", v)
}

// defaultDelimiter is "End Of Text" by convention
const defaultDelimiter = "EOT"

func formatMultilineString(v cty.Value, indent int) (string, bool) {
str := v.AsString()
lines := strings.Split(str, "\n")
Expand All @@ -96,8 +99,7 @@ func formatMultilineString(v cty.Value, indent int) (string, bool) {
operator = "<<-"
}

// Default delimiter is "End Of Text" by convention
delimiter := "EOT"
delimiter := defaultDelimiter

OUTER:
for {
Expand Down Expand Up @@ -175,7 +177,13 @@ func formatSequenceValue(v cty.Value, indent int, stripKeyQuotes bool) string {
_, v := it.Element()
buf.WriteByte('\n')
buf.WriteString(strings.Repeat(" ", indent))
buf.WriteString(FormatValue(v, indent, stripKeyQuotes))
formattedValue := FormatValue(v, indent, stripKeyQuotes)
buf.WriteString(formattedValue)
if strings.HasSuffix(formattedValue, defaultDelimiter) {
// write an additional newline if the value was a multiline string
buf.WriteByte('\n')
buf.WriteString(strings.Repeat(" ", indent))
}
buf.WriteByte(',')
}
indent -= 2
Expand Down
28 changes: 28 additions & 0 deletions contrib/hashicorp/terraform/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ EOT_`,
`[
"b",
"d",
]`,
},
{
cty.TupleVal([]cty.Value{
cty.StringVal("boop\nbeep"),
cty.StringVal("b"),
}),
`[
<<-EOT
boop
beep
EOT
,
"b",
]`,
},
{
cty.TupleVal([]cty.Value{
cty.StringVal("b"),
cty.StringVal("boop\nbeep"),
}),
`[
"b",
<<-EOT
boop
beep
EOT
,
]`,
},
{
Expand Down

0 comments on commit 32389ba

Please sign in to comment.