forked from lalamove/konfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
264 lines (222 loc) · 6.73 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
package konfig
import (
"fmt"
"time"
"github.com/spf13/cast"
)
type s map[string]interface{}
// Exists checks if a config key k is set in the Store
func Exists(k string) bool {
return instance().Exists(k)
}
func (c *store) 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 *store) 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 *store) 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 *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) 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)
}
func (c *store) StringMapString(k string) map[string]string {
return cast.ToStringMapString(c.Get(k))
}