@@ -121,12 +121,38 @@ def __init__(self, *operations: Union[JSONPatchOperation, Mapping[str, JSONCompa
121
121
for operation in operations
122
122
]
123
123
124
- def evaluate (self , document : JSONCompatible ) -> JSONCompatible :
124
+ def evaluate (self , document : JSONCompatible , resolve_array_inserts : bool = False ) -> JSONCompatible :
125
125
"""Return the result of sequentially applying all patch operations
126
- to `document`, as a new document. `document` itself is not modified."""
126
+ to `document`, as a new document. `document` itself is not modified.
127
+
128
+ :param document: The initial value of the result.
129
+ :param resolve_array_inserts: When a patch operation appends a new array item
130
+ (path ends with "/-"), then subsequent patch operations into the new item
131
+ (path matches up to "/-") are modified such that "-" is replaced with the
132
+ actual array index. Supported for one array level only.
133
+ """
127
134
result = deepcopy (document )
128
- for operation in self ._operations :
129
- result = operation .apply (result )
135
+
136
+ if resolve_array_inserts :
137
+ array , ptr = None , None
138
+ for operation in self ._operations :
139
+ if operation .op == PatchOp .ADD :
140
+ if operation .path and operation .path [- 1 ] == '-' :
141
+ array = operation .path [:- 1 ].evaluate (result )
142
+ ptr = operation .path
143
+ elif array is not None and str (operation .path ).startswith (str (ptr )):
144
+ operation .path = ptr [:- 1 ] / str (len (array ) - 1 ) / operation .path [len (ptr ):]
145
+ else :
146
+ array , ptr = None , None
147
+ else :
148
+ array , ptr = None , None
149
+
150
+ result = operation .apply (result )
151
+
152
+ else :
153
+ for operation in self ._operations :
154
+ result = operation .apply (result )
155
+
130
156
return result
131
157
132
158
def aslist (self ) -> List [Dict [str , JSONCompatible ]]:
0 commit comments