Skip to content

Commit b909755

Browse files
authored
feat: Actualise context schema, SPLIT reason weight formatting (#186)
1 parent d892342 commit b909755

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
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: 32 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+
context = getEnrichedEvaluationContext(context);
3841
SegmentEvaluationResult segmentEvaluationResult = evaluateSegments(context);
3942
Flags flags = evaluateFeatures(context, segmentEvaluationResult.getSegmentFeatureContexts());
4043

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

49+
/*
50+
* Get a version of the evaluation context enriched with derived data.
51+
*
52+
* @param context Evaluation context.
53+
* @return Enriched evaluation context, or the original if no enrichment was needed.
54+
*/
55+
private static EvaluationContext getEnrichedEvaluationContext(EvaluationContext context) {
56+
IdentityContext identity = context.getIdentity();
57+
if (identity != null) {
58+
if (StringUtils.isEmpty(identity.getKey())) {
59+
String identityKey = context.getEnvironment().getKey() + "_" + identity.getIdentifier();
60+
context = new EvaluationContext(context).withIdentity(
61+
new IdentityContext(identity).withKey(identityKey));
62+
}
63+
}
64+
return context;
65+
}
66+
4667
private static SegmentEvaluationResult evaluateSegments(
4768
EvaluationContext context) {
4869
List<SegmentResult> segments = new ArrayList<>();
@@ -53,19 +74,19 @@ private static SegmentEvaluationResult evaluateSegments(
5374
if (contextSegments != null) {
5475
for (SegmentContext segmentContext : contextSegments.getAdditionalProperties().values()) {
5576
if (SegmentEvaluator.isContextInSegment(context, segmentContext)) {
56-
segments.add(new SegmentResult().withKey(segmentContext.getKey())
77+
segments.add(new SegmentResult()
5778
.withName(segmentContext.getName())
5879
.withMetadata(segmentContext.getMetadata()));
5980

6081
List<FeatureContext> segmentOverrides = segmentContext.getOverrides();
6182

6283
if (segmentOverrides != null) {
6384
for (FeatureContext featureContext : segmentOverrides) {
64-
String featureKey = featureContext.getFeatureKey();
85+
String featureName = featureContext.getName();
6586

66-
if (segmentFeatureContexts.containsKey(featureKey)) {
87+
if (segmentFeatureContexts.containsKey(featureName)) {
6788
ImmutablePair<String, FeatureContext> existing = segmentFeatureContexts
68-
.get(featureKey);
89+
.get(featureName);
6990
FeatureContext existingFeatureContext = existing.getRight();
7091

7192
Double existingPriority = existingFeatureContext.getPriority() == null
@@ -79,7 +100,7 @@ private static SegmentEvaluationResult evaluateSegments(
79100
continue;
80101
}
81102
}
82-
segmentFeatureContexts.put(featureKey,
103+
segmentFeatureContexts.put(featureName,
83104
new ImmutablePair<String, FeatureContext>(
84105
segmentContext.getName(), featureContext));
85106
}
@@ -103,14 +124,13 @@ private static Flags evaluateFeatures(
103124

104125
if (contextFeatures != null) {
105126
for (FeatureContext featureContext : contextFeatures.getAdditionalProperties().values()) {
106-
if (segmentFeatureContexts.containsKey(featureContext.getFeatureKey())) {
127+
if (segmentFeatureContexts.containsKey(featureContext.getName())) {
107128
ImmutablePair<String, FeatureContext> segmentNameFeaturePair = segmentFeatureContexts
108-
.get(featureContext.getFeatureKey());
129+
.get(featureContext.getName());
109130
featureContext = segmentNameFeaturePair.getRight();
110131
flags.setAdditionalProperty(
111132
featureContext.getName(),
112133
new FlagResult().withEnabled(featureContext.getEnabled())
113-
.withFeatureKey(featureContext.getFeatureKey())
114134
.withName(featureContext.getName())
115135
.withValue(featureContext.getValue())
116136
.withReason(
@@ -148,10 +168,11 @@ private static FlagResult getFlagResultFromFeatureContext(
148168
Float limit = startPercentage + weight.floatValue();
149169
if (startPercentage <= percentageValue && percentageValue < limit) {
150170
return new FlagResult().withEnabled(featureContext.getEnabled())
151-
.withFeatureKey(featureContext.getFeatureKey())
152171
.withName(featureContext.getName())
153172
.withValue(variant.getValue())
154-
.withReason("SPLIT; weight=" + weight.intValue())
173+
.withReason("SPLIT; weight=" + BigDecimal.valueOf(weight)
174+
.stripTrailingZeros()
175+
.toPlainString())
155176
.withMetadata(featureContext.getMetadata());
156177
}
157178
startPercentage = limit;
@@ -160,7 +181,6 @@ private static FlagResult getFlagResultFromFeatureContext(
160181
}
161182

162183
return new FlagResult().withEnabled(featureContext.getEnabled())
163-
.withFeatureKey(featureContext.getFeatureKey())
164184
.withName(featureContext.getName())
165185
.withValue(featureContext.getValue())
166186
.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

src/test/java/com/flagsmith/flagengine/models/FlagsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ public void testFromEvaluationResult__metadata__expected() throws FlagsmithClien
1717
com.flagsmith.flagengine.Flags flagResults = new com.flagsmith.flagengine.Flags()
1818
.withAdditionalProperty("feature_1", new FlagResult()
1919
.withEnabled(true)
20-
.withFeatureKey("feature_1")
2120
.withName("Feature 1")
2221
.withValue("value_1")
2322
.withReason("DEFAULT")
2423
.withMetadata(Map.of("flagsmithId", 1)))
2524
.withAdditionalProperty("feature_2", new FlagResult()
2625
.withEnabled(false)
27-
.withFeatureKey("feature_2")
2826
.withName("Feature 2")
2927
.withValue(null)
3028
.withReason("DEFAULT")

0 commit comments

Comments
 (0)