-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
524 lines (444 loc) · 14.8 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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/config source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// Package config is nice and handy layer built around `forge` config syntax;
// which is similar to HOCON syntax. Internally `aah/config` uses `forge`
// syntax developed by `https://github.com/brettlangdon`.
//
// aah framework is powered with `aahframework.org/config` library.
package config
import (
"errors"
"fmt"
"strings"
"aahframework.org/forge.v0"
"aahframework.org/vfs.v0"
)
var errKeyNotFound = errors.New("config: not found")
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//______________________________________________________________________________
// NewEmpty method returns aah empty config instance.
func NewEmpty() *Config {
cfg, _ := ParseString("")
return cfg
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Config type and methods
//______________________________________________________________________________
// Config handles the configuration values and enables environment profile's,
// merge, etc. Also it provide nice and handly methods for accessing config values.
// Internally `aah config` uses `forge syntax` developed by `https://github.com/brettlangdon`.
type Config struct {
profile string
cfg *forge.Section
}
// Profile returns current active profile
func (c *Config) Profile() string {
return c.profile
}
// SetProfile actives the configuarion profile if found otherwise returns error
func (c *Config) SetProfile(profile string) error {
if !c.HasProfile(profile) {
return fmt.Errorf("profile doesn't exists: %v", profile)
}
c.profile = profile
return nil
}
// ClearProfile clears currently active configuration `Profile`
func (c *Config) ClearProfile() {
c.profile = ""
}
// HasProfile checks given configuration profile is exists or not
func (c *Config) HasProfile(profile string) bool {
cfg, found := c.getraw(profile)
return found && cfg.GetType() == forge.SECTION
}
// IsProfileEnabled returns true of profile enabled otherwise false
func (c *Config) IsProfileEnabled() bool {
if c == nil {
return false
}
return len(c.profile) > 0
}
// Keys returns all the key names at current level
func (c *Config) Keys() []string {
return c.cfg.Keys()
}
// GetSubConfig create new sub config from the given key path. Only `Section`
// type can be created as sub config. Profile value is not propagated to sub config.
func (c *Config) GetSubConfig(key string) (*Config, bool) {
v, err := c.cfg.Resolve(c.prepareKey(key))
if err != nil {
return nil, false
}
if s, ok := v.(*forge.Section); ok {
return &Config{cfg: s}, true
}
return nil, false
}
// KeysByPath is similar to `Config.Keys()`, however it returns key names for
// given key path.
func (c *Config) KeysByPath(path string) []string {
v, err := c.cfg.Resolve(path)
if err != nil {
return []string{}
}
if s, ok := v.(*forge.Section); ok {
return s.Keys()
}
return []string{}
}
// String gets the `string` value for the given key from the configuration.
func (c *Config) String(key string) (string, bool) {
if value, found := c.Get(key); found {
return value.(string), found
}
return "", false
}
// StringDefault gets the `string` value for the given key from the configuration.
// If key does not exists it returns default value.
func (c *Config) StringDefault(key, defaultValue string) string {
if value, found := c.String(key); found {
return value
}
return defaultValue
}
// Bool gets the `bool` value for the given key from the configuration.
func (c *Config) Bool(key string) (bool, bool) {
if value, found := c.Get(key); found {
return value.(bool), found
}
return false, false
}
// BoolDefault gets the `bool` value for the given key from the configuration.
// If key does not exists it returns default value.
func (c *Config) BoolDefault(key string, defaultValue bool) bool {
if value, found := c.Bool(key); found {
return value
}
return defaultValue
}
// Int gets the `int` value for the given key from the configuration.
func (c *Config) Int(key string) (int, bool) {
if value, found := c.Get(key); found {
return int(value.(int64)), found
}
return 0, false
}
// Int64 gets the `int64` value for the given key from the configuration.
func (c *Config) Int64(key string) (int64, bool) {
if value, found := c.Get(key); found {
return value.(int64), found
}
return int64(0), false
}
// IntDefault gets the `int` value for the given key from the configuration.
// If key does not exists it returns default value.
func (c *Config) IntDefault(key string, defaultValue int) int {
if value, found := c.Int(key); found {
return value
}
return defaultValue
}
// Float32 gets the `float32` value for the given key from the configuration.
func (c *Config) Float32(key string) (float32, bool) {
if value, found := c.Get(key); found {
return float32(value.(float64)), found
}
return float32(0.0), false
}
// Float32Default gets the `float32` value for the given key from the configuration.
// If key does not exists it returns default value.
func (c *Config) Float32Default(key string, defaultValue float32) float32 {
if value, found := c.Float32(key); found {
return value
}
return defaultValue
}
// Float64 gets the `float64` value for the given key from the configuration.
func (c *Config) Float64(key string) (float64, bool) {
if value, found := c.Get(key); found {
return value.(float64), found
}
return float64(0.0), false
}
// Get gets the value from configuration returns as `interface{}`.
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) Get(key string) (interface{}, bool) {
if c.IsProfileEnabled() {
if value, found := c.getByProfile(key); found {
return value, found
}
}
return c.get(key)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// List methods
//______________________________________________________________________________
// StringList method returns the string slice value for the given key.
// Eaxmple:-
//
// Config:
// ...
// excludes = ["*_test.go", ".*", "*.bak", "*.tmp", "vendor"]
// ...
//
// Accessing Values:
// values, found := cfg.StringList("excludes")
// fmt.Println("Found:", found)
// fmt.Println("Values:", strings.Join(values, ", "))
//
// Output:
// Found: true
// Values: *_test.go, .*, *.bak, *.tmp, vendor
//
func (c *Config) StringList(key string) ([]string, bool) {
values := []string{}
if lst, found := c.getListValue(key); found {
for idx := 0; idx < lst.Length(); idx++ {
if v, err := lst.GetString(idx); err == nil {
values = append(values, v)
}
}
return values, found
}
return values, false
}
// IntList method returns the int slice value for the given key.
// Eaxmple:-
//
// Config:
// ...
// int_list = [10, 20, 30, 40, 50]
// ...
//
// Accessing Values:
// values, found := cfg.IntList("int_list")
// fmt.Println("Found:", found)
// fmt.Println("Values:", values)
//
// Output:
// Found: true
// Values: [10, 20, 30, 40, 50]
//
func (c *Config) IntList(key string) ([]int, bool) {
var result []int
values, found := c.Int64List(key)
if !found {
return result, found
}
for _, v := range values {
result = append(result, int(v))
}
return result, true
}
// Int64List method returns the int64 slice value for the given key.
// Eaxmple:-
//
// Config:
// ...
// int64_list = [100000001, 100000002, 100000003, 100000004, 100000005]
// ...
//
// Accessing Values:
// values, found := cfg.Int64List("excludes")
// fmt.Println("Found:", found)
// fmt.Println("Values:", values)
//
// Output:
// Found: true
// Values: [100000001, 100000002, 100000003, 100000004, 100000005]
//
func (c *Config) Int64List(key string) ([]int64, bool) {
values := []int64{}
lst, found := c.getListValue(key)
if lst == nil || !found {
return values, found
}
for idx := 0; idx < lst.Length(); idx++ {
if v, err := lst.GetInteger(idx); err == nil {
values = append(values, v)
}
}
return values, true
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Config Setter methods
//______________________________________________________________________________
// SetString sets the given value string for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetString(key string, value string) {
if err := c.updateValue(key, value); err == errKeyNotFound {
c.addValue(key, forge.NewString(value))
}
}
// SetInt sets the given value int for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetInt(key string, value int) {
c.SetInt64(key, int64(value))
}
// SetInt64 sets the given value int64 for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetInt64(key string, value int64) {
if err := c.updateValue(key, value); err == errKeyNotFound {
c.addValue(key, forge.NewInteger(value))
}
}
// SetFloat32 sets the given value float32 for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetFloat32(key string, value float32) {
c.SetFloat64(key, float64(value))
}
// SetFloat64 sets the given value float64 for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetFloat64(key string, value float64) {
if err := c.updateValue(key, value); err == errKeyNotFound {
c.addValue(key, forge.NewFloat(value))
}
}
// SetBool sets the given value bool for config key
// First it tries to get value within enabled profile
// otherwise it tries without profile
func (c *Config) SetBool(key string, value bool) {
if err := c.updateValue(key, value); err == errKeyNotFound {
c.addValue(key, forge.NewBoolean(value))
}
}
// Merge merges the given section to current section. Settings from source
// section overwites the values in the current section
func (c *Config) Merge(source *Config) error {
if source == nil {
return errors.New("source is nil")
}
return c.cfg.Merge(source.cfg)
}
// IsExists returns true if given is exists in the config otherwise returns false
func (c *Config) IsExists(key string) bool {
_, found := c.Get(key)
return found
}
// ToJSON method returns the configuration values as JSON string.
func (c *Config) ToJSON() string {
if b, err := c.cfg.ToJSON(); err == nil {
return string(b)
}
return "{}"
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Config load/parse methods
//______________________________________________________________________________
// LoadFile loads the configuration from given config file.
func LoadFile(file string) (*Config, error) {
return VFSLoadFile(nil, file)
}
// VFSLoadFile loads the configuration from given vfs and config file.
func VFSLoadFile(fs *vfs.VFS, file string) (*Config, error) {
setting, err := loadFile(fs, file)
return &Config{cfg: setting}, err
}
// LoadFiles loads the configuration from given config files.
// It does merging of configuration in the order they are given.
func LoadFiles(files ...string) (*Config, error) {
return VFSLoadFiles(nil, files...)
}
// VFSLoadFiles loads the configuration from given config vfs and files.
// It does merging of configuration in the order they are given.
func VFSLoadFiles(fs *vfs.VFS, files ...string) (*Config, error) {
settings := forge.NewSection()
for _, file := range files {
setting, err := loadFile(fs, file)
if err != nil {
return nil, err
}
if err = settings.Merge(setting); err != nil {
return nil, err
}
}
return &Config{cfg: settings}, nil
}
// ParseString parses the configuration values from string
func ParseString(cfg string) (*Config, error) {
setting, err := forge.ParseString(cfg)
if err != nil {
return nil, err
}
return &Config{cfg: setting}, nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Config unexported methods
//______________________________________________________________________________
func loadFile(fs *vfs.VFS, file string) (*forge.Section, error) {
if _, err := vfs.Stat(fs, file); err != nil {
return nil, fmt.Errorf("configuration does not exists: %v", file)
}
return forge.VFSParseFile(fs, file)
}
func (c *Config) prepareKey(key string) string {
if c.IsProfileEnabled() {
return fmt.Sprintf("%s.%s", c.profile, key)
}
return key
}
func (c *Config) getByProfile(key string) (interface{}, bool) {
return c.get(c.prepareKey(key))
}
func (c *Config) get(key string) (interface{}, bool) {
if v, found := c.getraw(key); found {
return v.GetValue(), true // found
}
return nil, false // not found
}
func (c *Config) getListValue(key string) (*forge.List, bool) {
value, found := c.getraw(c.prepareKey(key))
if !found {
value, found = c.getraw(key)
if !found {
return nil, found
}
}
if value.GetType() != forge.LIST {
return nil, false
}
return value.(*forge.List), true
}
func (c *Config) getraw(key string) (forge.Value, bool) {
if c == nil || c.cfg == nil {
return nil, false
}
v, err := c.cfg.Resolve(key)
if err != nil {
return nil, false // not found
}
return v, true // found
}
func (c *Config) updateValue(key string, value interface{}) error {
if v, found := c.getraw(c.prepareKey(key)); found {
_ = v.UpdateValue(value)
}
return errKeyNotFound
}
func (c *Config) getSection(parts []string) *forge.Section {
current := c.cfg
for _, part := range parts {
if nc, err := current.GetSection(part); err == nil { // exists
current = nc
continue
}
current = current.AddSection(part)
}
return current
}
func (c *Config) addValue(key string, value forge.Value) {
parts := strings.Split(c.prepareKey(key), ".")
if len(parts) > 1 {
section := c.getSection(parts[:len(parts)-1])
section.Set(parts[len(parts)-1], value)
}
}