This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.go
265 lines (222 loc) · 5.48 KB
/
section.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
package configoration
import (
"fmt"
"strconv"
"strings"
"sync"
)
// Section provides functionalities to access
// sections and values in a Section by a key.
//
// A key can be a value or section key itself
// like "webserver" or it can span over sections
// like "general:webserver". In this case, the
// value after the last delimiter is selected
// section or value.
type Section interface {
// GetSection returns a section by key.
// If the desired section is not existent,
// the returned value will be nil.
GetSection(key string) Section
// GetValue returns an interface value by
// key. If the desired value could not be
// found, nil and ErrNil is returned.
GetValue(key string) (interface{}, error)
// GetString is shorthand for GetValue and
// returns a string or an ErrNil if the
// key was not found.
//
// If the value selected is not a string,
// ErrInvalidType will be returned.
GetString(key string) (string, error)
// GetInt is shorthand for GetValue and
// returns an int or an ErrNil if the
// key was not found.
//
// If the value selected is not an int,
// ErrInvalidType will be returned.
GetInt(key string) (int, error)
// GetBool is shorthand for GetValue and
// returns a bool or an ErrNil if the
// key was not found.
//
// If the value selected is not a bool,
// ErrInvalidType will be returned.
GetBool(key string) (bool, error)
// GetFloat64 is shorthand for GetValue and
// returns a float64 or an ErrNil if the
// key was not found.
//
// If the value selected is not a float64,
// ErrInvalidType will be returned.
GetFloat64(key string) (float64, error)
// GetValueOrDef returns an interface value
// by key. If the desired value could not be
// found, def will be returned.
GetValueOrDef(key string, def interface{}) interface{}
// GetStringOrDef is shorthand for GetValueOrDef
// and returns a string which is eather the
// found value or the vlaue of def.
GetStringOrDef(key string, def string) string
// GetIntOrDef is shorthand for GetValueOrDef
// and returns an int which is eather the
// found value or the vlaue of def.
GetIntOrDef(key string, def int) int
// GetBoolOrDef is shorthand for GetValueOrDef
// and returns a bool which is eather the
// found value or the vlaue of def.
GetBoolOrDef(key string, def bool) interface{}
// GetFloat64OrDef is shorthand for GetValueOrDef
// and returns a float64 which is eather the
// found value or the vlaue of def.
GetFloat64OrDef(key string, def float64) float64
// IsNil returns true if the current section
// instance is nil.
IsNil() bool
}
// section is the default implementation of
// the Section interface.
type section struct {
mtx sync.Mutex
m ConfigMap
}
func (s *section) GetSection(key string) Section {
for _, nextSelector := range splitSections(key) {
if s == nil {
return nil
}
s = s.getSection(nextSelector)
}
return s
}
func (s *section) GetValue(key string) (interface{}, error) {
if s == nil {
return nil, ErrNil
}
selectors := splitSections(key)
lenSelectors := len(selectors)
if lenSelectors > 1 {
for i := 0; i < lenSelectors-1; i++ {
s = s.getSection(selectors[i])
if s == nil {
return nil, ErrNil
}
}
}
s.mtx.Lock()
defer s.mtx.Unlock()
v, ok := s.m[selectors[lenSelectors-1]]
if !ok {
return nil, ErrNil
}
return v, nil
}
func (s *section) GetString(key string) (string, error) {
v, err := s.GetValue(key)
if err != nil {
return "", err
}
vt, ok := v.(string)
if !ok {
vt = valToString(v)
}
return vt, nil
}
func (s *section) GetInt(key string) (int, error) {
v, err := s.GetValue(key)
if err != nil {
return 0, err
}
vt, ok := v.(int)
if !ok {
vt, err = strconv.Atoi(valToString(v))
}
return vt, err
}
func (s *section) GetBool(key string) (bool, error) {
v, err := s.GetValue(key)
if err != nil {
return false, err
}
vt, ok := v.(bool)
if !ok {
vt, err = strconv.ParseBool(valToString(v))
}
return vt, err
}
func (s *section) GetFloat64(key string) (float64, error) {
v, err := s.GetValue(key)
if err != nil {
return 0, err
}
vt, ok := v.(float64)
if !ok {
vt, err = strconv.ParseFloat(valToString(v), 64)
}
return vt, err
}
func (s *section) GetValueOrDef(key string, def interface{}) interface{} {
v, err := s.GetValue(key)
if err != nil {
v = def
}
return v
}
func (s *section) GetStringOrDef(key string, def string) string {
v, err := s.GetString(key)
if err != nil {
v = def
}
return v
}
func (s *section) GetIntOrDef(key string, def int) int {
v, err := s.GetInt(key)
if err != nil {
v = def
}
return v
}
func (s *section) GetBoolOrDef(key string, def bool) interface{} {
v, err := s.GetBool(key)
if err != nil {
v = def
}
return v
}
func (s *section) GetFloat64OrDef(key string, def float64) float64 {
v, err := s.GetFloat64(key)
if err != nil {
v = def
}
return v
}
func (s *section) IsNil() bool {
return s == nil
}
// getSection returns the desired section
// or nil, if not found.
func (s *section) getSection(sec string) *section {
s.mtx.Lock()
defer s.mtx.Unlock()
v := s.m[sec]
vc, ok := v.(ConfigMap)
if !ok {
return nil
}
return §ion{
mtx: sync.Mutex{},
m: vc,
}
}
// splitSections splits the passed key by
// the Delimiter and returns the resulting
// array of strings.
func splitSections(key string) []string {
return strings.Split(key, Delimiter)
}
// valToString returns the passed interface
// as a string using fmt.Sprintf("%v", v) as
// converter.
func valToString(v interface{}) string {
return fmt.Sprintf("%v", v)
}