2727from flag_engine .utils .types import SupportsStr , get_casting_function
2828
2929
30+ class FeatureContextWithSegmentName (typing .TypedDict ):
31+ feature_context : FeatureContext
32+ segment_name : str
33+
34+
3035def get_evaluation_result (context : EvaluationContext ) -> EvaluationResult :
3136 """
3237 Get the evaluation result for a given context.
@@ -35,7 +40,10 @@ def get_evaluation_result(context: EvaluationContext) -> EvaluationResult:
3540 :return: EvaluationResult containing the context, flags, and segments
3641 """
3742 segments : list [SegmentResult ] = []
38- segment_feature_contexts : dict [SupportsStr , FeatureContext ] = {}
43+ flags : list [FlagResult ] = []
44+
45+ segment_feature_contexts : dict [SupportsStr , FeatureContextWithSegmentName ] = {}
46+
3947 for segment_context in (context .get ("segments" ) or {}).values ():
4048 if not is_context_in_segment (context , segment_context ):
4149 continue
@@ -56,35 +64,44 @@ def get_evaluation_result(context: EvaluationContext) -> EvaluationResult:
5664 "priority" ,
5765 constants .DEFAULT_PRIORITY ,
5866 )
59- < segment_feature_contexts [feature_key ].get (
67+ < ( segment_feature_contexts [feature_key ][ "feature_context" ]) .get (
6068 "priority" ,
6169 constants .DEFAULT_PRIORITY ,
6270 )
6371 ):
64- segment_feature_contexts [feature_key ] = override_feature_context
72+ segment_feature_contexts [feature_key ] = (
73+ FeatureContextWithSegmentName (
74+ feature_context = override_feature_context ,
75+ segment_name = segment_context ["name" ],
76+ )
77+ )
6578
66- identity_key = get_context_value (context , "$.identity.key" )
67- flags : list [FlagResult ] = [
68- (
69- {
70- "enabled" : segment_feature_context ["enabled" ],
71- "feature_key" : segment_feature_context ["feature_key" ],
72- "name" : segment_feature_context ["name" ],
73- "reason" : f"TARGETING_MATCH; segment={ segment_context ['name' ]} " ,
74- "value" : segment_feature_context .get ("value" ),
75- }
76- if (
77- segment_feature_context := segment_feature_contexts .get (
78- feature_context ["feature_key" ],
79- )
79+ identity_key = (
80+ identity_context ["key" ]
81+ if (identity_context := context .get ("identity" ))
82+ else None
83+ )
84+ for feature_context in (context .get ("features" ) or {}).values ():
85+ if feature_context_with_segment_name := segment_feature_contexts .get (
86+ feature_context ["feature_key" ],
87+ ):
88+ feature_context = feature_context_with_segment_name ["feature_context" ]
89+ flags .append (
90+ {
91+ "enabled" : feature_context ["enabled" ],
92+ "feature_key" : feature_context ["feature_key" ],
93+ "name" : feature_context ["name" ],
94+ "reason" : f"TARGETING_MATCH; segment={ feature_context_with_segment_name ['segment_name' ]} " ,
95+ "value" : feature_context .get ("value" ),
96+ }
8097 )
81- else get_flag_result_from_feature_context (
98+ continue
99+ flags .append (
100+ get_flag_result_from_feature_context (
82101 feature_context = feature_context ,
83102 key = identity_key ,
84103 )
85104 )
86- for feature_context in (context .get ("features" ) or {}).values ()
87- ]
88105
89106 return {
90107 "context" : context ,
0 commit comments