-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.go
72 lines (68 loc) · 2.08 KB
/
apply.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
package L
// ApplyOpts dictates how the application will occur.
//
// ApplyOpts is used as an argument to *Config.Apply,
// for example in the call `trg.Apply(mod, &ApplyOpts{...})`.
// In the following, we call 'trg' the target configurations
// and 'mod' the modifying configuration.
type ApplyOpts struct {
// whether to apply recursively to child loggers.
Recursive bool `json:"recursive,omitempty"`
// labels whose values are carried over to the result, independent
// of the setting of RemoveAbsentLabels or whether a label is in
// the key set of the labels of the modifying config (in Go, whether or
// or not 'ok' is true after calling '_, ok := mod.Labels[label]').
PreserveLabels map[string]bool `json:"preserveLabels,omitempty"`
// If true, the labels not in the modifying config are removed
// from the target config, unless they are specified in PreservedLabels
// above.
RemoveAbsentLabels bool `json:"removeAbsentLabels,omitempty"`
}
// Apply applies the configuration o to c. Fields are copied over if they are
// not nil in o, otherwise left untouched. Labels in o should not include the
// package name, but if they start with '.', they are expanded with the package
// name of 'c' when copied to c's Labels.
//
// if a label in 'c', with any package name stripped, is not in o, then
// it may be removed from c or preserved, according to 'opts'.
func (c *Config) Apply(o *Config, opts *ApplyOpts) {
if opts == nil {
opts = &ApplyOpts{}
}
if o.E != nil {
c.E = o.E
}
if o.W != nil {
c.W = o.W
}
if o.F != nil {
c.F = o.F
}
if o.Pre != nil {
c.Pre = append([]Middleware{}, o.Pre...)
}
if o.Post != nil {
c.Post = append([]Middleware{}, o.Post...)
}
if o.Labels == nil {
return
}
if c.Labels == nil {
c.Labels = make(map[string]int, len(o.Labels))
}
if opts.RemoveAbsentLabels {
for k := range c.Labels {
if _, ok := o.Labels[c.Localize(k)]; !ok {
if _, ok := opts.PreserveLabels[c.Localize(k)]; !ok {
delete(c.Labels, k)
}
}
}
}
for k, v := range o.Labels {
if opts.PreserveLabels[k] {
continue
}
c.Labels[c.Unlocalize(k)] = v
}
}