Skip to content

Commit c1710ae

Browse files
committed
Add CompoundJSONPath.query()
1 parent f766b50 commit c1710ae

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

jsonpath/path.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,30 @@ async def finditer_async(
432432

433433
return matches
434434

435+
def query(
436+
self,
437+
data: Union[str, IOBase, Sequence[Any], Mapping[str, Any]],
438+
*,
439+
filter_context: Optional[FilterContextVars] = None,
440+
) -> Query:
441+
"""Return a `Query` iterator over matches found by applying this path to _data_.
442+
443+
Arguments:
444+
data: A JSON document or Python object implementing the `Sequence`
445+
or `Mapping` interfaces.
446+
filter_context: Arbitrary data made available to filters using
447+
the _filter context_ selector.
448+
449+
Returns:
450+
A query iterator.
451+
452+
Raises:
453+
JSONPathSyntaxError: If the path is invalid.
454+
JSONPathTypeError: If a filter expression attempts to use types in
455+
an incompatible way.
456+
"""
457+
return Query(self.finditer(data, filter_context=filter_context), self.env)
458+
435459
def union(self, path: JSONPath) -> CompoundJSONPath:
436460
"""Union of this path and another path."""
437461
return self.__class__(

tests/test_fluent_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,10 @@ def test_query_from_compiled_path() -> None:
276276
path = compile("$.some.*")
277277
it = path.query({"some": [0, 1, 2, 3]}).values()
278278
assert list(it) == [0, 1, 2, 3]
279+
280+
281+
def test_query_from_compiled_compound_path() -> None:
282+
"""Test that we can get a query iterator from a compiled path."""
283+
path = compile("$.some[0] | $.some[2]")
284+
it = path.query({"some": [0, 1, 2, 3]}).values()
285+
assert list(it) == [0, 2]

0 commit comments

Comments
 (0)