Skip to content

Commit 4bbe449

Browse files
authored
Merge pull request #354 from ansforge/chore/clarify-transforming-message-logs
Chore/clarify transforming message logs
2 parents c01a339 + c83bb3a commit 4bbe449

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

converter/converter/utils.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,38 @@ def delete_paths(data: Dict[str, Any], paths: List[str]) -> None:
5050
paths: List of dot-separated paths (e.g., "a.b.c")
5151
"""
5252

53-
def delete_recursively(d: Dict[str, Any], keys: List[str]) -> None:
53+
def delete_recursively(
54+
d: Dict[str, Any], keys: List[str], current_path: str
55+
) -> None:
5456
if not keys or not isinstance(d, (dict, list)):
5557
return
5658

5759
key = keys[0]
5860
if key.endswith("[]"): # Handle array notation
5961
key = key[:-2] # Remove the array notation
6062
if key in d and isinstance(d[key], list):
61-
for item in d[key]:
63+
for index, item in enumerate(d[key]):
6264
if isinstance(item, dict):
63-
delete_recursively(item, keys[1:])
65+
new_current_path = f"{current_path}.{key}[{index}]"
66+
delete_recursively(item, keys[1:], new_current_path)
6467
elif len(keys) == 1:
6568
# Delete target key if it exists
6669
if isinstance(d, dict):
67-
logger.info("Deleting key: %s", key)
70+
new_current_path = f"{current_path}.{key}"
71+
logger.info("Removing path: %s", new_current_path)
6872
d.pop(key, None)
6973
else:
7074
# Recurse if intermediate key exists
7175
if key in d:
72-
delete_recursively(d[key], keys[1:])
76+
new_current_path = f"{current_path}.{key}"
77+
delete_recursively(d[key], keys[1:], new_current_path)
7378
# Clean up empty dictionaries or lists
7479
if isinstance(d[key], (dict, list)) and not d[key]:
75-
logger.info("Deleting key: %s", key)
80+
logger.info("Removing path: %s", new_current_path)
7681
d.pop(key)
7782

7883
for path in paths:
79-
delete_recursively(data, path.strip("$.").split("."))
84+
delete_recursively(data, path.strip("$.").split("."), "$")
8085

8186

8287
def add_space_before_uppercase(text):
@@ -199,7 +204,7 @@ def update_json_value(data, jsonpath_query, new_value):
199204
for match in matches:
200205
old_value = get_field_value(data, str(match.full_path))
201206
logger.info(
202-
"Updating value from %s to %s at path %s",
207+
"Updating value from %s to %s at path $.%s",
203208
old_value,
204209
new_value,
205210
match.full_path,

converter/tests/test_utils.py

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,73 @@ def test_delete_paths_logs_info(self, mock_logger):
163163
first_call_args, _ = mock_logger.info.call_args_list[0]
164164
second_call_args, _ = mock_logger.info.call_args_list[1]
165165

166-
assert first_call_args[0] == "Deleting key: %s"
167-
assert first_call_args[1] == "a"
166+
assert first_call_args[0] == "Removing path: %s"
167+
assert first_call_args[1] == "$.a"
168168

169-
assert second_call_args[0] == "Deleting key: %s"
170-
assert second_call_args[1] == "c"
169+
assert second_call_args[0] == "Removing path: %s"
170+
assert second_call_args[1] == "$.b.c"
171+
172+
@patch("converter.utils.logger")
173+
def test_delete_array_logs_info(self, mock_logger):
174+
# Arrange
175+
data = {
176+
"a": [
177+
{
178+
"b": 1,
179+
},
180+
{
181+
"b": 2,
182+
"c": 3,
183+
},
184+
],
185+
}
186+
187+
# Act
188+
delete_paths(data, ["a[].b"])
189+
190+
# Assert logger has been called twice
191+
assert mock_logger.info.call_count == 2
192+
193+
# Check the log message content
194+
first_call_args, _ = mock_logger.info.call_args_list[0]
195+
second_call_args, _ = mock_logger.info.call_args_list[1]
196+
197+
assert first_call_args[0] == "Removing path: %s"
198+
assert first_call_args[1] == "$.a[0].b"
199+
200+
assert second_call_args[0] == "Removing path: %s"
201+
assert second_call_args[1] == "$.a[1].b"
202+
203+
@patch("converter.utils.logger")
204+
def test_delete_empty_dict_logs_info(self, mock_logger):
205+
# Arrange
206+
data = {
207+
"a": {
208+
"b": {
209+
"c": 1,
210+
},
211+
}
212+
}
213+
214+
# Act
215+
delete_paths(data, ["a.b.c"])
216+
217+
# Assert logger has been called three times
218+
assert mock_logger.info.call_count == 3
219+
220+
# Check the log message content
221+
first_call_args, _ = mock_logger.info.call_args_list[0]
222+
second_call_args, _ = mock_logger.info.call_args_list[1]
223+
third_call_args, _ = mock_logger.info.call_args_list[2]
224+
225+
assert first_call_args[0] == "Removing path: %s"
226+
assert first_call_args[1] == "$.a.b.c"
227+
228+
assert second_call_args[0] == "Removing path: %s"
229+
assert second_call_args[1] == "$.a.b"
230+
231+
assert third_call_args[0] == "Removing path: %s"
232+
assert third_call_args[1] == "$.a"
171233

172234

173235
def test_translate_keys():
@@ -356,7 +418,7 @@ def test_update_json_value_logs_info(self, mock_logger):
356418

357419
# Check the log message content
358420
args, kwargs = mock_logger.info.call_args
359-
assert args[0] == "Updating value from %s to %s at path %s"
421+
assert args[0] == "Updating value from %s to %s at path $.%s"
360422
assert args[1] == "P1"
361423
assert args[2] == "P2"
362424
assert str(args[3]) == "qualification.details.priority"

0 commit comments

Comments
 (0)