-
Notifications
You must be signed in to change notification settings - Fork 55
/
config.go
297 lines (244 loc) · 6.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
/*
Package config is a go config management implement. support YAML,TOML,JSON,INI,HCL format.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/config
JSON format content example:
{
"name": "app",
"debug": false,
"baseKey": "value",
"age": 123,
"envKey": "${SHELL}",
"envKey1": "${NotExist|defValue}",
"map1": {
"key": "val",
"key1": "val1",
"key2": "val2"
},
"arr1": [
"val",
"val1",
"val2"
],
"lang": {
"dir": "res/lang",
"defLang": "en",
"allowed": {
"en": "val",
"zh-CN": "val2"
}
}
}
Usage please see example(more example please see examples folder in the lib):
*/
package config
import (
"fmt"
"sync"
)
// There are supported config format
const (
Ini = "ini"
Hcl = "hcl"
Yml = "yml"
JSON = "json"
Yaml = "yaml"
Toml = "toml"
Prop = "properties"
)
const (
// default delimiter
defaultDelimiter byte = '.'
// default struct tag name for binding data to struct
defaultStructTag = "mapstructure"
// struct tag name for set default-value on binding data
defaultValueTag = "default"
)
// internal vars
// type intArr []int
type strArr []string
// type intMap map[string]int
type strMap map[string]string
// This is a default config manager instance
var dc = New("default")
// Config structure definition
type Config struct {
// save the latest error, will clear after read.
err error
// config instance name
name string
lock sync.RWMutex
// config options
opts *Options
// all config data
data map[string]any
// loaded config files records
loadedUrls []string
loadedFiles []string
driverNames []string
// driver alias to name map.
aliasMap map[string]string
reloading bool
// TODO Deprecated decoder and encoder, use driver instead
// drivers map[string]Driver
// decoders["toml"] = func(blob []byte, v any) (err error){}
// decoders["yaml"] = func(blob []byte, v any) (err error){}
decoders map[string]Decoder
encoders map[string]Encoder
// cache on got config data
intCache map[string]int
strCache map[string]string
// iArrCache map[string]intArr TODO cache it
// iMapCache map[string]intMap
sArrCache map[string]strArr
sMapCache map[string]strMap
}
// New config instance with custom options, default with JSON driver
func New(name string, opts ...OptionFn) *Config {
return NewEmpty(name, opts...).WithDriver(JSONDriver)
}
// NewEmpty create config instance with custom options
func NewEmpty(name string, opts ...OptionFn) *Config {
c := &Config{
name: name,
opts: newDefaultOption(),
data: make(map[string]any),
// don't add any drivers
encoders: map[string]Encoder{},
decoders: map[string]Decoder{},
aliasMap: make(map[string]string),
}
return c.WithOptions(opts...)
}
// NewWith create config instance, and you can call some init func
func NewWith(name string, fn func(c *Config)) *Config {
return New(name).With(fn)
}
// NewWithOptions config instance. alias of New()
func NewWithOptions(name string, opts ...OptionFn) *Config {
return New(name).WithOptions(opts...)
}
// Default get the default instance
func Default() *Config { return dc }
/*************************************************************
* config drivers
*************************************************************/
// WithDriver set multi drivers at once.
func WithDriver(drivers ...Driver) { dc.WithDriver(drivers...) }
// WithDriver set multi drivers at once.
func (c *Config) WithDriver(drivers ...Driver) *Config {
for _, driver := range drivers {
c.AddDriver(driver)
}
return c
}
// AddDriver set a decoder and encoder driver for a format.
func AddDriver(driver Driver) { dc.AddDriver(driver) }
// AddDriver set a decoder and encoder driver for a format.
func (c *Config) AddDriver(driver Driver) {
format := driver.Name()
if len(driver.Aliases()) > 0 {
for _, alias := range driver.Aliases() {
c.aliasMap[alias] = format
}
}
c.driverNames = append(c.driverNames, format)
c.decoders[format] = driver.GetDecoder()
c.encoders[format] = driver.GetEncoder()
}
// HasDecoder has decoder
func (c *Config) HasDecoder(format string) bool {
format = c.resolveFormat(format)
_, ok := c.decoders[format]
return ok
}
// HasEncoder has encoder
func (c *Config) HasEncoder(format string) bool {
format = c.resolveFormat(format)
_, ok := c.encoders[format]
return ok
}
// DelDriver delete driver of the format
func (c *Config) DelDriver(format string) {
format = c.resolveFormat(format)
delete(c.decoders, format)
delete(c.encoders, format)
}
/*************************************************************
* helper methods
*************************************************************/
// Name get config name
func (c *Config) Name() string { return c.name }
// AddAlias add alias for a format(driver name)
func AddAlias(format, alias string) { dc.AddAlias(format, alias) }
// AddAlias add alias for a format(driver name)
//
// Example:
//
// config.AddAlias("ini", "conf")
func (c *Config) AddAlias(format, alias string) {
c.aliasMap[alias] = format
}
// AliasMap get alias map
func (c *Config) AliasMap() map[string]string { return c.aliasMap }
// Error get last error, will clear after read.
func (c *Config) Error() error {
err := c.err
c.err = nil
return err
}
// IsEmpty of the config
func (c *Config) IsEmpty() bool {
return len(c.data) == 0
}
// LoadedUrls get loaded urls list
func (c *Config) LoadedUrls() []string { return c.loadedUrls }
// LoadedFiles get loaded files name
func (c *Config) LoadedFiles() []string { return c.loadedFiles }
// DriverNames get loaded driver names
func (c *Config) DriverNames() []string { return c.driverNames }
// Reset data and caches
func Reset() { dc.ClearAll() }
// ClearAll data and caches
func ClearAll() { dc.ClearAll() }
// ClearAll data and caches
func (c *Config) ClearAll() {
c.ClearData()
c.ClearCaches()
c.aliasMap = make(map[string]string)
// options
c.opts.Readonly = false
}
// ClearData clear data
func (c *Config) ClearData() {
c.fireHook(OnCleanData)
c.data = make(map[string]any)
c.loadedUrls = []string{}
c.loadedFiles = []string{}
}
// ClearCaches clear caches
func (c *Config) ClearCaches() {
if c.opts.EnableCache {
c.intCache = nil
c.strCache = nil
c.sMapCache = nil
c.sArrCache = nil
}
}
/*************************************************************
* helper methods
*************************************************************/
// fire hook
func (c *Config) fireHook(name string) {
if c.opts.HookFunc != nil {
c.opts.HookFunc(name, c)
}
}
// record error
func (c *Config) addError(err error) {
c.err = err
}
// format and record error
func (c *Config) addErrorf(format string, a ...any) {
c.err = fmt.Errorf(format, a...)
}