From b13ca4704697a2ffd81183ac3d08f6f6d83da4c3 Mon Sep 17 00:00:00 2001 From: yuangongji Date: Tue, 28 Jul 2020 17:36:06 +0800 Subject: [PATCH 1/2] fix issue238 --- file.go | 6 +++--- file_test.go | 9 +++++++++ ini.go | 4 ++++ ini_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ parser.go | 4 ++-- section.go | 4 ++-- 6 files changed, 72 insertions(+), 7 deletions(-) diff --git a/file.go b/file.go index f95606f..fe9fc4f 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 ef18bc0..43022bb 100644 --- a/file_test.go +++ b/file_test.go @@ -332,6 +332,15 @@ func TestFile_DeleteSection(t *testing.T) { f.DeleteSection("") So(f.SectionStrings(), ShouldResemble, []string{"author", "package"}) }) + 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] From 30bf1f112c1a7531584e183ae6aa59155ae3e209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Sun, 16 Aug 2020 19:03:48 +0800 Subject: [PATCH 2/2] Update file_test.go --- file_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/file_test.go b/file_test.go index 49a8e9f..ef44a52 100644 --- a/file_test.go +++ b/file_test.go @@ -351,6 +351,7 @@ key1 = value1 `) }) + Convey("Delete a section with InsensitiveSections", t, func() { f := ini.Empty(ini.LoadOptions{InsensitiveSections: true}) So(f, ShouldNotBeNil)