-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyConfig.go
285 lines (243 loc) · 7.21 KB
/
easyConfig.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
package easyConfig
import (
"encoding/json"
"github.com/voyager-hang/go-easy-config/cast"
"github.com/voyager-hang/go-easy-config/file_conf"
"github.com/voyager-hang/go-easy-config/nacos_conf"
"github.com/voyager-hang/go-easy-config/tool"
"reflect"
"strings"
"time"
)
type ConfigType string
const (
ConfigTypeFile ConfigType = "file"
ConfigTypeNacos ConfigType = "nacos"
)
type EasyConfInter interface {
Load() error
GetConfig() map[string]interface{}
}
type NacosConf struct {
}
type EasyConfig struct {
configType ConfigType
FileConf file_conf.FileConfBox
NacosConf nacos_conf.ConfBox
confObj EasyConfInter
nacosConfObj *nacos_conf.NacosConf
fileConfObj *file_conf.FileConf
config map[string]interface{}
}
func New(confType ...ConfigType) *EasyConfig {
ec := new(EasyConfig)
ct := ConfigTypeFile
if len(confType) > 0 {
ct = confType[0]
}
ec.configType = ct
ec.config = make(map[string]interface{})
return ec
}
func (ec *EasyConfig) SetType(confType ConfigType) {
ec.configType = confType
}
func (ec *EasyConfig) SetFileConf(fc file_conf.FileConfBox) {
ec.FileConf = fc
}
func (ec *EasyConfig) SetNacosConf(nc nacos_conf.ConfBox) {
ec.NacosConf = nc
}
func (ec *EasyConfig) Load() error {
switch ec.configType {
case ConfigTypeFile:
f := file_conf.New()
if ec.FileConf.ConfigPaths != nil {
f.AddConfigPaths(ec.FileConf.ConfigPaths...)
}
if ec.FileConf.ConfigName != nil {
f.AddConfigName(ec.FileConf.ConfigName...)
}
if ec.FileConf.ConfigExt != nil {
f.AddConfigExt(ec.FileConf.ConfigExt...)
}
ec.confObj = f
ec.fileConfObj = f
case ConfigTypeNacos:
n := nacos_conf.New()
c := ec.NacosConf
n.Host = c.Host
n.HostYaml = c.HostYaml
n.ConfInfo = c.ConfInfo
n.TimeoutMs = c.TimeoutMs
n.NotLoadCacheAtStart = c.NotLoadCacheAtStart
n.LogDir = c.LogDir
n.CacheDir = c.CacheDir
n.LogLevel = c.LogLevel
ec.confObj = n
ec.nacosConfObj = n
}
err := ec.confObj.Load()
if err != nil {
return err
}
if ec.configType == ConfigTypeNacos {
ec.NacosConf.Host = ec.nacosConfObj.Host
ec.NacosConf.DefNamespace = ec.nacosConfObj.DefNamespace
}
ec.config = ec.confObj.GetConfig()
return nil
}
func (ec *EasyConfig) Find(key string) *EasyConfig {
findV := ec
data := ec.Get(key)
if data == nil {
return nil
}
if reflect.TypeOf(data).Kind() == reflect.Map {
findV.config = cast.ToStringMap(data)
return findV
}
return nil
}
func (ec *EasyConfig) Get(key string) interface{} {
return ec.find(key, true)
}
func (ec *EasyConfig) find(key string, flagDefault bool) interface{} {
var (
val interface{}
path = strings.Split(key, ".")
)
// Set() override first
val = ec.searchMap(ec.config, path)
return val
}
func (ec *EasyConfig) searchMap(source map[string]interface{}, path []string) interface{} {
if len(path) == 0 {
return source
}
next, ok := source[path[0]]
if ok {
// Fast path
if len(path) == 1 {
return next
}
// Nested case
switch next.(type) {
case map[interface{}]interface{}:
return ec.searchMap(cast.ToStringMap(next), path[1:])
case map[string]interface{}:
// Type assertion is safe here since it is only reached
// if the type of `next` is the same as the type being asserted
return ec.searchMap(next.(map[string]interface{}), path[1:])
default:
// got a value but nested key expected, return "nil" for not found
return nil
}
}
return nil
}
func (ec *EasyConfig) IsSet(key string) bool {
val := ec.find(key, false)
return val != nil
}
func (ec *EasyConfig) Set(key string, value interface{}) {
// If alias passed in, then set the proper override
value = tool.ToCaseInsensitiveValue(value)
path := strings.Split(key, ".")
lastKey := path[len(path)-1]
deepestMap := tool.DeepSearch(ec.config, path[0:len(path)-1])
// set innermost value
deepestMap[lastKey] = value
}
func (ec *EasyConfig) AllKeys() []string {
return ec.getAllKey(ec.config, "")
}
func (ec *EasyConfig) getAllKey(data map[string]interface{}, key string) []string {
keyArr := []string{}
for k, v := range data {
if key != "" {
k = key + "." + k
}
keyArr = append(keyArr, k)
vm, ok := v.(map[string]interface{})
if ok {
keyArr = append(keyArr, ec.getAllKey(vm, k)...)
}
}
return keyArr
}
func (ec *EasyConfig) GetAll() map[string]interface{} {
return ec.config
}
// GetString returns the value associated with the key as a string.
func (ec *EasyConfig) GetString(key string) string {
return cast.ToString(ec.Get(key))
}
// GetBool returns the value associated with the key as a boolean.
func (ec *EasyConfig) GetBool(key string) bool {
return cast.ToBool(ec.Get(key))
}
// GetInt returns the value associated with the key as an integer.
func (ec *EasyConfig) GetInt(key string) int {
return cast.ToInt(ec.Get(key))
}
// GetInt32 returns the value associated with the key as an integer.
func (ec *EasyConfig) GetInt32(key string) int32 {
return cast.ToInt32(ec.Get(key))
}
// GetInt64 returns the value associated with the key as an integer.
func (ec *EasyConfig) GetInt64(key string) int64 {
return cast.ToInt64(ec.Get(key))
}
// GetUint returns the value associated with the key as an unsigned integer.
func (ec *EasyConfig) GetUint(key string) uint {
return cast.ToUint(ec.Get(key))
}
// GetUint16 returns the value associated with the key as an unsigned integer.
func (ec *EasyConfig) GetUint16(key string) uint16 {
return cast.ToUint16(ec.Get(key))
}
// GetUint32 returns the value associated with the key as an unsigned integer.
func (ec *EasyConfig) GetUint32(key string) uint32 {
return cast.ToUint32(ec.Get(key))
}
// GetUint64 returns the value associated with the key as an unsigned integer.
func (ec *EasyConfig) GetUint64(key string) uint64 {
return cast.ToUint64(ec.Get(key))
}
// GetFloat64 returns the value associated with the key as a float64.
func (ec *EasyConfig) GetFloat64(key string) float64 {
return cast.ToFloat64(ec.Get(key))
}
// GetTime returns the value associated with the key as time.
func (ec *EasyConfig) GetTime(key string) time.Time {
return cast.ToTime(ec.Get(key))
}
// GetDuration returns the value associated with the key as a duration.
func (ec *EasyConfig) GetDuration(key string) time.Duration {
return cast.ToDuration(ec.Get(key))
}
// GetIntSlice returns the value associated with the key as a slice of int values.
func (ec *EasyConfig) GetIntSlice(key string) []int {
return cast.ToIntSlice(ec.Get(key))
}
// GetStringSlice returns the value associated with the key as a slice of strings.
func (ec *EasyConfig) GetStringSlice(key string) []string {
return cast.ToStringSlice(ec.Get(key))
}
// GetStringMap returns the value associated with the key as a map of interfaces.
func (ec *EasyConfig) GetStringMap(key string) map[string]interface{} {
return cast.ToStringMap(ec.Get(key))
}
// GetStringMapString returns the value associated with the key as a map of strings.
func (ec *EasyConfig) GetStringMapString(key string) map[string]string {
return cast.ToStringMapString(ec.Get(key))
}
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (ec *EasyConfig) GetStringMapStringSlice(key string) map[string][]string {
return cast.ToStringMapStringSlice(ec.Get(key))
}
func (ec *EasyConfig) ToJson() ([]byte, error) {
return json.Marshal(ec.config)
}