Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HEREDOC generation in hclwrite #540

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions hclwrite/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hclwrite

import (
"fmt"
"strings"
"unicode"
"unicode/utf8"

Expand Down Expand Up @@ -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("<<EOH\n"),
})
toks = append(toks, &Token{
Type: hclsyntax.TokenQuotedLit,
Bytes: src,
Bytes: []byte(val.AsString()),
})
toks = append(toks, &Token{
Type: hclsyntax.TokenOHeredoc,
Bytes: []byte("EOH"),
})
toks = append(toks, &Token{
Type: hclsyntax.TokenNewline,
Bytes: []byte{'\n'},
})
} else {
src := escapeQuotedStringLit(val.AsString())
toks = append(toks, &Token{
Type: hclsyntax.TokenOQuote,
Bytes: []byte{'"'},
})
if len(src) > 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{
Expand Down
28 changes: 18 additions & 10 deletions hclwrite/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,41 @@ func TestTokensForValue(t *testing.T) {
cty.StringVal("hello\nworld\n"),
Tokens{
{
Type: hclsyntax.TokenOQuote,
Bytes: []byte(`"`),
Type: hclsyntax.TokenOHeredoc,
Bytes: []byte("<<EOH\n"),
},
{
Type: hclsyntax.TokenQuotedLit,
Bytes: []byte(`hello\nworld\n`),
Bytes: []byte("hello\nworld\n"),
},
{
Type: hclsyntax.TokenCQuote,
Bytes: []byte(`"`),
Type: hclsyntax.TokenOHeredoc,
Bytes: []byte("EOH"),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
},
},
{
cty.StringVal("hello\r\nworld\r\n"),
Tokens{
{
Type: hclsyntax.TokenOQuote,
Bytes: []byte(`"`),
Type: hclsyntax.TokenOHeredoc,
Bytes: []byte("<<EOH\n"),
},
{
Type: hclsyntax.TokenQuotedLit,
Bytes: []byte(`hello\r\nworld\r\n`),
Bytes: []byte("hello\r\nworld\r\n"),
},
{
Type: hclsyntax.TokenCQuote,
Bytes: []byte(`"`),
Type: hclsyntax.TokenOHeredoc,
Bytes: []byte("EOH"),
},
{
Type: hclsyntax.TokenNewline,
Bytes: []byte("\n"),
},
},
},
Expand Down