use from/path instead of path/to for move, copy

This commit is contained in:
Stefan Kögl
2012-12-14 09:10:07 +01:00
parent 4803b288c2
commit bb46b6568f
2 changed files with 29 additions and 8 deletions

View File

@@ -409,16 +409,15 @@ class MoveOperation(PatchOperation):
"""Moves an object property or an array element to new location."""
def apply(self, obj):
subobj, part = self.pointer.to_last(obj)
from_ptr = jsonpointer.JsonPointer(self.operation['from'])
subobj, part = from_ptr.to_last(obj)
value = subobj[part]
to_ptr = jsonpointer.JsonPointer(self.operation['to'])
if self.pointer.contains(to_ptr):
if from_ptr.contains(self.pointer):
raise JsonPatchException('Cannot move values into its own children')
RemoveOperation({'op': 'remove', 'path': self.location}).apply(obj)
AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj)
RemoveOperation({'op': 'remove', 'path': self.operation['from']}).apply(obj)
AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)
class TestOperation(PatchOperation):
@@ -447,6 +446,7 @@ class CopyOperation(PatchOperation):
""" Copies an object property or an array element to a new location """
def apply(self, obj):
subobj, part = self.pointer.to_last(obj)
from_ptr = jsonpointer.JsonPointer(self.operation['from'])
subobj, part = from_ptr.to_last(obj)
value = copy.deepcopy(subobj[part])
AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj)
AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)

21
test.py Normal file
View File

@@ -0,0 +1,21 @@
import jsonpatch
import json
def run_test(test):
if 'comment' in test:
print test['comment']
if 'doc' in test and 'patch' in test:
try:
res = jsonpatch.apply_patch(test['doc'], test['patch'])
def tests_from_obj(tests):
for test in tests:
run_test(test)