diff --git a/file.go b/file.go index 733ab28..c022584 100644 --- a/file.go +++ b/file.go @@ -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) } diff --git a/file_test.go b/file_test.go index e956014..ef44a52 100644 --- a/file_test.go +++ b/file_test.go @@ -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) { diff --git a/ini.go b/ini.go index 2961543..39057a2 100644 --- a/ini.go +++ b/ini.go @@ -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. diff --git a/ini_test.go b/ini_test.go index e95aa01..55a0a56 100644 --- a/ini_test.go +++ b/ini_test.go @@ -317,6 +317,58 @@ e-mail = [email protected] }) }) + 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, "[email protected]") + + Convey("Write out", func() { + var buf bytes.Buffer + _, err := f.WriteTo(&buf) + So(err, ShouldBeNil) + So(buf.String(), ShouldEqual, `[author] +E-MAIL = [email protected] + +`) + }) + + 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, "[email protected]") + + Convey("Write out", func() { + var buf bytes.Buffer + _, err := f.WriteTo(&buf) + So(err, ShouldBeNil) + So(buf.String(), ShouldEqual, `[author] +e-mail = [email protected] + +`) + }) + + 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, diff --git a/parser.go b/parser.go index ea6c08b..6514716 100644 --- a/parser.go +++ b/parser.go @@ -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 } diff --git a/section.go b/section.go index 6ba5ac2..e08774c 100644 --- a/section.go +++ b/section.go @@ -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]