8000 Add options to control sections insensitive or keys insensitive respectively by ygj6 · Pull Request #253 · go-ini/ini · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add options to control sections insensitive or keys insensitive respectively #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 52 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions section.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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]
Expand Down
0