-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSegmentEvaluator.java
More file actions
222 lines (198 loc) · 7.35 KB
/
SegmentEvaluator.java
File metadata and controls
222 lines (198 loc) · 7.35 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
package com.flagsmith.flagengine.segments;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flagsmith.flagengine.EvaluationContext;
import com.flagsmith.flagengine.IdentityContext;
import com.flagsmith.flagengine.SegmentCondition;
import com.flagsmith.flagengine.SegmentContext;
import com.flagsmith.flagengine.SegmentRule;
import com.flagsmith.flagengine.segments.constants.SegmentConditions;
import com.flagsmith.flagengine.utils.Hashing;
import com.flagsmith.flagengine.utils.types.TypeCasting;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
public class SegmentEvaluator {
private static ObjectMapper mapper = new ObjectMapper();
private static Configuration jsonPathConfiguration = Configuration
.defaultConfiguration()
.setOptions(Option.SUPPRESS_EXCEPTIONS);
private static TypeReference<List<String>> stringListTypeRef = new TypeReference<List<String>>() {
};
/**
* Check if context is in segment.
*
* @param context Evaluation context.
* @param segment Segment context.
* @return true if context is in segment.
*/
public static Boolean isContextInSegment(EvaluationContext context, SegmentContext segment) {
List<SegmentRule> rules = segment.getRules();
return !rules.isEmpty() && rules.stream()
.allMatch((rule) -> contextMatchesRule(context, rule, segment.getKey()));
}
private static Boolean contextMatchesRule(EvaluationContext context, SegmentRule rule,
String segmentKey) {
Predicate<SegmentCondition> conditionPredicate = (condition) -> contextMatchesCondition(
context, condition, segmentKey);
Boolean isMatch;
List<SegmentCondition> conditions = rule.getConditions();
if (conditions.isEmpty()) {
isMatch = true;
} else {
switch (rule.getType()) {
case ALL:
isMatch = conditions.stream().allMatch(conditionPredicate);
break;
case ANY:
isMatch = conditions.stream().anyMatch(conditionPredicate);
break;
case NONE:
isMatch = conditions.stream().noneMatch(conditionPredicate);
break;
default:
return false;
}
}
return isMatch && rule.getRules().stream()
.allMatch((subRule) -> contextMatchesRule(context, subRule, segmentKey));
}
private static Boolean contextMatchesCondition(
EvaluationContext context,
SegmentCondition condition,
String segmentKey) {
Object contextValue = null;
Object conditionValue = condition.getValue();
String conditionProperty = condition.getProperty();
SegmentConditions operator = condition.getOperator();
if (operator == SegmentConditions.PERCENTAGE_SPLIT && StringUtils.isEmpty(conditionProperty)) {
// Currently, the only supported condition with a blank property
// is percentage split.
// In this case, we use the identity key as context value.
// This is mainly to support legacy segments created before
// we introduced JSONPath support.
IdentityContext identity = context.getIdentity();
if (!(identity == null)) {
contextValue = identity.getKey();
}
} else {
contextValue = getContextValue(context, conditionProperty);
}
switch (operator) {
case IN:
if (contextValue == null || contextValue instanceof Boolean) {
return false;
}
List<String> conditionList = new ArrayList<>();
if (conditionValue instanceof List) {
List<?> maybeConditionList = (List<?>) conditionValue;
conditionList = maybeConditionList.stream()
.map(Object::toString)
.collect(Collectors.toList());
} else if (conditionValue instanceof String) {
String stringConditionValue = (String) conditionValue;
try {
// Try parsing a JSON list first
conditionList = mapper.readValue(
stringConditionValue, stringListTypeRef);
} catch (IOException e) {
// As a fallback, split by comma
conditionList = Arrays.asList(stringConditionValue.split(","));
}
}
return conditionList.contains(String.valueOf(contextValue));
case PERCENTAGE_SPLIT:
if (contextValue == null) {
return false;
}
List<String> objectIds = List.of(segmentKey, contextValue.toString());
final float floatValue;
try {
floatValue = Float.parseFloat(String.valueOf(conditionValue));
} catch (NumberFormatException e) {
return false;
}
return Hashing.getInstance()
.getHashedPercentageForObjectIds(objectIds) <= floatValue;
case IS_NOT_SET:
return contextValue == null;
case IS_SET:
return contextValue != null;
case CONTAINS:
return (String.valueOf(contextValue)).indexOf(conditionValue.toString()) > -1;
case NOT_CONTAINS:
if (contextValue != null) {
return (String.valueOf(contextValue)).indexOf(conditionValue.toString()) == -1;
}
return false;
case REGEX:
if (contextValue != null) {
try {
Pattern pattern = Pattern.compile(conditionValue.toString());
return pattern.matcher(contextValue.toString()).find();
} catch (PatternSyntaxException pse) {
return false;
}
}
return false;
case MODULO:
if (contextValue instanceof Number && conditionValue instanceof String) {
try {
String[] parts = conditionValue.toString().split("\\|");
if (parts.length != 2) {
return false;
}
Double divisor = Double.parseDouble(parts[0]);
Double remainder = Double.parseDouble(parts[1]);
Double value = ((Number) contextValue).doubleValue();
return (value % divisor) == remainder;
} catch (NumberFormatException nfe) {
return false;
}
}
return false;
default:
if (contextValue == null) {
return false;
}
return TypeCasting.compare(operator, contextValue, conditionValue);
}
}
/**
* Get context value by property name.
*
* @param context Evaluation context.
* @param property Property name.
* @return Property value.
*/
private static Object getContextValue(EvaluationContext context, String property) {
Object result;
if (context.getIdentity() != null && context.getIdentity().getTraits() != null) {
result = context.getIdentity().getTraits().getAdditionalProperties().get(property);
if (result != null) {
return result;
}
}
if (property.startsWith("$.")) {
result = JsonPath
.using(jsonPathConfiguration)
.parse(mapper.convertValue(context, Map.class))
.read(property);
if (result instanceof List || result instanceof Map) {
return null;
}
return result;
}
return null;
}
}