-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
291 lines (262 loc) · 6.23 KB
/
params.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
package dyanmic_params
import (
"errors"
"strings"
"sync"
"time"
)
type DynamicParams struct {
Mx *sync.RWMutex
source ParamsSource
}
const (
ErrNotFound = "not found"
)
// Returns a new instance of DynamicParams
//
// source is the name of the source to use, available sources are:
// - SrcNameInternal
// - SrcNameArgs
//
// If you want to have DynamicParams concurrent safe, you must pass
// a *sync.Mutex{} as first argument in vars...
//
// Example: To create a new instance with SrnNameArgs, do this:
// NewDynamicParams(SrcNameArgs, &mx, os.Args)
// vars... it is a list of extra parameters that a source might need. For example,
// for SrcNameArgs, you need to pass os.Args (or an array of string you want to treat
// as list of arguments)
func NewDynamicParams(source string, vars ...interface{}) *DynamicParams {
return createDP(source, vars...)
}
func createDP(source string, vars ...interface{}) *DynamicParams {
var mx *sync.RWMutex
var varsNew []interface{}
if len(vars) > 0 {
if v, ok := vars[0].(*sync.RWMutex); ok {
mx = v
varsNew = make([]interface{}, len(vars)-1)
for i := 0; i < len(vars); i++ {
if i == 0 {
continue
}
varsNew = append(varsNew, vars[i])
}
} else {
varsNew = vars
}
}
return &DynamicParams{
Mx: mx,
source: NewSource(source, varsNew...),
}
}
// adds a key and value to the active underlying source
func (c *DynamicParams) Set(name string, value interface{}) *DynamicParams {
if c.Mx != nil {
c.Mx.Lock()
defer c.Mx.Unlock()
}
c.source.Add(name, value)
return c
}
// Checks to see if the value exists in the underlying source
func (c *DynamicParams) Has(name string) bool {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
return c.source.Has(name)
}
// returns the raw value, if exists, and if not found, returns nil
// this function is useful for storing struct and custom compound types
func (c *DynamicParams) Get(name string) interface{} {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
return c.source.Get(name)
}
func (c *DynamicParams) Scan(regex string) map[string]interface{} {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
return c.source.Scan(regex)
}
func (c *DynamicParams) Count() int64 {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
return c.source.Count()
}
// This function is not concurrent safe
// If you need to have concurrency and do locking over this usage,
// you must use your own function for iteration and handle that there.
func (c *DynamicParams) Iterate(fn func(key string, value interface{})) {
c.source.Iterate(fn)
}
// Casts the existing value to string and then returns it
// It tries to convert value of interface{} type to string
// and if it fails, it returns error
func (c *DynamicParams) GetAsString(name string) (string, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return "", errors.New(ErrNotFound)
}
return convertToString(v)
}
func (c *DynamicParams) GetAsBytes(name string) ([]byte, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return nil, errors.New(ErrNotFound)
}
return convertToBytes(v)
}
// this method removes any surrounding quotation marks (only surrounding)
func (c *DynamicParams) GetAsQuotedString(name string) (string, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return "", errors.New(ErrNotFound)
}
s, err := convertToString(v)
if err != nil {
return "", err
}
return strings.Trim(strings.Trim(s, "'"), "\""), nil
}
func (c *DynamicParams) GetAsInt(name string) (int, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertToInt(v)
}
func (c *DynamicParams) GetStringAsInt(name string) (int, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertNumericStrToInt(v)
}
// parses a string using time.ParseDuration() function
func (c *DynamicParams) GetStringAsTimeDuration(name string) (*time.Duration, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return nil, errors.New(ErrNotFound)
}
vs, err := convertToString(v)
if err != nil {
return nil, err
}
vd, err := time.ParseDuration(vs)
if err != nil {
return nil, errors.New(ErrCnvFailed)
}
return &vd, nil
}
// if string has these values: 0, 1, true or false,
// then this method converts them bool type and then returns
// the value
func (c *DynamicParams) GetStringAsBool(name string) (bool, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return false, errors.New(ErrNotFound)
}
return convertNumericStrToBool(v)
}
func (c *DynamicParams) GetAsInt32(name string) (int32, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertToInt32(v)
}
func (c *DynamicParams) GetAsInt64(name string) (int64, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertToInt64(v)
}
func (c *DynamicParams) GetAsInt8(name string) (int8, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertToInt8(v)
}
func (c *DynamicParams) GetAsInt16(name string) (int16, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return 0, errors.New(ErrNotFound)
}
return convertToInt16(v)
}
func (c *DynamicParams) GetAsTimeDuration(name string) (*time.Duration, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return nil, errors.New(ErrNotFound)
}
return convertToTimeDuration(v)
}
func (c *DynamicParams) GetAsBool(name string) (bool, error) {
if c.Mx != nil {
c.Mx.RLock()
defer c.Mx.RUnlock()
}
v := c.source.Get(name)
if v == nil {
return false, errors.New(ErrNotFound)
}
return convertToBool(v)
}