-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigmapper.go
More file actions
186 lines (170 loc) · 5.17 KB
/
Copy pathconfigmapper.go
File metadata and controls
186 lines (170 loc) · 5.17 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
package dtos
import "encoding/json"
// ConvertConfigToSplit converts a ConfigDTO to a SplitDTO
// This mapper bridges the /configs endpoint response to the internal Split representation
// used by the evaluator.
func ConvertConfigToSplit(config ConfigDTO) SplitDTO {
// Apply defaults as per SDK spec
trafficTypeName := config.TrafficTypeName
if trafficTypeName == "" {
trafficTypeName = "user"
}
// Default to ACTIVE if status is missing
status := config.Status
if status == "" {
status = "ACTIVE"
}
// Map Targeting.Default to DefaultTreatment
defaultTreatment := config.Targeting.Default
if defaultTreatment == "" {
defaultTreatment = "control"
}
// Build configurations map from Variants
// Each variant's Definition (acts as directive for evaluator) is marshaled to JSON
configurations := make(map[string]string)
for _, variant := range config.Variants {
if variant.Definition != nil {
// Marshal the Definition (directive) to JSON string
definitionJSON, err := json.Marshal(variant.Definition)
if err == nil {
configurations[variant.Name] = string(definitionJSON)
}
}
}
// Transform raw conditions from /configs format to evaluator ConditionDTO format
conditions := make([]ConditionDTO, 0, len(config.Targeting.Conditions)+1)
for _, rawCondition := range config.Targeting.Conditions {
conditions = append(conditions, convertRawCondition(rawCondition))
}
// ALWAYS append default rule condition at the end
conditions = append(conditions, ConditionDTO{
ConditionType: "ROLLOUT",
Label: "default rule",
MatcherGroup: MatcherGroupDTO{
Combiner: "AND",
Matchers: []MatcherDTO{
{
MatcherType: "ALL_KEYS",
Negate: false,
KeySelector: nil,
},
},
},
Partitions: []PartitionDTO{
{
Treatment: defaultTreatment,
Size: 100,
},
},
})
// Build the SplitDTO for the evaluator
return SplitDTO{
Name: config.Name,
Killed: config.Killed, // Defaults to false if missing in JSON
ChangeNumber: config.ChangeNumber,
Configurations: configurations,
DefaultTreatment: defaultTreatment,
TrafficTypeName: trafficTypeName,
Status: status,
Algo: 2, // Explicitly set to 2 (Murmur3) - not provided by /configs endpoint
Seed: config.Targeting.Seed,
TrafficAllocation: config.Targeting.TrafficAllocation,
TrafficAllocationSeed: config.Targeting.TrafficAllocationSeed,
Conditions: conditions,
Sets: config.Sets, // Feature flag sets
}
}
// convertRawCondition transforms a raw condition from /configs format to ConditionDTO
func convertRawCondition(raw RawConditionDTO) ConditionDTO {
// Determine condition type based on matchers
// If any matcher is WHITELIST type, the whole condition is WHITELIST, otherwise ROLLOUT
conditionType := "ROLLOUT"
for _, matcher := range raw.Matchers {
if matcher.Type == "WHITELIST" {
conditionType = "WHITELIST"
break
}
}
// Convert matchers
matchers := make([]MatcherDTO, 0, len(raw.Matchers))
for _, rawMatcher := range raw.Matchers {
matchers = append(matchers, convertMatcher(rawMatcher))
}
// Convert partitions: variant -> treatment
partitions := make([]PartitionDTO, 0, len(raw.Partitions))
for _, rawPartition := range raw.Partitions {
partitions = append(partitions, PartitionDTO{
Treatment: rawPartition.Variant, // Map "variant" to "treatment"
Size: rawPartition.Size,
})
}
return ConditionDTO{
ConditionType: conditionType,
Label: raw.Label,
MatcherGroup: MatcherGroupDTO{
Combiner: "AND", // Always AND for /configs conditions
Matchers: matchers,
},
Partitions: partitions,
}
}
// convertMatcher transforms a raw matcher from /configs format to MatcherDTO
func convertMatcher(raw RawMatcherDTO) MatcherDTO {
// Build keySelector if attribute is provided
var keySelector *KeySelectorDTO
if raw.Attribute != "" {
attr := raw.Attribute
keySelector = &KeySelectorDTO{
TrafficType: "user",
Attribute: &attr,
}
}
// Transform based on matcher type
switch raw.Type {
case "WHITELIST":
// Extract strings from data
var whitelist []string
if stringsData, ok := raw.Data["strings"].([]interface{}); ok {
whitelist = make([]string, 0, len(stringsData))
for _, s := range stringsData {
if str, ok := s.(string); ok {
whitelist = append(whitelist, str)
}
}
}
return MatcherDTO{
MatcherType: "WHITELIST",
Negate: false,
KeySelector: keySelector,
Whitelist: &WhitelistMatcherDataDTO{
Whitelist: whitelist,
},
}
case "IS_EQUAL_TO":
// Map to EQUAL_TO with unary numeric data
var dataType string
var value int64
if dt, ok := raw.Data["type"].(string); ok {
dataType = dt
}
if num, ok := raw.Data["number"].(float64); ok {
value = int64(num)
}
return MatcherDTO{
MatcherType: "EQUAL_TO",
Negate: false,
KeySelector: keySelector,
UnaryNumeric: &UnaryNumericMatcherDataDTO{
DataType: dataType,
Value: value,
},
}
default:
// Default to ALL_KEYS for unknown types
return MatcherDTO{
MatcherType: "ALL_KEYS",
Negate: false,
KeySelector: keySelector,
}
}
}