Skip to content

Commit 211c5bd

Browse files
committed
support feature metadata, unset segments in context
1 parent 000e96c commit 211c5bd

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
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 = git@github.com:Flagsmith/engine-test-data.git
4-
tag = v2.4.0
4+
tag = v2.5.0

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

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,41 @@ private static SegmentEvaluationResult evaluateSegments(
4848
List<SegmentResult> segments = new ArrayList<>();
4949
HashMap<String, ImmutablePair<String, FeatureContext>> segmentFeatureContexts = new HashMap<>();
5050

51-
for (SegmentContext segmentContext : context.getSegments().getAdditionalProperties().values()) {
52-
if (SegmentEvaluator.isContextInSegment(context, segmentContext)) {
53-
segments.add(new SegmentResult().withKey(segmentContext.getKey())
54-
.withName(segmentContext.getName())
55-
.withMetadata(segmentContext.getMetadata()));
56-
57-
List<FeatureContext> segmentOverrides = segmentContext.getOverrides();
58-
59-
if (segmentOverrides != null) {
60-
for (FeatureContext featureContext : segmentOverrides) {
61-
String featureKey = featureContext.getFeatureKey();
62-
63-
if (segmentFeatureContexts.containsKey(featureKey)) {
64-
ImmutablePair<String, FeatureContext> existing = segmentFeatureContexts
65-
.get(featureKey);
66-
FeatureContext existingFeatureContext = existing.getRight();
67-
68-
Double existingPriority = existingFeatureContext.getPriority() == null
69-
? EngineConstants.WEAKEST_PRIORITY
70-
: existingFeatureContext.getPriority();
71-
Double featurePriority = featureContext.getPriority() == null
72-
? EngineConstants.WEAKEST_PRIORITY
73-
: featureContext.getPriority();
74-
75-
if (existingPriority < featurePriority) {
76-
continue;
51+
Segments contextSegments = context.getSegments();
52+
53+
if (contextSegments != null) {
54+
for (SegmentContext segmentContext : contextSegments.getAdditionalProperties().values()) {
55+
if (SegmentEvaluator.isContextInSegment(context, segmentContext)) {
56+
segments.add(new SegmentResult().withKey(segmentContext.getKey())
57+
.withName(segmentContext.getName())
58+
.withMetadata(segmentContext.getMetadata()));
59+
60+
List<FeatureContext> segmentOverrides = segmentContext.getOverrides();
61+
62+
if (segmentOverrides != null) {
63+
for (FeatureContext featureContext : segmentOverrides) {
64+
String featureKey = featureContext.getFeatureKey();
65+
66+
if (segmentFeatureContexts.containsKey(featureKey)) {
67+
ImmutablePair<String, FeatureContext> existing = segmentFeatureContexts
68+
.get(featureKey);
69+
FeatureContext existingFeatureContext = existing.getRight();
70+
71+
Double existingPriority = existingFeatureContext.getPriority() == null
72+
? EngineConstants.WEAKEST_PRIORITY
73+
: existingFeatureContext.getPriority();
74+
Double featurePriority = featureContext.getPriority() == null
75+
? EngineConstants.WEAKEST_PRIORITY
76+
: featureContext.getPriority();
77+
78+
if (existingPriority < featurePriority) {
79+
continue;
80+
}
7781
}
82+
segmentFeatureContexts.put(featureKey,
83+
new ImmutablePair<String, FeatureContext>(
84+
segmentContext.getName(), featureContext));
7885
}
79-
segmentFeatureContexts.put(featureKey,
80-
new ImmutablePair<String, FeatureContext>(
81-
segmentContext.getName(), featureContext));
8286
}
8387
}
8488
}
@@ -110,7 +114,8 @@ private static Flags evaluateFeatures(
110114
.withName(featureContext.getName())
111115
.withValue(featureContext.getValue())
112116
.withReason(
113-
"TARGETING_MATCH; segment=" + segmentNameFeaturePair.getLeft()));
117+
"TARGETING_MATCH; segment=" + segmentNameFeaturePair.getLeft())
118+
.withMetadata(featureContext.getMetadata()));
114119
} else {
115120
flags.setAdditionalProperty(featureContext.getName(),
116121
getFlagResultFromFeatureContext(featureContext, identityKey));
@@ -146,7 +151,8 @@ private static FlagResult getFlagResultFromFeatureContext(
146151
.withFeatureKey(featureContext.getFeatureKey())
147152
.withName(featureContext.getName())
148153
.withValue(variant.getValue())
149-
.withReason("SPLIT; weight=" + weight.intValue());
154+
.withReason("SPLIT; weight=" + weight.intValue())
155+
.withMetadata(featureContext.getMetadata());
150156
}
151157
startPercentage = limit;
152158
}
@@ -157,6 +163,7 @@ private static FlagResult getFlagResultFromFeatureContext(
157163
.withFeatureKey(featureContext.getFeatureKey())
158164
.withName(featureContext.getName())
159165
.withValue(featureContext.getValue())
160-
.withReason("DEFAULT");
166+
.withReason("DEFAULT")
167+
.withMetadata(featureContext.getMetadata());
161168
}
162169
}

0 commit comments

Comments
 (0)