-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.go
287 lines (243 loc) · 7.42 KB
/
get.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conf
import (
"encoding/csv"
"strconv"
"strings"
)
// Sections returns the list of sections in the configuration.
// (The default section always exists.)
func (c *Config) Sections() (sections []string) {
sections = make([]string, len(c.data))
i := 0
for s, _ := range c.data {
sections[i] = s
i++
}
return sections
}
// HasSection checks if the configuration has the given section.
// (The default section always exists.)
func (c *Config) HasSection(section string) bool {
if section == "" {
section = "default"
}
_, ok := c.data[strings.ToLower(section)]
return ok
}
// Options returns the list of options available in the given section.
// It returns an error if the section does not exist and an empty list if the section is empty.
// Options within the default section are also included.
func (c *Config) Options(section string) (options []string, err error) {
if section == "" {
section = "default"
}
section = strings.ToLower(section)
if _, ok := c.data[section]; !ok {
return nil, GetError{SectionNotFound, "", "", section, ""}
}
options = make([]string, len(c.data[DefaultSection])+len(c.data[section]))
i := 0
for s, _ := range c.data[DefaultSection] {
options[i] = s
i++
}
for s, _ := range c.data[section] {
options[i] = s
i++
}
return options, nil
}
// HasOption checks if the configuration has the given option in the section.
// It returns false if either the option or section do not exist.
func (c *Config) HasOption(section string, option string) bool {
if section == "" {
section = "default"
}
section = strings.ToLower(section)
option = strings.ToLower(option)
if _, ok := c.data[section]; !ok {
return false
}
_, okd := c.data[DefaultSection][option]
_, oknd := c.data[section][option]
return okd || oknd
}
// RawString gets the (raw) string value for the given option in the section.
// The raw string value is not subjected to unfolding, which was illustrated in the beginning of this documentation.
// It returns an error if either the section or the option do not exist.
func (c *Config) RawString(section string, option string) (value string, err error) {
if section == "" {
section = "default"
}
section = strings.ToLower(section)
option = strings.ToLower(option)
if _, ok := c.data[section]; ok {
if value, ok = c.data[section][option]; ok {
return value, nil
}
return "", GetError{OptionNotFound, "", "", section, option}
}
return "", GetError{SectionNotFound, "", "", section, option}
}
// String gets the string value for the given option in the section.
// If the value needs to be unfolded (see e.g. %(host)s example in the beginning of this documentation),
// then String does this unfolding automatically, up to DepthValues number of iterations.
// It returns an error if either the section or the option do not exist, or the unfolding cycled.
func (c *Config) String(section string, option string) (value string, err error) {
value, err = c.RawString(section, option)
if err != nil {
return "", err
}
return value, nil
}
// StringList gets the string values for the given option in the section.
// It returns an error if either the section or the option do not exist,
// or the unfolding cycled.
func (c *Config) StringList(section string, option string) (values []string, err error) {
value, err := c.RawString(section, option)
if err != nil {
return nil, err
}
if strings.Contains(value, "\n") {
v := strings.Split(value, "\n")
if len(v[0]) == 0 {
// Remove first empty element
copy(v[0:], v[0+1:])
v[len(v)-1] = ""
v = v[:len(v)-1]
}
for _, val := range v {
values = append(values, strings.Trim(val, " \t\r\n"))
}
} else {
v := strings.NewReader(value)
csvData := csv.NewReader(v)
values, err = csvData.Read()
if err != nil {
return nil, err
}
for i, val := range values {
values[i] = strings.Trim(val, " \t\r\n")
}
}
return values, nil
}
// Int has the same behaviour as String but converts the response to int.
func (c *Config) Int(section string, option string) (value int, err error) {
sv, err := c.String(section, option)
if err == nil {
value, err = strconv.Atoi(sv)
if err != nil {
err = GetError{CouldNotParse, "int", sv, section, option}
}
}
return value, err
}
// IntList has the same behaviour as StringList but converts the response
// to []int.
func (c *Config) IntList(section string, option string) (values []int, err error) {
slvs, err := c.StringList(section, option)
if err != nil {
return nil, err
}
for _, val := range slvs {
value, err := strconv.Atoi(val)
if err != nil {
err = GetError{CouldNotParse, "int", val, section, option}
return nil, err
}
values = append(values, value)
}
return values, nil
}
// Int64 has the same behaviour as String but converts the response
// to int64.
func (c *Config) Int64(section string, option string) (value int64, err error) {
sv, err := c.String(section, option)
if err == nil {
value, err = strconv.ParseInt(sv, 10, 64)
if err != nil {
err = GetError{CouldNotParse, "int64", sv, section, option}
}
}
return value, err
}
// Int64List has the same behaviour as StringList but converts the response
// to []int64.
func (c *Config) Int64List(section string, option string) (values []int64, err error) {
slvs, err := c.StringList(section, option)
if err != nil {
return nil, err
}
for _, val := range slvs {
value, err := strconv.ParseInt(val, 10, 64)
if err != nil {
err = GetError{CouldNotParse, "int64", val, section, option}
return nil, err
}
values = append(values, value)
}
return values, nil
}
// Float64 has the same behaviour as String but converts the response
// to float64.
func (c *Config) Float64(section string, option string) (value float64, err error) {
sv, err := c.String(section, option)
if err == nil {
value, err = strconv.ParseFloat(sv, 64)
if err != nil {
err = GetError{CouldNotParse, "float64", sv, section, option}
}
}
return value, err
}
// Float64List has the same behaviour as StringList but converts the response
// to []float64.
func (c *Config) Float64List(section string, option string) (values []float64, err error) {
slvs, err := c.StringList(section, option)
if err != nil {
return nil, err
}
for _, val := range slvs {
value, err := strconv.ParseFloat(val, 64)
if err != nil {
print(err.Error())
err = GetError{CouldNotParse, "float64", val, section, option}
return nil, err
}
values = append(values, value)
}
return values, nil
}
// Bool has the same behaviour as String but converts the response to bool.
// See constant BoolStrings for string values converted to bool.
func (c *Config) Bool(section string, option string) (value bool, err error) {
sv, err := c.String(section, option)
if err != nil {
return false, err
}
value, ok := BoolStrings[strings.ToLower(sv)]
if !ok {
return false, GetError{CouldNotParse, "bool", sv, section, option}
}
return value, nil
}
// BoolList has the same behaviour as StringList but converts the response
// to []bool.
func (c *Config) BoolList(section string, option string) (values []bool, err error) {
slvs, err := c.StringList(section, option)
if err != nil {
return nil, err
}
for _, val := range slvs {
value, ok := BoolStrings[strings.ToLower(val)]
if !ok {
err = GetError{CouldNotParse, "int64", val, section, option}
return nil, err
}
values = append(values, value)
}
return values, nil
}