From fa5bae867567c38b4f9d2c86af53d54ac6a3168f Mon Sep 17 00:00:00 2001
From: Bryan Joshua Pedini
Date: Fri, 19 Nov 2021 23:18:43 +0100
Subject: [PATCH 1/3] added feature HasSection (fix #271) copied implementation
from section.go
---
file.go | 6 ++++++
1 file changed, 6 insertions(+)
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 {
From 38cfa9c47fde4de7618b3608e1312aeb5039d174 Mon Sep 17 00:00:00 2001
From: Bryan Joshua Pedini
Date: Fri, 19 Nov 2021 23:30:08 +0100
Subject: [PATCH 2/3] added tests for HasSection feature
---
file_test.go | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/file_test.go b/file_test.go
index 36126f8..9bdc20a 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.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)
From 94a5bcccd88bfdce44045869c4e8a1c29a5e437c Mon Sep 17 00:00:00 2001
From: Joe Chen
Date: Tue, 30 Nov 2021 10:45:13 +0800
Subject: [PATCH 3/3] Apply suggestions from code review
---
file_test.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/file_test.go b/file_test.go
index 9bdc20a..6ab6739 100644
--- a/file_test.go
+++ b/file_test.go
@@ -243,11 +243,11 @@ func TestFile_HasSection(t *testing.T) {
require.NotNil(t, f)
sec := f.HasSection("author")
- assert.Equal(t, true, sec)
+ assert.True(t, sec)
t.Run("section not exists", func(t *testing.T) {
nonexistent := f.HasSection("404")
- assert.Equal(t, false, nonexistent)
+ assert.False(t, nonexistent)
})
}