This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
config.go
551 lines (484 loc) · 12.6 KB
/
config.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* Copyright © 2019 Hedzr Yeh.
*/
package vxconf
import (
"flag"
"fmt"
"strconv"
"strings"
"sync"
"syscall"
"gopkg.in/hedzr/errors.v2"
)
type KVStore map[string]string
// AppConfig represents a configuration with convenient access methods.
type AppConfig struct {
// loaded from meta.yaml
Root interface{}
// app runtime k/v store
KV KVStore
}
var (
once sync.Once
instance AppConfig
)
func New() AppConfig {
once.Do(func() {
instance = AppConfig{
KV: make(KVStore),
}
})
return instance
}
// Config ---------------------------------------------------------------------
// Get returns a nested config according to a dotted path.
func (c *AppConfig) Get(path string) (*AppConfig, error) {
n, err := Get(c.Root, path)
if err != nil {
return nil, err
}
return &AppConfig{Root: n}, nil
}
// Set a nested config according to a dotted path.
func (c *AppConfig) Set(path string, val interface{}) error {
return Set(c.Root, path, val)
}
// Fetch data from system env, based on existing config keys.
func (c *AppConfig) Env() *AppConfig {
return c.EnvPrefix("")
}
// Fetch data from system env using prefix, based on existing config keys.
func (c *AppConfig) EnvPrefix(prefix string) *AppConfig {
if prefix != "" {
prefix = strings.ToUpper(prefix) + "_"
}
keys := getKeys(c.Root)
for _, key := range keys {
k := strings.ToUpper(strings.Join(key, "_"))
if val, exist := syscall.Getenv(prefix + k); exist {
c.Set(strings.Join(key, "."), val)
}
}
return c
}
// Parse command line arguments, based on existing config keys.
func (c *AppConfig) Flag() *AppConfig {
keys := getKeys(c.Root)
hash := map[string]*string{}
for _, key := range keys {
k := strings.Join(key, "-")
hash[k] = new(string)
val, _ := c.String(k)
flag.StringVar(hash[k], k, val, "")
}
flag.Parse()
flag.Visit(func(f *flag.Flag) {
name := strings.Replace(f.Name, "-", ".", -1)
c.Set(name, f.Value.String())
})
return c
}
// Get all keys for given interface
func getKeys(source interface{}, base ...string) [][]string {
acc := [][]string{}
// Copy "base" so that underlying slice array is not
// modified in recursive calls
nextBase := make([]string, len(base))
copy(nextBase, base)
switch c := source.(type) {
case map[string]interface{}:
for k, v := range c {
keys := getKeys(v, append(nextBase, k)...)
acc = append(acc, keys...)
}
case []interface{}:
for i, v := range c {
k := strconv.Itoa(i)
keys := getKeys(v, append(nextBase, k)...)
acc = append(acc, keys...)
}
default:
acc = append(acc, nextBase)
return acc
}
return acc
}
// Bool returns a bool according to a dotted path.
func (c *AppConfig) Bool(path string) (bool, error) {
n, err := Get(c.Root, path)
if err != nil {
return false, err
}
switch n := n.(type) {
case bool:
return n, nil
case string:
return strconv.ParseBool(n)
}
return false, typeMismatch("bool or string", n)
}
// UBool returns a bool according to a dotted path or default value or false.
func (c *AppConfig) UBool(path string, defaults ...bool) bool {
value, err := c.Bool(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return false
}
// Float64 returns a float64 according to a dotted path.
func (c *AppConfig) Float64(path string) (float64, error) {
n, err := Get(c.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
return n, nil
case int:
return float64(n), nil
case string:
return strconv.ParseFloat(n, 64)
}
return 0, typeMismatch("float64, int or string", n)
}
// UFloat64 returns a float64 according to a dotted path or default value or 0.
func (c *AppConfig) UFloat64(path string, defaults ...float64) float64 {
value, err := c.Float64(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return float64(0)
}
// Int returns an int according to a dotted path.
func (c *AppConfig) Int(path string) (int, error) {
n, err := Get(c.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
// encoding/json unmarshals numbers into floats, so we compare
// the string representation to see if we can return an int.
if i := int(n); fmt.Sprint(i) == fmt.Sprint(n) {
return i, nil
} else {
return 0, errors.New("Value can't be converted to int: %v", n)
}
case int:
return n, nil
case string:
if v, err := strconv.ParseInt(n, 10, 0); err == nil {
return int(v), nil
} else {
return 0, err
}
}
return 0, typeMismatch("float64, int or string", n)
}
// UInt returns an int according to a dotted path or default value or 0.
func (c *AppConfig) UInt(path string, defaults ...int) int {
value, err := c.Int(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return 0
}
// List returns a []interface{} according to a dotted path.
func (c *AppConfig) List(path string) ([]interface{}, error) {
n, err := Get(c.Root, path)
if err != nil {
return nil, err
}
if value, ok := n.([]interface{}); ok {
return value, nil
}
return nil, typeMismatch("[]interface{}", n)
}
// UList returns a []interface{} according to a dotted path or defaults or []interface{}.
func (c *AppConfig) UList(path string, defaults ...[]interface{}) []interface{} {
value, err := c.List(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return make([]interface{}, 0)
}
// Map returns a map[string]interface{} according to a dotted path.
func (c *AppConfig) Map(path string) (map[string]interface{}, error) {
n, err := Get(c.Root, path)
if err != nil {
return nil, err
}
if value, ok := n.(map[string]interface{}); ok {
return value, nil
}
return nil, typeMismatch("map[string]interface{}", n)
}
// UMap returns a map[string]interface{} according to a dotted path or default or map[string]interface{}.
func (c *AppConfig) UMap(path string, defaults ...map[string]interface{}) map[string]interface{} {
value, err := c.Map(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return map[string]interface{}{}
}
// String returns a string according to a dotted path.
func (c *AppConfig) String(path string) (string, error) {
n, err := Get(c.Root, path)
if err != nil {
return "", err
}
switch n := n.(type) {
case bool, float64, int:
return fmt.Sprint(n), nil
case string:
return n, nil
}
return "", typeMismatch("bool, float64, int or string", n)
}
// UString returns a string according to a dotted path or default or "".
func (c *AppConfig) UString(path string, defaults ...string) string {
value, err := c.String(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return ""
}
// Copy returns a deep copy with given path or without.
func (c *AppConfig) Copy(dottedPath ...string) (*AppConfig, error) {
toJoin := []string{}
for _, part := range dottedPath {
if len(part) != 0 {
toJoin = append(toJoin, part)
}
}
var err error
var path = strings.Join(toJoin, ".")
var cfg = c
var root = ""
if len(path) > 0 {
if cfg, err = c.Get(path); err != nil {
return nil, err
}
}
if root, err = RenderYaml(cfg.Root); err != nil {
return nil, err
}
return ParseYaml(root)
}
// Extend returns extended copy of current config with applied
// values from the given config instance. Note that if you extend
// with different structure you will get an error. See: `.Set()` method
// for details.
func (c *AppConfig) Extend(cfg *AppConfig) (*AppConfig, error) {
n, err := c.Copy()
if err != nil {
return nil, err
}
keys := getKeys(cfg.Root)
for _, key := range keys {
k := strings.Join(key, ".")
i, err := Get(cfg.Root, k)
if err != nil {
return nil, err
}
if err := n.Set(k, i); err != nil {
return nil, err
}
}
return n, nil
}
// typeMismatch returns an error for an expected type.
func typeMismatch(expected string, got interface{}) error {
return errors.New("Type mismatch: expected %s; got %T", expected, got)
}
// Fetching -------------------------------------------------------------------
// Get returns a child of the given value according to a dotted path.
func Get(cfg interface{}, path string) (interface{}, error) {
parts := strings.Split(path, ".")
// Normalize path.
for k, v := range parts {
if v == "" {
if k == 0 {
parts = parts[1:]
} else {
return nil, errors.New("Invalid path %q", path)
}
}
}
// Get the value.
for pos, part := range parts {
switch c := cfg.(type) {
case []interface{}:
if i, error := strconv.ParseInt(part, 10, 0); error == nil {
if int(i) < len(c) {
cfg = c[i]
} else {
return nil, errors.New(
"Index out of range at %q: list has only %v items",
strings.Join(parts[:pos+1], "."), len(c))
}
} else {
return nil, errors.New("Invalid list index at %q",
strings.Join(parts[:pos+1], "."))
}
case map[string]interface{}:
if value, ok := c[part]; ok {
cfg = value
} else {
return nil, errors.New("Nonexistent map key at %q",
strings.Join(parts[:pos+1], "."))
}
default:
return nil, errors.New(
"Invalid type at %q: expected []interface{} or map[string]interface{}; got %T",
strings.Join(parts[:pos+1], "."), cfg)
}
}
return cfg, nil
}
// Set returns an error, in case when it is not possible to
// establish the value obtained in accordance with given dotted path.
func Set(cfg interface{}, path string, value interface{}) error {
parts := strings.Split(path, ".")
// Normalize path.
for k, v := range parts {
if v == "" {
if k == 0 {
parts = parts[1:]
} else {
return errors.New("Invalid path %q", path)
}
}
}
point := &cfg
for pos, part := range parts {
switch c := (*point).(type) {
case []interface{}:
if i, err := strconv.ParseInt(part, 10, 0); err == nil {
// 1. normalize slice capacity
if int(i) >= cap(c) {
c = append(c, make([]interface{}, int(i)-cap(c)+1, int(i)-cap(c)+1)...)
}
// 2. set value or go further
if pos+1 == len(parts) {
c[i] = value
} else {
// if exists just pick the pointer
if va := c[i]; va != nil {
point = &va
} else {
// is next part slice or map?
if i, err := strconv.ParseInt(parts[pos+1], 10, 0); err == nil {
va = make([]interface{}, int(i)+1, int(i)+1)
} else {
va = make(map[string]interface{})
}
c[i] = va
point = &va
}
}
} else {
return errors.New("Invalid list index at %q",
strings.Join(parts[:pos+1], "."))
}
case map[string]interface{}:
if pos+1 == len(parts) {
c[part] = value
} else {
// if exists just pick the pointer
if va, ok := c[part]; ok {
point = &va
} else {
// is next part slice or map?
if i, err := strconv.ParseInt(parts[pos+1], 10, 0); err == nil {
va = make([]interface{}, int(i)+1, int(i)+1)
} else {
va = make(map[string]interface{})
}
c[part] = va
point = &va
}
}
default:
return errors.New(
"Invalid type at %q: expected []interface{} or map[string]interface{}; got %T",
strings.Join(parts[:pos+1], "."), cfg)
}
}
return nil
}
// Parsing --------------------------------------------------------------------
// Must is a wrapper for parsing functions to be used during initialization.
// It panics on failure.
func Must(cfg *AppConfig, err error) *AppConfig {
if err != nil {
panic(err)
}
return cfg
}
// normalizeValue normalizes a unmarshalled value. This is needed because
// encoding/json doesn't support marshalling map[interface{}]interface{}.
func normalizeValue(value interface{}) (interface{}, error) {
switch value := value.(type) {
case map[interface{}]interface{}:
node := make(map[string]interface{}, len(value))
for k, v := range value {
key, ok := k.(string)
if !ok {
return nil, errors.New("Unsupported map key: %#v", k)
}
item, err := normalizeValue(v)
if err != nil {
return nil, errors.New("Unsupported map value: %#v", v)
}
node[key] = item
}
return node, nil
case map[string]interface{}:
node := make(map[string]interface{}, len(value))
for key, v := range value {
item, err := normalizeValue(v)
if err != nil {
return nil, errors.New("Unsupported map value: %#v", v)
}
node[key] = item
}
return node, nil
case []interface{}:
node := make([]interface{}, len(value))
for key, v := range value {
item, err := normalizeValue(v)
if err != nil {
return nil, errors.New("Unsupported list item: %#v", v)
}
node[key] = item
}
return node, nil
case bool, float64, int, string, nil:
return value, nil
}
return nil, errors.New("Unsupported type: %T", value)
}
//
//
// ----------------------------------------------------------------
//