-
Notifications
You must be signed in to change notification settings - Fork 54
/
value.go
388 lines (320 loc) · 8.57 KB
/
value.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package konfig
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/jinzhu/copier"
"github.com/spf13/cast"
)
const (
// TagKey is the tag key to unmarshal config values to bound value
TagKey = "konfig"
// KeySep is the separator for config keys
KeySep = "."
)
var (
// ErrIncorrectValue is the error thrown when trying to bind an invalid type to a config store
ErrIncorrectValue = errors.New("Bind takes a map[string]interface{} or a struct")
// ErrIncorrectStructValue is the error thrown when trying to bind a non struct value with the BindStructStrict method
ErrIncorrectStructValue = errors.New("BindStructStrict takes a struct")
)
type value struct {
s *S
v *atomic.Value
vt reflect.Type
mut *sync.Mutex
isMap bool
}
// Value returns the value bound to the root config store
func Value() interface{} {
return instance().Value()
}
// Bind binds a value to the root config store
func Bind(v interface{}) {
instance().Bind(v)
}
// BindStructStrict binds a value to the root config store and adds the exposed keys as strict keys
func BindStructStrict(v interface{}) {
instance().BindStructStrict(v)
}
// Value returns the value bound to the config store
func (c *S) Value() interface{} {
return c.v.v.Load()
}
// Bind binds a value (either a map[string]interface{} or a struct) to the config store.
// When config values are set on the config store, they are also set on the bound value.
func (c *S) Bind(v interface{}) {
var t = reflect.TypeOf(v)
var k = t.Kind()
// if it is neither a map nor a struct
if k != reflect.Map && k != reflect.Struct {
panic(ErrIncorrectValue)
}
// if it is a map check map[string]interface{}
if k == reflect.Map &&
(t.Key().Kind() != reflect.String || t.Elem().Kind() != reflect.Interface) {
panic(ErrIncorrectValue)
}
var val = &value{
s: c,
isMap: k == reflect.Map,
mut: &sync.Mutex{},
}
val.vt = t
// create a new pointer to the given value and store it
var atomicValue atomic.Value
var n = reflect.Zero(val.vt)
atomicValue.Store(n.Interface())
val.v = &atomicValue
c.v = val
}
// BindStructStrict binds a value (must a struct) to the config store and adds the exposed fields as strick keys.
func (c *S) BindStructStrict(v interface{}) {
var t = reflect.TypeOf(v)
var k = t.Kind()
// if it not a struct
if k != reflect.Struct {
panic(ErrIncorrectStructValue)
}
keys := getStructKeys(t, "")
c.Strict(keys...)
c.Bind(v)
}
func getStructKeys(t reflect.Type, prefix string) []string {
var keys []string
for i := 0; i < t.NumField(); i++ {
var fieldValue = t.Field(i)
var tag = fieldValue.Tag.Get(TagKey)
if tag == "-" {
continue
}
// use field name when konfig tag is not specified
if tag == "" {
tag = strings.ToLower(fieldValue.Name)
}
if fieldValue.Type.Kind() == reflect.Struct {
structKeys := getStructKeys(fieldValue.Type, tag+KeySep)
keys = append(keys, structKeys...)
// don't add the parent tag
continue
}
keys = append(keys, prefix+tag)
}
return keys
}
func (val *value) set(k string, v interface{}) {
val.mut.Lock()
defer val.mut.Unlock()
var configValue = val.v.Load()
// if value is a map
// store things in a map
if val.isMap {
var mapV = configValue.(map[string]interface{})
var nMap = make(map[string]interface{})
for kk, vv := range mapV {
nMap[kk] = vv
}
nMap[k] = v
val.v.Store(nMap)
return
}
// make a copy
var t = reflect.TypeOf(configValue)
var nVal = reflect.New(t)
copier.Copy(nVal.Interface(), configValue)
val.setStruct(k, v, nVal)
val.v.Store(nVal.Elem().Interface())
}
func (val *value) setValues(x s) {
val.mut.Lock()
defer val.mut.Unlock()
var configValue = val.v.Load()
// if value is a map
// store things in a map
if val.isMap {
var mapV = configValue.(map[string]interface{})
var nMap = make(map[string]interface{})
for kk, vv := range mapV {
nMap[kk] = vv
}
for kk, vv := range x {
nMap[kk] = vv
}
val.v.Store(nMap)
return
}
// make a copy
var t = reflect.TypeOf(configValue)
var nVal = reflect.New(t)
for kk, vv := range x {
val.setStruct(kk, vv, nVal)
}
val.v.Store(nVal.Elem().Interface())
}
func (val *value) setStruct(k string, v interface{}, targetValue reflect.Value) bool {
// is a struct, find matching tag
var valTypePtr = targetValue.Type()
var valType = valTypePtr.Elem()
var valValuePtr = targetValue
var valValue = valValuePtr.Elem()
var set bool
for i := 0; i < valType.NumField(); i++ {
var fieldValue = valType.Field(i)
var fieldName = fieldValue.Name
var tag = fieldValue.Tag.Get(TagKey)
// check tag, if it matches key
// assign v to field
if tag == k || strings.EqualFold(fieldName, k) {
var field = valValue.FieldByName(fieldValue.Name)
if field.CanSet() {
field.Set(reflect.ValueOf(castValue(field.Interface(), v)))
}
set = true
continue
// else if key has tag in prefix
} else if strings.HasPrefix(k, tag+KeySep) ||
strings.HasPrefix(strings.ToLower(k), strings.ToLower(fieldName)+KeySep) {
var nK string
if strings.HasPrefix(k, tag+KeySep) {
nK = k[len(tag+KeySep):]
} else {
nK = k[len(fieldName+KeySep):]
}
switch fieldValue.Type.Kind() {
// Is a map.
// Only map[string]someStruct is supported.
// The idea is to be able to store lists of key value where the keys are not known.
case reflect.Map:
var keyKind = fieldValue.Type.Key().Kind()
var eltKind = fieldValue.Type.Elem().Kind()
// if map key is a string and elem is a struct
// else we skip this field
if keyKind == reflect.String {
var structType reflect.Type
var ptr bool
if eltKind == reflect.Struct {
structType = fieldValue.Type.Elem()
} else if eltKind == reflect.Ptr && fieldValue.Type.Elem().Elem().Kind() == reflect.Struct {
structType = fieldValue.Type.Elem().Elem()
ptr = true
} else {
continue
}
var nVal = reflect.New(structType)
var field = valValue.FieldByName(fieldValue.Name)
// cut the key until the next sep
var keyElt = strings.SplitN(nK, KeySep, 2)
if len(keyElt) == 2 {
var mapKey = keyElt[0]
// check if map is nil, if yes create new one
var mapVal = field
if mapVal.IsNil() {
mapVal = reflect.MakeMap(fieldValue.Type)
field.Set(mapVal)
}
var mapKeyVal = reflect.ValueOf(mapKey)
var ov = mapVal.MapIndex(mapKeyVal)
// we copy the old value, to make sure we don't lose anything
if ov.IsValid() {
copier.Copy(nVal.Interface(), ov.Interface())
}
// we set the field with the new struct
if ok := val.setStruct(
keyElt[1],
v,
nVal,
); ok {
if !ptr {
mapVal.SetMapIndex(mapKeyVal, nVal.Elem())
} else {
mapVal.SetMapIndex(mapKeyVal, nVal)
}
set = true
}
}
continue
}
case reflect.Struct:
var field = valValue.FieldByName(fieldValue.Name)
// if field can be set
if field.CanSet() {
var structType = field.Type()
var nVal = reflect.New(structType)
// we copy it
copier.Copy(nVal.Interface(), field.Interface())
// we set the field with the new struct
if ok := val.setStruct(nK, v, nVal); ok {
field.Set(nVal.Elem())
set = true
}
continue
}
case reflect.Ptr:
if fieldValue.Type.Elem().Kind() == reflect.Struct {
var field = valValue.FieldByName(fieldValue.Name)
if field.CanSet() {
var nVal = reflect.New(fieldValue.Type.Elem())
// if field is not nil
// we copy it
if !field.IsNil() {
copier.Copy(nVal.Interface(), field.Interface())
}
if ok := val.setStruct(nK, v, nVal); ok {
field.Set(nVal)
set = true
}
continue
}
}
}
}
}
if !set {
val.s.cfg.Logger.Get().Debug(
fmt.Sprintf(
"Config key %s not found in bound value",
k,
),
)
}
return set
}
func castValue(f interface{}, v interface{}) interface{} {
switch f.(type) {
case string:
return cast.ToString(v)
case bool:
return cast.ToBool(v)
case int:
return cast.ToInt(v)
case int64:
return cast.ToInt64(v)
case int32:
return cast.ToInt32(v)
case float64:
return cast.ToFloat64(v)
case float32:
return cast.ToFloat32(v)
case uint64:
return cast.ToUint64(v)
case uint32:
return cast.ToUint32(v)
case uint8:
return cast.ToUint8(v)
case []string:
return cast.ToStringSlice(v)
case []int:
return cast.ToIntSlice(v)
case time.Time:
return cast.ToTime(v)
case time.Duration:
return cast.ToDuration(v)
case map[string]string:
return cast.ToStringMapString(v)
}
return v
}