22
33import com .flagsmith .flagengine .segments .SegmentEvaluator ;
44import com .flagsmith .flagengine .utils .Hashing ;
5+ import java .math .BigDecimal ;
56import java .util .ArrayList ;
67import java .util .HashMap ;
78import java .util .List ;
9+ import org .apache .commons .lang3 .StringUtils ;
810import 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" )
0 commit comments