Skip to content

Commit f1d9ab1

Browse files
ygj6unknwon
andauthored
Add options to control sections insensitive or keys insensitive respectively (#253)
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]>
1 parent 1a3a8f7 commit f1d9ab1

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

file.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (f *File) NewSection(name string) (*Section, error) {
8282
return nil, errors.New("empty section name")
8383
}
8484

85-
if f.options.Insensitive && name != DefaultSection {
85+
if (f.options.Insensitive || f.options.InsensitiveSections) && name != DefaultSection {
8686
name = strings.ToLower(name)
8787
}
8888

@@ -144,7 +144,7 @@ func (f *File) SectionsByName(name string) ([]*Section, error) {
144144
if len(name) == 0 {
145145
name = DefaultSection
146146
}
147-
if f.options.Insensitive {
147+
if f.options.Insensitive || f.options.InsensitiveSections {
148148
name = strings.ToLower(name)
149149
}
150150

@@ -236,7 +236,7 @@ func (f *File) DeleteSectionWithIndex(name string, index int) error {
236236
if len(name) == 0 {
237237
name = DefaultSection
238238
}
239-
if f.options.Insensitive {
239+
if f.options.Insensitive || f.options.InsensitiveSections {
240240
name = strings.ToLower(name)
241241
}
242242

file_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ key1 = value1
351351
352352
`)
353353
})
354+
355+
Convey("Delete a section with InsensitiveSections", t, func() {
356+
f := ini.Empty(ini.LoadOptions{InsensitiveSections: true})
357+
So(f, ShouldNotBeNil)
358+
359+
_ = f.NewSections("author", "package", "features")
360+
f.DeleteSection("FEATURES")
361+
f.DeleteSection("")
362+
So(f.SectionStrings(), ShouldResemble, []string{"author", "package"})
363+
})
354364
}
355365

356366
func TestFile_Append(t *testing.T) {

ini.go

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ type LoadOptions struct {
7171
Loose bool
7272
// Insensitive indicates whether the parser forces all section and key names to lowercase.
7373
Insensitive bool
74+
// InsensitiveSections indicates whether the parser forces all section to lowercase.
75+
InsensitiveSections bool
76+
// InsensitiveKeys indicates whether the parser forces all key names to lowercase.
77+
InsensitiveKeys bool
7478
// IgnoreContinuation indicates whether to ignore continuation lines while parsing.
7579
IgnoreContinuation bool
7680
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.

ini_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,58 @@ e-mail = [email protected]
317317
})
318318
})
319319

320+
Convey("Insensitive to sections and sensitive to key names", func() {
321+
f, err := ini.LoadSources(ini.LoadOptions{InsensitiveSections: true}, minimalConf)
322+
So(err, ShouldBeNil)
323+
So(f, ShouldNotBeNil)
324+
325+
So(f.Section("Author").Key("E-MAIL").String(), ShouldEqual, "[email protected]")
326+
327+
Convey("Write out", func() {
328+
var buf bytes.Buffer
329+
_, err := f.WriteTo(&buf)
330+
So(err, ShouldBeNil)
331+
So(buf.String(), ShouldEqual, `[author]
332+
333+
334+
`)
335+
})
336+
337+
Convey("Inverse case", func() {
338+
f, err := ini.LoadSources(ini.LoadOptions{}, minimalConf)
339+
So(err, ShouldBeNil)
340+
So(f, ShouldNotBeNil)
341+
342+
So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
343+
})
344+
})
345+
346+
Convey("Sensitive to sections and insensitive to key names", func() {
347+
f, err := ini.LoadSources(ini.LoadOptions{InsensitiveKeys: true}, minimalConf)
348+
So(err, ShouldBeNil)
349+
So(f, ShouldNotBeNil)
350+
351+
So(f.Section("author").Key("e-mail").String(), ShouldEqual, "[email protected]")
352+
353+
Convey("Write out", func() {
354+
var buf bytes.Buffer
355+
_, err := f.WriteTo(&buf)
356+
So(err, ShouldBeNil)
357+
So(buf.String(), ShouldEqual, `[author]
358+
359+
360+
`)
361+
})
362+
363+
Convey("Inverse case", func() {
364+
f, err := ini.LoadSources(ini.LoadOptions{}, minimalConf)
365+
So(err, ShouldBeNil)
366+
So(f, ShouldNotBeNil)
367+
368+
So(f.Section("Author").Key("e-mail").String(), ShouldBeEmpty)
369+
})
370+
})
371+
320372
Convey("Ignore continuation lines", func() {
321373
f, err := ini.LoadSources(ini.LoadOptions{
322374
AllowPythonMultilineValues: true,

parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (f *File) parse(reader io.Reader) (err error) {
377377

378378
// Ignore error because default section name is never empty string.
379379
name := DefaultSection
380-
if f.options.Insensitive {
380+
if f.options.Insensitive || f.options.InsensitiveSections {
381381
name = strings.ToLower(DefaultSection)
382382
}
383383
section, _ := f.NewSection(name)
@@ -469,7 +469,7 @@ func (f *File) parse(reader io.Reader) (err error) {
469469
inUnparseableSection = false
470470
for i := range f.options.UnparseableSections {
471471
if f.options.UnparseableSections[i] == name ||
472-
(f.options.Insensitive && strings.EqualFold(f.options.UnparseableSections[i], name)) {
472+
((f.options.Insensitive || f.options.InsensitiveSections) && strings.EqualFold(f.options.UnparseableSections[i], name)) {
473473
inUnparseableSection = true
474474
continue
475475
}

section.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *Section) SetBody(body string) {
6666
func (s *Section) NewKey(name, val string) (*Key, error) {
6767
if len(name) == 0 {
6868
return nil, errors.New("error creating new key: empty key name")
69-
} else if s.f.options.Insensitive {
69+
} else if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
7070
name = strings.ToLower(name)
7171
}
7272

@@ -109,7 +109,7 @@ func (s *Section) GetKey(name string) (*Key, error) {
109109
if s.f.BlockMode {
110110
s.f.lock.RLock()
111111
}
112-
if s.f.options.Insensitive {
112+
if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
113113
name = strings.ToLower(name)
114114
}
115115
key := s.keys[name]

0 commit comments

Comments
 (0)