From 7fd9a6e0994548a748521fb74d3018ce3328e877 Mon Sep 17 00:00:00 2001 From: Maor Friedman Date: Mon, 3 Jun 2024 16:57:12 +0300 Subject: [PATCH] little helper --- reconcile/utils/helpers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/reconcile/utils/helpers.py b/reconcile/utils/helpers.py index 85895b7510..bbe9064e84 100644 --- a/reconcile/utils/helpers.py +++ b/reconcile/utils/helpers.py @@ -39,6 +39,23 @@ def flatten( return dict(items) +def unflatten( + d: Mapping[str, str], parent_key: str = "", sep: str = "." +) -> dict[str, Any]: + result = {} + for key, value in d.items(): + parts = key.split(sep) + d_ref = result + for part in parts[:-1]: + if part not in d_ref: + d_ref[part] = {} + d_ref = d_ref[part] + d_ref[parts[-1]] = value + if parent_key: + return result[parent_key] + return result + + Item = TypeVar("Item")