Skip to content

Commit e22abbf

Browse files
committed
$.
1 parent fa183fb commit e22abbf

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

flag_engine/segments/evaluator.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing
55
import warnings
66
from contextlib import suppress
7-
from functools import lru_cache, wraps
7+
from functools import lru_cache, partial, wraps
88

99
import jsonpath_rfc9535
1010
import semver
@@ -21,7 +21,7 @@
2121
from flag_engine.result.types import EvaluationResult, FlagResult, SegmentResult
2222
from flag_engine.segments import constants
2323
from flag_engine.segments.types import ConditionOperator, ContextValue, is_context_value
24-
from flag_engine.segments.utils import escape_double_quotes, get_matching_function
24+
from flag_engine.segments.utils import get_matching_function
2525
from flag_engine.utils.hashing import get_hashed_percentage_for_object_ids
2626
from flag_engine.utils.semver import is_semver
2727
from flag_engine.utils.types import SupportsStr, get_casting_function
@@ -350,6 +350,16 @@ def inner(
350350
}
351351

352352

353+
def _get_trait_value(
354+
context: EvaluationContext,
355+
trait_key: str,
356+
) -> ContextValue:
357+
if identity_context := context.get("identity"):
358+
if traits := identity_context.get("traits"):
359+
return traits.get(trait_key)
360+
return None
361+
362+
353363
@lru_cache
354364
def _get_context_value_getter(
355365
property: str,
@@ -366,11 +376,11 @@ def _get_context_value_getter(
366376
except jsonpath_rfc9535.JSONPathSyntaxError:
367377
# This covers a rare case when a trait starting with "$.",
368378
# but not a valid JSONPath, is used.
369-
compiled_query = jsonpath_rfc9535.compile(
370-
f'$.identity.traits["{escape_double_quotes(property)}"]',
371-
)
379+
return partial(_get_trait_value, trait_key=property)
372380

373381
def getter(context: EvaluationContext) -> ContextValue:
382+
if trait_value := _get_trait_value(context, property):
383+
return trait_value
374384
if typing.TYPE_CHECKING: # pragma: no cover
375385
# Ugly hack to satisfy mypy :(
376386
data = dict(context)

0 commit comments

Comments
 (0)