forked from elastic/go-ucfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
584 lines (520 loc) · 14.4 KB
/
merge.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ucfg
import (
"fmt"
"reflect"
"regexp"
"time"
"unicode"
"unicode/utf8"
)
// Merge a map, a slice, a struct or another Config object into c.
//
// Merge traverses the value from recursively copying all values into a hierarchy
// of Config objects plus primitives into c.
//
// Merge supports the options: PathSep, MetaData, StructTag, VarExp, ReplaceValues, AppendValues, PrependValues
//
// Merge uses the type-dependent default encodings:
// - Boolean values are encoded as booleans.
// - Integer are encoded as int64 values, unsigned integer values as uint64 and
// floats as float64 values.
// - Strings are copied into string values.
// If the VarExp is set, string fields will be parsed into
// variable expansion expressions. The expression can reference any
// other setting by absolute name.
// - Array and slices are copied into new Config objects with index accessors only.
// - Struct values and maps with key type string are encoded as Config objects with
// named field accessors.
// - Config objects will be copied and added to the current hierarchy.
//
// The `config` struct tag (configurable via StructTag option) can be used to
// set the field name and enable additional merging settings per field:
//
// // field appears in Config as key "myName"
// Field int `config:"myName"`
//
// // field appears in sub-Config "mySub" as key "myName" (requires PathSep("."))
// Field int `config:"mySub.myName"`
//
// // field is processed as if keys are part of outer struct (type can be a
// // struct, a slice, an array, a map or of type *Config)
// Field map[string]interface{} `config:",inline"`
//
// // field is ignored by Merge
// Field string `config:",ignore"`
//
// Returns an error if merging fails to normalize and validate the from value.
// If duplicate setting names are detected in the input, merging fails as well.
//
// Config cannot represent cyclic structures and Merge does not handle them
// well. Passing cyclic structures to Merge will result in an infinite recursive
// loop.
func (c *Config) Merge(from interface{}, options ...Option) error {
// from is empty in case of empty config file
if from == nil {
return nil
}
opts := makeOptions(options)
other, err := normalize(opts, from)
if err != nil {
return err
}
return mergeConfig(opts, c, other)
}
func mergeConfig(opts *options, to, from *Config) Error {
if err := mergeConfigDict(opts, to, from); err != nil {
return err
}
return mergeConfigArr(opts, to, from)
}
func mergeConfigDict(opts *options, to, from *Config) Error {
dict := from.fields.dict()
if len(dict) == 0 {
return nil
}
ok := false
if opts.configValueHandling == cfgReplaceValue {
old := to.fields.dict()
to.fields.d = nil
defer func() {
if !ok {
to.fields.d = old
}
}()
}
for k, v := range dict {
ctx := context{
parent: cfgSub{to},
field: k,
}
old, _ := to.fields.get(k)
opts, err := fieldOptsOverride(opts, k, -1)
if err != nil {
return err
}
merged, err := mergeValues(opts, old, v)
if err != nil {
return err
}
to.fields.set(k, merged.cpy(ctx))
}
ok = true
return nil
}
func mergeConfigArr(opts *options, to, from *Config) Error {
currHandling := opts.configValueHandling
opts, err := fieldOptsOverride(opts, "*", -1)
if err != nil {
return err
}
switch currHandling {
case cfgReplaceValue, cfgArrReplaceValue:
return mergeConfigReplaceArr(opts, to, from)
case cfgArrPrepend:
return mergeConfigPrependArr(opts, to, from)
case cfgArrAppend:
return mergeConfigAppendArr(opts, to, from)
case cfgDefaultHandling, cfgMergeValues:
return mergeConfigMergeArr(opts, to, from)
default:
return mergeConfigMergeArr(opts, to, from)
}
}
func mergeConfigReplaceArr(opts *options, to, from *Config) Error {
a := from.fields.array()
if len(a) == 0 {
return nil
}
var parent value = cfgSub{to}
var fields = fields{
d: to.fields.d,
a: make([]value, 0, len(a)),
}
fields.append(parent, a)
*to.fields = fields
return nil
}
func mergeConfigMergeArr(opts *options, to, from *Config) Error {
l := len(to.fields.array())
arr := from.fields.array()
if l > len(arr) {
l = len(arr)
}
var parent value = cfgSub{to}
// merge array indexes available in to and from
for i := 0; i < l; i++ {
ctx := context{
parent: parent,
field: fmt.Sprintf("%v", i),
}
// possible for individual index to be replaced
idxOpts, err := fieldOptsOverride(opts, "", i)
if err != nil {
return err
}
old := to.fields.array()[i]
merged, err := mergeValues(idxOpts, old, arr[i])
if err != nil {
return err
}
to.fields.setAt(i, parent, merged.cpy(ctx))
}
if len(arr) > l {
// add additional array entries not yet in 'to'
to.fields.append(parent, arr[l:])
}
return nil
}
func mergeConfigPrependArr(opts *options, to, from *Config) Error {
a1 := to.fields.array()
a2 := from.fields.array()
if len(a2) == 0 {
return nil
}
var parent value = cfgSub{to}
var fields = fields{
d: to.fields.d,
a: make([]value, 0, len(a1)+len(a2)),
}
fields.append(parent, a2)
fields.append(parent, a1)
*to.fields = fields
return nil
}
func mergeConfigAppendArr(opts *options, to, from *Config) Error {
to.fields.append(cfgSub{to}, from.fields.array())
return nil
}
func mergeValues(opts *options, old, v value) (value, Error) {
if old == nil {
return v, nil
}
// check if new and old value evaluate to sub-configurations. If one is no
// sub-configuration, use new value only.
subOld, err := old.toConfig(opts)
if err != nil {
return v, nil
}
subV, err := v.toConfig(opts)
if err != nil {
return v, nil
}
// merge new and old evaluated sub-configurations and return subOld for
// reassigning to old key in case of subOld being generated dynamically
if err := mergeConfig(opts, subOld, subV); err != nil {
return nil, err
}
return cfgSub{subOld}, nil
}
// convert from into normalized *Config checking for errors
// before merging generated(normalized) config with current config
func normalize(opts *options, from interface{}) (*Config, Error) {
vFrom := chaseValue(reflect.ValueOf(from))
switch vFrom.Type() {
case tConfig:
return vFrom.Addr().Interface().(*Config), nil
case tConfigMap:
return normalizeMap(opts, vFrom)
default:
// try to convert vFrom into Config (rebranding)
if v, ok := tryTConfig(vFrom); ok {
return v.Addr().Interface().(*Config), nil
}
// normalize given map/struct value
switch vFrom.Kind() {
case reflect.Struct:
return normalizeStruct(opts, vFrom)
case reflect.Map:
return normalizeMap(opts, vFrom)
case reflect.Array, reflect.Slice:
tmp, err := normalizeArray(opts, tagOptions{}, context{}, vFrom)
if err != nil {
return nil, err
}
c, _ := tmp.toConfig(opts)
return c, nil
}
}
return nil, raiseInvalidTopLevelType(from, opts.meta)
}
func normalizeMap(opts *options, from reflect.Value) (*Config, Error) {
cfg := New()
cfg.metadata = opts.meta
if err := normalizeMapInto(cfg, opts, from); err != nil {
return nil, err
}
return cfg, nil
}
func normalizeMapInto(cfg *Config, opts *options, from reflect.Value) Error {
k := from.Type().Key().Kind()
if k != reflect.String && k != reflect.Interface {
return raiseKeyInvalidTypeMerge(cfg, from.Type())
}
for _, k := range from.MapKeys() {
k = chaseValueInterfaces(k)
if k.Kind() != reflect.String {
return raiseKeyInvalidTypeMerge(cfg, from.Type())
}
err := normalizeSetField(cfg, opts, noTagOpts, k.String(), from.MapIndex(k))
if err != nil {
return err
}
}
return nil
}
func normalizeStruct(opts *options, from reflect.Value) (*Config, Error) {
cfg := New()
cfg.metadata = opts.meta
if err := normalizeStructInto(cfg, opts, from); err != nil {
return nil, err
}
return cfg, nil
}
func normalizeStructInto(cfg *Config, opts *options, from reflect.Value) Error {
v := chaseValue(from)
numField := v.NumField()
for i := 0; i < numField; i++ {
var err Error
stField := v.Type().Field(i)
// ignore non exported fields
if rune, _ := utf8.DecodeRuneInString(stField.Name); !unicode.IsUpper(rune) {
continue
}
name, tagOpts := parseTags(stField.Tag.Get(opts.tag))
if tagOpts.ignore {
continue
}
if tagOpts.squash {
vField := chaseValue(v.Field(i))
switch vField.Kind() {
case reflect.Struct:
err = normalizeStructInto(cfg, opts, vField)
case reflect.Map:
err = normalizeMapInto(cfg, opts, vField)
default:
return raiseSquashNeedsObject(cfg, opts, stField.Name, vField.Type())
}
} else {
name = fieldName(name, stField.Name)
err = normalizeSetField(cfg, opts, tagOpts, name, v.Field(i))
}
if err != nil {
return err
}
}
return nil
}
func normalizeSetField(
cfg *Config,
opts *options,
tagOpts tagOptions,
name string,
v reflect.Value,
) Error {
val, err := normalizeValue(opts, tagOpts, context{}, v)
if err != nil {
return err
}
p := parsePathWithOpts(name, opts)
old, err := p.GetValue(cfg, opts)
if err != nil {
if err.Reason() != ErrMissing {
return err
}
old = nil
}
switch {
case !isNil(old) && isNil(val):
return nil
case isNil(old):
return p.SetValue(cfg, opts, val)
case isSub(old) && isSub(val):
cfgOld, _ := old.toConfig(opts)
cfgVal, _ := val.toConfig(opts)
return mergeConfig(opts, cfgOld, cfgVal)
default:
return raiseDuplicateKey(cfg, name)
}
}
func normalizeStructValue(opts *options, ctx context, from reflect.Value) (value, Error) {
sub, err := normalizeStruct(opts, from)
if err != nil {
return nil, err
}
v := cfgSub{sub}
v.SetContext(ctx)
return v, nil
}
func normalizeMapValue(opts *options, ctx context, from reflect.Value) (value, Error) {
sub, err := normalizeMap(opts, from)
if err != nil {
return nil, err
}
v := cfgSub{sub}
v.SetContext(ctx)
return v, nil
}
func normalizeArray(
opts *options,
tagOpts tagOptions,
ctx context,
v reflect.Value,
) (value, Error) {
l := v.Len()
out := make([]value, 0, l)
cfg := New()
cfg.metadata = opts.meta
cfg.ctx = ctx
val := cfgSub{cfg}
for i := 0; i < l; i++ {
idx := fmt.Sprintf("%v", i)
ctx := context{
parent: val,
field: idx,
}
tmp, err := normalizeValue(opts, tagOpts, ctx, v.Index(i))
if err != nil {
return nil, err
}
out = append(out, tmp)
}
cfg.fields.a = out
return val, nil
}
func normalizeValue(
opts *options,
tagOpts tagOptions,
ctx context,
v reflect.Value,
) (value, Error) {
v = chaseValue(v)
switch v.Type() {
case tDuration:
d := v.Interface().(time.Duration)
return newString(ctx, opts.meta, d.String()), nil
case tRegexp:
r := v.Addr().Interface().(*regexp.Regexp)
return newString(ctx, opts.meta, r.String()), nil
}
// handle primitives
switch v.Kind() {
case reflect.Bool:
return newBool(ctx, opts.meta, v.Bool()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i := v.Int()
if i > 0 {
return newUint(ctx, opts.meta, uint64(i)), nil
}
return newInt(ctx, opts.meta, i), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return newUint(ctx, opts.meta, v.Uint()), nil
case reflect.Float32, reflect.Float64:
f := v.Float()
return newFloat(ctx, opts.meta, f), nil
case reflect.String:
return normalizeString(ctx, opts, v.String())
case reflect.Array, reflect.Slice:
return normalizeArray(opts, tagOpts, ctx, v)
case reflect.Map:
return normalizeMapValue(opts, ctx, v)
case reflect.Struct:
if v, ok := tryTConfig(v); ok {
c := v.Addr().Interface().(*Config)
ret := cfgSub{c}
if ret.Context().parent != ctx.parent {
ret.SetContext(ctx)
}
return ret, nil
}
return normalizeStructValue(opts, ctx, v)
default:
if v.IsNil() {
return &cfgNil{cfgPrimitive{ctx, opts.meta}}, nil
}
return nil, raiseUnsupportedInputType(ctx, opts.meta, v)
}
}
func normalizeString(ctx context, opts *options, str string) (value, Error) {
if !opts.varexp {
return newString(ctx, opts.meta, str), nil
}
varexp, err := parseSplice(str, opts.pathSep, opts.maxIdx, opts.enableNumKeys)
if err != nil {
return nil, raiseParseSplice(ctx, opts.meta, err)
}
switch p := varexp.(type) {
case constExp:
return newString(ctx, opts.meta, string(p)), nil
case *reference:
return newRef(ctx, opts.meta, p), nil
}
return newSplice(ctx, opts.meta, varexp), nil
}
func fieldOptsOverride(opts *options, fieldName string, idx int) (*options, Error) {
if opts.fieldHandlingTree == nil {
return opts, nil
}
cfgHandling, child, ok := opts.fieldHandlingTree.fieldHandling(fieldName, idx)
child, err := includeWildcard(child, opts.fieldHandlingTree)
if err != nil {
return nil, err
}
if !ok {
// Only return a new `options` when arriving at new nested child. This
// combined with optimizations in `includeWildcard` will ensure that only
// a new opts will be created and returned when absolutely required.
if child != nil && opts.fieldHandlingTree != child {
newOpts := *opts
newOpts.fieldHandlingTree = child
opts = &newOpts
}
return opts, nil
}
// Only return a new `options` if absolutely required.
if opts.configValueHandling != cfgHandling || opts.fieldHandlingTree != child {
newOpts := *opts
newOpts.configValueHandling = cfgHandling
newOpts.fieldHandlingTree = child
opts = &newOpts
}
return opts, nil
}
func includeWildcard(child *fieldHandlingTree, parent *fieldHandlingTree) (*fieldHandlingTree, Error) {
if parent == nil {
return child, nil
}
wildcard, err := parent.wildcard()
if err != nil {
return child, nil
}
if child == nil && len(parent.fields.dict()) == 1 {
// parent is already config with just wildcard
return parent, nil
}
sub := newFieldHandlingTree()
if child != nil {
if err := sub.merge(child); err != nil {
return nil, err.(Error)
}
}
if err := sub.setWildcard(wildcard); err != nil {
return nil, err.(Error)
}
return sub, nil
}