-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.go
106 lines (84 loc) · 2.8 KB
/
updater.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
package proteus
import (
"fmt"
"github.com/simplesurance/proteus/plog"
"github.com/simplesurance/proteus/sources"
"github.com/simplesurance/proteus/types"
)
// updater mediates the communication between a parameter source and the
// parsed values. See sources.Updater for details.
type updater struct {
parsed *Parsed
// providerIndex identifies the position on Parsed where values from this
// provider should go. Parsed holds values for all providers in a
// slice, this is the providerIndex for that slice.
providerIndex int
providerName string
updatesEnabled chan struct{} // close this to allow updates
}
var _ sources.Updater = &updater{}
func (u *updater) Update(v types.ParamValues) {
// this is for proteus to delay updates until everything gets initialized
<-u.updatesEnabled
u.update(v, true)
}
func (u *updater) Log(entry plog.Entry) {
entry.Message = u.providerName + ": " + entry.Message
u.parsed.settings.loggerFn(entry)
}
func (u *updater) Peek(setName, paramName string) (*string, error) {
u.parsed.protected.valuesMutex.Lock()
defer u.parsed.protected.valuesMutex.Unlock()
if u.providerIndex == 0 {
return nil, fmt.Errorf("first provider can't peek values from providers before it")
}
for _, provData := range u.parsed.protected.values[:u.providerIndex] {
ret := provData.Get(setName, paramName)
if ret != nil {
return ret, nil
}
}
return nil, nil
}
func (u *updater) update(v types.ParamValues, refresh bool) {
v = v.Copy()
for _, set := range v {
for paramName, paramValue := range set {
set[paramName] = u.parsed.settings.valueFormatting.apply(paramValue)
}
}
u.mustBeOnValidIDs(v)
u.validateValues(v)
u.parsed.protected.valuesMutex.Lock()
defer u.parsed.protected.valuesMutex.Unlock()
u.parsed.protected.values[u.providerIndex] = v
if refresh {
u.parsed.refresh(false) // update only dynamic parameters
}
}
func (u *updater) validateValues(v types.ParamValues) {
for setName, set := range v {
for paramName, value := range set {
err := u.parsed.validValue(setName, paramName, &value)
if err != nil {
u.parsed.settings.loggerFn.E(fmt.Sprintf(
"provider %q update: parameter %s.%s: %v",
u.providerName, setName, paramName, err))
}
}
}
}
// mustBeOnValidIDs panics if one of the values provided by the provided is
// not on the list of ids the application registered.
func (u *updater) mustBeOnValidIDs(v types.ParamValues) {
for setName, set := range v {
if _, ok := u.parsed.inferedConfig[setName]; !ok {
panic(fmt.Errorf("parameter source is providing value for unsolicited paramset %q", setName))
}
for paramName := range set {
if _, ok := u.parsed.inferedConfig[setName].fields[paramName]; !ok {
panic(fmt.Errorf("parameter source is providing value for unsolicited parameter %s.%s", setName, paramName))
}
}
}
}