Skip to content

Commit 525acfd

Browse files
committed
feat: JSONPointer.join
1 parent cf97fec commit 525acfd

3 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
**Features**
1010

1111
- Added a command line interface, exposing JSONPath, JSON Pointer and JSON Patch features.
12-
- Added `JSONPointer.parent()`, a method that returns a the parent of the pointer, as a new `JSONPointer`.
13-
- Implemented `JSONPointer.__truediv__()` to allow creation of child pointers from an existing pointer.
12+
- Added `JSONPointer.parent()`, a method that returns the parent of the pointer, as a new `JSONPointer`.
13+
- Implemented `JSONPointer.__truediv__()` to allow creation of child pointers from an existing pointer using the slash (`/`) operator.
14+
- Added `JSONPointer.join()`, a method for creating child pointers. This is equivalent to using the slash (`/`) operator for each argument given to `join()`.
1415
- Added `JSONPointer.exists()`, a method that returns `True` if a the pointer can be resolved against some data, or `False` otherwise.
1516

1617
## Version 0.8.1

‎jsonpath/pointer.py‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
if TYPE_CHECKING:
2525
from .match import JSONPathMatch
2626

27-
UNDEFINED = object()
27+
28+
class _Undefined:
29+
def __str__(self) -> str:
30+
return "<jsonpath.pointer.UNDEFINED>"
31+
32+
33+
UNDEFINED = _Undefined()
2834

2935

3036
class JSONPointer:
@@ -195,8 +201,8 @@ def resolve_parent(
195201
Returns:
196202
A `(parent, object)` tuple, where parent will be `None` if this
197203
pointer points to the root node in the document. If the parent
198-
exists but the last object does not, `(parent, None)` will be
199-
returned.
204+
exists but the last object does not, `(parent, UNDEFINED)` will
205+
be returned.
200206
201207
Raises:
202208
JSONPointerIndexError: When attempting to access a sequence by
@@ -336,6 +342,7 @@ def exists(
336342
_True_ if this pointer can be resolved against _data_, or _False_
337343
otherwise.
338344
345+
**_New in version 0.9.0_**
339346
"""
340347
try:
341348
self.resolve(data)
@@ -347,6 +354,8 @@ def parent(self) -> JSONPointer:
347354
"""Return this pointer's parent, as a new `JSONPointer`.
348355
349356
If this pointer points to the document root, _self_ is returned.
357+
358+
**_New in version 0.9.0_**
350359
"""
351360
if not self.parts:
352361
return self
@@ -359,11 +368,11 @@ def parent(self) -> JSONPointer:
359368
)
360369

361370
def __truediv__(self, other: object) -> JSONPointer:
362-
"""Join this path with _other_.
371+
"""Join this pointer with _other_.
363372
364373
_other_ is expected to be a JSON Pointer string, possibly without a
365374
leading slash. If _other_ does have a leading slash, the previous
366-
pointer is ignored and a new JSONPath is returns from _other_.
375+
pointer is ignored and a new JSONPath is returned from _other_.
367376
368377
_other_ should not be a "Relative JSON Pointer".
369378
"""
@@ -386,6 +395,19 @@ def __truediv__(self, other: object) -> JSONPointer:
386395
self._encode(parts), parts=parts, unicode_escape=False, uri_decode=False
387396
)
388397

398+
def join(self, *parts: str) -> JSONPointer:
399+
"""Join this pointer with _parts_.
400+
401+
Each part is expected to be a JSON Pointer string, possibly without a
402+
leading slash. If a part does have a leading slash, the previous
403+
pointer is ignored and a new `JSONPath` is created, and processing of
404+
remaining parts continues.
405+
"""
406+
pointer = self
407+
for part in parts:
408+
pointer = pointer / part
409+
return pointer
410+
389411

390412
def resolve(
391413
pointer: Union[str, Iterable[Union[str, int]]],

‎tests/test_json_pointer.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_pointer_parent() -> None:
244244
assert parent.resolve(data) == {"some": {"thing": [1, 2, 3]}}
245245

246246

247-
def test_join_pointers() -> None:
247+
def test_join_pointers_with_slash() -> None:
248248
"""Test that we can join a pointer to a relative path with the `/` operator."""
249249
pointer = JSONPointer("/foo")
250250
assert str(pointer) == "/foo"
@@ -258,6 +258,21 @@ def test_join_pointers() -> None:
258258
pointer / 0
259259

260260

261+
def test_join_pointers() -> None:
262+
pointer = JSONPointer("/foo")
263+
assert str(pointer) == "/foo"
264+
assert str(pointer.join("bar")) == "/foo/bar"
265+
assert str(pointer.join("baz")) == "/foo/baz"
266+
assert str(pointer.join("bar/baz")) == "/foo/bar/baz"
267+
assert str(pointer.join("bar", "baz")) == "/foo/bar/baz"
268+
assert str(pointer.join("bar/baz", "0")) == "/foo/bar/baz/0"
269+
assert str(pointer.join("/bar")) == "/bar"
270+
assert str(pointer.join("/bar", "0")) == "/bar/0"
271+
272+
with pytest.raises(TypeError):
273+
pointer.join(0) # type: ignore
274+
275+
261276
def test_pointer_exists() -> None:
262277
data = {"some": {"thing": [1, 2, 3]}, "other": None}
263278
assert JSONPointer("/some/thing").exists(data) is True

0 commit comments

Comments
 (0)