mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
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:
@@ -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
@@ -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
|
||||
`)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user