Skip to content

Commit ef840f4

Browse files
committed
Remove index from patch class.
1 parent 8af3c86 commit ef840f4

File tree

2 files changed

+58
-57
lines changed
  • stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers

2 files changed

+58
-57
lines changed

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/utils.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def check_commands(
103103
"""
104104
if path.nest:
105105
part_nest = ""
106-
107106
for index, path_part in enumerate(path.parts):
108107

109108
# Create nested dictionaries if not present for merge operations
@@ -126,20 +125,21 @@ def check_commands(
126125

127126
part_nest += f"['{path_part}']"
128127

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"]:
134129

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+
)
143143

144144

145145
def remove_commands(commands: ESCommandSet, path: ElasticPath) -> None:
@@ -150,15 +150,15 @@ def remove_commands(commands: ESCommandSet, path: ElasticPath) -> None:
150150
path (ElasticPath): Path to value to be removed
151151
152152
"""
153-
if path.index is not None:
153+
if isinstance(path.key, int):
154154
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 "
156157
)
157158

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+
)
162162

163163

164164
def add_commands(
@@ -180,21 +180,21 @@ def add_commands(
180180
value = (
181181
from_path.variable_name
182182
if operation.op == "move"
183-
else f"ctx._source{from_path.es_location}"
183+
else f"ctx._source{from_path.es_path}"
184184
)
185+
185186
else:
186187
value = f"params.{path.param_key}"
187188
params[path.param_key] = operation.value
188189

189-
if path.index is not None:
190+
if isinstance(path.key, int):
190191
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 "
194195
)
195196

196-
else:
197-
commands.add(f"ctx._source{path.es_location} = {value};")
197+
commands.add(f"ctx._source{path.es_location} = {value};")
198198

199199

200200
def test_commands(
@@ -210,10 +210,19 @@ def test_commands(
210210
value = f"params.{path.param_key}"
211211
params[path.param_key] = operation.value
212212

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+
213222
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});}}"
217226
)
218227

219228

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/models/patch.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ class ElasticPath(BaseModel):
7373

7474
parts: list[str] = []
7575

76+
path: Optional[str] = None
7677
key: Optional[str] = None
7778
nest: Optional[str] = None
78-
location: Optional[str] = None
79-
index: Optional[int] = None
8079

80+
es_path: Optional[str] = None
8181
es_key: Optional[str] = None
8282
es_nest: Optional[str] = None
83-
es_location: Optional[str] = None
84-
es_index: Optional[str] = None
8583

8684
variable_name: Optional[str] = None
8785
param_key: Optional[str] = None
@@ -100,34 +98,28 @@ def validate_model(cls, data: Any):
10098
data (Any): input data
10199
"""
102100
data["parts"] = data["path"].lstrip("/").split("/")
101+
103102
data["key"] = data["parts"].pop(-1)
103+
data["nest"] = "/".join(data["parts"])
104+
data["path"] = data["nest"] + "/" + data["key"]
104105

105-
if data["key"].lstrip("-").isdigit() or data["key"] == "-":
106-
data["index"] = -1 if data["key"] == "-" else int(data["key"])
107-
data["key"] = data["parts"].pop(-1)
106+
data["es_key"] = data["key"]
107+
data["es_nest"] = "".join([f"['{part}']" for part in data["parts"]])
108+
data["es_path"] = data["es_nest"] + f"['{data['es_key']}']"
108109

109-
data["nest"] = ".".join(data["parts"])
110-
data["location"] = data["nest"] + "." + data["key"]
110+
if data["key"].lstrip("-").isdigit() or data["key"] == "-":
111+
data["key"] = -1 if data["key"] == "-" else int(data["key"])
112+
data["es_key"] = (
113+
f"ctx._source{data['es_nest']}.size() - {-data['key']}"
114+
if data["key"] < 0
115+
else str(data["key"])
116+
)
117+
# data["es_key"] = f"[{data['key']}]"
118+
data["es_path"] = data["es_nest"] + f"[{data['es_key']}]"
111119

112-
data["es_key"] = f"['{data['key']}']"
113-
data["es_nest"] = "".join([f"['{part}']" for part in data["parts"]])
114-
data["es_location"] = data["es_nest"] + data["es_key"]
115120
data[
116121
"variable_name"
117-
] = f"{data['nest'].replace('.','_').replace(':','_')}_{data['key'].replace(':','_')}"
118-
data["param_key"] = data["location"].translate(replacements)
119-
120-
if "index" in data:
121-
data["es_index"] = (
122-
f"ctx._source{data['es_location']}.size() - {-data['index']}"
123-
if data["index"] < 0
124-
else str(data["index"])
125-
)
126-
127-
data["es_location"] = data["es_location"] + f"[{data['es_index']}]"
128-
129-
data[
130-
"variable_name"
131-
] = f"{data['location'].replace('.','_').replace(':','_')}_{data['index']}"
122+
] = f"{data['nest'].replace('.','_').replace(':','_')}_{str(data['key']).replace(':','_')}"
123+
data["param_key"] = data["path"].translate(replacements)
132124

133125
return data

0 commit comments

Comments
 (0)