-
Notifications
You must be signed in to change notification settings - Fork 19
/
methods.go
318 lines (267 loc) · 7.55 KB
/
methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package configparser
import (
"fmt"
"sort"
"strconv"
)
func (p *ConfigParser) isDefaultSection(section string) bool {
return section == p.opt.defaultSection
}
// Defaults returns the items in the map used for default values.
func (p *ConfigParser) Defaults() Dict {
return p.defaults.Items()
}
// Sections returns a list of section names, excluding [DEFAULT].
func (p *ConfigParser) Sections() []string {
sections := make([]string, 0)
for section := range p.config {
sections = append(sections, section)
}
sort.Strings(sections)
return sections
}
// AddSection creates a new section in the configuration.
//
// Returns an error if a section by the specified name
// already exists.
// Returns an error if the specified name DEFAULT or any of its
// case-insensitive variants.
// Returns nil if no error and the section is created
func (p *ConfigParser) AddSection(section string) error {
if p.isDefaultSection(section) {
return fmt.Errorf("invalid section name: %q", section)
} else if p.HasSection(section) {
return fmt.Errorf("section %q already exists", section)
}
p.config[section] = newSection(section)
return nil
}
// HasSection returns true if the named section is present in the
// configuration.
//
// The DEFAULT section is not acknowledged.
func (p *ConfigParser) HasSection(section string) bool {
_, present := p.config[section]
return present
}
// Options returns a list of option mames for the given section name.
//
// Returns an error if the section does not exist.
func (p *ConfigParser) Options(section string) ([]string, error) {
if !p.HasSection(section) {
return nil, getNoSectionError(section)
}
seenOptions := make(map[string]bool)
for _, option := range p.config[section].Options() {
seenOptions[option] = true
}
for _, option := range p.defaults.Options() {
seenOptions[option] = true
}
options := make([]string, 0)
for option := range seenOptions {
options = append(options, option)
}
sort.Strings(options)
return options, nil
}
// Get returns string value for the named option.
//
// Returns an error if a section does not exist.
// Returns an error if the option does not exist either in the section or in
// the defaults.
func (p *ConfigParser) Get(section, option string) (string, error) {
result, err := p.get(section, option)
if err != nil {
return "", err
}
value, err := p.opt.converters[StringConv](result)
if err != nil {
return "", err
}
return value.(string), nil
}
func (p *ConfigParser) get(section, option string) (string, error) {
if !p.HasSection(section) {
if !p.isDefaultSection(section) {
return "", getNoSectionError(section)
}
if value, err := p.defaults.Get(option); err != nil {
return "", getNoOptionError(section, option)
} else {
return value, nil
}
} else if value, err := p.config[section].Get(option); err == nil {
return value, nil
} else if value, err := p.defaults.Get(option); err == nil {
return value, nil
}
return "", getNoOptionError(section, option)
}
// ItemsWithDefaults returns a copy of the named section Dict including
// any values from the Defaults.
//
// NOTE: This is different from the Python version which returns a list of
// tuples
func (p *ConfigParser) ItemsWithDefaults(section string) (Dict, error) {
if !p.HasSection(section) {
return nil, getNoSectionError(section)
}
s := make(Dict)
for k, v := range p.defaults.Items() {
s[k] = v
}
for k, v := range p.config[section].Items() {
s[k] = v
}
return s, nil
}
// Items returns a copy of the section Dict not including the Defaults.
//
// NOTE: This is different from the Python version which returns a list of
// tuples.
func (p *ConfigParser) Items(section string) (Dict, error) {
if section == p.opt.defaultSection {
return p.defaults.Items(), nil
}
if !p.HasSection(section) {
return nil, getNoSectionError(section)
}
return p.config[section].Items(), nil
}
// Set puts the given option into the named section.
//
// Returns an error if the section does not exist.
func (p *ConfigParser) Set(section, option, value string) error {
var setSection *Section
if p.isDefaultSection(section) {
setSection = p.defaults
} else if _, present := p.config[section]; !present {
return getNoSectionError(section)
} else {
setSection = p.config[section]
}
return setSection.Add(option, value)
}
// GetInt64 returns int64 representation of the named option.
//
// Returns an error if a section does not exist.
// Returns an error if the option does not exist either in the section or in
// the defaults.
func (p *ConfigParser) GetInt64(section, option string) (int64, error) {
result, err := p.get(section, option)
if err != nil {
return 0, err
}
value, err := p.opt.converters[IntConv](result)
if err != nil {
return 0, err
}
return value.(int64), nil
}
// GetFloat64 returns float64 representation of the named option.
//
// Returns an error if a section does not exist.
// Returns an error if the option does not exist either in the section or in
// the defaults.
func (p *ConfigParser) GetFloat64(section, option string) (float64, error) {
result, err := p.get(section, option)
if err != nil {
return 0, err
}
value, err := p.opt.converters[FloatConv](result)
if err != nil {
return 0, err
}
return value.(float64), nil
}
// GetBool returns bool representation of the named option.
//
// Returns an error if a section does not exist.
// Returns an error if the option does not exist either in the section or in
// the defaults.
func (p *ConfigParser) GetBool(section, option string) (bool, error) {
result, err := p.get(section, option)
if err != nil {
return false, err
}
value, err := p.opt.converters[BoolConv](result)
if err != nil {
return false, err
}
return value.(bool), nil
}
// RemoveSection removes given section from the ConfigParser.
func (p *ConfigParser) RemoveSection(section string) error {
if !p.HasSection(section) {
return getNoSectionError(section)
}
delete(p.config, section)
return nil
}
// HasOption checks if section contains option.
func (p *ConfigParser) HasOption(section, option string) (bool, error) {
var s *Section
if p.isDefaultSection(section) {
s = p.defaults
} else if _, present := p.config[section]; !present {
return false, getNoSectionError(section)
} else {
s = p.config[section]
}
_, err := s.Get(option)
return err == nil, nil
}
// RemoveOption removes option from the section.
func (p *ConfigParser) RemoveOption(section, option string) error {
var s *Section
if p.isDefaultSection(section) {
s = p.defaults
} else if _, present := p.config[section]; !present {
return getNoSectionError(section)
} else {
s = p.config[section]
}
return s.Remove(option)
}
func (p *ConfigParser) inOptions(key string) error {
opts, err := p.allOptions()
if err != nil {
return err
}
for _, o := range opts {
if key == o {
return fmt.Errorf(
"option %q already exists and strict flag was set",
key,
)
}
}
return nil
}
func (p *ConfigParser) allOptions() ([]string, error) {
sections := p.Sections()
options := make([]string, 0)
for _, s := range sections {
o, err := p.Options(s)
if err != nil {
return nil, err
}
options = append(options, o...)
}
return options, nil
}
func defaultGet(value string) (any, error) { return value, nil }
func defaultGetInt64(value string) (any, error) {
return strconv.ParseInt(value, 10, 64)
}
func defaultGetFloat64(value string) (any, error) {
return strconv.ParseFloat(value, 64)
}
func defaultGetBool(value string) (any, error) {
booleanValue, present := boolMapping[value]
if !present {
return false, fmt.Errorf("not a boolean: %q", value)
}
return booleanValue, nil
}