Skip to content

Commit 8f031a5

Browse files
committed
Treat bracketed quoted properties as single element lists.
1 parent 8ee4804 commit 8f031a5

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

‎jsonpath/lex.py‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ def __init__(self, *, env: JSONPathEnvironment) -> None:
8484
# [thing]
8585
self.bracketed_property_pattern = rf"\[\s*(?P<G_BPROP>{env.key_pattern})\s*]"
8686

87-
# ["thing"] or ['thing']
88-
self.quoted_property_pattern = (
89-
r"\[\s*(?P<G_PQUOTE>[\"'])(?P<G_PQUOTED>.*?)(?P=G_PQUOTE)\s*]"
90-
)
91-
9287
# .1
9388
# NOTE: `.1` can be a dot property where the key is "1".
9489
self.dot_index_pattern = r"\.\s*(?P<G_DINDEX>\d+)\b"
@@ -129,7 +124,6 @@ def __init__(self, *, env: JSONPathEnvironment) -> None:
129124
def compile_rules(self) -> Pattern[str]:
130125
"""Prepare regular expression rules."""
131126
rules = [
132-
(TOKEN_QUOTE_PROPERTY, self.quoted_property_pattern),
133127
(TOKEN_STRING, self.string_pattern),
134128
(TOKEN_RE_PATTERN, self.re_pattern),
135129
(TOKEN_DOT_INDEX, self.dot_index_pattern),
@@ -192,15 +186,10 @@ def tokenize(self, path: str) -> Iterator[Token]:
192186

193187
for match in self.rules.finditer(path):
194188
kind = match.lastgroup
189+
print(match, kind)
195190
assert kind is not None
196191

197-
if kind == TOKEN_QUOTE_PROPERTY:
198-
yield _token(
199-
kind=TOKEN_PROPERTY,
200-
value=match.group("G_PQUOTED"),
201-
index=match.start("G_PQUOTED"),
202-
)
203-
elif kind == TOKEN_DOT_PROPERTY:
192+
if kind == TOKEN_DOT_PROPERTY:
204193
yield _token(
205194
kind=TOKEN_PROPERTY,
206195
value=match.group("G_PROP"),

‎tests/test_lex.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ class Case:
8282
path='$["some"]',
8383
want=[
8484
Token(kind=TOKEN_ROOT, value="$", index=0, path='$["some"]'),
85-
Token(kind=TOKEN_PROPERTY, value="some", index=3, path='$["some"]'),
85+
Token(kind=TOKEN_LIST_START, value="[", index=1, path='$["some"]'),
86+
Token(kind=TOKEN_STRING, value="some", index=3, path='$["some"]'),
87+
Token(kind=TOKEN_LIST_END, value="]", index=8, path='$["some"]'),
8688
],
8789
),
8890
Case(
8991
description="root single quoted property",
9092
path="$['some']",
9193
want=[
9294
Token(kind=TOKEN_ROOT, value="$", index=0, path="$['some']"),
93-
Token(kind=TOKEN_PROPERTY, value="some", index=3, path="$['some']"),
95+
Token(kind=TOKEN_LIST_START, value="[", index=1, path="$['some']"),
96+
Token(kind=TOKEN_STRING, value="some", index=3, path="$['some']"),
97+
Token(kind=TOKEN_LIST_END, value="]", index=8, path="$['some']"),
9498
],
9599
),
96100
Case(
@@ -1024,6 +1028,20 @@ class Case:
10241028
),
10251029
],
10261030
),
1031+
Case(
1032+
description="list of quoted properties",
1033+
path="$['some', 'thing']",
1034+
want=[
1035+
Token(kind=TOKEN_ROOT, value="$", index=0, path="$['some', 'thing']"),
1036+
Token(kind=TOKEN_LIST_START, value="[", index=1, path="$['some', 'thing']"),
1037+
Token(kind=TOKEN_STRING, value="some", index=3, path="$['some', 'thing']"),
1038+
Token(kind=TOKEN_COMMA, value=",", index=8, path="$['some', 'thing']"),
1039+
Token(
1040+
kind=TOKEN_STRING, value="thing", index=11, path="$['some', 'thing']"
1041+
),
1042+
Token(kind=TOKEN_LIST_END, value="]", index=17, path="$['some', 'thing']"),
1043+
],
1044+
),
10271045
]
10281046

10291047

0 commit comments

Comments
 (0)