Skip to content

Commit 785068d

Browse files
committed
Canonical logical operators.
1 parent 0579c02 commit 785068d

7 files changed

Lines changed: 114 additions & 38 deletions

File tree

‎jsonpath/env.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ def is_truthy(self, obj: object) -> bool:
177177
def compare(self, left: object, operator: str, right: object) -> bool:
178178
"""Object comparison within JSONPath filters."""
179179
# TODO: better - this (whole method) is temporary
180-
if operator == "and":
180+
if operator == "&&":
181181
return self.is_truthy(left) and self.is_truthy(right)
182-
if operator == "or":
182+
if operator == "||":
183183
return self.is_truthy(left) or self.is_truthy(right)
184184

185185
if operator == "==":

‎jsonpath/filter.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def __init__(self, operator: str, right: FilterExpression):
184184
self.right = right
185185

186186
def __str__(self) -> str:
187-
return f"{self.operator} {self.right}"
187+
return f"{self.operator}{self.right}"
188188

189189
def __eq__(self, other: object) -> bool:
190190
return (
@@ -194,7 +194,7 @@ def __eq__(self, other: object) -> bool:
194194
)
195195

196196
def _evaluate(self, context: FilterContext, right: object) -> object:
197-
if self.operator == "not":
197+
if self.operator == "!":
198198
return not context.env.is_truthy(right)
199199
raise JSONPathTypeError(f"unknown operator {self.operator} {self.right}")
200200

‎jsonpath/lex.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ def compile_rules(self) -> Pattern[str]:
145145
(TOKEN_FLOAT, r"-?\d+\.\d*(?:e[+-]?\d+)?"),
146146
(TOKEN_INT, r"-?\d+(?:e[+\-]?\d+)?\b"),
147147
(TOKEN_DDOT, r"\.\."),
148+
(TOKEN_AND, self.bool_and_pattern),
149+
(TOKEN_OR, self.bool_or_pattern),
148150
(TOKEN_ROOT, re.escape(self.env.root_token)),
149151
(TOKEN_SELF, re.escape(self.env.self_token)),
150152
(TOKEN_UNION, re.escape(self.env.union_token)),
151153
(TOKEN_INTERSECTION, re.escape(self.env.intersection_token)),
152154
(TOKEN_FILTER_CONTEXT, re.escape(self.env.filter_context_token)),
153-
(TOKEN_AND, self.bool_and_pattern),
154-
(TOKEN_OR, self.bool_or_pattern),
155155
(TOKEN_IN, r"in"),
156156
(TOKEN_TRUE, r"[Tt]rue"),
157157
(TOKEN_FALSE, r"[Ff]alse"),

‎jsonpath/parse.py‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,21 @@ class Parser:
121121
TOKEN_RPAREN: PRECEDENCE_LOWEST,
122122
}
123123

124-
BINARY_OPERATORS = frozenset(
125-
(
126-
TOKEN_AND,
127-
TOKEN_CONTAINS,
128-
TOKEN_EQ,
129-
TOKEN_GE,
130-
TOKEN_GT,
131-
TOKEN_IN,
132-
TOKEN_LE,
133-
TOKEN_LG,
134-
TOKEN_LT,
135-
TOKEN_NE,
136-
TOKEN_OR,
137-
TOKEN_RE,
138-
)
139-
)
124+
# Mapping of operator token to canonical string.
125+
BINARY_OPERATORS = {
126+
TOKEN_AND: "&&",
127+
TOKEN_CONTAINS: "contains",
128+
TOKEN_EQ: "==",
129+
TOKEN_GE: ">=",
130+
TOKEN_GT: ">",
131+
TOKEN_IN: "in",
132+
TOKEN_LE: "<",
133+
TOKEN_LG: "<>",
134+
TOKEN_LT: "<",
135+
TOKEN_NE: "!=",
136+
TOKEN_OR: "||",
137+
TOKEN_RE: "=~",
138+
}
140139

141140
PREFIX_OPERATORS = frozenset(
142141
[
@@ -343,7 +342,7 @@ def parse_prefix_expression(self, stream: TokenStream) -> FilterExpression:
343342
tok = stream.next_token()
344343
assert tok.kind == TOKEN_NOT
345344
return PrefixExpression(
346-
operator="not",
345+
operator="!",
347346
right=self.parse_filter_selector(
348347
stream, precedence=self.PRECEDENCE_LOGICALRIGHT
349348
),
@@ -355,7 +354,9 @@ def parse_infix_expression(
355354
tok = stream.next_token()
356355
precedence = self.PRECEDENCES.get(tok.kind, self.PRECEDENCE_LOWEST)
357356
return InfixExpression(
358-
left, tok.value, self.parse_filter_selector(stream, precedence)
357+
left,
358+
self.BINARY_OPERATORS[tok.kind],
359+
self.parse_filter_selector(stream, precedence),
359360
)
360361

361362
def parse_grouped_expression(self, stream: TokenStream) -> FilterExpression:

‎tests/test_ietf.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,26 @@ class Case:
159159
want=[{"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
160160
),
161161
# Case(
162-
# description="filter selector - ",
163-
# path="",
162+
# description="filter selector - Existence of non-singular queries",
163+
# path="$[?(@.*)]",
164164
# data=FILTER_SELECTOR_DATA,
165-
# want=[],
165+
# want=[
166+
# [3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
167+
# {"p": 1, "q": 2, "r": 3, "s": 5, "t": {"u": 6}},
168+
# ],
166169
# ),
167170
# Case(
168-
# description="filter selector - ",
169-
# path="",
171+
# description="filter selector - Nested filters",
172+
# path="$[?(@[?(@.b)])] ",
170173
# data=FILTER_SELECTOR_DATA,
171-
# want=[],
172-
# ),
173-
# Case(
174-
# description="filter selector - ",
175-
# path="",
176-
# data=FILTER_SELECTOR_DATA,
177-
# want=[],
174+
# want=[[3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}]],
178175
# ),
176+
Case(
177+
description="filter selector - Array value logical OR",
178+
path='$.a[?(@<2 || @.b == "k")]',
179+
data=FILTER_SELECTOR_DATA,
180+
want=[1, {"b": "k"}],
181+
),
179182
# Case(
180183
# description="filter selector - ",
181184
# path="",

‎tests/test_lex.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,78 @@ class Case:
643643
),
644644
],
645645
),
646+
Case(
647+
description="filter expression with logical ||",
648+
path="[?(@.some == 1 || @.some == 5)]",
649+
want=[
650+
Token(
651+
kind=TOKEN_FILTER_START,
652+
value="[?(",
653+
index=0,
654+
path="[?(@.some == 1 || @.some == 5)]",
655+
),
656+
Token(
657+
kind=TOKEN_SELF,
658+
value="@",
659+
index=3,
660+
path="[?(@.some == 1 || @.some == 5)]",
661+
),
662+
Token(
663+
kind=TOKEN_PROPERTY,
664+
value="some",
665+
index=5,
666+
path="[?(@.some == 1 || @.some == 5)]",
667+
),
668+
Token(
669+
kind=TOKEN_EQ,
670+
value="==",
671+
index=10,
672+
path="[?(@.some == 1 || @.some == 5)]",
673+
),
674+
Token(
675+
kind=TOKEN_INT,
676+
value="1",
677+
index=13,
678+
path="[?(@.some == 1 || @.some == 5)]",
679+
),
680+
Token(
681+
kind=TOKEN_OR,
682+
value="||",
683+
index=15,
684+
path="[?(@.some == 1 || @.some == 5)]",
685+
),
686+
Token(
687+
kind=TOKEN_SELF,
688+
value="@",
689+
index=18,
690+
path="[?(@.some == 1 || @.some == 5)]",
691+
),
692+
Token(
693+
kind=TOKEN_PROPERTY,
694+
value="some",
695+
index=20,
696+
path="[?(@.some == 1 || @.some == 5)]",
697+
),
698+
Token(
699+
kind=TOKEN_EQ,
700+
value="==",
701+
index=25,
702+
path="[?(@.some == 1 || @.some == 5)]",
703+
),
704+
Token(
705+
kind=TOKEN_INT,
706+
value="5",
707+
index=28,
708+
path="[?(@.some == 1 || @.some == 5)]",
709+
),
710+
Token(
711+
kind=TOKEN_FILTER_END,
712+
value=")]",
713+
index=29,
714+
path="[?(@.some == 1 || @.some == 5)]",
715+
),
716+
],
717+
),
646718
Case(
647719
description="filter self dot property in list literal",
648720
path="[?(@.thing in [1, '1'])]",

‎tests/test_parse.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ class Case:
160160
Case(
161161
description="filter with logical not",
162162
path="$.some[?(@.thing > 1 and not $.other)]",
163-
want="$['some'][?(@['thing'] > 1 and not $['other'])]",
163+
want="$['some'][?(@['thing'] > 1 && !$['other'])]",
164164
),
165165
Case(
166166
description="filter with grouped expression",
167167
path="$.some[?(@.thing > 1 and ($.foo or $.bar))]",
168-
want="$['some'][?(@['thing'] > 1 and $['foo'] or $['bar'])]",
168+
want="$['some'][?(@['thing'] > 1 && $['foo'] || $['bar'])]",
169169
),
170170
]
171171

0 commit comments

Comments
 (0)