Skip to content

Commit a679eef

Browse files
authored
Add options to NewWithDefaults, update bool (#20)
* Add supprt for options in NewWithDefaults * Make getting boolean values case-insensitive, i.e. "yes", "true", "on", "True", "ON" are all considered to be `true`.
1 parent e51f64e commit a679eef

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

configparser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ func NewWithOptions(opts ...optFunc) *ConfigParser {
9191
}
9292

9393
// NewWithDefaults allows creation of a new ConfigParser with a pre-existing Dict.
94-
func NewWithDefaults(defaults Dict) (*ConfigParser, error) {
95-
p := New()
94+
func NewWithDefaults(defaults Dict, opts ...optFunc) (*ConfigParser, error) {
95+
p := NewWithOptions(opts...)
9696
for key, value := range defaults {
9797
// Add never returns an error.
9898
_ = p.defaults.Add(key, value)

methods.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"sort"
66
"strconv"
7+
"strings"
78
)
89

910
func (p *ConfigParser) isDefaultSection(section string) bool {
@@ -16,8 +17,9 @@ func (p *ConfigParser) Defaults() Dict {
1617
}
1718

1819
// Sections returns a list of section names, excluding [DEFAULT].
20+
// Returned slice is sorted.
1921
func (p *ConfigParser) Sections() []string {
20-
sections := make([]string, 0)
22+
sections := make([]string, 0, len(p.config))
2123
for section := range p.config {
2224
sections = append(sections, section)
2325
}
@@ -54,7 +56,8 @@ func (p *ConfigParser) HasSection(section string) bool {
5456
return present
5557
}
5658

57-
// Options returns a list of option mames for the given section name.
59+
// Options returns a list of option names for the given section name.
60+
// Returned slice is sorted.
5861
//
5962
// Returns an error if the section does not exist.
6063
func (p *ConfigParser) Options(section string) ([]string, error) {
@@ -317,7 +320,7 @@ func defaultGetFloat64(value string) (any, error) {
317320
}
318321

319322
func defaultGetBool(value string) (any, error) {
320-
booleanValue, present := boolMapping[value]
323+
booleanValue, present := boolMapping[strings.ToLower(value)]
321324
if !present {
322325
return false, fmt.Errorf("not a boolean: %q", value)
323326
}

methods_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package configparser_test
22

33
import (
4+
"errors"
45
"strings"
56

67
"github.com/bigkevmcd/go-configparser"
@@ -101,6 +102,22 @@ func (s *ConfigParserSuite) TestGet(c *gc.C) {
101102
c.Assert(result, gc.Equals, "200")
102103
}
103104

105+
func (s *ConfigParserSuite) TestGetConvError(c *gc.C) {
106+
p, err := configparser.NewWithDefaults(
107+
configparser.Dict{"key": "value"},
108+
configparser.Converters(
109+
configparser.Converter{
110+
configparser.StringConv: func(s string) (any, error) {
111+
return nil, errors.New("invalid string")
112+
},
113+
},
114+
),
115+
)
116+
c.Assert(err, gc.IsNil)
117+
_, err = p.Get("DEFAULT", "key")
118+
c.Assert(err, gc.ErrorMatches, "invalid string")
119+
}
120+
104121
// Get(section, option) should return the option value for the named section
105122
// regardless of case
106123
func (s *ConfigParserSuite) TestGetCamelCase(c *gc.C) {
@@ -279,14 +296,14 @@ func (s *ConfigParserSuite) TestGetBool(c *gc.C) {
279296
newParser := configparser.New()
280297
newParser.AddSection("testing")
281298

282-
for _, value := range []string{"1", "yes", "true", "on"} {
299+
for _, value := range []string{"1", "yes", "true", "on", "True", "ON"} {
283300
newParser.Set("testing", "value", value)
284301
result, err := newParser.GetBool("testing", "value")
285302
c.Assert(err, gc.IsNil)
286303
c.Assert(result, gc.Equals, true)
287304
}
288305

289-
for _, value := range []string{"0", "no", "false", "off"} {
306+
for _, value := range []string{"0", "no", "false", "off", "False", "OFF"} {
290307
newParser.Set("testing", "value", value)
291308
result, err := newParser.GetBool("testing", "value")
292309
c.Assert(err, gc.IsNil)

0 commit comments

Comments
 (0)