Skip to content

Commit 31bf7ce

Browse files
authored
file: fix whitespaces are filtered in the SaveTo method (#260)
1 parent 29f972a commit 31bf7ce

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

file.go

+2
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
454454
val = `"""` + val + `"""`
455455
} else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
456456
val = "`" + val + "`"
457+
} else if len(strings.TrimSpace(val)) != len(val) {
458+
val = `"` + val + `"`
457459
}
458460
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
459461
return nil, err

file_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,25 @@ test =
437437
`)
438438

439439
})
440+
441+
Convey("Keep leading and trailing spaces in value", t, func() {
442+
f, _ := ini.Load([]byte(`[foo]
443+
bar1 = ' val ue1 '
444+
bar2 = """ val ue2 """
445+
bar3 = " val ue3 "
446+
`))
447+
So(f, ShouldNotBeNil)
448+
449+
var buf bytes.Buffer
450+
_, err := f.WriteTo(&buf)
451+
So(err, ShouldBeNil)
452+
So(buf.String(),ShouldEqual,`[foo]
453+
bar1 = " val ue1 "
454+
bar2 = " val ue2 "
455+
bar3 = " val ue3 "
456+
457+
`)
458+
})
440459
}
441460

442461
func TestFile_SaveTo(t *testing.T) {

testdata/TestFile_WriteTo.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ true = 2+3=5
8484
ADDRESS = """404 road,
8585
NotFound, State, 50000"""
8686
two_lines = how about continuation lines?
87-
lots_of_lines = 1 2 3 4
87+
lots_of_lines = "1 2 3 4 "
8888

0 commit comments

Comments
 (0)