From 727e1c16f7683a846864b176a95a8437b98465ab Mon Sep 17 00:00:00 2001 From: vparis Date: Thu, 26 Nov 2026 16:10:35 +0100 Subject: [PATCH 1/3] implement read multiline heredoc --- file_test.go | 20 +++++------ ini_test.go | 62 ++++++++++++++++++++++++++++++++++ lexer.go | 30 +++++++++++++--- options.go | 7 ++++ testdata/multiline_heredoc.ini | 21 ++++++++++++ 5 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 testdata/multiline_heredoc.ini 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 + } 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 +359,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 { diff --git a/options.go b/options.go index 643986f..38ee4aa 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,12 @@ 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. +func AllowHeredocMultilineValues(v bool) LoadOption { + return func(o *loadOptions) { o.AllowHeredocMultilineValues = v } +} + // SpaceBeforeInlineComment starts an inline comment only at a comment symbol // that follows a space, so values can contain comment symbols. func SpaceBeforeInlineComment(v bool) LoadOption { diff --git a/testdata/multiline_heredoc.ini b/testdata/multiline_heredoc.ini new file mode 100644 index 0000000..d6be6f8 --- /dev/null +++ b/testdata/multiline_heredoc.ini @@ -0,0 +1,21 @@ +value1 = < Date: Thu, 26 Nov 2026 16:55:26 +0100 Subject: [PATCH 2/3] implement write multiline heredoc --- file.go | 4 ++++ ini_test.go | 11 ++++------- key.go | 3 +++ lexer.go | 6 ++++++ testdata/multiline_heredoc.ini | 1 + 5 files changed, 18 insertions(+), 7 deletions(-) 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/ini_test.go b/ini_test.go index c6b6a07..5f333cd 100644 --- a/ini_test.go +++ b/ini_test.go @@ -1599,31 +1599,28 @@ func TestHeredocMultiline(t *testing.T) { assert.Equal(t, "regular value", testData.Value3) assert.Equal(t, "line 1\n\n", testData.Value4) // multiline heredoc should store text as is, no transformation - assert.Equal(t, "\t%(value3)\n\\\n\t'\"\\\"\\'\\\\\\\n", testData.Value5) + assert.Equal(t, "\t%(value3)\n\\\n\t'\"\\\"\\'\\\\\\\n EOF\n", testData.Value5) t.Run("write out", func(t *testing.T) { var buf bytes.Buffer _, err := f.WriteTo(&buf) require.NoError(t, err) assert.Equal(t, `value1 = < -1 { if q == `"` { @@ -540,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/testdata/multiline_heredoc.ini b/testdata/multiline_heredoc.ini index d6be6f8..692379a 100644 --- a/testdata/multiline_heredoc.ini +++ b/testdata/multiline_heredoc.ini @@ -18,4 +18,5 @@ value5 =< Date: Thu, 26 Nov 2026 17:29:17 +0100 Subject: [PATCH 3/3] add test + doc for new option --- ini_test.go | 6 ++++++ options.go | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ini_test.go b/ini_test.go index 5f333cd..b8c80dc 100644 --- a/ini_test.go +++ b/ini_test.go @@ -1575,6 +1575,7 @@ type testHeredocData struct { Value3 string `ini:"value3"` Value4 string `ini:"value4"` Value5 string `ini:"value5"` + Value6 string `ini:"value6"` } func TestHeredocMultiline(t *testing.T) { @@ -1598,6 +1599,7 @@ func TestHeredocMultiline(t *testing.T) { assert.Equal(t, "line 1\n\tline 2\nline 3\n", testData.Value2) assert.Equal(t, "regular value", testData.Value3) assert.Equal(t, "line 1\n\n", testData.Value4) + assert.Equal(t, "line\n'\n", testData.Value6) // multiline heredoc should store text as is, no transformation assert.Equal(t, "\t%(value3)\n\\\n\t'\"\\\"\\'\\\\\\\n EOF\n", testData.Value5) t.Run("write out", func(t *testing.T) { @@ -1622,6 +1624,10 @@ value5 = <