@@ -98,9 +98,9 @@ def matching_keys(self, keys: KeysView[str], key_regex: str) -> list[str]:
9898
9999 return list (filter (regex .match , keys ))
100100
101- def rename (
102- self , body : dict [str , Any ], key_parts : list [str ], output_key : str
103- ) -> dict [str , Any ]:
101+ def find (
102+ self , body : dict [str , Any ], key_parts : list [str ]
103+ ) -> tuple [ dict [str , Any ] , Any ]:
104104 """
105105 Rename terms
106106
@@ -112,16 +112,37 @@ def rename(
112112 :return: dict
113113 :rtype: update body
114114 """
115-
115+ value = None
116116 for key in self .matching_keys (body .keys (), key_parts [0 ]):
117-
118117 if len (key_parts ) > 1 :
119- body [key ] = self .rename (body [key ], key_parts [1 :], output_key )
118+ body [key ], value = self .find (body [key ], key_parts [1 :])
120119
121120 else :
122- body [ output_key ] = body [key ]
121+ value = body [key ]
123122 del body [key ]
124123
124+ return body , value
125+
126+ def add (
127+ self , body : dict [str , Any ], key_parts : list [str ], value : Any
128+ ) -> dict [str , Any ]:
129+ """
130+ Rename terms
131+
132+ :param body: current body
133+ :type body: dict
134+ :param key_parts: key parts seperated by delimiter
135+ :type key_parts: list
136+
137+ :return: dict
138+ :rtype: update body
139+ """
140+ if len (key_parts ) > 1 :
141+ body [key_parts [0 ]] = self .add (body [key_parts [0 ]], key_parts [1 :], value )
142+
143+ else :
144+ body [key_parts [0 ]] = value
145+
125146 return body
126147
127148 @update_input
@@ -134,6 +155,13 @@ def run(self, body: dict[str, Any]) -> dict[str, Any]:
134155 else [swap .regex ]
135156 )
136157
137- body = self .rename (body , key_parts , swap .output_key )
158+ output_key_parts = (
159+ swap .output_key .split (self .input .delimiter )
160+ if self .input .delimiter
161+ else [swap .output_key ]
162+ )
163+
164+ body , value = self .find (body , key_parts )
165+ body = self .add (body , output_key_parts , value )
138166
139167 return body
0 commit comments