77import json
88from typing import Any , Dict , List , Optional , Set , Union
99
10- from stac_fastapi .types .stac import Item
10+ from stac_fastapi .types .stac import Item , PatchAddReplaceTest , PatchRemove
1111
1212MAX_LIMIT = 10000
1313
@@ -151,18 +151,17 @@ def merge_to_operations(data: Dict) -> List:
151151 for key , value in data .copy ().items ():
152152
153153 if value is None :
154- operations .append ({"op" : "remove" , "path" : key })
155- continue
154+ operations .append (PatchRemove (op = "remove" , path = key ))
156155
157156 elif isinstance (value , dict ):
158157 nested_operations = merge_to_operations (value )
159158
160159 for nested_operation in nested_operations :
161- nested_operation [ " path" ] = f"{ key } .{ nested_operation [ ' path' ] } "
160+ nested_operation . path = f"{ key } .{ nested_operation . path } "
162161 operations .append (nested_operation )
163162
164163 else :
165- operations .append ({ "op" : " add" , " path" : key , " value" : value } )
164+ operations .append (PatchAddReplaceTest ( op = " add" , path = key , value = value ) )
166165
167166 return operations
168167
@@ -178,19 +177,15 @@ def operations_to_script(operations: List) -> Dict:
178177 """
179178 source = ""
180179 for operation in operations :
181- if operation ["op" ] in ["copy" , "move" ]:
182- source += (
183- f"ctx._source.{ operation ['path' ]} = ctx._source.{ operation ['from' ]} ;"
184- )
180+ if operation .op in ["copy" , "move" ]:
181+ source += f"ctx._source.{ operation .path } = ctx._source.{ getattr (operation , 'from' )} ;"
185182
186- if operation [ "op" ] in ["remove" , "move" ]:
187- nest , partition , key = operation [ " path" ] .rpartition ("." )
183+ if operation . op in ["remove" , "move" ]:
184+ nest , partition , key = operation . path .rpartition ("." )
188185 source += f"ctx._source.{ nest + partition } remove('{ key } ');"
189186
190- if operation ["op" ] in ["add" , "replace" ]:
191- source += (
192- f"ctx._source.{ operation ['path' ]} = { json .dumps (operation ['value' ])} ;"
193- )
187+ if operation .op in ["add" , "replace" ]:
188+ source += f"ctx._source.{ operation .path } = { json .dumps (operation .value )} ;"
194189
195190 return {
196191 "source" : source ,
0 commit comments