Skip to content

file: add HasSection method #308

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
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ func (f *File) GetSection(name string) (*Section, error) {
return secs[0], err
}

// HasSection returns true if the file contains a section with given name.
func (f *File) HasSection(name string) bool {
section, _ := f.GetSection(name)
return section != nil
}

// SectionsByName returns all sections with given name.
func (f *File) SectionsByName(name string) ([]*Section, error) {
if len(name) == 0 {
Expand Down
14 changes: 14 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ func TestFile_GetSection(t *testing.T) {
})
}

func TestFile_HasSection(t *testing.T) {
f, err := Load(fullConf)
require.NoError(t, err)
require.NotNil(t, f)

sec := f.HasSection("author")
assert.Equal(t, true, sec)

t.Run("section not exists", func(t *testing.T) {
nonexistent := f.HasSection("404")
assert.Equal(t, false, nonexistent)
})
}

func TestFile_Section(t *testing.T) {
t.Run("get a section", func(t *testing.T) {
f, err := Load(fullConf)
Expand Down