-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
557 lines (411 loc) · 19.4 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
552
553
554
555
556
557
package artifact
import (
"github.com/goldeneggg/structil"
"github.com/mcuadros/go-defaults"
"github.com/shipu/artifact/env"
"github.com/spf13/viper"
"log"
"reflect"
"strings"
"unsafe"
)
var Config *Configuration
type Configuration struct {
RegisteredConfigStruct map[string]interface{}
LoadedConfig map[string]interface{}
NoSqlConfig string
RelationDBConfig string
}
type Getter struct {
*structil.Getter
}
func NewConfig() *Configuration {
return &Configuration{
RegisteredConfigStruct: make(map[string]interface{}),
}
}
func (configuration *Configuration) Load() map[string]interface{} {
newConfig := make(map[string]interface{})
v := env.New(viper.New())
v.SetConfigFile(".env")
verr := v.ReadInConfig()
if verr != nil {
v = env.New(viper.New())
v.AutomaticEnv()
log.Println("Currently using environment from os. If you want to modify environment then please create a file with .env extension.")
}
for name, value := range configuration.RegisteredConfigStruct {
err := v.Unmarshal(&value)
if err != nil {
log.Fatal("environment cant be loaded: ", err)
}
defaults.SetDefaults(value)
newConfig[name] = value
}
if configuration.NoSqlConfig == "" {
configuration.NoSqlConfig = "NoSql"
newConfig["NoSqlConfig"] = "NoSql"
}
if configuration.RelationDBConfig == "" {
configuration.RelationDBConfig = "DB"
newConfig["RelationDBConfig"] = "DB"
}
configuration.LoadedConfig = newConfig
return newConfig
}
func (configuration *Configuration) AddConfig(name string, userConfig interface{}) *Configuration {
configuration.RegisteredConfigStruct[name] = userConfig
return configuration
}
func (configuration *Configuration) AddNoSqlConfig(name string, userConfig interface{}) *Configuration {
configuration.NoSqlConfig = name
configuration.AddConfig(name, userConfig)
return configuration
}
func (configuration *Configuration) AddRelationDBConfig(name string, userConfig interface{}) *Configuration {
configuration.RelationDBConfig = name
configuration.AddConfig(name, userConfig)
return configuration
}
func (configuration Configuration) keySplit(key string) (string, string) {
var rootKey string
splitKey := strings.Split(key, ".")
rootKey, splitKey = splitKey[0], splitKey[1:]
key = strings.Join(splitKey, ".")
return rootKey, key
}
func (configuration Configuration) prepareConfigKey(key string) (string, *structil.Getter, error) {
rootKey, key := configuration.keySplit(key)
newGetter, err := structil.NewGetter(configuration.LoadedConfig[rootKey])
return key, newGetter, err
}
func (configuration Configuration) GetString(key string) string {
value, _ := configuration.String(key)
return value
}
// String returns the string of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not string.
func (configuration *Configuration) String(key string) (string, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.String(key)
}
// Int returns the int of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not int.
func (configuration Configuration) Int(key string) (int, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Int(key)
}
// Get returns the interface of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
func (configuration Configuration) Get(key string) (interface{}, bool) {
newKey, newGetter, _ := configuration.prepareConfigKey(key)
if newKey == "" {
return newGetter.ToMap(), true
}
return newGetter.Get(newKey)
}
func (configuration Configuration) Getter(key string) *Getter {
_, newGetter, _ := configuration.prepareConfigKey(key)
return &Getter{newGetter}
}
// NumField returns num of struct field.
func (configuration Configuration) NumField(key string) int {
newGetter := configuration.Getter(key)
return newGetter.NumField()
}
// Names returns names of struct field.
func (configuration Configuration) Names(key string) []string {
newGetter := configuration.Getter(key)
return newGetter.Names()
}
// Has tests whether the original struct has a field named "name".
func (configuration Configuration) Has(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Has(key)
}
// GetType returns the reflect.Type object of the original struct field named "name".
// 2nd return value will be false if the original struct does not have a "name" field.
func (configuration Configuration) GetType(key string) (reflect.Type, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.GetType(key)
}
// GetValue returns the reflect.Value object of the original struct field named "name".
// 2nd return value will be false if the original struct does not have a "name" field.
func (configuration Configuration) GetValue(key string) (reflect.Value, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.GetValue(key)
}
// ToMap returns a map converted from this Getter.
func (configuration Configuration) ToMap(key string) map[string]interface{} {
newGetter := configuration.Getter(key)
return newGetter.ToMap()
}
// IsSlice reports whether type of the original struct field named name is slice.
func (configuration Configuration) IsSlice(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsSlice(key)
}
// Slice returns the slice of interface of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not slice of interface.
func (configuration Configuration) Slice(key string) ([]interface{}, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Slice(key)
}
// IsBool reports whether type of the original struct field named name is bool.
func (configuration Configuration) IsBool(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsBool(key)
}
// Bool returns the byte of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not bool.
func (configuration Configuration) Bool(key string) (bool, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Bool(key)
}
// IsByte reports whether type of the original struct field named name is byte.
func (configuration Configuration) IsByte(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsByte(key)
}
// Byte returns the byte of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not byte.
func (configuration Configuration) Byte(key string) (byte, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Byte(key)
}
// IsBytes reports whether type of the original struct field named name is []byte.
func (configuration Configuration) IsBytes(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsBytes(key)
}
// Bytes returns the []byte of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not []byte.
func (configuration Configuration) Bytes(key string) ([]byte, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Bytes(key)
}
// IsString reports whether type of the original struct field named name is string.
func (configuration Configuration) IsString(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsString(key)
}
// IsInt reports whether type of the original struct field named name is int.
func (configuration Configuration) IsInt(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsInt(key)
}
// IsInt8 reports whether type of the original struct field named name is int8.
func (configuration Configuration) IsInt8(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsInt8(key)
}
// Int8 returns the int8 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not int8.
func (configuration Configuration) Int8(key string) (int8, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Int8(key)
}
// IsInt16 reports whether type of the original struct field named name is int16.
func (configuration Configuration) IsInt16(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsInt16(key)
}
// Int16 returns the int16 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not int16.
func (configuration Configuration) Int16(key string) (int16, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Int16(key)
}
// IsInt32 reports whether type of the original struct field named name is int32.
func (configuration Configuration) IsInt32(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsInt32(key)
}
// Int32 returns the int32 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not int32.
func (configuration Configuration) Int32(key string) (int32, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Int32(key)
}
// IsInt64 reports whether type of the original struct field named name is int64.
func (configuration Configuration) IsInt64(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsInt64(key)
}
// Int64 returns the int64 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not int64.
func (configuration Configuration) Int64(key string) (int64, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Int64(key)
}
// IsUint reports whether type of the original struct field named name is uint.
func (configuration Configuration) IsUint(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUint(key)
}
// Uint returns the uint of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uint.
func (configuration Configuration) Uint(key string) (uint, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uint(key)
}
// IsUint8 reports whether type of the original struct field named name is uint8.
func (configuration Configuration) IsUint8(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUint8(key)
}
// Uint8 returns the uint8 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uint8.
func (configuration Configuration) Uint8(key string) (uint8, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uint8(key)
}
// IsUint16 reports whether type of the original struct field named name is uint16.
func (configuration Configuration) IsUint16(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUint16(key)
}
// Uint16 returns the uint16 of the original struct field named name.Getter
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uint16.
func (configuration Configuration) Uint16(key string) (uint16, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uint16(key)
}
// IsUint32 reports whether type of the original struct field named name is uint32.
func (configuration Configuration) IsUint32(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUint32(key)
}
// Uint32 returns the uint32 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uint32.
func (configuration Configuration) Uint32(key string) (uint32, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uint32(key)
}
// IsUint64 reports whether type of the original struct field named name is uint64.
func (configuration Configuration) IsUint64(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUint64(key)
}
// Uint64 returns the uint64 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uint64.
func (configuration Configuration) Uint64(key string) (uint64, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uint64(key)
}
// IsUintptr reports whether type of the original struct field named name is uintptr.
func (configuration Configuration) IsUintptr(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUintptr(key)
}
// Uintptr returns the uintptr of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not uintptr.
func (configuration Configuration) Uintptr(key string) (uintptr, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Uintptr(key)
}
// IsFloat32 reports whether type of the original struct field named name is float32.
func (configuration Configuration) IsFloat32(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsFloat32(key)
}
// Float32 returns the float32 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not float32.
func (configuration Configuration) Float32(key string) (float32, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Float32(key)
}
// IsFloat64 reports whether type of the original struct field named name is float64.
func (configuration Configuration) IsFloat64(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsFloat64(key)
}
// Float64 returns the float64 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not float64.
func (configuration Configuration) Float64(key string) (float64, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Float64(key)
}
// IsComplex64 reports whether type of the original struct field named name is []byte.
func (configuration Configuration) IsComplex64(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsComplex64(key)
}
// Complex64 returns the complex64 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not complex64.
func (configuration Configuration) Complex64(key string) (complex64, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Complex64(key)
}
// IsComplex128 reports whether type of the original struct field named name is []byte.
func (configuration Configuration) IsComplex128(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsComplex128(key)
}
// Complex128 returns the complex128 of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not complex128.
func (configuration Configuration) Complex128(key string) (complex128, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.Complex128(key)
}
// IsUnsafePointer reports whether type of the original struct field named name is []byte.
func (configuration Configuration) IsUnsafePointer(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsUnsafePointer(key)
}
// UnsafePointer returns the unsafe.Pointer of the original struct field named name.
// 2nd return value will be false if the original struct does not have a "name" field.
// 2nd return value will be false if type of the original struct "name" field is not unsafe.Pointer.
func (configuration Configuration) UnsafePointer(key string) (unsafe.Pointer, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.UnsafePointer(key)
}
// IsMap reports whether type of the original struct field named name is map.
func (configuration Configuration) IsMap(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsMap(key)
}
// IsFunc reports whether type of the original struct field named name is func.
func (configuration Configuration) IsFunc(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsFunc(key)
}
// IsChan reports whether type of the original struct field named name is chan.
func (configuration Configuration) IsChan(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsChan(key)
}
// IsStruct reports whether type of the original struct field named name is struct.
func (configuration Configuration) IsStruct(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsStruct(key)
}
// IsArray reports whether type of the original struct field named name is slice.
func (configuration Configuration) IsArray(key string) bool {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.IsArray(key)
}
func (configuration Configuration) GetGetter(key string) (*structil.Getter, bool) {
key, newGetter, _ := configuration.prepareConfigKey(key)
return newGetter.GetGetter(key)
}