|
| 1 | +"""A patched version of jsonpatch |
| 2 | +
|
| 3 | +We need this because of: https://github.com/stefankoegl/python-json-patch/issues/138 |
| 4 | +
|
| 5 | +The core of this patch is in `DiffBuilder._item_removed`. The rest is just boilerplate |
| 6 | +that's been copied over with little to no changes. |
| 7 | +""" |
| 8 | + |
| 9 | +from jsonpatch import _ST_REMOVE |
| 10 | +from jsonpatch import DiffBuilder as _DiffBuilder |
| 11 | +from jsonpatch import JsonPatch as _JsonPatch |
| 12 | +from jsonpatch import JsonPointer, RemoveOperation, _path_join |
| 13 | + |
| 14 | + |
| 15 | +def apply_patch(doc, patch, in_place=False, pointer_cls=JsonPointer): |
| 16 | + if isinstance(patch, (str, bytes)): |
| 17 | + patch = JsonPatch.from_string(patch, pointer_cls=pointer_cls) |
| 18 | + else: |
| 19 | + patch = JsonPatch(patch, pointer_cls=pointer_cls) |
| 20 | + return patch.apply(doc, in_place) |
| 21 | + |
| 22 | + |
| 23 | +def make_patch(src, dst, pointer_cls=JsonPointer): |
| 24 | + return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls) |
| 25 | + |
| 26 | + |
| 27 | +class JsonPatch(_JsonPatch): |
| 28 | + @classmethod |
| 29 | + def from_diff( |
| 30 | + cls, |
| 31 | + src, |
| 32 | + dst, |
| 33 | + optimization=True, |
| 34 | + dumps=None, |
| 35 | + pointer_cls=JsonPointer, |
| 36 | + ): |
| 37 | + json_dumper = dumps or cls.json_dumper |
| 38 | + builder = DiffBuilder(src, dst, json_dumper, pointer_cls=pointer_cls) |
| 39 | + builder._compare_values("", None, src, dst) |
| 40 | + ops = list(builder.execute()) |
| 41 | + return cls(ops, pointer_cls=pointer_cls) |
| 42 | + |
| 43 | + |
| 44 | +class DiffBuilder(_DiffBuilder): |
| 45 | + def _item_removed(self, path, key, item): |
| 46 | + new_op = RemoveOperation( |
| 47 | + { |
| 48 | + "op": "remove", |
| 49 | + "path": _path_join(path, key), |
| 50 | + }, |
| 51 | + pointer_cls=self.pointer_cls, |
| 52 | + ) |
| 53 | + new_index = self.insert(new_op) |
| 54 | + self.store_index(item, new_index, _ST_REMOVE) |
0 commit comments