boolean keys do not get sanitised from the \n (#77)

* unit test to isolate the problem

* the error is not related to the comment, but simply that the key does not get sanitized...

* this fixes the \n issue, but highlights an error in the Writer

* correcting as well the WriteTo function

* unnecessary comment

* rebase and fix of the import
This commit is contained in:
Guido Serra
2017-01-22 06:40:42 -05:00
committed by 无闻
parent 766e555c68
commit 2ba7300c05
3 changed files with 15 additions and 2 deletions
+3
View File
@@ -472,6 +472,9 @@ func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
}
if key.isBooleanType {
if kname != sec.keyList[len(sec.keyList)-1] {
buf.WriteString(LineBreak)
}
continue
}
+10 -1
View File
@@ -17,6 +17,7 @@ package ini
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"time"
@@ -206,16 +207,24 @@ key2=c\d\`))
Convey("Load with boolean type keys", t, func() {
cfg, err := LoadSources(LoadOptions{AllowBooleanKeys: true}, []byte(`key1=hello
key2`))
key2
#key3
key4
key5`))
So(err, ShouldBeNil)
So(cfg, ShouldNotBeNil)
So(strings.Join(cfg.Section("").KeyStrings(), ","), ShouldEqual, "key1,key2,key4,key5")
So(cfg.Section("").Key("key2").MustBool(false), ShouldBeTrue)
var buf bytes.Buffer
cfg.WriteTo(&buf)
// there is always a trailing \n at the end of the section
So(buf.String(), ShouldEqual, `key1 = hello
key2
#key3
key4
key5
`)
})
}
+2 -1
View File
@@ -318,7 +318,8 @@ func (f *File) parse(reader io.Reader) (err error) {
if err != nil {
// Treat as boolean key when desired, and whole line is key name.
if IsErrDelimiterNotFound(err) && f.options.AllowBooleanKeys {
key, err := section.NewKey(string(line), "true")
kname, err := p.readValue(line, f.options.IgnoreContinuation)
key, err := section.NewKey(kname, "true")
if err != nil {
return err
}