Skip to content

Commit 896102d

Browse files
authored
Merge pull request #46 from PeterlitsZo/patch-1
Add method and add classmethod tag
2 parents 7d146bd + a5e9f91 commit 896102d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

jsonpointer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def to_last(self, doc):
193193
for part in self.parts[:-1]:
194194
doc = self.walk(doc, part)
195195

196-
return doc, self.get_part(doc, self.parts[-1])
196+
return doc, JsonPointer.get_part(doc, self.parts[-1])
197197

198198
def resolve(self, doc, default=_nothing):
199199
"""Resolves the pointer against doc and returns the referenced object"""
@@ -228,7 +228,8 @@ def set(self, doc, value, inplace=True):
228228
parent[part] = value
229229
return doc
230230

231-
def get_part(self, doc, part):
231+
@classmethod
232+
def get_part(cls, doc, part):
232233
"""Returns the next step in the correct type"""
233234

234235
if isinstance(doc, Mapping):
@@ -239,7 +240,7 @@ def get_part(self, doc, part):
239240
if part == '-':
240241
return part
241242

242-
if not self._RE_ARRAY_INDEX.match(str(part)):
243+
if not JsonPointer._RE_ARRAY_INDEX.match(str(part)):
243244
raise JsonPointerException("'%s' is not a valid sequence index" % part)
244245

245246
return int(part)
@@ -252,12 +253,17 @@ def get_part(self, doc, part):
252253
else:
253254
raise JsonPointerException("Document '%s' does not support indexing, "
254255
"must be mapping/sequence or support __getitem__" % type(doc))
256+
257+
def get_parts(self):
258+
"""Returns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b']"""
259+
260+
return self.parts
255261

256262

257263
def walk(self, doc, part):
258264
""" Walks one step in doc and returns the referenced part """
259265

260-
part = self.get_part(doc, part)
266+
part = JsonPointer.get_part(doc, part)
261267

262268
assert hasattr(doc, '__getitem__'), "invalid document type %s" % (type(doc),)
263269

tests.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,31 @@ def test_round_trip(self):
7070
ptr = JsonPointer(path)
7171
self.assertEqual(path, ptr.path)
7272

73-
parts = ptr.parts
73+
parts = ptr.get_parts()
74+
self.assertEqual(parts, ptr.parts)
7475
new_ptr = JsonPointer.from_parts(parts)
7576
self.assertEqual(ptr, new_ptr)
7677

78+
def test_parts(self):
79+
paths = [
80+
("", []),
81+
("/foo", ['foo']),
82+
("/foo/0", ['foo', '0']),
83+
("/", ['']),
84+
("/a~1b", ['a/b']),
85+
("/c%d", ['c%d']),
86+
("/e^f", ['e^f']),
87+
("/g|h", ['g|h']),
88+
("/i\\j", ['i\j']),
89+
("/k\"l", ['k"l']),
90+
("/ ", [' ']),
91+
("/m~0n", ['m~n']),
92+
('/\xee', ['\xee']),
93+
]
94+
for path in paths:
95+
ptr = JsonPointer(path[0])
96+
self.assertEqual(ptr.get_parts(), path[1])
97+
7798

7899
class ComparisonTests(unittest.TestCase):
79100

0 commit comments

Comments
 (0)