|
3 | 3 | import com.fasterxml.jackson.core.type.TypeReference; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.flagsmith.flagengine.EvaluationContext; |
| 6 | +import com.flagsmith.flagengine.IdentityContext; |
6 | 7 | import com.flagsmith.flagengine.SegmentCondition; |
7 | 8 | import com.flagsmith.flagengine.SegmentContext; |
8 | 9 | import com.flagsmith.flagengine.SegmentRule; |
|
21 | 22 | import java.util.regex.Pattern; |
22 | 23 | import java.util.regex.PatternSyntaxException; |
23 | 24 | import java.util.stream.Collectors; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
24 | 26 |
|
25 | 27 | public class SegmentEvaluator { |
26 | 28 | private static ObjectMapper mapper = new ObjectMapper(); |
@@ -77,10 +79,25 @@ private static Boolean contextMatchesCondition( |
77 | 79 | EvaluationContext context, |
78 | 80 | SegmentCondition condition, |
79 | 81 | String segmentKey) { |
80 | | - Object contextValue = getContextValue(context, condition.getProperty()); |
| 82 | + Object contextValue = null; |
81 | 83 | Object conditionValue = condition.getValue(); |
| 84 | + String conditionProperty = condition.getProperty(); |
82 | 85 | SegmentConditions operator = condition.getOperator(); |
83 | 86 |
|
| 87 | + if (operator == SegmentConditions.PERCENTAGE_SPLIT && StringUtils.isEmpty(conditionProperty)) { |
| 88 | + // Currently, the only supported condition with a blank property |
| 89 | + // is percentage split. |
| 90 | + // In this case, we use the identity key as context value. |
| 91 | + // This is mainly to support legacy segments created before |
| 92 | + // we introduced JSONPath support. |
| 93 | + IdentityContext identity = context.getIdentity(); |
| 94 | + if (!(identity == null)) { |
| 95 | + contextValue = identity.getKey(); |
| 96 | + } |
| 97 | + } else { |
| 98 | + contextValue = getContextValue(context, conditionProperty); |
| 99 | + } |
| 100 | + |
84 | 101 | switch (operator) { |
85 | 102 | case IN: |
86 | 103 | if (contextValue == null || contextValue instanceof Boolean) { |
@@ -109,20 +126,14 @@ private static Boolean contextMatchesCondition( |
109 | 126 | return conditionList.contains(String.valueOf(contextValue)); |
110 | 127 |
|
111 | 128 | case PERCENTAGE_SPLIT: |
112 | | - String key; |
113 | 129 | if (contextValue == null) { |
114 | | - if (context.getIdentity() == null) { |
115 | | - return false; |
116 | | - } |
117 | | - key = context.getIdentity().getKey(); |
118 | | - } else { |
119 | | - key = contextValue.toString(); |
| 130 | + return false; |
120 | 131 | } |
121 | | - List<String> objectIds = List.of(segmentKey, key); |
| 132 | + List<String> objectIds = List.of(segmentKey, contextValue.toString()); |
122 | 133 |
|
123 | 134 | final float floatValue; |
124 | 135 | try { |
125 | | - floatValue = Float.parseFloat(String.valueOf(condition.getValue())); |
| 136 | + floatValue = Float.parseFloat(String.valueOf(conditionValue)); |
126 | 137 | } catch (NumberFormatException e) { |
127 | 138 | return false; |
128 | 139 | } |
|
0 commit comments