Skip to content

Commit

Permalink
Add pointers to the fluent API
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Feb 29, 2024
1 parent 6fc896a commit 2d9e952
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ for value in values:
# ...
```

`Query` objects are iterable and can only be iterated once. Pass the query to `list()` (or other sequence) to get a list of results that can be iterated multiple times or otherwise manipulated.

```python
from jsonpath import query

# data = ...

values = list(
query("$.some[[email protected]]", data)
.skip(5)
.limit(10)
.values()
)

print(values[1])
```

## Chainable methods

The following `Query` methods all return `self` (the same `Query` instance), so method calls can be chained to further manipulate the underlying iterator.
Expand Down
9 changes: 6 additions & 3 deletions jsonpath/fluent_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

if TYPE_CHECKING:
from jsonpath import JSONPathMatch
from jsonpath import JSONPointer


class Query:
Expand Down Expand Up @@ -123,14 +124,16 @@ def values(self) -> Iterable[object]:
return (m.obj for m in self._it)

def locations(self) -> Iterable[str]:
"""Return an iterable of normalized paths for each match."""
"""Return an iterable of normalized paths, one for each match."""
return (m.path for m in self._it)

def items(self) -> Iterable[Tuple[str, object]]:
"""Return an iterable of (object, normalized path) tuples for each match."""
"""Return an iterable of (object, path) tuples, one for each match."""
return ((m.path, m.obj) for m in self._it)

# TODO: def pointers
def pointers(self) -> Iterable[JSONPointer]:
"""Return an iterable of JSONPointers, one for each match."""
return (m.pointer() for m in self._it)

def first_one(self) -> Optional[JSONPathMatch]:
"""Return the first `JSONPathMatch` or `None` if there were no matches."""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_fluent_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from jsonpath import JSONPathMatch
from jsonpath import JSONPointer
from jsonpath import query


Expand Down Expand Up @@ -240,3 +241,10 @@ def test_query_tee() -> None:
rv2 = it2.skip(2).one()
assert rv2 is not None
assert rv2.value == 2 # noqa: PLR2004


def test_query_pointers() -> None:
"""Test that we can get pointers from a query."""
pointers = list(query("$.some.*", {"some": [0, 1, 2, 3]}).pointers())
assert len(pointers) == 4 # noqa: PLR2004
assert pointers[0] == JSONPointer("/some/0")

0 comments on commit 2d9e952

Please sign in to comment.