/
gcfg_api.go
465 lines (409 loc) · 14.9 KB
/
gcfg_api.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
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcfg
import (
"errors"
"time"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/os/gtime"
)
// Set sets value with specified <pattern>.
// It supports hierarchical data access by char separator, which is '.' in default.
// It is commonly used for updates certain configuration value in runtime.
func (c *Config) Set(pattern string, value interface{}) error {
if j := c.getJson(); j != nil {
return j.Set(pattern, value)
}
return nil
}
// Get retrieves and returns value by specified <pattern>.
// It returns all values of current Json object if <pattern> is given empty or string ".".
// It returns nil if no value found by <pattern>.
//
// We can also access slice item by its index number in <pattern> like:
// "list.10", "array.0.name", "array.0.1.id".
//
// It returns a default value specified by <def> if value for <pattern> is not found.
func (c *Config) Get(pattern string, def ...interface{}) interface{} {
if j := c.getJson(); j != nil {
return j.Get(pattern, def...)
}
return nil
}
// GetVar returns a gvar.Var with value by given <pattern>.
func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var {
if j := c.getJson(); j != nil {
return j.GetVar(pattern, def...)
}
return gvar.New(nil)
}
// Contains checks whether the value by specified <pattern> exist.
func (c *Config) Contains(pattern string) bool {
if j := c.getJson(); j != nil {
return j.Contains(pattern)
}
return false
}
// GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{} {
if j := c.getJson(); j != nil {
return j.GetMap(pattern, def...)
}
return nil
}
// GetMapStrStr retrieves and returns the value by specified <pattern> as map[string]string.
func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]string {
if j := c.getJson(); j != nil {
return j.GetMapStrStr(pattern, def...)
}
return nil
}
// GetArray retrieves the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} {
if j := c.getJson(); j != nil {
return j.GetArray(pattern, def...)
}
return nil
}
// GetBytes retrieves the value by specified <pattern> and converts it to []byte.
func (c *Config) GetBytes(pattern string, def ...interface{}) []byte {
if j := c.getJson(); j != nil {
return j.GetBytes(pattern, def...)
}
return nil
}
// GetString retrieves the value by specified <pattern> and converts it to string.
func (c *Config) GetString(pattern string, def ...interface{}) string {
if j := c.getJson(); j != nil {
return j.GetString(pattern, def...)
}
return ""
}
// GetStrings retrieves the value by specified <pattern> and converts it to []string.
func (c *Config) GetStrings(pattern string, def ...interface{}) []string {
if j := c.getJson(); j != nil {
return j.GetStrings(pattern, def...)
}
return nil
}
// GetInterfaces is alias of GetArray.
// See GetArray.
func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{} {
if j := c.getJson(); j != nil {
return j.GetInterfaces(pattern, def...)
}
return nil
}
// GetBool retrieves the value by specified <pattern>,
// converts and returns it as bool.
// It returns false when value is: "", 0, false, off, nil;
// or returns true instead.
func (c *Config) GetBool(pattern string, def ...interface{}) bool {
if j := c.getJson(); j != nil {
return j.GetBool(pattern, def...)
}
return false
}
// GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 {
if j := c.getJson(); j != nil {
return j.GetFloat32(pattern, def...)
}
return 0
}
// GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 {
if j := c.getJson(); j != nil {
return j.GetFloat64(pattern, def...)
}
return 0
}
// GetFloats retrieves the value by specified <pattern> and converts it to []float64.
func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 {
if j := c.getJson(); j != nil {
return j.GetFloats(pattern, def...)
}
return nil
}
// GetInt retrieves the value by specified <pattern> and converts it to int.
func (c *Config) GetInt(pattern string, def ...interface{}) int {
if j := c.getJson(); j != nil {
return j.GetInt(pattern, def...)
}
return 0
}
// GetInt8 retrieves the value by specified <pattern> and converts it to int8.
func (c *Config) GetInt8(pattern string, def ...interface{}) int8 {
if j := c.getJson(); j != nil {
return j.GetInt8(pattern, def...)
}
return 0
}
// GetInt16 retrieves the value by specified <pattern> and converts it to int16.
func (c *Config) GetInt16(pattern string, def ...interface{}) int16 {
if j := c.getJson(); j != nil {
return j.GetInt16(pattern, def...)
}
return 0
}
// GetInt32 retrieves the value by specified <pattern> and converts it to int32.
func (c *Config) GetInt32(pattern string, def ...interface{}) int32 {
if j := c.getJson(); j != nil {
return j.GetInt32(pattern, def...)
}
return 0
}
// GetInt64 retrieves the value by specified <pattern> and converts it to int64.
func (c *Config) GetInt64(pattern string, def ...interface{}) int64 {
if j := c.getJson(); j != nil {
return j.GetInt64(pattern, def...)
}
return 0
}
// GetInts retrieves the value by specified <pattern> and converts it to []int.
func (c *Config) GetInts(pattern string, def ...interface{}) []int {
if j := c.getJson(); j != nil {
return j.GetInts(pattern, def...)
}
return nil
}
// GetUint retrieves the value by specified <pattern> and converts it to uint.
func (c *Config) GetUint(pattern string, def ...interface{}) uint {
if j := c.getJson(); j != nil {
return j.GetUint(pattern, def...)
}
return 0
}
// GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 {
if j := c.getJson(); j != nil {
return j.GetUint8(pattern, def...)
}
return 0
}
// GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 {
if j := c.getJson(); j != nil {
return j.GetUint16(pattern, def...)
}
return 0
}
// GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 {
if j := c.getJson(); j != nil {
return j.GetUint32(pattern, def...)
}
return 0
}
// GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 {
if j := c.getJson(); j != nil {
return j.GetUint64(pattern, def...)
}
return 0
}
// GetTime retrieves the value by specified <pattern> and converts it to time.Time.
func (c *Config) GetTime(pattern string, format ...string) time.Time {
if j := c.getJson(); j != nil {
return j.GetTime(pattern, format...)
}
return time.Time{}
}
// GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration {
if j := c.getJson(); j != nil {
return j.GetDuration(pattern, def...)
}
return 0
}
// GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time {
if j := c.getJson(); j != nil {
return j.GetGTime(pattern, format...)
}
return nil
}
// GetJson gets the value by specified <pattern>,
// and converts it to a un-concurrent-safe Json object.
func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json {
if j := c.getJson(); j != nil {
return j.GetJson(pattern, def...)
}
return nil
}
// GetJsons gets the value by specified <pattern>,
// and converts it to a slice of un-concurrent-safe Json object.
func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json {
if j := c.getJson(); j != nil {
return j.GetJsons(pattern, def...)
}
return nil
}
// GetJsonMap gets the value by specified <pattern>,
// and converts it to a map of un-concurrent-safe Json object.
func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjson.Json {
if j := c.getJson(); j != nil {
return j.GetJsonMap(pattern, def...)
}
return nil
}
// GetStruct retrieves the value by specified <pattern> and converts it to specified object
// <pointer>. The <pointer> should be the pointer to an object.
func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStruct(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetStructDeep does GetStruct recursively.
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetStructs converts any slice to given struct slice.
func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructs(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetStructsDeep converts any slice to given struct slice recursively.
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructsDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable.
// See gconv.MapToMap.
func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapToMap(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map
// variable recursively.
// See gconv.MapToMapDeep.
func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapToMapDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice
// variable.
// See gconv.MapToMaps.
func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapToMaps(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice
// variable recursively.
// See gconv.MapToMapsDeep.
func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapToMapsDeep(pattern, pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMap converts current Json object to map[string]interface{}.
// It returns nil if fails.
func (c *Config) ToMap() map[string]interface{} {
if j := c.getJson(); j != nil {
return j.ToMap()
}
return nil
}
// ToArray converts current Json object to []interface{}.
// It returns nil if fails.
func (c *Config) ToArray() []interface{} {
if j := c.getJson(); j != nil {
return j.ToArray()
}
return nil
}
// ToStruct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStruct(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructs(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructsDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMap(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMapDeep converts current Json object to specified map variable recursively.
// The parameter of <pointer> should be type of *map.
func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMapDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMaps(pointer, mapping...)
}
return errors.New("configuration not found")
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
func (c *Config) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapToMapsDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// Clear removes all parsed configuration files content cache,
// which will force reload configuration content from file.
func (c *Config) Clear() {
c.jsons.Clear()
}
// Dump prints current Json object with more manually readable.
func (c *Config) Dump() {
if j := c.getJson(); j != nil {
j.Dump()
}
}