Skip to content

Commit d9c9ace

Browse files
committed
Allow wildcards in segment lists.
1 parent 552f098 commit d9c9ace

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added the built-in `search` filter function.
99
- Added the built-in `value` filter function.
1010
- Pass the current environment to filter function validation.
11+
- Added support for the wildcard selector in selector segment lists.
1112

1213
**Fixes**
1314

jsonpath/parse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ def parse_slice(self, stream: TokenStream) -> SliceSelector:
328328
def parse_selector_list(self, stream: TokenStream) -> ListSelector:
329329
"""Parse a comma separated list JSONPath selectors from a stream of tokens."""
330330
tok = stream.next_token()
331-
list_items: List[Union[IndexSelector, PropertySelector, SliceSelector]] = []
331+
list_items: List[
332+
Union[IndexSelector, PropertySelector, SliceSelector, WildSelector]
333+
] = []
332334

333335
while stream.current.kind != TOKEN_RBRACKET:
334336
if stream.current.kind == TOKEN_INT:
@@ -371,6 +373,8 @@ def parse_selector_list(self, stream: TokenStream) -> ListSelector:
371373
)
372374
elif stream.current.kind == TOKEN_SLICE_START:
373375
list_items.append(self.parse_slice(stream))
376+
elif stream.current.kind == TOKEN_WILD:
377+
list_items.append(WildSelector(env=self.env, token=stream.current))
374378

375379
if stream.peek.kind == TOKEN_EOF:
376380
raise JSONPathSyntaxError(

jsonpath/selectors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ def __init__(
408408
*,
409409
env: JSONPathEnvironment,
410410
token: Token,
411-
items: List[Union[SliceSelector, IndexSelector, PropertySelector]],
411+
items: List[
412+
Union[SliceSelector, IndexSelector, PropertySelector, WildSelector]
413+
],
412414
) -> None:
413415
super().__init__(env=env, token=token)
414416
self.items = items
@@ -423,6 +425,8 @@ def __str__(self) -> str:
423425
buf.append(f"{start}:{stop}:{step}")
424426
elif isinstance(item, PropertySelector):
425427
buf.append(f"'{item.name}'")
428+
elif isinstance(item, WildSelector):
429+
buf.append("*")
426430
else:
427431
buf.append(str(item.index))
428432
return f"[{', '.join(buf)}]"

tests/test_ietf.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ class Case:
9696
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
9797
want=[1, 2],
9898
),
99-
# Case(
100-
# description="wildcard selector - double wild",
101-
# path="$.o[*, *]",
102-
# data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
103-
# want=[1, 2, 1, 2],
104-
# ),
99+
Case(
100+
description="wildcard selector - double wild",
101+
path="$.o[*, *]",
102+
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
103+
want=[1, 2, 1, 2],
104+
),
105105
Case(
106106
description="wildcard selector - dot property wild",
107107
path="$.a[*]",

0 commit comments

Comments
 (0)