Skip to content

Commit

Permalink
Adding checks for existing properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysrevans3 committed Sep 17, 2024
1 parent 5400e42 commit fe8530a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 7 additions & 1 deletion stac_fastapi/core/stac_fastapi/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,17 @@ def operations_to_script(operations: List) -> Dict:
"""
source = ""
for operation in operations:
nest, partition, key = operation.path.rpartition(".")
if nest:
source += f"if (!ctx._source.containsKey('{nest}')){{Debug.explain('{nest} does not exist');}}"

if operation.op != "add":
source += f"if (!ctx._source.{nest + partition}containsKey('{key}')){{Debug.explain('{operation.path} does not exist');}}"

if operation.op in ["copy", "move"]:
source += f"ctx._source.{operation.path} = ctx._source.{getattr(operation, 'from')};"

if operation.op in ["remove", "move"]:
nest, partition, key = operation.path.rpartition(".")
source += f"ctx._source.{nest + partition}remove('{key}');"

if operation.op in ["add", "replace"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,12 +947,15 @@ async def json_patch_item(
script = operations_to_script(script_operations)

if not new_collection_id and not new_item_id:
await self.client.update(
index=index_by_collection_id(collection_id),
id=mk_item_id(item_id, collection_id),
script=script,
refresh=refresh,
)
try:
await self.client.update(
index=index_by_collection_id(collection_id),
id=mk_item_id(item_id, collection_id),
script=script,
refresh=refresh,
)
except exceptions.BadRequestError as e:
raise KeyError(f"{e.info['error']['caused_by']['to_string']}")

if new_collection_id:
await self.client.reindex(
Expand Down

0 comments on commit fe8530a

Please sign in to comment.