Skip to content

Commit

Permalink
Add CompoundJSONPath.query()
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Jul 10, 2024
1 parent f766b50 commit c1710ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions jsonpath/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ async def finditer_async(

return matches

def query(
self,
data: Union[str, IOBase, Sequence[Any], Mapping[str, Any]],
*,
filter_context: Optional[FilterContextVars] = None,
) -> Query:
"""Return a `Query` iterator over matches found by applying this path to _data_.
Arguments:
data: A JSON document or Python object implementing the `Sequence`
or `Mapping` interfaces.
filter_context: Arbitrary data made available to filters using
the _filter context_ selector.
Returns:
A query iterator.
Raises:
JSONPathSyntaxError: If the path is invalid.
JSONPathTypeError: If a filter expression attempts to use types in
an incompatible way.
"""
return Query(self.finditer(data, filter_context=filter_context), self.env)

def union(self, path: JSONPath) -> CompoundJSONPath:
"""Union of this path and another path."""
return self.__class__(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_fluent_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,10 @@ def test_query_from_compiled_path() -> None:
path = compile("$.some.*")
it = path.query({"some": [0, 1, 2, 3]}).values()
assert list(it) == [0, 1, 2, 3]


def test_query_from_compiled_compound_path() -> None:
"""Test that we can get a query iterator from a compiled path."""
path = compile("$.some[0] | $.some[2]")
it = path.query({"some": [0, 1, 2, 3]}).values()
assert list(it) == [0, 2]

0 comments on commit c1710ae

Please sign in to comment.