Skip to content

Commit

Permalink
Updating patch types.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysrevans3 committed Sep 16, 2024
1 parent 59661c5 commit 5400e42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
25 changes: 10 additions & 15 deletions stac_fastapi/core/stac_fastapi/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
from typing import Any, Dict, List, Optional, Set, Union

from stac_fastapi.types.stac import Item
from stac_fastapi.types.stac import Item, PatchAddReplaceTest, PatchRemove

MAX_LIMIT = 10000

Expand Down Expand Up @@ -151,18 +151,17 @@ def merge_to_operations(data: Dict) -> List:
for key, value in data.copy().items():

if value is None:
operations.append({"op": "remove", "path": key})
continue
operations.append(PatchRemove(op="remove", path=key))

elif isinstance(value, dict):
nested_operations = merge_to_operations(value)

for nested_operation in nested_operations:
nested_operation["path"] = f"{key}.{nested_operation['path']}"
nested_operation.path = f"{key}.{nested_operation.path}"
operations.append(nested_operation)

else:
operations.append({"op": "add", "path": key, "value": value})
operations.append(PatchAddReplaceTest(op="add", path=key, value=value))

return operations

Expand All @@ -178,19 +177,15 @@ def operations_to_script(operations: List) -> Dict:
"""
source = ""
for operation in operations:
if operation["op"] in ["copy", "move"]:
source += (
f"ctx._source.{operation['path']} = ctx._source.{operation['from']};"
)
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(".")
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"]:
source += (
f"ctx._source.{operation['path']} = {json.dumps(operation['value'])};"
)
if operation.op in ["add", "replace"]:
source += f"ctx._source.{operation.path} = {json.dumps(operation.value)};"

return {
"source": source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,16 +930,16 @@ async def json_patch_item(
script_operations = []

for operation in operations:
if operation["op"] in ["add", "replace"]:
if (
operation["path"] == "collection"
and collection_id != operation["value"]
):
await self.check_collection_exists(collection_id=operation["value"])
new_collection_id = operation["value"]
if operation.path in [
"collection",
"id",
] and operation.op in ["add", "replace"]:
if operation.path == "collection" and collection_id != operation.value:
await self.check_collection_exists(collection_id=operation.value)
new_collection_id = operation.value

if operation["path"] == "id" and item_id != operation["value"]:
new_item_id = operation["value"]
if operation.path == "id" and item_id != operation.value:
new_item_id = operation.value

else:
script_operations.append(operation)
Expand Down Expand Up @@ -1167,8 +1167,8 @@ async def json_patch_collection(

for operation in operations:
if (
operation["op"] in ["add", "replace"]
and operation["path"] == "collection"
operation.get("op") in ["add", "replace"]
and operation.get("path") == "collection"
and collection_id != operation["value"]
):
new_collection_id = operation["value"]
Expand Down

0 comments on commit 5400e42

Please sign in to comment.