Skip to content

str/repr, join, and setting - for arrays #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions jsonpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@
try:
from itertools import izip
str = unicode
encode_str = lambda u: u.encode("raw_unicode_escape")
except ImportError: # Python 3
izip = zip
encode_str = lambda u: u

try:
from collections.abc import Mapping, Sequence
except ImportError: # Python 3
from collections import Mapping, Sequence

from itertools import tee
from itertools import tee, chain
import re
import copy

Expand Down Expand Up @@ -225,7 +227,11 @@ def set(self, doc, value, inplace=True):

(parent, part) = self.to_last(doc)

parent[part] = value
if isinstance(parent, Sequence) and part == '-':
parent.append(value)
else:
parent[part] = value

return doc

@classmethod
Expand Down Expand Up @@ -293,6 +299,23 @@ def __contains__(self, item):
""" Returns True if self contains the given ptr """
return self.contains(item)

def join(self, suffix):
""" Returns a new JsonPointer with the given suffix append to this ptr """
if isinstance(suffix, JsonPointer):
suffix_parts = suffix.parts
elif isinstance(suffix, str):
suffix_parts = JsonPointer(suffix).parts
else:
suffix_parts = suffix
try:
return JsonPointer.from_parts(chain(self.parts, suffix_parts))
except:
raise JsonPointerException("Invalid suffix")

def __truediv__(self, suffix): # Python 3
return self.join(suffix)
__div__ = __truediv__ # Python 2

@property
def path(self):
"""Returns the string representation of the pointer
Expand All @@ -318,6 +341,12 @@ def __eq__(self, other):
def __hash__(self):
return hash(tuple(self.parts))

def __str__(self):
return encode_str(self.path)

def __repr__(self):
return "JsonPointer(" + repr(self.path) + ")"

@classmethod
def from_parts(cls, parts):
"""Constructs a JsonPointer from a list of (unescaped) paths
Expand Down
91 changes: 91 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,50 @@ def test_round_trip(self):
new_ptr = JsonPointer.from_parts(parts)
self.assertEqual(ptr, new_ptr)

def test_str_and_repr(self):
paths = [
("", "", "JsonPointer({u}'')"),
("/foo", "/foo", "JsonPointer({u}'/foo')"),
("/foo/0", "/foo/0", "JsonPointer({u}'/foo/0')"),
("/", "/", "JsonPointer({u}'/')"),
("/a~1b", "/a~1b", "JsonPointer({u}'/a~1b')"),
("/c%d", "/c%d", "JsonPointer({u}'/c%d')"),
("/e^f", "/e^f", "JsonPointer({u}'/e^f')"),
("/g|h", "/g|h", "JsonPointer({u}'/g|h')"),
("/i\\j", "/i\\j", "JsonPointer({u}'/i\\\\j')"),
("/k\"l", "/k\"l", "JsonPointer({u}'/k\"l')"),
("/ ", "/ ", "JsonPointer({u}'/ ')"),
("/m~0n", "/m~0n", "JsonPointer({u}'/m~0n')"),
]
for path, ptr_str, ptr_repr in paths:
ptr = JsonPointer(path)
self.assertEqual(path, ptr.path)

if sys.version_info[0] == 2:
u_str = "u"
else:
u_str = ""
self.assertEqual(ptr_str, str(ptr))
self.assertEqual(ptr_repr.format(u=u_str), repr(ptr))

if sys.version_info[0] == 2:
path = "/\xee"
ptr_str = b"/\xee"
ptr_repr = "JsonPointer(u'/\\xee')"
else:
path = "/\xee"
ptr_str = "/\xee"
ptr_repr = "JsonPointer('/\xee')"
ptr = JsonPointer(path)
self.assertEqual(path, ptr.path)

self.assertEqual(ptr_str, str(ptr))
self.assertEqual(ptr_repr, repr(ptr))

# should not be unicode in Python 2
self.assertIsInstance(str(ptr), str)
self.assertIsInstance(repr(ptr), str)

def test_parts(self):
paths = [
("", []),
Expand Down Expand Up @@ -131,6 +175,42 @@ def test_contains_magic(self):
self.assertTrue(self.ptr1 in self.ptr1)
self.assertFalse(self.ptr3 in self.ptr1)

def test_join(self):

ptr12a = self.ptr1.join(self.ptr2)
self.assertEqual(ptr12a.path, "/a/b/c/a/b")

ptr12b = self.ptr1.join(self.ptr2.parts)
self.assertEqual(ptr12b.path, "/a/b/c/a/b")

ptr12c = self.ptr1.join(self.ptr2.parts[0:1])
self.assertEqual(ptr12c.path, "/a/b/c/a")

ptr12d = self.ptr1.join("/a/b")
self.assertEqual(ptr12d.path, "/a/b/c/a/b")

ptr12e = self.ptr1.join(["a", "b"])
self.assertEqual(ptr12e.path, "/a/b/c/a/b")

self.assertRaises(JsonPointerException, self.ptr1.join, 0)

def test_join_magic(self):

ptr12a = self.ptr1 / self.ptr2
self.assertEqual(ptr12a.path, "/a/b/c/a/b")

ptr12b = self.ptr1 / self.ptr2.parts
self.assertEqual(ptr12b.path, "/a/b/c/a/b")

ptr12c = self.ptr1 / self.ptr2.parts[0:1]
self.assertEqual(ptr12c.path, "/a/b/c/a")

ptr12d = self.ptr1 / "/a/b"
self.assertEqual(ptr12d.path, "/a/b/c/a/b")

ptr12e = self.ptr1 / ["a", "b"]
self.assertEqual(ptr12e.path, "/a/b/c/a/b")

class WrongInputTests(unittest.TestCase):

def test_no_start_slash(self):
Expand Down Expand Up @@ -193,6 +273,12 @@ def test_set(self):
newdoc = set_pointer(doc, "/foo/1", "cod", inplace=False)
self.assertEqual(resolve_pointer(newdoc, "/foo/1"), "cod")

self.assertEqual(len(doc["foo"]), 2)
newdoc = set_pointer(doc, "/foo/-", "xyz", inplace=False)
self.assertEqual(resolve_pointer(newdoc, "/foo/2"), "xyz")
self.assertEqual(len(doc["foo"]), 2)
self.assertEqual(len(newdoc["foo"]), 3)

newdoc = set_pointer(doc, "/", 9, inplace=False)
self.assertEqual(resolve_pointer(newdoc, "/"), 9)

Expand All @@ -209,6 +295,11 @@ def test_set(self):
set_pointer(doc, "/foo/1", "cod")
self.assertEqual(resolve_pointer(doc, "/foo/1"), "cod")

self.assertEqual(len(doc["foo"]), 2)
set_pointer(doc, "/foo/-", "xyz")
self.assertEqual(resolve_pointer(doc, "/foo/2"), "xyz")
self.assertEqual(len(doc["foo"]), 3)

set_pointer(doc, "/", 9)
self.assertEqual(resolve_pointer(doc, "/"), 9)

Expand Down