mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
file: fix incorrect multiline comment handling when write out (#157)
This commit is contained in:
@@ -237,15 +237,20 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
||||
for i, sname := range f.sectionList {
|
||||
sec := f.Section(sname)
|
||||
if len(sec.Comment) > 0 {
|
||||
if sec.Comment[0] != '#' && sec.Comment[0] != ';' {
|
||||
sec.Comment = "; " + sec.Comment
|
||||
// Support multiline comments
|
||||
lines := strings.Split(sec.Comment, LineBreak)
|
||||
for i := range lines {
|
||||
if lines[i][0] != '#' && lines[i][0] != ';' {
|
||||
lines[i] = "; " + lines[i]
|
||||
} else {
|
||||
sec.Comment = sec.Comment[:1] + " " + strings.TrimSpace(sec.Comment[1:])
|
||||
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
||||
}
|
||||
if _, err := buf.WriteString(sec.Comment + LineBreak); err != nil {
|
||||
|
||||
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i > 0 || DefaultHeader {
|
||||
if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
|
||||
@@ -300,19 +305,21 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
||||
if len(indent) > 0 && sname != DEFAULT_SECTION {
|
||||
buf.WriteString(indent)
|
||||
}
|
||||
if key.Comment[0] != '#' && key.Comment[0] != ';' {
|
||||
key.Comment = "; " + key.Comment
|
||||
} else {
|
||||
key.Comment = key.Comment[:1] + " " + strings.TrimSpace(key.Comment[1:])
|
||||
}
|
||||
|
||||
// Support multiline comments
|
||||
key.Comment = strings.Replace(key.Comment, "\n", "\n; ", -1)
|
||||
lines := strings.Split(key.Comment, LineBreak)
|
||||
for i := range lines {
|
||||
if lines[i][0] != '#' && lines[i][0] != ';' {
|
||||
lines[i] = "; " + lines[i]
|
||||
} else {
|
||||
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
||||
}
|
||||
|
||||
if _, err := buf.WriteString(key.Comment + LineBreak); err != nil {
|
||||
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(indent) > 0 && sname != DEFAULT_SECTION {
|
||||
buf.WriteString(indent)
|
||||
|
||||
+16
-3
@@ -266,14 +266,27 @@ func TestFile_WriteTo(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Support multiline comments", t, func() {
|
||||
f := ini.Empty()
|
||||
f, err := ini.Load([]byte(`
|
||||
#
|
||||
# general.domain
|
||||
#
|
||||
# Domain name of XX system.
|
||||
domain = mydomain.com
|
||||
`))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
f.Section("").Key("test").Comment = "Multiline\nComment"
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err := f.WriteTo(&buf)
|
||||
_, err = f.WriteTo(&buf)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(buf.String(), ShouldEqual, `; Multiline
|
||||
So(buf.String(), ShouldEqual, `#
|
||||
# general.domain
|
||||
#
|
||||
# Domain name of XX system.
|
||||
domain = mydomain.com
|
||||
; Multiline
|
||||
; Comment
|
||||
test =
|
||||
|
||||
|
||||
Reference in New Issue
Block a user