Skip to content

Commit d6c4d4e

Browse files
committedJan 27, 2023
switched options to func
1 parent 0ff251a commit d6c4d4e

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed
 

‎configparser.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ type Config map[string]*Section
4646
type ConfigParser struct {
4747
config Config
4848
defaults *Section
49-
opt *Options
50-
}
51-
52-
// Options allows to control parser behavior.
53-
type Options struct {
54-
AllowNoValue bool
49+
opt *options
5550
}
5651

5752
// Keys returns a sorted slice of keys
@@ -79,12 +74,17 @@ func New() *ConfigParser {
7974
return &ConfigParser{
8075
config: make(Config),
8176
defaults: newSection(defaultSectionName),
82-
opt: &Options{},
77+
opt: &options{},
8378
}
8479
}
8580

8681
// NewWithOptions creates a new ConfigParser with options.
87-
func NewWithOptions(opt *Options) *ConfigParser {
82+
func NewWithOptions(opts ...OptFunc) *ConfigParser {
83+
opt := &options{}
84+
for _, fn := range opts {
85+
fn(opt)
86+
}
87+
8888
return &ConfigParser{
8989
config: make(Config),
9090
defaults: newSection(defaultSectionName),
@@ -123,8 +123,8 @@ func ParseReader(in io.Reader) (*ConfigParser, error) {
123123
}
124124

125125
// ParseReaderWithOptions parses a ConfigParser from the provided input with given options.
126-
func ParseReaderWithOptions(in io.Reader, opt *Options) (*ConfigParser, error) {
127-
p := NewWithOptions(opt)
126+
func ParseReaderWithOptions(in io.Reader, opts ...OptFunc) (*ConfigParser, error) {
127+
p := NewWithOptions(opts...)
128128
err := p.ParseReader(in)
129129

130130
return p, err
@@ -145,8 +145,8 @@ func Parse(filename string) (*ConfigParser, error) {
145145
}
146146

147147
// ParseWithOptions takes a filename and parses it into a ConfigParser value with given options.
148-
func ParseWithOptions(filename string, opt *Options) (*ConfigParser, error) {
149-
p := NewWithOptions(opt)
148+
func ParseWithOptions(filename string, opts ...OptFunc) (*ConfigParser, error) {
149+
p := NewWithOptions(opts...)
150150
data, err := os.ReadFile(filename)
151151
if err != nil {
152152
return nil, err
@@ -237,7 +237,7 @@ func (p *ConfigParser) ParseReader(in io.Reader) error {
237237
if err := curSect.Add(key, value); err != nil {
238238
return fmt.Errorf("failed to add %q = %q: %w", key, value, err)
239239
}
240-
} else if match = keyWNoValue.FindStringSubmatch(line); len(match) > 0 && p.opt.AllowNoValue && curSect != nil {
240+
} else if match = keyWNoValue.FindStringSubmatch(line); len(match) > 0 && p.opt.allowNoValue && curSect != nil {
241241
key := strings.TrimSpace(match[1])
242242
value := match[4]
243243
if err := curSect.Add(key, value); err != nil {

‎configparser_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ func (s *ConfigParserSuite) TestParseFromReader(c *gc.C) {
124124

125125
// If AllowNoValue is set to true, parser should recognize options without values.
126126
func (s *ConfigParserSuite) TestParseReaderWithOptionsWNoValue(c *gc.C) {
127-
opt := &configparser.Options{AllowNoValue: true}
128-
parsed, err := configparser.ParseReaderWithOptions(strings.NewReader("[empty]\noption\n\n"), opt)
127+
parsed, err := configparser.ParseReaderWithOptions(
128+
strings.NewReader("[empty]\noption\n\n"), configparser.AllowNoValue,
129+
)
129130
c.Assert(err, gc.IsNil)
130131

131132
ok, err := parsed.HasOption("empty", "option")
@@ -138,8 +139,9 @@ func assertSuccessful(c *gc.C, err error) {
138139
}
139140

140141
func (s *ConfigParserSuite) TestParseWithOptions(c *gc.C) {
141-
opt := &configparser.Options{AllowNoValue: true}
142-
parsed, err := configparser.ParseWithOptions("testdata/example.cfg", opt)
142+
parsed, err := configparser.ParseWithOptions(
143+
"testdata/example.cfg", configparser.AllowNoValue,
144+
)
143145
c.Assert(err, gc.IsNil)
144146

145147
ok, err := parsed.HasOption("empty", "foo")

‎options.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package configparser
2+
3+
type options struct {
4+
allowNoValue bool
5+
}
6+
7+
// OptFunc modifies options.
8+
type OptFunc func(*options)
9+
10+
// AllowNoValue allows option with no value to be saved
11+
// as empty line.
12+
func AllowNoValue(o *options) { o.allowNoValue = true }

0 commit comments

Comments
 (0)
Please sign in to comment.