8000 add union update and delete · testerman77/python-jsonpath-rw@5d653b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d653b2

Browse files
committed
add union update and delete
1 parent 0655c46 commit 5d653b2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

jsonpath_rw/jsonpath.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ def is_singular(self):
425425
def find(self, data):
426426
return self.left.find(data) + self.right.find(data)
427427

428+
def update(self, data, val):
429+
self.left.update(data, val)
430+
self.right.update(data, val)
431+
return data
432+
433+
def delete(self, data):
434+
self.left.delete(data)
435+
self.right.delete(data)
436+
return data
437+
428438
class Intersect(JSONPath):
429439
"""
430440
JSONPath for bits that match *both* patterns.

tests/test_jsonpath.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ def test_descendants_value(self):
162162
('foo..baz', {'foo': [{'baz': 1}, {'baz': 2}]}, [1, 2] ),
163163
])
164164

165+
def test_union_value(self):
166+
self.check_cases([
167+
('foo | bar', {'foo': 1, 'bar': 2}, [1, 2])
168+
])
169+
165170
def test_parent_value(self):
166171
self.check_cases([('foo.baz.`parent`', {'foo': {'baz': 3}}, [{'baz': 3}]),
167172
('foo.`parent`.foo.baz.`parent`.baz.bizzle', {'foo': {'baz': {'bizzle': 5}}}, [5])])
@@ -328,6 +333,11 @@ def test_update_child(self):
328333
({'foo': {'bar': 1}}, 'foo.bar', 'baz', {'foo': {'bar': 'baz'}})
329334
])
330335

336+
def test_update_union(self):
337+
self.check_update_cases([
338+
({'foo': 1, 'bar': 2}, 'foo | bar', 3, {'foo': 3, 'bar': 3})
339+
])
340+
331341
def test_update_where(self):
332342
self.check_update_cases([
333343
({'foo': {'bar': {'baz': 1}}, 'bar': {'baz': 2}},
@@ -426,6 +436,12 @@ def test_delete_descendants_where(self):
426436
{'foo': {'flag': 1}, 'baz': {'bar': 2}})
427437
])
428438

439+
def test_delete_union(self):
440+
self.check_delete_cases([
441+
({'foo': 1, 'bar': 2}, 'foo | bar', {}),
442+
({'foo': 1, 'bar': 2, 'baz': 3}, 'foo | bar', {'baz': 3}),
443+
])
444+
429445
def test_delete_index(self):
430446
self.check_delete_cases([
431447
([42], '[0]', []),

0 commit comments

Comments
 (0)
0