@@ -199,42 +199,15 @@ def check_commands(
199199            f"{{Debug.explain('{ path .key }    does not exist in { path .nest }  ');}}" 
200200        )
201201
202- 
203- def  copy_commands (
204-     commands : List [str ],
205-     operation : PatchOperation ,
206-     path : ElasticPath ,
207-     from_path : ElasticPath ,
208- ) ->  None :
209-     """Copy value from path to from path. 
210- 
211-     Args: 
212-         commands (List[str]): current commands 
213-         operation (PatchOperation): Operation to be converted 
214-         op_path (ElasticPath): Path to copy to 
215-         from_path (ElasticPath): Path to copy from 
216- 
217-     """ 
218-     check_commands (commands = commands , op = operation .op , path = from_path , from_path = True )
219- 
220-     if  from_path .index :
202+     if  from_path  and  path .index  is  not   None :
221203        commands .append (
222-             f"if ((ctx._source.{ from_path .location }   instanceof ArrayList" 
223-             f" && ctx._source.{ from_path .location }  .size() < { from_path .index }  )" 
224-             f" || (!ctx._source.{ from_path .location }  .containsKey('{ from_path .index }  '))" 
225-             f"{{Debug.explain('{ from_path .path }   does not exist');}}" 
204+             f"if ((ctx._source.{ path .location }   instanceof ArrayList" 
205+             f" && ctx._source.{ path .location }  .size() < { path .index }  )" 
206+             f" || (!(ctx._source.properties.hello instanceof ArrayList)" 
207+             f" && !ctx._source.{ path .location }  .containsKey('{ path .index }  ')))" 
208+             f"{{Debug.explain('{ path .path }   does not exist');}}" 
226209        )
227210
228-     if  path .index :
229-         commands .append (
230-             f"if (ctx._source.{ path .location }   instanceof ArrayList)" 
231-             f"{{ctx._source.{ path .location }  .add({ path .index }  , { from_path .path }  )}}" 
232-             f"else{{ctx._source.{ path .path }   = { from_path .path }  }}" 
233-         )
234- 
235-     else :
236-         commands .append (f"ctx._source.{ path .path }   = ctx._source.{ from_path .path }  ;" )
237- 
238211
239212def  remove_commands (commands : List [str ], path : ElasticPath ) ->  None :
240213    """Remove value at path. 
@@ -244,15 +217,19 @@ def remove_commands(commands: List[str], path: ElasticPath) -> None:
244217        path (ElasticPath): Path to value to be removed 
245218
246219    """ 
247-     if  path .index :
248-         commands .append (f"ctx._source.{ path .location }  .remove({ path .index }  );" )
220+     print ("REMOVE PATH" , path )
221+     if  path .index  is  not   None :
222+         commands .append (f"def temp = ctx._source.{ path .location }  .remove({ path .index }  );" )
249223
250224    else :
251-         commands .append (f"ctx._source.{ path .nest }  .remove('{ path .key }  ');" )
225+         commands .append (f"def temp =  ctx._source.{ path .nest }  .remove('{ path .key }  ');" )
252226
253227
254228def  add_commands (
255-     commands : List [str ], operation : PatchOperation , path : ElasticPath 
229+     commands : List [str ],
230+     operation : PatchOperation ,
231+     path : ElasticPath ,
232+     from_path : ElasticPath ,
256233) ->  None :
257234    """Add value at path. 
258235
@@ -262,15 +239,20 @@ def add_commands(
262239        path (ElasticPath): path for value to be added 
263240
264241    """ 
265-     if  path .index :
242+     if  from_path  is  not   None :
243+         value  =  "temp"  if  operation .op  ==  "move"  else  f"ctx._source.{ from_path .path }  " 
244+     else :
245+         value  =  operation .json_value 
246+ 
247+     if  path .index  is  not   None :
266248        commands .append (
267249            f"if (ctx._source.{ path .location }   instanceof ArrayList)" 
268-             f"{{ctx._source.{ path .location }  .add({ path .index }  , { operation . json_value }  )}}" 
269-             f"else{{ctx._source.{ path .path }   = { operation . json_value }  }}" 
250+             f"{{ctx._source.{ path .location }  .{ ' add'   if   operation . op   in  [ 'add' ,  'move' ]  else   'set' }  ({ path .index }  , { value }  )}}" 
251+             f"else{{ctx._source.{ path .path }   = { value }  }}" 
270252        )
271253
272254    else :
273-         commands .append (f"ctx._source.{ path .path }   = { operation . json_value }  ;" )
255+         commands .append (f"ctx._source.{ path .path }   = { value }  ;" )
274256
275257
276258def  test_commands (
@@ -335,24 +317,26 @@ def operations_to_script(operations: List) -> Dict:
335317        )
336318
337319        check_commands (commands = commands , op = operation .op , path = path )
338- 
339-         if  operation .op  in  ["copy" , "move" ]:
340-             copy_commands (
341-                 commands = commands , operation = operation , path = path , from_path = from_path 
320+         if  from_path  is  not   None :
321+             check_commands (
322+                 commands = commands , op = operation .op , path = from_path , from_path = True 
342323            )
343324
344325        if  operation .op  in  ["remove" , "move" ]:
345326            remove_path  =  from_path  if  from_path  else  path 
346327            remove_commands (commands = commands , path = remove_path )
347328
348-         if  operation .op  in  ["add" , "replace" ]:
349-             add_commands (commands = commands , operation = operation , path = path )
329+         if  operation .op  in  ["add" , "replace" , "copy" , "move" ]:
330+             add_commands (
331+                 commands = commands , operation = operation , path = path , from_path = from_path 
332+             )
350333
351334        if  operation .op  ==  "test" :
352335            test_commands (commands = commands , operation = operation , path = path )
353336
354337        source  =  commands_to_source (commands = commands )
355338
339+     print ("____SOURCE" , source )
356340    return  {
357341        "source" : source ,
358342        "lang" : "painless" ,
0 commit comments