-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurgical-update.go
345 lines (316 loc) · 8.69 KB
/
surgical-update.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
// Copyright © 2020 Herb Stahl <ghstahl@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// ViperEx adds some missing gap items from the awesome Viper project is a application configuration system.
package viperEx
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
const defaultKeyDelimiter = "."
func newChangeAllKeysToLowerCase(m map[string]interface{}) map[string]interface{} {
newMap := make(map[string]interface{})
for k, v := range m {
switch val := v.(type) {
case map[string]interface{}:
newMap[strings.ToLower(k)] = newChangeAllKeysToLowerCase(val)
case []interface{}:
var newSlice []interface{}
for _, item := range val {
if reflect.TypeOf(item).Kind() == reflect.Map {
newSlice = append(newSlice, newChangeAllKeysToLowerCase(item.(map[string]interface{})))
} else {
newSlice = append(newSlice, item)
}
}
newMap[strings.ToLower(k)] = newSlice
default:
newMap[strings.ToLower(k)] = val
}
}
return newMap
}
// WithEnvPrefix sets the prefix for environment variables
func WithEnvPrefix(envPrefix string) func(*ViperEx) error {
return func(v *ViperEx) error {
v.EnvPrefix = envPrefix + "_"
return nil
}
}
// WithDelimiter sets the delimiter for keys
func WithDelimiter(delimiter string) func(*ViperEx) error {
return func(v *ViperEx) error {
v.KeyDelimiter = delimiter
return nil
}
}
// New creates a new ViperEx instance with optional options
func New(allsettings map[string]interface{}, options ...func(*ViperEx) error) (*ViperEx, error) {
changeAllKeysToLowerCase(allsettings)
changeStringArrayToInterfaceArray(allsettings)
changeStringMapStringToStringMapInterface(allsettings)
viperEx := &ViperEx{
KeyDelimiter: defaultKeyDelimiter,
AllSettings: newChangeAllKeysToLowerCase(allsettings),
}
var err error
for _, option := range options {
err = option(viperEx)
if err != nil {
return nil, err
}
}
return viperEx, nil
}
// ViperEx type
type ViperEx struct {
KeyDelimiter string
AllSettings map[string]interface{}
EnvPrefix string
}
// UpdateFromEnv will find potential ENV candidates to merge in
func (ve *ViperEx) UpdateFromEnv() error {
potential := ve.getPotentialEnvVariables()
for key, value := range potential {
ve.UpdateDeepPath(key, value)
}
return nil
}
// Find will return the interface to the data if it exists
func (ve *ViperEx) Find(key string) interface{} {
lcaseKey := strings.ToLower(key)
path := strings.Split(lcaseKey, ve.KeyDelimiter)
lastKey := strings.ToLower(path[len(path)-1])
fmt.Println(lastKey)
path = path[0 : len(path)-1]
if len(lastKey) == 0 {
return nil
}
deepestEntity := ve.deepSearch(ve.AllSettings, path)
deepestMap, ok := deepestEntity.(map[string]interface{})
if ok {
return deepestMap[lastKey]
}
deepestArray, ok := deepestEntity.([]interface{})
if ok {
// lastKey has to be a num
idx, err := strconv.Atoi(lastKey)
if err == nil {
return deepestArray[idx]
}
}
return nil
}
// UpdateDeepPath will update the value if it exists
func (ve *ViperEx) UpdateDeepPath(key string, value interface{}) {
lcaseKey := strings.ToLower(key)
path := strings.Split(lcaseKey, ve.KeyDelimiter)
lastKey := strings.ToLower(path[len(path)-1])
path = path[0 : len(path)-1]
if len(lastKey) == 0 {
return
}
deepestEntity := ve.deepSearch(ve.AllSettings, path)
deepestMap, ok := deepestEntity.(map[string]interface{})
if ok {
// set innermost value
_, ok := deepestMap[lastKey]
if ok {
deepestMap[lastKey] = value
}
} else {
// is this an array
deepestArray, ok := deepestEntity.([]interface{})
if ok {
// lastKey has to be a num
idx, err := strconv.Atoi(lastKey)
if err == nil {
if idx < len(deepestArray) && idx >= 0 {
deepestArray[idx] = value
}
}
}
}
}
func (ve *ViperEx) getPotentialEnvVariables() map[string]string {
var result map[string]string
result = make(map[string]string)
for _, element := range os.Environ() {
var index = strings.Index(element, "=")
key := element[0:index]
// check for prefix
if len(ve.EnvPrefix) > 0 {
if strings.HasPrefix(key, ve.EnvPrefix) {
key = key[len(ve.EnvPrefix):]
}
}
value := element[index+1:]
if strings.Contains(key, ve.KeyDelimiter) {
result[key] = value
}
}
return result
}
func (ve *ViperEx) deepSearch(m map[string]interface{}, path []string) interface{} {
if len(path) == 0 {
return m
}
var currentPath string
var stepArray = false
var currentArray []interface{}
var currentEntity interface{}
for _, k := range path {
if len(currentPath) == 0 {
currentPath = k
} else {
currentPath = fmt.Sprintf("%v.%v", currentPath, k)
}
if stepArray {
idx, err := strconv.Atoi(k)
if err != nil {
log.Error().Err(err).Msgf("No such path exists, must be an array idx: %v", currentPath)
return nil
}
if len(currentArray) <= idx {
log.Error().Msgf("No such path exists: %v", currentPath)
return nil
}
m3, ok := currentArray[idx].(map[string]interface{})
if !ok {
log.Error().Msgf("No such path exists: %v, error in mapping to a map[string]interface{}", currentPath)
return nil
}
// continue search from here
m = m3
currentEntity = m
stepArray = false // don't support arrays of arrays
} else {
m2, ok := m[k]
if !ok {
// intermediate key does not exist
return nil
}
m3, ok := m2.(map[string]interface{})
if !ok {
// is this an array
m4, ok := m2.([]interface{})
if ok {
// continue search from here
currentArray = m4
currentEntity = currentArray
stepArray = true
m3 = nil
} else {
// intermediate key is a value
return nil
}
} else {
// continue search from here
m = m3
currentEntity = m
}
}
}
return currentEntity
}
// code copied from the viper project
// defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot
// of time.Duration values & string slices
func defaultDecoderConfig(output interface{}, opts ...viper.DecoderConfigOption) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
}
for _, opt := range opts {
opt(c)
}
return c
}
// Unmarshal to struct
func (ve *ViperEx) Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return decode(ve.AllSettings, defaultDecoderConfig(rawVal, opts...))
}
// A wrapper around mapstructure.Decode that mimics the WeakDecode functionality
func decode(input interface{}, config *mapstructure.DecoderConfig) error {
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}
func changeStringMapStringToStringMapInterface(m map[string]interface{}) {
var currentKeys []string
for key := range m {
currentKeys = append(currentKeys, key)
}
for _, key := range currentKeys {
vv, ok := m[key].(map[string]string)
if ok {
m2 := make(map[string]interface{})
for k, v := range vv {
m2[k] = v
}
m[key] = m2
} else {
v2, ok := m[key].(map[string]interface{})
if ok {
changeStringMapStringToStringMapInterface(v2)
}
}
}
}
func changeStringArrayToInterfaceArray(m map[string]interface{}) {
var currentKeys []string
for key := range m {
currentKeys = append(currentKeys, key)
}
for _, key := range currentKeys {
vv, ok := m[key].([]string)
if ok {
m2 := make([]interface{}, 0)
for idx := range vv {
v := vv[idx]
m2 = append(m2, &v)
}
m[key] = m2
} else {
v2, ok := m[key].(map[string]interface{})
if ok {
changeStringArrayToInterfaceArray(v2)
}
}
}
}
func changeAllKeysToLowerCase(m map[string]interface{}) {
var lcMap = make(map[string]interface{})
var currentKeys []string
for key, value := range m {
currentKeys = append(currentKeys, key)
lcMap[strings.ToLower(key)] = value
}
// delete original values
for _, k := range currentKeys {
delete(m, k)
}
// put the lowercase ones in the original map
for key, value := range lcMap {
m[key] = value
vMap, ok := value.(map[string]interface{})
if ok {
// if the current value is a map[string]interface{}, keep going
changeAllKeysToLowerCase(vMap)
}
}
}