diff --git a/hclwrite/generate.go b/hclwrite/generate.go index 6f6a2e63..53b903c4 100644 --- a/hclwrite/generate.go +++ b/hclwrite/generate.go @@ -2,6 +2,7 @@ package hclwrite import ( "fmt" + "strings" "unicode" "unicode/utf8" @@ -212,23 +213,40 @@ func appendTokensForValue(val cty.Value, toks Tokens) Tokens { }) case val.Type() == cty.String: - // TODO: If it's a multi-line string ending in a newline, format - // it as a HEREDOC instead. - src := escapeQuotedStringLit(val.AsString()) - toks = append(toks, &Token{ - Type: hclsyntax.TokenOQuote, - Bytes: []byte{'"'}, - }) - if len(src) > 0 { + if strings.HasSuffix(val.AsString(), "\n") { + toks = append(toks, &Token{ + Type: hclsyntax.TokenOHeredoc, + Bytes: []byte("< 0 { + toks = append(toks, &Token{ + Type: hclsyntax.TokenQuotedLit, + Bytes: src, + }) + } + toks = append(toks, &Token{ + Type: hclsyntax.TokenCQuote, + Bytes: []byte{'"'}, }) } - toks = append(toks, &Token{ - Type: hclsyntax.TokenCQuote, - Bytes: []byte{'"'}, - }) case val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType(): toks = append(toks, &Token{ diff --git a/hclwrite/generate_test.go b/hclwrite/generate_test.go index 06fa0944..58cf5f9e 100644 --- a/hclwrite/generate_test.go +++ b/hclwrite/generate_test.go @@ -122,16 +122,20 @@ func TestTokensForValue(t *testing.T) { cty.StringVal("hello\nworld\n"), Tokens{ { - Type: hclsyntax.TokenOQuote, - Bytes: []byte(`"`), + Type: hclsyntax.TokenOHeredoc, + Bytes: []byte("<