@@ -103,7 +103,6 @@ def check_commands(
103
103
"""
104
104
if path .nest :
105
105
part_nest = ""
106
-
107
106
for index , path_part in enumerate (path .parts ):
108
107
109
108
# Create nested dictionaries if not present for merge operations
@@ -126,20 +125,21 @@ def check_commands(
126
125
127
126
part_nest += f"['{ path_part } ']"
128
127
129
- if path .index or from_path or op in ["remove" , "replace" , "test" ]:
130
- commands .add (
131
- f"if (!ctx._source{ path .es_nest } .containsKey('{ path .key } '))"
132
- f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
133
- )
128
+ if from_path or op in ["remove" , "replace" , "test" ]:
134
129
135
- if from_path and path .index is not None :
136
- commands .add (
137
- f"if ((ctx._source{ path .es_location } instanceof ArrayList"
138
- f" && ctx._source{ path .es_location } .size() < { abs (path .index )} )"
139
- f" || (!(ctx._source{ path .es_location } instanceof ArrayList)"
140
- f" && !ctx._source{ path .es_location } .containsKey('{ path .index } ')))"
141
- f"{{Debug.explain('{ path .es_location } does not exist');}}"
142
- )
130
+ if isinstance (path .key , int ):
131
+ commands .add (
132
+ f"if ((ctx._source{ path .es_nest } instanceof ArrayList"
133
+ f" && ctx._source{ path .es_nest } .size() < { abs (path .key )} )"
134
+ f" || (!(ctx._source{ path .es_nest } instanceof ArrayList)"
135
+ f" && !ctx._source{ path .es_nest } .containsKey('{ path .key } ')))"
136
+ f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
137
+ )
138
+ else :
139
+ commands .add (
140
+ f"if (!ctx._source{ path .es_nest } .containsKey('{ path .key } '))"
141
+ f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
142
+ )
143
143
144
144
145
145
def remove_commands (commands : ESCommandSet , path : ElasticPath ) -> None :
@@ -150,15 +150,15 @@ def remove_commands(commands: ESCommandSet, path: ElasticPath) -> None:
150
150
path (ElasticPath): Path to value to be removed
151
151
152
152
"""
153
- if path .index is not None :
153
+ if isinstance ( path .key , int ) :
154
154
commands .add (
155
- f"def { path .variable_name } = ctx._source{ path .es_location } .remove({ path .es_index } );"
155
+ f"if ((ctx._source{ path .es_nest } instanceof ArrayList"
156
+ f"{{def { path .variable_name } = ctx._source{ path .es_nest } .remove({ path .es_key } );}} else "
156
157
)
157
158
158
- else :
159
- commands .add (
160
- f"def { path .variable_name } = ctx._source{ path .es_nest } .remove('{ path .key } ');"
161
- )
159
+ commands .add (
160
+ f"def { path .variable_name } = ctx._source{ path .es_nest } .remove('{ path .key } ');"
161
+ )
162
162
163
163
164
164
def add_commands (
@@ -180,21 +180,21 @@ def add_commands(
180
180
value = (
181
181
from_path .variable_name
182
182
if operation .op == "move"
183
- else f"ctx._source{ from_path .es_location } "
183
+ else f"ctx._source{ from_path .es_path } "
184
184
)
185
+
185
186
else :
186
187
value = f"params.{ path .param_key } "
187
188
params [path .param_key ] = operation .value
188
189
189
- if path .index is not None :
190
+ if isinstance ( path .key , int ) :
190
191
commands .add (
191
- f"if (ctx._source{ path .es_location } instanceof ArrayList)"
192
- f"{{ctx._source{ path .es_location } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .es_index } , { value } )}}"
193
- f"else{{ctx._source { path . es_location } [' { path . index } '] = { value } }} "
192
+ f"if (ctx._source{ path .es_nest } instanceof ArrayList)"
193
+ f"{{ctx._source{ path .es_nest } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .es_key } , { value } )}}"
194
+ f" else "
194
195
)
195
196
196
- else :
197
- commands .add (f"ctx._source{ path .es_location } = { value } ;" )
197
+ commands .add (f"ctx._source{ path .es_location } = { value } ;" )
198
198
199
199
200
200
def test_commands (
@@ -210,10 +210,19 @@ def test_commands(
210
210
value = f"params.{ path .param_key } "
211
211
params [path .param_key ] = operation .value
212
212
213
+ if isinstance (path .key , int ):
214
+ commands .add (
215
+ f"if (ctx._source{ path .es_nest } instanceof ArrayList)"
216
+ f"{{if (ctx._source{ path .es_nest } [{ path .es_key } ] != { value } )"
217
+ f"{{Debug.explain('Test failed `{ path .es_path } `"
218
+ f" != ' + ctx._source{ path .es_path } );}}"
219
+ f"}} else "
220
+ )
221
+
213
222
commands .add (
214
- f"if (ctx._source{ path .es_location } != { value } )"
215
- f"{{Debug.explain('Test failed `{ path .location } `"
216
- f" != ' + ctx._source{ path .es_location } );}}"
223
+ f"if (ctx._source{ path .es_path } != { value } )"
224
+ f"{{Debug.explain('Test failed `{ path .es_path } `"
225
+ f" != ' + ctx._source{ path .es_path } );}}"
217
226
)
218
227
219
228
0 commit comments