-
Notifications
You must be signed in to change notification settings - Fork 15
/
onion.go
496 lines (417 loc) · 11.7 KB
/
onion.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package onion
import (
"context"
"reflect"
"strconv"
"strings"
"sync"
"time"
)
// Layer is an interface to handle the load phase.
type Layer interface {
// Load is called once to get the initial data, it can return nil if there is no initial data
Load() map[string]interface{}
// Watch is called as soon as the layer registered in the onion. if the layer is persistent
// it can return nil or a closed channel
// Also this function may called several time and should return the same channel each time
// and should not block
Watch() <-chan map[string]interface{}
}
var o = &Onion{}
// Onion is a layer base configuration system
type Onion struct {
lock sync.RWMutex
delimiter string
ll []Layer
// Loaded data
data map[Layer]map[string]interface{}
reload chan struct{}
}
func (o *Onion) watchLayer(ctx context.Context, l Layer) {
c := l.Watch()
if c == nil {
return
}
for {
select {
case data, ok := <-c:
if !ok {
return
}
o.setLayerData(l, data, true)
case <-ctx.Done():
return
}
}
}
func (o *Onion) setLayerData(l Layer, data map[string]interface{}, watch bool) {
o.lock.Lock()
defer o.lock.Unlock()
if o.data == nil {
o.data = make(map[Layer]map[string]interface{})
}
o.data[l] = data
if !watch {
return
}
if o.reload != nil {
close(o.reload)
o.reload = nil
}
}
// AddLayersContext add a new layer to global config
func AddLayersContext(ctx context.Context, l ...Layer) {
o.AddLayersContext(ctx, l...)
}
// AddLayersContext add new layers to the end of config layers. last layer is loaded after all other
// layer
func (o *Onion) AddLayersContext(ctx context.Context, l ...Layer) {
if len(l) == 0 {
return
}
o.lock.Lock()
o.ll = append(o.ll, l...)
o.lock.Unlock()
for i := range l {
o.setLayerData(l[i], l[i].Load(), false)
go o.watchLayer(ctx, l[i])
}
}
// AddLayers add a new layer to global config
func AddLayers(l ...Layer) {
o.AddLayersContext(context.Background(), l...)
}
// AddLayers add new layers to onion
func (o *Onion) AddLayers(l ...Layer) {
o.AddLayersContext(context.Background(), l...)
}
// GetDelimiter return the delimiter for nested key
func GetDelimiter() string {
return o.GetDelimiter()
}
// GetDelimiter return the delimiter for nested key
func (o *Onion) GetDelimiter() string {
if o.delimiter == "" {
o.delimiter = "."
}
return o.delimiter
}
// SetDelimiter set the current delimiter on global config
func SetDelimiter(d string) {
o.SetDelimiter(d)
}
// SetDelimiter set the current delimiter
func (o *Onion) SetDelimiter(d string) {
o.delimiter = d
}
// ReloadWatch see onion.ReloadWatch
func ReloadWatch() <-chan struct{} {
return o.ReloadWatch()
}
// ReloadWatch returns a channel to watch new layer data change, it just work for once, after the first change
// the channel will be changed to a new channel (the old channel will be closed to signal all listeners)
func (o *Onion) ReloadWatch() <-chan struct{} {
o.lock.Lock()
defer o.lock.Unlock()
if o.reload == nil {
o.reload = make(chan struct{})
}
return o.reload
}
// Get try to get the key from config layers
func Get(key string) (interface{}, bool) {
return o.Get(key)
}
// Get try to get the key from config layers
func (o *Onion) Get(key string) (interface{}, bool) {
o.lock.RLock()
defer o.lock.RUnlock()
path := strings.Split(key, o.GetDelimiter())
for i := len(o.ll); i > 0; i-- {
v, ok := searchStringMap(o.data[o.ll[i-1]], path...)
if ok {
return v, ok
}
}
return nil, false
}
// GetIntDefault return an int value from Onion, if the value is not exists or its not an
// integer , default is returned
func GetIntDefault(key string, def int) int {
return o.GetIntDefault(key, def)
}
// GetIntDefault return an int value from Onion, if the value is not exists or its not an
// integer , default is returned
func (o *Onion) GetIntDefault(key string, def int) int {
return int(o.GetInt64Default(key, int64(def)))
}
// GetInt return an int value, if the value is not there, then it return zero value
func GetInt(key string) int {
return o.GetInt(key)
}
// GetInt return an int value, if the value is not there, then it return zero value
func (o *Onion) GetInt(key string) int {
return o.GetIntDefault(key, 0)
}
// GetInt64Default return an int64 value from Onion, if the value is not exists or if the value is not
// int64 then return the default
func GetInt64Default(key string, def int64) int64 {
return o.GetInt64Default(key, def)
}
// GetInt64Default return an int64 value from Onion, if the value is not exists or if the value is not
// int64 then return the default
func (o *Onion) GetInt64Default(key string, def int64) int64 {
v, ok := o.Get(key)
if !ok {
return def
}
switch nv := v.(type) {
case string:
// Env is not typed and always is String, so try to convert it to int
// if possible
i, err := strconv.ParseInt(nv, 10, 64)
if err != nil {
return def
}
return i
case int:
return int64(nv)
case int64:
return nv
case float32:
return int64(nv)
case float64:
return int64(nv)
default:
return def
}
}
// GetInt64 return the int64 value from config, if its not there, return zero
func GetInt64(key string) int64 {
return o.GetInt64(key)
}
// GetInt64 return the int64 value from config, if its not there, return zero
func (o *Onion) GetInt64(key string) int64 {
return o.GetInt64Default(key, 0)
}
// GetFloat32Default return an float32 value from Onion, if the value is not exists or its not a
// float32, default is returned
func GetFloat32Default(key string, def float32) float32 {
return o.GetFloat32Default(key, def)
}
// GetFloat32Default return an float32 value from Onion, if the value is not exists or its not a
// float32, default is returned
func (o *Onion) GetFloat32Default(key string, def float32) float32 {
return float32(o.GetFloat64Default(key, float64(def)))
}
// GetFloat32 return an float32 value, if the value is not there, then it returns zero value
func GetFloat32(key string) float32 {
return o.GetFloat32(key)
}
// GetFloat32 return an float32 value, if the value is not there, then it returns zero value
func (o *Onion) GetFloat32(key string) float32 {
return o.GetFloat32Default(key, 0)
}
// GetFloat64Default return an float64 value from Onion, if the value is not exists or if the value is not
// float64 then return the default
func GetFloat64Default(key string, def float64) float64 {
return o.GetFloat64Default(key, def)
}
// GetFloat64Default return an float64 value from Onion, if the value is not exists or if the value is not
// float64 then return the default
func (o *Onion) GetFloat64Default(key string, def float64) float64 {
v, ok := o.Get(key)
if !ok {
return def
}
switch nv := v.(type) {
case string:
// Env is not typed and always is String, so try to convert it to int
// if possible
f, err := strconv.ParseFloat(nv, 64)
if err != nil {
return def
}
return f
case int:
return float64(nv)
case int64:
return float64(nv)
case float32:
return float64(nv)
case float64:
return nv
default:
return def
}
}
// GetFloat64 return the float64 value from config, if its not there, return zero
func GetFloat64(key string) float64 {
return o.GetFloat64(key)
}
// GetFloat64 return the float64 value from config, if its not there, return zero
func (o *Onion) GetFloat64(key string) float64 {
return o.GetFloat64Default(key, 0)
}
// GetStringDefault get a string from Onion. if the value is not exists or if tha value is not
// string, return the default
func GetStringDefault(key string, def string) string {
return o.GetStringDefault(key, def)
}
// GetStringDefault get a string from Onion. if the value is not exists or if tha value is not
// string, return the default
func (o *Onion) GetStringDefault(key string, def string) string {
v, ok := o.Get(key)
if !ok {
return def
}
s, ok := v.(string)
if !ok {
return def
}
return s
}
// GetString is for getting an string from conig. if the key is not
func GetString(key string) string {
return o.GetString(key)
}
// GetString is for getting an string from conig. if the key is not
func (o *Onion) GetString(key string) string {
return o.GetStringDefault(key, "")
}
// GetBoolDefault return bool value from Onion. if the value is not exists or if tha value is not
// boolean, return the default
func GetBoolDefault(key string, def bool) bool {
return o.GetBoolDefault(key, def)
}
// GetBoolDefault return bool value from Onion. if the value is not exists or if tha value is not
// boolean, return the default
func (o *Onion) GetBoolDefault(key string, def bool) bool {
v, ok := o.Get(key)
if !ok {
return def
}
switch nv := v.(type) {
case string:
// Env is not typed and always is String, so try to convert it to boolean
// if possible
i, err := strconv.ParseBool(nv)
if err != nil {
return def
}
return i
case bool:
return nv
default:
return def
}
}
// GetBool is used to get a boolean value fro config, with false as default
func GetBool(key string) bool {
return o.GetBool(key)
}
// GetBool is used to get a boolean value fro config, with false as default
func (o *Onion) GetBool(key string) bool {
return o.GetBoolDefault(key, false)
}
// GetDurationDefault is a function to get duration from config. it support both
// string duration (like 1h3m2s) and integer duration
func GetDurationDefault(key string, def time.Duration) time.Duration {
return o.GetDurationDefault(key, def)
}
// GetDurationDefault is a function to get duration from config. it support both
// string duration (like 1h3m2s) and integer duration
func (o *Onion) GetDurationDefault(key string, def time.Duration) time.Duration {
v, ok := o.Get(key)
if !ok {
return def
}
switch nv := v.(type) {
case string:
d, err := time.ParseDuration(nv)
if err != nil {
return def
}
return d
case int:
return time.Duration(nv)
case int64:
return time.Duration(nv)
case time.Duration:
return nv
default:
return def
}
}
// GetDuration is for getting duration from config, it cast both int and string
// to duration
func GetDuration(key string) time.Duration {
return o.GetDuration(key)
}
// GetDuration is for getting duration from config, it cast both int and string
// to duration
func (o *Onion) GetDuration(key string) time.Duration {
return o.GetDurationDefault(key, 0)
}
func (o *Onion) getSlice(key string) (interface{}, bool) {
v, ok := o.Get(key)
if !ok {
return nil, false
}
if reflect.TypeOf(v).Kind() != reflect.Slice { // Not good
return nil, false
}
return v, true
}
// GetStringSlice try to get a slice from the config, also it support comma separated value
// if there is no array at the key.
func GetStringSlice(key string) []string {
return o.GetStringSlice(key)
}
// GetStringSlice try to get a slice from the config, also it support comma separated value
// if there is no array at the key.
func (o *Onion) GetStringSlice(key string) []string {
var ok bool
v, ok := o.getSlice(key)
if !ok {
if v := o.GetString(key); len(v) > 0 {
return strings.Split(v, ",")
}
return nil
}
switch nv := v.(type) {
case []string:
return nv
case []interface{}:
res := make([]string, len(nv))
for i := range nv {
if res[i], ok = nv[i].(string); !ok {
return nil
}
}
return res
}
return nil
}
// LayersData is used to get all layers data at once, useful for test and also
// used in the config writer
func (o *Onion) LayersData() []map[string]interface{} {
o.lock.RLock()
defer o.lock.RUnlock()
res := make([]map[string]interface{}, 0, len(o.ll))
for i := range o.ll {
l := o.ll[i]
res = append(res, o.data[l])
}
return res
}
// NewContext return a new Onion, context is used for watch
func NewContext(ctx context.Context, layers ...Layer) *Onion {
o := &Onion{}
o.AddLayersContext(ctx, layers...)
return o
}
// New returns a new onion
func New(layers ...Layer) *Onion {
return NewContext(context.Background(), layers...)
}