-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
295 lines (258 loc) · 8.91 KB
/
config.go
File metadata and controls
295 lines (258 loc) · 8.91 KB
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
package config
import "sync"
//Config provides methods to store, retrieve, and remove arbitrary values
//that are referenced by keys.
//
//The keys to a Config type are of type string.
//These string keys are parsed into Key types via a KeyParser.
//Key types that are parsed by a Config type are then used to reference into Values.
//Values is the storage type providing all functionality to Config.
//
//Config is safe for use by multiple goroutines.
//Though the KeyParser is not protected from concurrent use, implementations can be
//implemented in a safe manner.
//
//The zero value for *Config is not in a valid state and will likely cause panics if
//used.
type Config struct {
//KeyParser that turns strings into Keys that are then used with
//this Config's underlying Values.
//This enables easier access to Config's values with a simple string as opposed
//to a Key type.
KeyParser KeyParser
values *Values
lock *sync.Mutex
loaders []Loader
}
//New creates a new *Config with an empty Values and Loaders.
//KeyParser is set to PeriodSeparatorKeyParser.
func New() *Config {
return &Config{
KeyParser: PeriodSeparatorKeyParser,
values: NewValues(),
lock: &sync.Mutex{},
loaders: []Loader{},
}
}
//AddLoaders adds Loaders to an internal slice of loaders.
//There is no check for duplicates or nil Loaders.
//This method servers as a utility to store Loaders associated with a Config
//to be merged into c at a later time by c.LoadAll().
func (c *Config) AddLoaders(loaders ...Loader) *Config {
c.lock.Lock()
defer c.lock.Unlock()
c.loaders = append(c.loaders, loaders...)
return c
}
//LoadAll is a helper for c.MergeLoaders() called with all Loaders
//added previously with c.AddLoaders().
func (c *Config) LoadAll() (bool, error) {
c.lock.Lock()
defer c.lock.Unlock()
return c.MergeLoaders(c.loaders...)
}
//Values returns the internal Values used for storage.
//The Values type is safe for use by multiple goroutines independent of the Config type.
//The internal reference is simply returned, and thus modifications to the
//returned *Values WILL AFFECT c.
func (c *Config) Values() *Values {
return c.values
}
//EqualValues is sugar for c.Values().Equal(other.Values()).
func (c *Config) EqualValues(other *Config) bool {
return c.values.Equal(other.values)
}
//Clone creates and returns a new *Config with KeyParser and added loaders
//shallow copied, and with *Values cloned via *Values.Clone().
func (c *Config) Clone() *Config {
c.lock.Lock()
defer c.lock.Unlock()
return &Config{
KeyParser: c.KeyParser,
values: c.values.Clone(),
lock: &sync.Mutex{},
loaders: c.loaders,
}
}
//GetInt64 returns an int64 casted integer type stored at key.
//The zero value for int64 is returned if an integer type does not exist at key.
func (c *Config) GetInt64(key string) (i int64) {
i64, _ := c.GetInt64Ok(key)
return i64
}
//GetInt64Ok returns an int64 casted integer type stored at key.
//The zero value for int64 is returned if an integer type does not exist at key.
//The return value ok indicates whether or not an integer type actually exists at key.
func (c *Config) GetInt64Ok(key string) (i int64, ok bool) {
v, ok := c.GetOk(key)
if !ok {
return 0, false
}
i, ok = int64(0), false
switch iType := v.(type) {
case uint8:
i, ok = int64(iType), true
case int8:
i, ok = int64(iType), true
case uint16:
i, ok = int64(iType), true
case int16:
i, ok = int64(iType), true
case uint32:
i, ok = int64(iType), true
case int32:
i, ok = int64(iType), true
case uint:
i, ok = int64(iType), true
case int:
i, ok = int64(iType), true
case uint64:
i, ok = int64(iType), true
case int64:
i, ok = iType, true
}
return
}
//GetBool returns a bool stored at key.
//The zero value for bool is returned if a bool does not exist at key.
func (c *Config) GetBool(key string) (b bool) {
b, _ = c.GetBoolOk(key)
return
}
//GetBoolOk returns a bool stored at key.
//The zero value for bool is returned if a bool does not exist at key.
//The return value ok indicates whether or not a bool actually exists at key.
func (c *Config) GetBoolOk(key string) (b bool, ok bool) {
v, ok := c.GetOk(key)
if !ok {
return false, false
}
b, ok = v.(bool)
return
}
//GetString returns a string stored at key.
//The zero value for string is returned if a string does not exist at key.
func (c *Config) GetString(key string) (s string) {
s, _ = c.GetStringOk(key)
return
}
//GetStringOk returns a string stored at key.
//The zero value for string is returned if a string does not exist at key.
//The return value ok indicates whether or not a string actually exists at key.
func (c *Config) GetStringOk(key string) (s string, ok bool) {
v, ok := c.GetOk(key)
if !ok {
return "", false
}
s, ok = v.(string)
return
}
//GetFloat64 returns a float64 casted floating point type stored at key.
//The zero value for float64 is returned if a floating point type does not exist at key.
func (c *Config) GetFloat64(key string) (f float64) {
f, _ = c.GetFloat64Ok(key)
return
}
//GetFloat64Ok returns a float64 casted floating point type stored at key.
//The zero value for float64 is returned if a floating point type does not exist at key.
//The return value ok indicates whether or not a floating point type actually exists at key.
func (c *Config) GetFloat64Ok(key string) (f float64, ok bool) {
v, ok := c.GetOk(key)
if !ok {
return 0, false
}
f, ok = float64(0), false
switch fType := v.(type) {
case float32:
f, ok = float64(fType), true
case float64:
f, ok = fType, true
}
return
}
//GetValues returns a *Values stored at key.
//This means that there exists some value stored at a longer Key.
//The returned *Values is cloned and thus changes to v DO NOT AFFECT c and vice versa.
//nil is returned if a *Values does not exist at key.
func (c *Config) GetValues(key string) (v *Values) {
v, _ = c.GetValuesOk(key)
return
}
//GetValuesOk returns a *Values stored at key.
//This means that there exists some value stored at a longer Key.
//The returned *Values is cloned and thus changes to v DO NOT AFFECT c and vice versa.
//nil is returned if more values do not exist at key.
//The return value ok indicates whether or not there are more values stored at key.
func (c *Config) GetValuesOk(key string) (v *Values, ok bool) {
vInterface, ok := c.GetOk(key)
if !ok {
return nil, false
}
v, ok = vInterface.(*Values)
return
}
//Get is sugar for c.GetKey(c.NewKey(key)).
//It returns a raw interface{} value stored at key or nil if a value does not
//exist at key.
func (c *Config) Get(key string) (v interface{}) {
return c.GetKey(c.NewKey(key))
}
//GetOk is sugar for c.GetKeyOk(c.NewKey(key)).
//It returns a raw interface{} value stored at key or nil if a value does not
//exist at key.
//The return value ok indicates whether or not any value is actually stored at key.
func (c *Config) GetOk(key string) (v interface{}, ok bool) {
return c.GetKeyOk(c.NewKey(key))
}
//GetKey returns Get(key) called on c's internal *Values instance.
//It returns a raw interface{} value stored at key or nil if a value does not
//exist at key.
func (c *Config) GetKey(key Key) (v interface{}) {
return c.values.Get(key)
}
//GetKeyOk returns GetKey(key) called on c's internal *Values instance.
//It returns a raw interface{} value stored at key or nil if a value does not
//exist at key.
//The return value ok indicates whether or not any value is actually stored at key.
func (c *Config) GetKeyOk(key Key) (v interface{}, ok bool) {
return c.values.GetOk(key)
}
//Merge is sugar for c.Values().Merge(Key(nil), other.Values()).
func (c *Config) Merge(other *Config) (changed bool) {
return c.values.Merge(nil, other.values)
}
//Put is sugar for c.PutKey(c.NewKey(key), value).
func (c *Config) Put(key string, value interface{}) (changed bool) {
return c.PutKey(c.NewKey(key), value)
}
//PutKey is sugar for c.Values().Put(key, value).
func (c *Config) PutKey(key Key, value interface{}) (changed bool) {
return c.values.Put(key, value)
}
//MergeLoaders creates a temporary Values into which all Loaders in loaders are merged.
//If an error occurs on any individual Loader.Load(), then MergeLoaders returns
//immediately with that error and does not change c in any way.
//If all Loader.Load() do not error, then the temporary Values are merged into
//c's Values.
func (c *Config) MergeLoaders(loaders ...Loader) (changed bool, err error) {
temp := NewValues()
for _, loader := range loaders {
loaderValues, err := loader.Load()
if err != nil {
return false, err
}
changed = temp.Merge(nil, loaderValues) || changed
}
c.values.Merge(nil, temp)
return changed, nil
}
//Remove removes a Key value association in c's Values.
//It returns the value being removed. ok indicates whether or not a value was
//actually stored at key and was removed.
func (c *Config) Remove(key string) (value interface{}, ok bool) {
return c.values.Remove(c.NewKey(key))
}
//NewKey returns the Key created by c.KeyParser.Parse(k).
func (c *Config) NewKey(k string) Key {
return c.KeyParser.Parse(k)
}