Skip to content

Commit 6f9e3be

Browse files
authored
Fix priority of JSONPath lexer rules. (#39)
1 parent 2ad5520 commit 6f9e3be

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python JSONPath Change Log
22

3+
## Version 0.10.1
4+
5+
**Hot fix**
6+
7+
- Fixed priority of JSONPath lexer rules. Previously, standard short tokens (like `*` and `?`) had a higher priority than environment-controlled tokens (like `JSONPathEnvironment.keys_selector_token`), making it impossible to incorporate short token characters into longer environment-controlled tokens.
8+
39
## Version 0.10.0
410

511
**Breaking Changes**

jsonpath/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: 2023-present James Prior <jamesgr.prior@gmail.com>
22
#
33
# SPDX-License-Identifier: MIT
4-
__version__ = "0.10.0"
4+
__version__ = "0.10.1"

jsonpath/lex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ def compile_rules(self) -> Pattern[str]:
108108
(TOKEN_DOUBLE_QUOTE_STRING, self.double_quote_pattern),
109109
(TOKEN_SINGLE_QUOTE_STRING, self.single_quote_pattern),
110110
(TOKEN_RE_PATTERN, self.re_pattern),
111-
(TOKEN_WILD, r"\*"),
112111
(TOKEN_LIST_SLICE, self.slice_list_pattern),
113-
(TOKEN_FILTER, r"\?"),
114112
(TOKEN_FUNCTION, self.function_pattern),
115113
(TOKEN_DOT_PROPERTY, self.dot_property_pattern),
116114
(TOKEN_FLOAT, r"-?\d+\.\d*(?:e[+-]?\d+)?"),
@@ -125,6 +123,8 @@ def compile_rules(self) -> Pattern[str]:
125123
(TOKEN_INTERSECTION, re.escape(self.env.intersection_token)),
126124
(TOKEN_FILTER_CONTEXT, re.escape(self.env.filter_context_token)),
127125
(TOKEN_KEYS, re.escape(self.env.keys_selector_token)),
126+
(TOKEN_WILD, r"\*"),
127+
(TOKEN_FILTER, r"\?"),
128128
(TOKEN_IN, r"in"),
129129
(TOKEN_TRUE, r"[Tt]rue"),
130130
(TOKEN_FALSE, r"[Ff]alse"),

tests/test_env.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,15 @@ def test_no_unicode_escape() -> None:
158158
env = JSONPathEnvironment(unicode_escape=False)
159159
assert env.findall(selector, document) == []
160160
assert env.findall(selector, {"\\uD834\\uDD1E": "B"}) == ["B"]
161+
162+
163+
def test_custom_keys_selector_token() -> None:
164+
"""Test that we can change the non-standard keys selector."""
165+
166+
class MyJSONPathEnvironment(JSONPathEnvironment):
167+
keys_selector_token = "*~"
168+
169+
env = MyJSONPathEnvironment()
170+
data = {"foo": {"a": 1, "b": 2, "c": 3}}
171+
assert env.findall("$.foo.*~", data) == ["a", "b", "c"]
172+
assert env.findall("$.foo.*", data) == [1, 2, 3]

0 commit comments

Comments
 (0)