Skip to content

Commit 7d428da

Browse files
committed
actually use the mapper
1 parent e395bc8 commit 7d428da

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

flag_engine/context/mappers.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@
44

55
from 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

168
def 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.

flag_engine/segments/evaluator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jsonpath_rfc9535
1010
import semver
1111

12+
from flag_engine.context.mappers import map_any_value_to_context_value
1213
from 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

225227
def _matches_context_value(

tests/unit/context/__init__.py

Whitespace-only changes.

tests/unit/context/test_mappers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

0 commit comments

Comments
 (0)