-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_parser.go
323 lines (296 loc) · 8.84 KB
/
config_parser.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
package configcat
import (
"bytes"
"encoding/json"
"sync"
"time"
)
type config struct {
jsonBody []byte
etag string
root *ConfigJson
// Note: this is a pointer because the configuration
// can be copied (with the withFetchTime method).
evaluators []settingEvalFunc
allKeys []string
keyValues map[string]keyValue
fetchTime time.Time
userInfos *sync.Map
// values holds all the values that can be returned from the
// configuration, keyed by valueID-1.
values []interface{}
// valueIds holds value IDs for keys that we know
// the values of ahead of time because they're not
// dependent on the user value, indexed by key id.
// For values that we don't know ahead of time, the
// valueIds entry contains a negative number n. (-n - 1) is the index
// into the cache slice which will hold the value eventually
// when it's been calculated for a particular user.
valueIds []valueID
// keysWithRules holds the number of keys that have
// rules (i.e. that do not have a value in valueIds).
keysWithRules int
// defaultUserSnapshot holds a predefined snapshot of the
// configuration with the default user.
defaultUserSnapshot *Snapshot
// defaultUser holds the user that defaultUserSnapshot was
// created with.
defaultUser User
}
// valueID holds an integer representation of a value that
// can be returned from a feature flag. It's one more
// than the index into the config.values or Snapshot.values slice.
type valueID = int32
func parseConfig(jsonBody []byte, etag string, fetchTime time.Time, logger *leveledLogger, defaultUser User, overrides *FlagOverrides, hooks *Hooks) (*config, error) {
var root ConfigJson
// Note: jsonBody can be nil when we've got overrides only.
if jsonBody != nil {
if err := json.Unmarshal(jsonBody, &root); err != nil {
return nil, err
}
}
fixupSegmentsAndSalt(&root)
mergeWithOverrides(&root, overrides)
conf := &config{
jsonBody: jsonBody,
root: &root,
keyValues: keyValuesForRootNode(&root),
allKeys: keysForRootNode(&root),
etag: etag,
fetchTime: fetchTime,
valueIds: make([]valueID, numKeys()),
defaultUser: defaultUser,
userInfos: new(sync.Map),
}
conf.fixup(make(map[interface{}]valueID))
conf.checkCycles()
conf.preCalculateValueIds()
conf.generateEvaluators()
conf.defaultUserSnapshot = _newSnapshot(conf, defaultUser, logger, hooks)
return conf, nil
}
// preCalculateValueIds populates valueIds with value IDs that
// are known ahead of time or negative indexes into the cache slice
// where the value will be stored later.
func (c *config) preCalculateValueIds() {
for name, entry := range c.root.Settings {
id := idForKey(name, true)
if int(id) >= len(c.valueIds) {
value := make([]valueID, id+1)
copy(value, c.valueIds)
c.valueIds = value
}
if len(entry.TargetingRules) == 0 && len(entry.PercentageOptions) == 0 {
c.valueIds[id] = entry.valueID
continue
}
c.keysWithRules++
c.valueIds[id] = -valueID(c.keysWithRules)
}
}
func (c *config) equal(c1 *config) bool {
if c == c1 || c == nil || c1 == nil {
return c == c1
}
return c.fetchTime.Equal(c1.fetchTime) && c.etag == c1.etag && bytes.Equal(c.jsonBody, c1.jsonBody)
}
func (c *config) equalContent(c1 *config) bool {
if c == c1 || c == nil || c1 == nil {
return c == c1
}
return bytes.Equal(c.jsonBody, c1.jsonBody)
}
func (c *config) withFetchTime(t time.Time) *config {
c1 := *c
c1.fetchTime = t
return &c1
}
func (c *config) body() string {
if c == nil {
return ""
}
return string(c.jsonBody)
}
func (c *config) getKeyAndValueForVariation(variationID string) (string, interface{}) {
if c == nil {
return "", nil
}
kv := c.keyValues[variationID]
return kv.key, kv.value
}
func (c *config) keys() []string {
if c == nil {
return nil
}
return c.allKeys
}
// fixup populates the valueID fields in conf.root and presets related fields.
func (c *config) fixup(valueMap map[interface{}]valueID) {
for _, setting := range c.root.Settings {
setting.valueID = c.idForValue(setting.Value.Value, valueMap)
for _, rule := range setting.TargetingRules {
if rule.ServedValue != nil {
rule.ServedValue.valueID = c.idForValue(rule.ServedValue.Value.Value, valueMap)
}
if rule.Conditions != nil {
for _, condition := range rule.Conditions {
if condition.PrerequisiteFlagCondition != nil {
if prerequisite, ok := c.root.Settings[condition.PrerequisiteFlagCondition.FlagKey]; ok {
condition.PrerequisiteFlagCondition.valueID = c.idForValue(valueFor(condition.PrerequisiteFlagCondition.Value), valueMap)
condition.PrerequisiteFlagCondition.prerequisiteSettingType = prerequisite.Type
}
}
}
}
if rule.PercentageOptions != nil {
for _, option := range rule.PercentageOptions {
option.valueID = c.idForValue(option.Value.Value, valueMap)
}
}
}
for _, rule := range setting.PercentageOptions {
rule.valueID = c.idForValue(rule.Value.Value, valueMap)
}
}
}
func (c *config) idForValue(val interface{}, valueMap map[interface{}]valueID) valueID {
if id, ok := valueMap[val]; ok {
return id
}
// Start at 1 so the zero value always means "not known yet"
// so we can rely on zero-initialization of the evaluation context.
id := valueID(len(c.values) + 1)
valueMap[val] = id
c.values = append(c.values, val)
return id
}
type cycleTracker struct {
visitedKeys []string
}
func (c *cycleTracker) contains(key string) bool {
for _, v := range c.visitedKeys {
if v == key {
return true
}
}
return false
}
func (c *cycleTracker) append(key string) {
c.visitedKeys = append(c.visitedKeys, key)
}
func (c *cycleTracker) removeLast() {
if len(c.visitedKeys) > 0 {
c.visitedKeys = c.visitedKeys[:len(c.visitedKeys)-1]
}
}
func (c *config) checkCycles() {
for key, setting := range c.root.Settings {
tracker := &cycleTracker{}
if c.checkCyclesForSetting(setting, key, tracker) {
setting.prerequisiteCycle = tracker.visitedKeys
}
}
}
func (c *config) checkCyclesForSetting(s *Setting, key string, tracker *cycleTracker) bool {
tracker.append(key)
if len(s.TargetingRules) > 0 {
for _, rule := range s.TargetingRules {
if len(rule.Conditions) > 0 {
for _, cond := range rule.Conditions {
if cond.PrerequisiteFlagCondition != nil {
if prerequisite, ok := c.root.Settings[cond.PrerequisiteFlagCondition.FlagKey]; ok {
if tracker.contains(cond.PrerequisiteFlagCondition.FlagKey) {
tracker.append(cond.PrerequisiteFlagCondition.FlagKey)
return true
}
if c.checkCyclesForSetting(prerequisite, cond.PrerequisiteFlagCondition.FlagKey, tracker) {
return true
} else {
tracker.removeLast()
}
}
}
}
}
}
}
return false
}
func mergeWithOverrides(root *ConfigJson, overrides *FlagOverrides) {
if overrides == nil {
return
}
if overrides.Behavior == LocalOnly || len(root.Settings) == 0 {
root.Settings = overrides.settings
return
}
if root.Settings == nil {
root.Settings = make(map[string]*Setting)
}
for key, localEntry := range overrides.settings {
setting, ok := root.Settings[key]
switch {
case !ok:
root.Settings[key] = localEntry
case overrides.Behavior == RemoteOverLocal:
case setting.Type == localEntry.Type:
*setting = *localEntry
case setting.Type == IntSetting && localEntry.Type == FloatSetting:
*setting = *localEntry
changeToInt(setting)
default:
// Type clash. Just override anyway.
// TODO could return an error in this case, as it's likely to be a local config issue.
*setting = *localEntry
}
}
}
func changeToInt(setting *Setting) {
setting.Type = IntSetting
setting.Value.Value = toIntVal(setting.Value.Value)
if len(setting.TargetingRules) > 0 {
for _, rule := range setting.TargetingRules {
if rule.ServedValue != nil {
rule.ServedValue.Value.Value = toIntVal(rule.ServedValue.Value.Value)
}
if len(rule.PercentageOptions) > 0 {
for _, opt := range rule.PercentageOptions {
opt.Value.Value = toIntVal(opt.Value.Value)
}
}
}
if len(setting.PercentageOptions) > 0 {
for _, opt := range setting.PercentageOptions {
opt.Value.Value = toIntVal(opt.Value.Value)
}
}
}
}
func toIntVal(v interface{}) interface{} {
f, ok := v.(float64)
if !ok {
return v
}
return int(f)
}
func fixupSegmentsAndSalt(root *ConfigJson) {
var saltBytes []byte
if root.Preferences != nil {
saltBytes = []byte(root.Preferences.Salt)
} else {
saltBytes = make([]byte, 0)
}
for _, setting := range root.Settings {
setting.saltBytes = saltBytes
for _, rule := range setting.TargetingRules {
if rule.Conditions != nil {
for _, condition := range rule.Conditions {
if condition.SegmentCondition != nil {
condition.SegmentCondition.relatedSegment = root.Segments[condition.SegmentCondition.Index]
condition.SegmentCondition.relatedSegment.nameBytes = []byte(condition.SegmentCondition.relatedSegment.Name)
}
}
}
}
}
}