File tree Expand file tree Collapse file tree 4 files changed +36
-13
lines changed
Expand file tree Collapse file tree 4 files changed +36
-13
lines changed Original file line number Diff line number Diff line change 44
55from flag_engine .segments .types import ContextValue , is_context_value
66
7- OverrideKey = tuple [
8- str ,
9- str ,
10- bool ,
11- typing .Any ,
12- ]
13- OverridesKey = tuple [OverrideKey , ...]
14-
157
168def map_any_value_to_context_value (value : typing .Any ) -> ContextValue :
179 """
18- Try to coerce a value of arbitrary type to a trait value type.
10+ Try to coerce a value of arbitrary type to a context value type.
1911 Union member-specific constraints, such as max string value length, are ignored here.
2012 Replicate behaviour from marshmallow/pydantic V1 for number-like strings.
2113 For decimals return an int in case of unset exponent.
Original file line number Diff line number Diff line change 99import jsonpath_rfc9535
1010import semver
1111
12+ from flag_engine .context .mappers import map_any_value_to_context_value
1213from flag_engine .context .types import (
1314 EvaluationContext ,
1415 FeatureContext ,
@@ -214,12 +215,13 @@ def get_context_value(
214215 context : EvaluationContext ,
215216 property : str ,
216217) -> ContextValue :
218+ value = None
217219 if property .startswith ("$." ):
218- return _get_context_value_getter (property )(context )
219- if identity_context := context .get ("identity" ):
220+ value = _get_context_value_getter (property )(context )
221+ elif identity_context := context .get ("identity" ):
220222 if traits := identity_context .get ("traits" ):
221- return traits .get (property )
222- return None
223+ value = traits .get (property )
224+ return map_any_value_to_context_value ( value )
223225
224226
225227def _matches_context_value (
Original file line number Diff line number Diff line change 1+ import typing
2+ from decimal import Decimal
3+
4+ import pytest
5+
6+ from flag_engine .context .mappers import map_any_value_to_context_value
7+ from flag_engine .segments .types import ContextValue
8+
9+
10+ @pytest .mark .parametrize (
11+ "value, expected_result" ,
12+ [
13+ (Decimal ("1" ), 1 ),
14+ (Decimal ("1.1" ), 1.1 ),
15+ ("1" , 1 ),
16+ ("1.0" , 1.0 ),
17+ ("-1.2" , - 1.2 ),
18+ ("-42" , - 42 ),
19+ ],
20+ )
21+ def test_map_any_value_to_context_value__returns_expected (
22+ value : typing .Any ,
23+ expected_result : ContextValue ,
24+ ) -> None :
25+ # When
26+ result = map_any_value_to_context_value (value )
27+
28+ # Then
29+ assert result == expected_result
You can’t perform that action at this time.
0 commit comments