Skip to content

Commit 5c972d1

Browse files
committed
Consolidating add and copy commands.
1 parent b1aa252 commit 5c972d1

File tree

3 files changed

+41
-52
lines changed

3 files changed

+41
-52
lines changed

stac_fastapi/core/stac_fastapi/core/utilities.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

239212
def 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

254228
def 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

276258
def 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",

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,13 @@ async def json_patch_item(
988988
)
989989

990990
except exceptions.BadRequestError as exc:
991-
raise HTTPException(
992-
status_code=400, detail=exc.info["error"]["caused_by"]["to_string"]
993-
) from exc
991+
detail = (
992+
exc.info["error"]["caused_by"]["to_string"]
993+
if "to_string" in exc.info["error"]["caused_by"]
994+
else exc.info["error"]["caused_by"]
995+
)
996+
print("____________EXC INFO", exc.info)
997+
raise HTTPException(status_code=400, detail=detail) from exc
994998

995999
item = await self.get_one_item(collection_id, item_id)
9961000

@@ -1225,6 +1229,7 @@ async def json_patch_collection(
12251229
)
12261230

12271231
except exceptions.BadRequestError as exc:
1232+
print("EXC", exc.info)
12281233
raise HTTPException(
12291234
status_code=400, detail=exc.info["error"]["caused_by"]["to_string"]
12301235
) from exc

stac_fastapi/tests/clients/test_elasticsearch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ async def test_json_patch_item_add(ctx, core_client, txn_client):
307307

308308
assert updated_item["properties"]["foo"] == "bar"
309309
assert updated_item["properties"]["ext:hello"] == "world"
310-
assert updated_item["properties"]["area"] == [2500, -100, 10]
310+
assert updated_item["properties"]["area"] == [2500, 10, -200]
311311

312312

313313
@pytest.mark.asyncio
@@ -735,7 +735,7 @@ async def test_json_patch_collection_add(ctx, core_client, txn_client):
735735
)
736736

737737
assert updated_collection["summaries"]["foo"] == "bar"
738-
assert updated_collection["summaries"]["gsd"] == [15, 100]
738+
assert updated_collection["summaries"]["gsd"] == [30, 100]
739739

740740

741741
@pytest.mark.asyncio

0 commit comments

Comments
 (0)