Skip to content

Commit d63dc57

Browse files
authored
Merge pull request #58 from jg-rp/logical-existence
Fix filter query existence tests in logical expressions
2 parents 2832305 + 02e332c commit d63dc57

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

jsonpath/filter.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def set_children(self, children: List[FilterExpression]) -> None:
318318
class InfixExpression(FilterExpression):
319319
"""A pair of expressions and a comparison or logical operator."""
320320

321-
__slots__ = ("left", "operator", "right")
321+
__slots__ = ("left", "operator", "right", "logical")
322322

323323
def __init__(
324324
self,
@@ -329,10 +329,11 @@ def __init__(
329329
self.left = left
330330
self.operator = operator
331331
self.right = right
332+
self.logical = operator in ("&&", "||")
332333
super().__init__()
333334

334335
def __str__(self) -> str:
335-
if self.operator in ("&&", "||"):
336+
if self.logical:
336337
return f"({self.left} {self.operator} {self.right})"
337338
return f"{self.left} {self.operator} {self.right}"
338339

@@ -346,22 +347,22 @@ def __eq__(self, other: object) -> bool:
346347

347348
def evaluate(self, context: FilterContext) -> bool:
348349
left = self.left.evaluate(context)
349-
if isinstance(left, NodeList) and len(left) == 1:
350+
if not self.logical and isinstance(left, NodeList) and len(left) == 1:
350351
left = left[0].obj
351352

352353
right = self.right.evaluate(context)
353-
if isinstance(right, NodeList) and len(right) == 1:
354+
if not self.logical and isinstance(right, NodeList) and len(right) == 1:
354355
right = right[0].obj
355356

356357
return context.env.compare(left, self.operator, right)
357358

358359
async def evaluate_async(self, context: FilterContext) -> bool:
359360
left = await self.left.evaluate_async(context)
360-
if isinstance(left, NodeList) and len(left) == 1:
361+
if not self.logical and isinstance(left, NodeList) and len(left) == 1:
361362
left = left[0].obj
362363

363364
right = await self.right.evaluate_async(context)
364-
if isinstance(right, NodeList) and len(right) == 1:
365+
if not self.logical and isinstance(right, NodeList) and len(right) == 1:
365366
right = right[0].obj
366367

367368
return context.env.compare(left, self.operator, right)

tests/test_find.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class Case:
7575
data={"some": {"thing": "else", "foo": {"bar": "baz"}}},
7676
want=["some", "thing", "foo", "bar"],
7777
),
78+
Case(
79+
description="logical expr existence tests",
80+
path="$[[email protected] && @.b]",
81+
data=[{"a": True, "b": False}],
82+
want=[{"a": True, "b": False}],
83+
),
84+
Case(
85+
description="logical expr existence tests, alternate and",
86+
path="$[[email protected] and @.b]",
87+
data=[{"a": True, "b": False}],
88+
want=[{"a": True, "b": False}],
89+
),
7890
]
7991

8092

0 commit comments

Comments
 (0)