diff --git a/reconcile/templating/lib/rendering.py b/reconcile/templating/lib/rendering.py index 2fe5f359ef..3dac8e77b2 100644 --- a/reconcile/templating/lib/rendering.py +++ b/reconcile/templating/lib/rendering.py @@ -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"): @@ -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)