mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
Add options to control sections insensitive or keys insensitive respectively (#253)
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
This commit is contained in:
@@ -82,7 +82,7 @@ func (f *File) NewSection(name string) (*Section, error) {
|
||||
return nil, errors.New("empty section name")
|
||||
}
|
||||
|
||||
if f.options.Insensitive && name != DefaultSection {
|
||||
if (f.options.Insensitive || f.options.InsensitiveSections) && name != DefaultSection {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func (f *File) SectionsByName(name string) ([]*Section, error) {
|
||||
if len(name) == 0 {
|
||||
name = DefaultSection
|
||||
}
|
||||
if f.options.Insensitive {
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ func (f *File) DeleteSectionWithIndex(name string, index int) error {
|
||||
if len(name) == 0 {
|
||||
name = DefaultSection
|
||||
}
|
||||
if f.options.Insensitive {
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
|
||||
@@ -351,6 +351,16 @@ key1 = value1
|
||||
|
||||
`)
|
||||
})
|
||||
|
||||
Convey("Delete a section with InsensitiveSections", t, func() {
|
||||
f := ini.Empty(ini.LoadOptions{InsensitiveSections: true})
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
_ = f.NewSections("author", "package", "features")
|
||||
f.DeleteSection("FEATURES")
|
||||
f.DeleteSection("")
|
||||
So(f.SectionStrings(), ShouldResemble, []string{"author", "package"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestFile_Append(t *testing.T) {
|
||||
|
||||
@@ -71,6 +71,10 @@ type LoadOptions struct {
|
||||
Loose bool
|
||||
// Insensitive indicates whether the parser forces all section and key names to lowercase.
|
||||
Insensitive bool
|
||||
// InsensitiveSections indicates whether the parser forces all section to lowercase.
|
||||
InsensitiveSections bool
|
||||
// InsensitiveKeys indicates whether the parser forces all key names to lowercase.
|
||||
InsensitiveKeys bool
|
||||
// IgnoreContinuation indicates whether to ignore continuation lines while parsing.
|
||||
IgnoreContinuation bool
|
||||
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
|
||||
|
||||
+52
@@ -317,6 +317,58 @@ e-mail = u@gogs.io
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Insensitive to sections and sensitive to key names", func() {
|
||||
f, err := ini.LoadSources(ini.LoadOptions{InsensitiveSections: true}, minimalConf)
|
||||
So(err, ShouldBeNil)
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
So(f.Section("Author").Key("E-MAIL").String(), ShouldEqual, "u@gogs.io")
|
||||
|
||||
Convey("Write out", func() {
|
||||
var buf bytes.Buffer
|
||||
_, err := f.WriteTo(&buf)
|
||||
So(err, ShouldBeNil)
|
||||
So(buf.String(), ShouldEqual, `[author]
|
||||
E-MAIL = u@gogs.io
|
||||
|
||||
`)
|
||||
})
|
||||
|
||||
Convey("Inverse case", func() {
|
||||
f, err := ini.LoadSources(ini.LoadOptions{}, minimalConf)
|
||||
So(err, ShouldBeNil)
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Sensitive to sections and insensitive to key names", func() {
|
||||
f, err := ini.LoadSources(ini.LoadOptions{InsensitiveKeys: true}, minimalConf)
|
||||
So(err, ShouldBeNil)
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
So(f.Section("author").Key("e-mail").String(), ShouldEqual, "u@gogs.io")
|
||||
|
||||
Convey("Write out", func() {
|
||||
var buf bytes.Buffer
|
||||
_, err := f.WriteTo(&buf)
|
||||
So(err, ShouldBeNil)
|
||||
So(buf.String(), ShouldEqual, `[author]
|
||||
e-mail = u@gogs.io
|
||||
|
||||
`)
|
||||
})
|
||||
|
||||
Convey("Inverse case", func() {
|
||||
f, err := ini.LoadSources(ini.LoadOptions{}, minimalConf)
|
||||
So(err, ShouldBeNil)
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Ignore continuation lines", func() {
|
||||
f, err := ini.LoadSources(ini.LoadOptions{
|
||||
AllowPythonMultilineValues: true,
|
||||
|
||||
@@ -377,7 +377,7 @@ func (f *File) parse(reader io.Reader) (err error) {
|
||||
|
||||
// Ignore error because default section name is never empty string.
|
||||
name := DefaultSection
|
||||
if f.options.Insensitive {
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(DefaultSection)
|
||||
}
|
||||
section, _ := f.NewSection(name)
|
||||
@@ -469,7 +469,7 @@ func (f *File) parse(reader io.Reader) (err error) {
|
||||
inUnparseableSection = false
|
||||
for i := range f.options.UnparseableSections {
|
||||
if f.options.UnparseableSections[i] == name ||
|
||||
(f.options.Insensitive && strings.EqualFold(f.options.UnparseableSections[i], name)) {
|
||||
((f.options.Insensitive || f.options.InsensitiveSections) && strings.EqualFold(f.options.UnparseableSections[i], name)) {
|
||||
inUnparseableSection = true
|
||||
continue
|
||||
}
|
||||
|
||||
+2
-2
@@ -66,7 +66,7 @@ func (s *Section) SetBody(body string) {
|
||||
func (s *Section) NewKey(name, val string) (*Key, error) {
|
||||
if len(name) == 0 {
|
||||
return nil, errors.New("error creating new key: empty key name")
|
||||
} else if s.f.options.Insensitive {
|
||||
} else if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ func (s *Section) GetKey(name string) (*Key, error) {
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.RLock()
|
||||
}
|
||||
if s.f.options.Insensitive {
|
||||
if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
key := s.keys[name]
|
||||
|
||||
Reference in New Issue
Block a user