Skip to content

Commit

Permalink
allow to patch multiple object in patch path by a single template
Browse files Browse the repository at this point in the history
Signed-off-by: Feng Huang <fehuang@redhat.com>
  • Loading branch information
BumbleFeng committed Jun 28, 2024
1 parent 2864f14 commit 9e59f56
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions reconcile/templating/lib/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ def render_output(self) -> str:
if isinstance(matched_value, list):

def get_identifier(data: dict[str, Any]) -> Any:
assert self.template.patch is not None # mypy
if not self.template.patch.identifier:
raise ValueError(
f"Expected identifier in patch for list at {self.template}"
)
if self.template.patch.identifier.startswith(
"$"
) and not self.template.patch.identifier.startswith("$ref"):
Expand All @@ -144,24 +139,35 @@ def get_identifier(data: dict[str, Any]) -> Any:
return None
return data.get(self.template.patch.identifier)

dta_identifier = get_identifier(data_to_add)
if not dta_identifier:
raise ValueError(
f"Expected identifier {self.template.patch.identifier} in data to add"
def update_data(data_to_add: dict[str, Any], matched_value: list) -> None:
assert self.template.patch is not None # mypy
if not self.template.patch.identifier:
raise ValueError(
f"Expected identifier in patch for list at {self.template}"
)
dta_identifier = get_identifier(data_to_add)
if not dta_identifier:
raise ValueError(
f"Expected identifier {self.template.patch.identifier} in data to add"
)
index = next(
(
index
for index, data in enumerate(matched_value)
if get_identifier(data) == dta_identifier
),
None,
)

index = next(
(
index
for index, data in enumerate(matched_value)
if get_identifier(data) == dta_identifier
),
None,
)
if index is None:
matched_value.append(data_to_add)
if index is None:
matched_value.append(data_to_add)
else:
matched_value[index] = data_to_add

if isinstance(data_to_add, list):
for d in data_to_add:
update_data(d, matched_value)
else:
matched_value[index] = data_to_add
update_data(data_to_add, matched_value)
else:
matched_value.update(data_to_add)

Expand Down

0 comments on commit 9e59f56

Please sign in to comment.