Skip to content

Commit 475a567

Browse files
committed
feat: Actualise context schema, SPLIT reason weight formatting
1 parent d892342 commit 475a567

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
22
path = src/test/java/com/flagsmith/flagengine/enginetestdata
33
url = [email protected]:Flagsmith/engine-test-data.git
4-
tag = v2.5.0
4+
tag = v3.2.1

src/main/java/com/flagsmith/flagengine/Engine.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.flagsmith.flagengine.segments.SegmentEvaluator;
44
import com.flagsmith.flagengine.utils.Hashing;
5+
import java.math.BigDecimal;
56
import java.util.ArrayList;
67
import java.util.HashMap;
78
import java.util.List;
9+
import org.apache.commons.lang3.StringUtils;
810
import org.apache.commons.lang3.tuple.ImmutablePair;
911

10-
public class Engine {
12+
public class Engine {
1113
private static class SegmentEvaluationResult {
1214
List<SegmentResult> segments;
1315
HashMap<String, ImmutablePair<String, FeatureContext>> segmentFeatureContexts;
@@ -35,6 +37,7 @@ public HashMap<String, ImmutablePair<String, FeatureContext>> getSegmentFeatureC
3537
* @return Evaluation result.
3638
*/
3739
public static EvaluationResult getEvaluationResult(EvaluationContext context) {
40+
enrichEvaluationContext(context);
3841
SegmentEvaluationResult segmentEvaluationResult = evaluateSegments(context);
3942
Flags flags = evaluateFeatures(context, segmentEvaluationResult.getSegmentFeatureContexts());
4043

@@ -43,6 +46,15 @@ public static EvaluationResult getEvaluationResult(EvaluationContext context) {
4346
.withSegments(segmentEvaluationResult.getSegments());
4447
}
4548

49+
private static void enrichEvaluationContext(EvaluationContext context) {
50+
IdentityContext identity = context.getIdentity();
51+
if (identity != null) {
52+
if (StringUtils.isEmpty(identity.getKey())) {
53+
identity.setKey(context.getEnvironment().getKey() + "_" + identity.getIdentifier());
54+
}
55+
}
56+
}
57+
4658
private static SegmentEvaluationResult evaluateSegments(
4759
EvaluationContext context) {
4860
List<SegmentResult> segments = new ArrayList<>();
@@ -53,19 +65,19 @@ private static SegmentEvaluationResult evaluateSegments(
5365
if (contextSegments != null) {
5466
for (SegmentContext segmentContext : contextSegments.getAdditionalProperties().values()) {
5567
if (SegmentEvaluator.isContextInSegment(context, segmentContext)) {
56-
segments.add(new SegmentResult().withKey(segmentContext.getKey())
68+
segments.add(new SegmentResult()
5769
.withName(segmentContext.getName())
5870
.withMetadata(segmentContext.getMetadata()));
5971

6072
List<FeatureContext> segmentOverrides = segmentContext.getOverrides();
6173

6274
if (segmentOverrides != null) {
6375
for (FeatureContext featureContext : segmentOverrides) {
64-
String featureKey = featureContext.getFeatureKey();
76+
String featureName = featureContext.getName();
6577

66-
if (segmentFeatureContexts.containsKey(featureKey)) {
78+
if (segmentFeatureContexts.containsKey(featureName)) {
6779
ImmutablePair<String, FeatureContext> existing = segmentFeatureContexts
68-
.get(featureKey);
80+
.get(featureName);
6981
FeatureContext existingFeatureContext = existing.getRight();
7082

7183
Double existingPriority = existingFeatureContext.getPriority() == null
@@ -79,7 +91,7 @@ private static SegmentEvaluationResult evaluateSegments(
7991
continue;
8092
}
8193
}
82-
segmentFeatureContexts.put(featureKey,
94+
segmentFeatureContexts.put(featureName,
8395
new ImmutablePair<String, FeatureContext>(
8496
segmentContext.getName(), featureContext));
8597
}
@@ -103,14 +115,13 @@ private static Flags evaluateFeatures(
103115

104116
if (contextFeatures != null) {
105117
for (FeatureContext featureContext : contextFeatures.getAdditionalProperties().values()) {
106-
if (segmentFeatureContexts.containsKey(featureContext.getFeatureKey())) {
118+
if (segmentFeatureContexts.containsKey(featureContext.getName())) {
107119
ImmutablePair<String, FeatureContext> segmentNameFeaturePair = segmentFeatureContexts
108-
.get(featureContext.getFeatureKey());
120+
.get(featureContext.getName());
109121
featureContext = segmentNameFeaturePair.getRight();
110122
flags.setAdditionalProperty(
111123
featureContext.getName(),
112124
new FlagResult().withEnabled(featureContext.getEnabled())
113-
.withFeatureKey(featureContext.getFeatureKey())
114125
.withName(featureContext.getName())
115126
.withValue(featureContext.getValue())
116127
.withReason(
@@ -148,10 +159,11 @@ private static FlagResult getFlagResultFromFeatureContext(
148159
Float limit = startPercentage + weight.floatValue();
149160
if (startPercentage <= percentageValue && percentageValue < limit) {
150161
return new FlagResult().withEnabled(featureContext.getEnabled())
151-
.withFeatureKey(featureContext.getFeatureKey())
152162
.withName(featureContext.getName())
153163
.withValue(variant.getValue())
154-
.withReason("SPLIT; weight=" + weight.intValue())
164+
.withReason("SPLIT; weight=" + BigDecimal.valueOf(weight)
165+
.stripTrailingZeros()
166+
.toPlainString())
155167
.withMetadata(featureContext.getMetadata());
156168
}
157169
startPercentage = limit;
@@ -160,7 +172,6 @@ private static FlagResult getFlagResultFromFeatureContext(
160172
}
161173

162174
return new FlagResult().withEnabled(featureContext.getEnabled())
163-
.withFeatureKey(featureContext.getFeatureKey())
164175
.withName(featureContext.getName())
165176
.withValue(featureContext.getValue())
166177
.withReason("DEFAULT")

src/main/java/com/flagsmith/mappers/EngineMappers.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public static EvaluationContext mapContextAndIdentityDataToContext(
8080

8181
// Create identity context
8282
IdentityContext identityContext = new IdentityContext()
83-
.withIdentifier(identifier)
84-
.withKey(context.getEnvironment().getKey() + "_" + identifier);
83+
.withIdentifier(identifier);
8584

8685
// Map traits if provided
8786
if (traits != null && !traits.isEmpty()) {
@@ -196,7 +195,6 @@ private static Map<String, SegmentContext> mapIdentityOverridesToSegments(
196195
FeatureModel feature = featureState.getFeature();
197196
FeatureContext featureContext = new FeatureContext()
198197
.withKey("")
199-
.withFeatureKey(String.valueOf(feature.getId()))
200198
.withName(feature.getName())
201199
.withEnabled(featureState.getEnabled())
202200
.withValue(featureState.getValue())
@@ -349,7 +347,6 @@ private static double getMultivariateFeatureValuePriority(
349347
private static FeatureContext mapFeatureStateToFeatureContext(FeatureStateModel featureState) {
350348
FeatureContext featureContext = new FeatureContext()
351349
.withKey(getFeatureStateKey(featureState))
352-
.withFeatureKey(String.valueOf(featureState.getFeature().getId()))
353350
.withName(featureState.getFeature().getName())
354351
.withEnabled(featureState.getEnabled())
355352
.withValue(featureState.getValue())
Submodule enginetestdata updated 158 files

0 commit comments

Comments
 (0)