From c03aff1121e84453f1407e2433a7623b29c3672b Mon Sep 17 00:00:00 2001 From: Jongmin Kim Date: Thu, 9 Jan 2025 12:35:24 +0900 Subject: [PATCH 1/2] refactor: refactor dict utils Signed-off-by: Jongmin Kim --- src/spaceone/core/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/spaceone/core/utils.py b/src/spaceone/core/utils.py index 4c6ffcd..4d28959 100644 --- a/src/spaceone/core/utils.py +++ b/src/spaceone/core/utils.py @@ -397,7 +397,11 @@ def _check_condition(match_option: str, val1, val2): def change_dict_value( - data: dict, dotted_key: str, change_value, change_type="value" + data: dict, + dotted_key: str, + change_value, + change_type="value", + is_new: bool = False, ) -> dict: # change_value = func or value(any type) if "." in dotted_key: @@ -423,7 +427,7 @@ def change_dict_value( data[key], rest, change_value, change_type ) else: - if dotted_key in data: + if dotted_key in data or is_new: data[dotted_key] = _change_value_by_type( change_type, data[dotted_key], change_value ) From 5b578aaec47822a60916ee29965e7b693798157c Mon Sep 17 00:00:00 2001 From: Jongmin Kim Date: Thu, 16 Jan 2025 17:51:01 +0900 Subject: [PATCH 2/2] refactor: refactor utils Signed-off-by: Jongmin Kim --- src/spaceone/core/utils.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/spaceone/core/utils.py b/src/spaceone/core/utils.py index 4d28959..7da532e 100644 --- a/src/spaceone/core/utils.py +++ b/src/spaceone/core/utils.py @@ -401,7 +401,7 @@ def change_dict_value( dotted_key: str, change_value, change_type="value", - is_new: bool = False, + allow_new_key: bool = False, ) -> dict: # change_value = func or value(any type) if "." in dotted_key: @@ -418,19 +418,30 @@ def change_dict_value( sub_rest = rest.split(".", 1)[1] list_data.append( change_dict_value( - sub_data, sub_rest, change_value, change_type + sub_data, + sub_rest, + change_value, + change_type, + allow_new_key=allow_new_key, ) ) data[key] = list_data elif isinstance(data[key], dict): data[key] = change_dict_value( - data[key], rest, change_value, change_type + data[key], + rest, + change_value, + change_type, + allow_new_key=allow_new_key, ) else: - if dotted_key in data or is_new: + if dotted_key in data: data[dotted_key] = _change_value_by_type( change_type, data[dotted_key], change_value ) + else: + if allow_new_key: + data[dotted_key] = change_value return data