@@ -286,26 +286,29 @@ def compare_values(path, value, other):
286
286
for operation in compare_lists (path , value , other ):
287
287
yield operation
288
288
else :
289
- yield {'op' : 'replace' , 'path' : '/' .join (path ), 'value' : other }
289
+ ptr = JsonPointer .from_parts (path )
290
+ yield {'op' : 'replace' , 'path' : ptr .path , 'value' : other }
290
291
291
292
def compare_dicts (path , src , dst ):
292
293
for key in src :
293
294
if key not in dst :
294
- yield {'op' : 'remove' , 'path' : '/' .join (path + [key ])}
295
+ ptr = JsonPointer .from_parts (path + [key ])
296
+ yield {'op' : 'remove' , 'path' : ptr .path }
295
297
continue
296
298
current = path + [key ]
297
299
for operation in compare_values (current , src [key ], dst [key ]):
298
300
yield operation
299
301
for key in dst :
300
302
if key not in src :
303
+ ptr = JsonPointer .from_parts (path + [key ])
301
304
yield {'op' : 'add' ,
302
- 'path' : '/' . join ( path + [ key ]) ,
305
+ 'path' : ptr . path ,
303
306
'value' : dst [key ]}
304
307
305
308
def compare_lists (path , src , dst ):
306
309
return _compare_lists (path , src , dst )
307
310
308
- return cls (list (compare_dicts (['' ], src , dst )))
311
+ return cls (list (compare_dicts ([], src , dst )))
309
312
310
313
def to_string (self ):
311
314
"""Returns patch set as JSON string."""
@@ -644,14 +647,15 @@ def _compare_left(path, src, left, shift):
644
647
end = len (src )
645
648
# we need to `remove` elements from list tail to not deal with index shift
646
649
for idx in reversed (range (start + shift , end + shift )):
647
- current = path + [str (idx )]
650
+ ptr = JsonPointer . from_parts ( path + [str (idx )])
648
651
yield (
649
652
{'op' : 'remove' ,
650
653
# yes, there should be any value field, but we'll use it
651
654
# to apply `move` optimization a bit later and will remove
652
655
# it in _optimize function.
653
656
'value' : src [idx - shift ],
654
- 'path' : '/' .join (current )},
657
+ 'path' : ptr .path ,
658
+ },
655
659
shift - 1
656
660
)
657
661
shift -= 1
@@ -664,9 +668,9 @@ def _compare_right(path, dst, right, shift):
664
668
if end == - 1 :
665
669
end = len (dst )
666
670
for idx in range (start , end ):
667
- current = path + [str (idx )]
671
+ ptr = JsonPointer . from_parts ( path + [str (idx )])
668
672
yield (
669
- {'op' : 'add' , 'path' : '/' . join ( current ) , 'value' : dst [idx ]},
673
+ {'op' : 'add' , 'path' : ptr . path , 'value' : dst [idx ]},
670
674
shift + 1
671
675
)
672
676
shift += 1
0 commit comments