diff --git a/file.go b/file.go index eb12f32..9983d6f 100644 --- a/file.go +++ b/file.go @@ -570,6 +570,10 @@ func (f *File) writeToBuffer(opts []WriteOption) (*bytes.Buffer, error) { if !f.options.AllowBooleanKeys { return line, nil } + case f.options.AllowHeredocMultilineValues && key.heredoc != "": + // keeps the original heredoc delimiter + delim := key.heredoc + line = form + eq + "<<" + delim + lineBreak + val + delim + lineBreak case strings.Contains(val, "\n") || (val != "" && val[0] == '`'): line = form + eq + `"""` + val + `"""` + lineBreak case !f.options.IgnoreInlineComment && strings.ContainsAny(val, symbols): diff --git a/file_test.go b/file_test.go index ab77940..5fc0ffb 100644 --- a/file_test.go +++ b/file_test.go @@ -709,7 +709,7 @@ func TestFile_WriteTo(t *testing.T) { golden := "testdata/TestFile_WriteTo.golden" if *update { - require.NoError(t, os.WriteFile(golden, buf.Bytes(), 0644)) + require.NoError(t, os.WriteFile(golden, buf.Bytes(), 0o644)) } expected, err := os.ReadFile(golden) @@ -742,7 +742,6 @@ domain = mydomain.com ; Comment test = `, buf.String()) - }) t.Run("keep leading and trailing spaces in value", func(t *testing.T) { @@ -788,7 +787,7 @@ Note = Hello world!`)) require.NotNil(t, f) var actual bytes.Buffer - var expected = []byte(`[Others] + expected := []byte(`[Others] Cities -> HangZhou|Boston Visits -> 1993-10-07T20:17:05Z, 1993-10-07T20:17:05Z Years -> 1993,1994 @@ -1114,13 +1113,14 @@ func TestWriteReadsBack(t *testing.T) { {"k =\n n\nk\n", []LoadOption{AllowNestedValues(true), AllowBooleanKeys(true)}}, {"k = \"\"\n n\n", []LoadOption{AllowNestedValues(true), AllowPythonMultilineValues(true)}}, {"NAME = x\n[DEFAULT]\nk1 = v1\n", []LoadOption{CaseInsensitive(true), AllowNonUniqueSections(true)}}, - {"[DEFAULT] ; c\n[s]\nk = v\n", nil}, // comment of an empty default section - {"# BEGIN\n[p]\nk = v\n# END\n", nil}, // comment after the last key - {"# only a comment\n", nil}, // comment of an empty file - {"[raw]\nbody\n# c\n", []LoadOption{RawSections("raw")}}, // comment after a raw body - {"[custom]\n", []LoadOption{RawSections("custom")}}, // empty raw section - {"k = v\n[DEFAULT]\n", []LoadOption{RawSections("DEFAULT")}}, // empty raw header of a section with keys - {"k = a\n\n b\nn = 1\n", []LoadOption{AllowPythonMultilineValues(true)}}, // empty line in a multiline value + {"[DEFAULT] ; c\n[s]\nk = v\n", nil}, // comment of an empty default section + {"# BEGIN\n[p]\nk = v\n# END\n", nil}, // comment after the last key + {"# only a comment\n", nil}, // comment of an empty file + {"[raw]\nbody\n# c\n", []LoadOption{RawSections("raw")}}, // comment after a raw body + {"[custom]\n", []LoadOption{RawSections("custom")}}, // empty raw section + {"k = v\n[DEFAULT]\n", []LoadOption{RawSections("DEFAULT")}}, // empty raw header of a section with keys + {"k = a\n\n b\nn = 1\n", []LoadOption{AllowPythonMultilineValues(true)}}, // empty line in a multiline value + {"NAME = < 3 && line[:2] == `<<`: + data := reg.FindStringSubmatch(line) + if len(data) == 0 || len(data[1]) < 3 { + // bad heredoc + return "" + } + return data[1] // '< -1 { - if q == `"` { - return strings.ReplaceAll(rest[:i], `\"`, `"`), nil + inHeredoc := false + if l.opts.AllowHeredocMultilineValues && strings.HasPrefix(q, "<<") { + q = q[2:] // strip << to get the heredoc token + rest = "" // skip, whole line is and heredoc delimiter + inHeredoc = true + l.heredoc = q + } else { + if i := strings.LastIndex(rest, q); i > -1 { + if q == `"` { + return strings.ReplaceAll(rest[:i], `\"`, `"`), nil + } + return rest[:i], nil } - return rest[:i], nil } - var b strings.Builder b.WriteString(rest) for { @@ -342,6 +364,9 @@ func (l *lexer) readQuoted(line, q string) (string, error) { case !l.opts.IgnoreContinuation && strings.HasSuffix(strings.TrimSpace(next[i+len(q):]), `\`): // A backslash after the closing quote continues the value. b.WriteString(next) + case inHeredoc && i != 0: + // heredoc must start the line, no space allowed before + b.WriteString(next) default: b.WriteString(next[:i]) if j := l.opts.commentSet.index(next[i:]); j > -1 { @@ -520,6 +545,7 @@ func (f *File) parse(src string) error { return tok.errorf(err) } key.isAutoIncrement = isAutoIncr + key.heredoc = tok.heredoc key.comment = tok.comment lastRegularKey = key } diff --git a/options.go b/options.go index 643986f..7cde8c8 100644 --- a/options.go +++ b/options.go @@ -26,6 +26,7 @@ type loadOptions struct { AllowShadows bool AllowNestedValues bool AllowPythonMultilineValues bool + AllowHeredocMultilineValues bool SpaceBeforeInlineComment bool UnescapeValueDoubleQuotes bool UnescapeValueCommentSymbols bool @@ -133,6 +134,13 @@ func AllowPythonMultilineValues(v bool) LoadOption { return func(o *loadOptions) { o.AllowPythonMultilineValues = v } } +// AllowHeredocMultilineValues continues a value on the following lines that +// start with a heredoc string (like <