diff --git a/file.go b/file.go index 1345918..7b4e560 100644 --- a/file.go +++ b/file.go @@ -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 { diff --git a/file_test.go b/file_test.go index 36126f8..6ab6739 100644 --- a/file_test.go +++ b/file_test.go @@ -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.True(t, sec) + + t.Run("section not exists", func(t *testing.T) { + nonexistent := f.HasSection("404") + assert.False(t, nonexistent) + }) +} + func TestFile_Section(t *testing.T) { t.Run("get a section", func(t *testing.T) { f, err := Load(fullConf)