Skip to content

Commit 5400e42

Browse files
committed
Updating patch types.
1 parent 59661c5 commit 5400e42

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

stac_fastapi/core/stac_fastapi/core/utilities.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
from typing import Any, Dict, List, Optional, Set, Union
99

10-
from stac_fastapi.types.stac import Item
10+
from stac_fastapi.types.stac import Item, PatchAddReplaceTest, PatchRemove
1111

1212
MAX_LIMIT = 10000
1313

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

153153
if value is None:
154-
operations.append({"op": "remove", "path": key})
155-
continue
154+
operations.append(PatchRemove(op="remove", path=key))
156155

157156
elif isinstance(value, dict):
158157
nested_operations = merge_to_operations(value)
159158

160159
for nested_operation in nested_operations:
161-
nested_operation["path"] = f"{key}.{nested_operation['path']}"
160+
nested_operation.path = f"{key}.{nested_operation.path}"
162161
operations.append(nested_operation)
163162

164163
else:
165-
operations.append({"op": "add", "path": key, "value": value})
164+
operations.append(PatchAddReplaceTest(op="add", path=key, value=value))
166165

167166
return operations
168167

@@ -178,19 +177,15 @@ def operations_to_script(operations: List) -> Dict:
178177
"""
179178
source = ""
180179
for operation in operations:
181-
if operation["op"] in ["copy", "move"]:
182-
source += (
183-
f"ctx._source.{operation['path']} = ctx._source.{operation['from']};"
184-
)
180+
if operation.op in ["copy", "move"]:
181+
source += f"ctx._source.{operation.path} = ctx._source.{getattr(operation, 'from')};"
185182

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

190-
if operation["op"] in ["add", "replace"]:
191-
source += (
192-
f"ctx._source.{operation['path']} = {json.dumps(operation['value'])};"
193-
)
187+
if operation.op in ["add", "replace"]:
188+
source += f"ctx._source.{operation.path} = {json.dumps(operation.value)};"
194189

195190
return {
196191
"source": source,

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -930,16 +930,16 @@ async def json_patch_item(
930930
script_operations = []
931931

932932
for operation in operations:
933-
if operation["op"] in ["add", "replace"]:
934-
if (
935-
operation["path"] == "collection"
936-
and collection_id != operation["value"]
937-
):
938-
await self.check_collection_exists(collection_id=operation["value"])
939-
new_collection_id = operation["value"]
933+
if operation.path in [
934+
"collection",
935+
"id",
936+
] and operation.op in ["add", "replace"]:
937+
if operation.path == "collection" and collection_id != operation.value:
938+
await self.check_collection_exists(collection_id=operation.value)
939+
new_collection_id = operation.value
940940

941-
if operation["path"] == "id" and item_id != operation["value"]:
942-
new_item_id = operation["value"]
941+
if operation.path == "id" and item_id != operation.value:
942+
new_item_id = operation.value
943943

944944
else:
945945
script_operations.append(operation)
@@ -1167,8 +1167,8 @@ async def json_patch_collection(
11671167

11681168
for operation in operations:
11691169
if (
1170-
operation["op"] in ["add", "replace"]
1171-
and operation["path"] == "collection"
1170+
operation.get("op") in ["add", "replace"]
1171+
and operation.get("path") == "collection"
11721172
and collection_id != operation["value"]
11731173
):
11741174
new_collection_id = operation["value"]

0 commit comments

Comments
 (0)