-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatafile_reader.go
More file actions
558 lines (481 loc) · 14.4 KB
/
datafile_reader.go
File metadata and controls
558 lines (481 loc) · 14.4 KB
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
package featurevisor
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
// DatafileReaderOptions contains options for creating a datafile reader
type DatafileReaderOptions struct {
Datafile DatafileContent
Logger *Logger
}
// ForceResult represents the result of a force lookup
type ForceResult struct {
Force *Force `json:"force,omitempty"`
ForceIndex *int `json:"forceIndex,omitempty"`
}
// DatafileReader provides functionality to read and query datafile content
type DatafileReader struct {
schemaVersion string
revision string
segments map[SegmentKey]Segment
features map[FeatureKey]Feature
logger *Logger
regexCache map[string]*regexp.Regexp
}
// NewDatafileReader creates a new datafile reader instance
func NewDatafileReader(options DatafileReaderOptions) *DatafileReader {
return &DatafileReader{
schemaVersion: options.Datafile.SchemaVersion,
revision: options.Datafile.Revision,
segments: options.Datafile.Segments,
features: options.Datafile.Features,
logger: options.Logger,
regexCache: make(map[string]*regexp.Regexp),
}
}
// GetRevision returns the revision of the datafile
func (d *DatafileReader) GetRevision() string {
return d.revision
}
// GetSchemaVersion returns the schema version of the datafile
func (d *DatafileReader) GetSchemaVersion() string {
return d.schemaVersion
}
// GetSegment returns a segment by its key
func (d *DatafileReader) GetSegment(segmentKey SegmentKey) *Segment {
segment, exists := d.segments[segmentKey]
if !exists {
return nil
}
segment.Conditions = d.parseConditionsIfStringified(segment.Conditions)
return &segment
}
// GetFeatureKeys returns all feature keys
func (d *DatafileReader) GetFeatureKeys() []string {
keys := make([]string, 0, len(d.features))
for key := range d.features {
keys = append(keys, string(key))
}
return keys
}
// GetFeature returns a feature by its key
func (d *DatafileReader) GetFeature(featureKey FeatureKey) *Feature {
feature, exists := d.features[featureKey]
if !exists {
return nil
}
// Parse required features if they exist
if feature.Required != nil {
feature.Required = d.parseRequiredIfStringified(feature.Required)
}
return &feature
}
// GetVariableKeys returns the variable keys for a feature
func (d *DatafileReader) GetVariableKeys(featureKey FeatureKey) []string {
feature := d.GetFeature(featureKey)
if feature == nil || feature.VariablesSchema == nil {
return []string{}
}
keys := make([]string, 0, len(feature.VariablesSchema))
for key := range feature.VariablesSchema {
keys = append(keys, string(key))
}
return keys
}
// HasVariations checks if a feature has variations
func (d *DatafileReader) HasVariations(featureKey FeatureKey) bool {
feature := d.GetFeature(featureKey)
if feature == nil {
return false
}
return feature.Variations != nil && len(feature.Variations) > 0
}
// GetRegex returns a regex pattern with caching
func (d *DatafileReader) GetRegex(regexString string, regexFlags string) *regexp.Regexp {
flags := regexFlags
if flags == "" {
flags = ""
}
cacheKey := fmt.Sprintf("%s-%s", regexString, flags)
if d.regexCache[cacheKey] != nil {
return d.regexCache[cacheKey]
}
regex := regexp.MustCompile(regexString)
d.regexCache[cacheKey] = regex
return regex
}
// AllConditionsAreMatched checks if all conditions are matched given a context
func (d *DatafileReader) AllConditionsAreMatched(conditions Condition, context Context) bool {
// Add error handling wrapper like in TypeScript version
defer func() {
if r := recover(); r != nil {
d.logger.Warn("Error in condition matching", LogDetails{
"error": r,
"conditions": conditions,
"context": context,
})
}
}()
// Handle string conditions
if conditionStr, ok := conditions.(string); ok {
if conditionStr == "*" {
return true
}
return false
}
// Handle plain conditions
if plainCondition, ok := conditions.(PlainCondition); ok {
getRegex := func(regexString string, regexFlags string) *regexp.Regexp {
return d.GetRegex(regexString, regexFlags)
}
matched := ConditionIsMatched(plainCondition, context, getRegex)
return matched
}
// Handle conditions from JSON unmarshaling (map[string]interface{})
if conditionMap, ok := conditions.(map[string]interface{}); ok {
// Check if it's a plain condition
if attribute, ok := conditionMap["attribute"].(string); ok {
if operator, ok := conditionMap["operator"].(string); ok {
// Handle operators that don't have a value (exists, notExists)
if operator == "exists" || operator == "notExists" {
plainCondition := PlainCondition{
Attribute: AttributeKey(attribute),
Operator: Operator(operator),
Value: nil, // exists/notExists don't have values
}
getRegex := func(regexString string, regexFlags string) *regexp.Regexp {
return d.GetRegex(regexString, regexFlags)
}
matched := ConditionIsMatched(plainCondition, context, getRegex)
return matched
}
// Handle operators that have a value
if value, ok := conditionMap["value"]; ok {
conditionValue := ConditionValue(value)
plainCondition := PlainCondition{
Attribute: AttributeKey(attribute),
Operator: Operator(operator),
Value: &conditionValue,
}
getRegex := func(regexString string, regexFlags string) *regexp.Regexp {
return d.GetRegex(regexString, regexFlags)
}
// Add error handling like in TypeScript version
matched := ConditionIsMatched(plainCondition, context, getRegex)
return matched
}
}
}
// Check if it's an and condition
if andConditions, ok := conditionMap["and"].([]interface{}); ok {
for _, condition := range andConditions {
if !d.AllConditionsAreMatched(condition, context) {
return false
}
}
return true
}
// Check if it's an or condition
if orConditions, ok := conditionMap["or"].([]interface{}); ok {
for _, condition := range orConditions {
if d.AllConditionsAreMatched(condition, context) {
return true
}
}
return false
}
// Check if it's a not condition
if notCondition, ok := conditionMap["not"]; ok {
return !d.AllConditionsAreMatched(notCondition, context)
}
}
// Handle and conditions
if andCondition, ok := conditions.(AndCondition); ok {
for _, condition := range andCondition.And {
if !d.AllConditionsAreMatched(condition, context) {
return false
}
}
return true
}
// Handle or conditions
if orCondition, ok := conditions.(OrCondition); ok {
for _, condition := range orCondition.Or {
if d.AllConditionsAreMatched(condition, context) {
return true
}
}
return false
}
// Handle not conditions
if notCondition, ok := conditions.(NotCondition); ok {
return !d.AllConditionsAreMatched(notCondition.Not, context)
}
// Handle array of conditions (from JSON unmarshaling)
if conditionArray, ok := conditions.([]interface{}); ok {
for _, condition := range conditionArray {
if !d.AllConditionsAreMatched(condition, context) {
return false
}
}
return true
}
// Handle array of conditions (typed)
if conditionArray, ok := conditions.([]Condition); ok {
for _, condition := range conditionArray {
if !d.AllConditionsAreMatched(condition, context) {
return false
}
}
return true
}
return false
}
// SegmentIsMatched checks if a segment is matched given a context
func (d *DatafileReader) SegmentIsMatched(segment *Segment, context Context) bool {
return d.AllConditionsAreMatched(segment.Conditions, context)
}
// AllSegmentsAreMatched checks if all segments are matched given a context
func (d *DatafileReader) AllSegmentsAreMatched(groupSegments interface{}, context Context) bool {
// Add error handling wrapper like in TypeScript version
defer func() {
if r := recover(); r != nil {
d.logger.Warn("Error in segment matching", LogDetails{
"error": r,
"groupSegments": groupSegments,
"context": context,
})
}
}()
// Handle wildcard
if groupSegments == "*" {
d.logger.Debug("matched wildcard segment", LogDetails{
"segments": groupSegments,
})
return true
}
// Handle string segment
if segmentKey, ok := groupSegments.(string); ok {
segment := d.GetSegment(SegmentKey(segmentKey))
if segment != nil {
matched := d.SegmentIsMatched(segment, context)
d.logger.Debug("checked single segment", LogDetails{
"segment": segmentKey,
"matched": matched,
})
return matched
}
return false
}
// Handle and segments
if andSegments, ok := groupSegments.(AndGroupSegment); ok {
for _, groupSegment := range andSegments.And {
if !d.AllSegmentsAreMatched(groupSegment, context) {
return false
}
}
return true
}
// Handle or segments
if orSegments, ok := groupSegments.(OrGroupSegment); ok {
for _, groupSegment := range orSegments.Or {
if d.AllSegmentsAreMatched(groupSegment, context) {
return true
}
}
return false
}
// Handle not segments
if notSegments, ok := groupSegments.(NotGroupSegment); ok {
if d.AllSegmentsAreMatched(notSegments.Not, context) {
return false
}
return true
}
// Handle array of segments (from JSON unmarshaling)
if segmentArray, ok := groupSegments.([]interface{}); ok {
for _, groupSegment := range segmentArray {
if !d.AllSegmentsAreMatched(groupSegment, context) {
return false
}
}
return true
}
// Handle array of segments (typed)
if segmentArray, ok := groupSegments.([]GroupSegment); ok {
for _, groupSegment := range segmentArray {
if !d.AllSegmentsAreMatched(groupSegment, context) {
return false
}
}
return true
}
// Handle segments from JSON unmarshaling (map[string]interface{})
if segmentMap, ok := groupSegments.(map[string]interface{}); ok {
// Check if it's an "or" segment
if orSegments, ok := segmentMap["or"].([]interface{}); ok {
for _, segment := range orSegments {
if d.AllSegmentsAreMatched(segment, context) {
return true
}
}
return false
}
// Check if it's an "and" segment
if andSegments, ok := segmentMap["and"].([]interface{}); ok {
for _, segment := range andSegments {
if !d.AllSegmentsAreMatched(segment, context) {
return false
}
}
return true
}
// Check if it's a "not" segment
if notSegment, ok := segmentMap["not"]; ok {
return !d.AllSegmentsAreMatched(notSegment, context)
}
}
d.logger.Debug("no segments matched", LogDetails{
"segments": groupSegments,
})
return false
}
// GetMatchedTraffic returns the matched traffic for a given context
func (d *DatafileReader) GetMatchedTraffic(traffic []Traffic, context Context) *Traffic {
for _, t := range traffic {
segments := d.parseSegmentsIfStringified(t.Segments)
if d.AllSegmentsAreMatched(segments, context) {
d.logger.Debug("matched traffic rule", LogDetails{
"ruleKey": t.Key,
"segments": t.Segments,
})
return &t
}
}
return nil
}
// GetMatchedAllocation returns the matched allocation for a given bucket value
func (d *DatafileReader) GetMatchedAllocation(traffic *Traffic, bucketValue int) *Allocation {
if traffic.Allocation == nil {
return nil
}
for _, allocation := range traffic.Allocation {
start := allocation.Range[0]
end := allocation.Range[1]
if start <= bucketValue && end >= bucketValue {
return &allocation
}
}
return nil
}
// GetMatchedForce returns the matched force for a given feature and context
func (d *DatafileReader) GetMatchedForce(featureKey interface{}, context Context) ForceResult {
result := ForceResult{
Force: nil,
ForceIndex: nil,
}
var feature *Feature
switch key := featureKey.(type) {
case FeatureKey:
feature = d.GetFeature(key)
case *Feature:
feature = key
default:
return result
}
if feature == nil || feature.Force == nil {
return result
}
for i, currentForce := range feature.Force {
// Check conditions
if currentForce.Conditions != nil {
conditions := d.parseConditionsIfStringified(currentForce.Conditions)
if d.AllConditionsAreMatched(conditions, context) {
result.Force = ¤tForce
result.ForceIndex = &i
break
}
}
// Check segments
if currentForce.Segments != nil {
segments := d.parseSegmentsIfStringified(currentForce.Segments)
if d.AllSegmentsAreMatched(segments, context) {
result.Force = ¤tForce
result.ForceIndex = &i
break
}
}
}
return result
}
// parseConditionsIfStringified parses conditions if they are stringified
func (d *DatafileReader) parseConditionsIfStringified(conditions Condition) Condition {
if conditionStr, ok := conditions.(string); ok {
if conditionStr == "*" {
return conditions
}
var parsedCondition Condition
err := json.Unmarshal([]byte(conditionStr), &parsedCondition)
if err != nil {
d.logger.Error("Error parsing conditions", LogDetails{
"error": err,
"conditions": conditionStr,
})
return conditions
}
return parsedCondition
}
return conditions
}
// parseSegmentsIfStringified parses segments if they are stringified
func (d *DatafileReader) parseSegmentsIfStringified(segments interface{}) interface{} {
if segmentStr, ok := segments.(string); ok {
if strings.HasPrefix(segmentStr, "{") || strings.HasPrefix(segmentStr, "[") {
var parsedSegments interface{}
err := json.Unmarshal([]byte(segmentStr), &parsedSegments)
if err != nil {
d.logger.Error("Error parsing segments", LogDetails{
"error": err,
"segments": segmentStr,
})
return segments
}
return parsedSegments
}
}
return segments
}
// parseRequiredIfStringified parses required features if they are stringified
func (d *DatafileReader) parseRequiredIfStringified(required []Required) []Required {
parsedRequired := make([]Required, len(required))
for i, req := range required {
// If it's already a string, it's a simple required feature
if reqStr, ok := req.(string); ok {
parsedRequired[i] = reqStr
continue
}
// If it's a map, try to parse it as RequiredWithVariation
if reqMap, ok := req.(map[string]interface{}); ok {
if key, exists := reqMap["key"]; exists {
if variation, exists := reqMap["variation"]; exists {
// Convert to RequiredWithVariation
parsedRequired[i] = RequiredWithVariation{
Key: FeatureKey(key.(string)),
Variation: VariationValue(variation.(string)),
}
continue
}
}
}
// If it's already a RequiredWithVariation, keep it as is
if _, ok := req.(RequiredWithVariation); ok {
parsedRequired[i] = req
continue
}
// If we can't parse it, keep the original
parsedRequired[i] = req
}
return parsedRequired
}