-
Notifications
You must be signed in to change notification settings - Fork 54
/
util.go
335 lines (278 loc) · 9.34 KB
/
util.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
package konfig
import (
"fmt"
"time"
"github.com/spf13/cast"
)
type s map[string]interface{}
// exists returns a boolean indicating if the key exists in the map
func (m s) exists(k string) bool {
_, ok := m[k]
return ok
}
// checkStrictKeys checks if the given keys are present in the map. If a key is not
// found checkStrictKeys returns a non nil error.
func (m s) checkStrictKeys(keys []string) error {
for _, k := range keys {
if !m.exists(k) {
return fmt.Errorf(ErrStrictKeyNotFoundMsg, k)
}
}
return nil
}
// Exists checks if a config key k is set in the Store
func Exists(k string) bool {
return instance().Exists(k)
}
// Exists checks if a config key k is set in the Store
func (c *S) Exists(k string) bool {
var m = c.m.Load().(s)
_, ok := m[k]
return ok
}
// Get will return the value in config with given key k
// If not value is found, Get it returns nil
func Get(k string) interface{} {
return instance().Get(k)
}
// MustGet returns the value in config with given key k
// If not found it panics
func MustGet(k string) interface{} {
return instance().MustGet(k)
}
// Set will set the key value to the sync.Map
func Set(k string, v interface{}) {
instance().Set(k, v)
}
// Set sets a value in config
func (c *S) Set(k string, v interface{}) {
c.mut.Lock()
defer c.mut.Unlock()
var m = c.m.Load().(s)
var nm = make(s)
for kk, vv := range m {
nm[kk] = vv
}
nm[k] = v
// if there is a value bound we set it there also
if c.v != nil {
c.v.set(k, v)
}
c.m.Store(nm)
}
// Get gets a value from config
func (c *S) Get(k string) interface{} {
var m = c.m.Load().(s)
if v, ok := m[k]; ok {
return v
}
return nil
}
// MustGet gets a value from config and panics if the value does not exist
func (c *S) MustGet(k string) interface{} {
var m = c.m.Load().(s)
if v, ok := m[k]; ok {
return v
}
panic(fmt.Errorf(ErrConfigNotFoundMsg, k))
}
// MustInt gets the config k and tries to convert it to an int
// it panics if the config does not exist or it fails to convert it to an int.
func MustInt(k string) int {
return instance().MustInt(k)
}
// MustInt gets the config k and tries to convert it to an int
// it panics if the config does not exist or it fails to convert it to an int.
func (c *S) MustInt(k string) int {
return cast.ToInt(c.MustGet(k))
}
// Int gets the config k and tries to convert it to an int
// It returns the zero value if it doesn't find the config.
func Int(k string) int {
return instance().Int(k)
}
// Int gets the config k and tries to convert it to an int
// It returns the zero value if it doesn't find the config.
func (c *S) Int(k string) int {
return cast.ToInt(c.Get(k))
}
// MustFloat gets the config k and tries to convert it to a float64
// it panics if it fails.
func MustFloat(k string) float64 {
return instance().MustFloat(k)
}
// MustFloat gets the config k and tries to convert it to a float64
// it panics if it fails.
func (c *S) MustFloat(k string) float64 {
return cast.ToFloat64(c.MustGet(k))
}
// Float gets the config k and tries to convert it to float64
// It returns the zero value if it doesn't find the config.
func Float(k string) float64 {
return instance().Float(k)
}
// Float gets the config k and tries to convert it to float64
// It returns the zero value if it doesn't find the config.
func (c *S) Float(k string) float64 {
return cast.ToFloat64(c.Get(k))
}
// MustString gets the config k and tries to convert it to a string
// it panics if it fails.
func MustString(k string) string {
return instance().MustString(k)
}
// MustString gets the config k and tries to convert it to a string
// it panics if it fails.
func (c *S) MustString(k string) string {
return cast.ToString(c.MustGet(k))
}
// String gets the config k and tries to convert it to a string
// It returns the zero value if it doesn't find the config.
func String(k string) string {
return instance().String(k)
}
// String gets the config k and tries to convert it to a string
// It returns the zero value if it doesn't find the config.
func (c *S) String(k string) string {
return cast.ToString(c.Get(k))
}
// MustBool gets the config k and tries to convert it to a bool
// it panics if it fails.
func MustBool(k string) bool {
return instance().MustBool(k)
}
// MustBool gets the config k and tries to convert it to a bool
// it panics if it fails.
func (c *S) MustBool(k string) bool {
return cast.ToBool(c.MustGet(k))
}
// Bool gets the config k and converts it to a bool.
// It returns the zero value if it doesn't find the config.
func Bool(k string) bool {
return instance().Bool(k)
}
// Bool gets the config k and converts it to a bool.
// It returns the zero value if it doesn't find the config.
func (c *S) Bool(k string) bool {
return cast.ToBool(c.Get(k))
}
// MustDuration gets the config k and tries to convert it to a duration
// it panics if it fails.
func MustDuration(k string) time.Duration {
return instance().MustDuration(k)
}
// MustDuration gets the config k and tries to convert it to a duration
// it panics if it fails.
func (c *S) MustDuration(k string) time.Duration {
return cast.ToDuration(c.MustGet(k))
}
// Duration gets the config k and converts it to a duration.
// It returns the zero value if it doesn't find the config.
func Duration(k string) time.Duration {
return instance().Duration(k)
}
// Duration gets the config k and converts it to a duration.
// It returns the zero value if it doesn't find the config.
func (c *S) Duration(k string) time.Duration {
return cast.ToDuration(c.Get(k))
}
// MustTime gets the config k and tries to convert it to a time.Time
// it panics if it fails.
func MustTime(k string) time.Time {
return instance().MustTime(k)
}
// MustTime gets the config k and tries to convert it to a time.Time
// it panics if it fails.
func (c *S) MustTime(k string) time.Time {
return cast.ToTime(c.MustGet(k))
}
// Time gets the config k and converts it to a time.Time.
// It returns the zero value if it doesn't find the config.
func Time(k string) time.Time {
return instance().Time(k)
}
// Time gets the config k and converts it to a time.Time.
// It returns the zero value if it doesn't find the config.
func (c *S) Time(k string) time.Time {
return cast.ToTime(c.Get(k))
}
// MustStringSlice gets the config k and tries to convert it to a []string
// it panics if it fails.
func MustStringSlice(k string) []string {
return instance().MustStringSlice(k)
}
// MustStringSlice gets the config k and tries to convert it to a []string
// it panics if it fails.
func (c *S) MustStringSlice(k string) []string {
return cast.ToStringSlice(c.MustGet(k))
}
// StringSlice gets the config k and converts it to a []string.
// It returns the zero value if it doesn't find the config.
func StringSlice(k string) []string {
return instance().StringSlice(k)
}
// StringSlice gets the config k and converts it to a []string.
// It returns the zero value if it doesn't find the config.
func (c *S) StringSlice(k string) []string {
return cast.ToStringSlice(c.Get(k))
}
// MustIntSlice gets the config k and tries to convert it to a []int
// it panics if it fails.
func MustIntSlice(k string) []int {
return instance().MustIntSlice(k)
}
// MustIntSlice gets the config k and tries to convert it to a []int
// it panics if it fails.
func (c *S) MustIntSlice(k string) []int {
return cast.ToIntSlice(c.MustGet(k))
}
// IntSlice gets the config k and converts it to a []int.
// it returns the zero value if it doesn't find the config.
func IntSlice(k string) []int {
return instance().IntSlice(k)
}
// IntSlice gets the config k and converts it to a []int.
// it returns the zero value if it doesn't find the config.
func (c *S) IntSlice(k string) []int {
return cast.ToIntSlice(c.Get(k))
}
// MustStringMap gets the config k and tries to convert it to a map[string]interface{}
// it panics if it fails.
func MustStringMap(k string) map[string]interface{} {
return instance().MustStringMap(k)
}
// MustStringMap gets the config k and tries to convert it to a map[string]interface{}
// it panics if it fails.
func (c *S) MustStringMap(k string) map[string]interface{} {
return cast.ToStringMap(c.MustGet(k))
}
// StringMap gets the config k and converts it to a map[string]interface{}.
// it returns the zero value if it doesn't find the config.
func StringMap(k string) map[string]interface{} {
return instance().StringMap(k)
}
// StringMap gets the config k and converts it to a map[string]interface{}.
// it returns the zero value if it doesn't find the config.
func (c *S) StringMap(k string) map[string]interface{} {
return cast.ToStringMap(c.Get(k))
}
// MustStringMapString gets the config k and tries to convert it to a map[string]string
// it panics if it fails.
func MustStringMapString(k string) map[string]string {
return instance().MustStringMapString(k)
}
// MustStringMapString gets the config k and tries to convert it to a map[string]string
// it panics if it fails.
func (c *S) MustStringMapString(k string) map[string]string {
return cast.ToStringMapString(c.MustGet(k))
}
// StringMapString gets the config k and converts it to a map[string]string.
// it returns the zero value if it doesn't find the config.
func StringMapString(k string) map[string]string {
return instance().StringMapString(k)
}
// StringMapString gets the config k and converts it to a map[string]string.
// it returns the zero value if it doesn't find the config.
func (c *S) StringMapString(k string) map[string]string {
return cast.ToStringMapString(c.Get(k))
}