Skip to content

Commit

Permalink
Re-use normalize_dict_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
castrapel committed May 12, 2023
1 parent e0facac commit 3a6478b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
25 changes: 2 additions & 23 deletions iambic/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,27 +733,6 @@ async def remove_expired_resources(
return resource


def recursively_convert_dict_keys(
obj: Union[dict, list], convert_function: callable[[str], str]
) -> Union[dict, list, str]:
"""
Recursively traverse a dictionary or list, converting all dictionary keys using the provided conversion function.
:param obj: The dictionary or list to convert.
:param convert_function: A function that takes a string and returns a string.
:return: The converted dictionary or list.
"""
if isinstance(obj, list):
return [recursively_convert_dict_keys(i, convert_function) for i in obj]
elif isinstance(obj, dict):
new_dict = {}
for k, v in obj.items():
new_key = convert_function(k)
new_dict[new_key] = recursively_convert_dict_keys(v, convert_function)
return new_dict
return obj


def convert_between_json_and_yaml(input_string: str) -> str:
"""
Convert a string from AWS PascalCase JSON to IAMbic compatible YAML, or visa-versa.
Expand All @@ -773,14 +752,14 @@ def convert_between_json_and_yaml(input_string: str) -> str:
# Try parsing the input as JSON
data = json.loads(input_string)
# Convert keys from PascalCase/camelCase to snake_case
converted_data = recursively_convert_dict_keys(data, snakecase)
converted_data = normalize_dict_keys(data, snakecase)
# Convert to YAML
output = yaml.dump(converted_data)
except JSONDecodeError:
# If the input is not JSON, try parsing it as YAML
data = yaml.load(input_string)
# Convert keys from snake_case to PascalCase
converted_data = recursively_convert_dict_keys(data, pascalcase)
converted_data = normalize_dict_keys(data, pascalcase)
# Convert to JSON
output = json.dumps(converted_data, indent=2)

Expand Down
8 changes: 4 additions & 4 deletions test/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
GlobalRetryController,
convert_between_json_and_yaml,
create_commented_map,
recursively_convert_dict_keys,
normalize_dict_keys,
simplify_dt,
sort_dict,
transform_comments,
Expand Down Expand Up @@ -266,17 +266,17 @@ async def test_gather_templates(tmpdir):
assert len(result) == len(set(result))


def test_recursively_convert_dict_keys():
def test_normalize_dict_keys():
# Test converting dictionary keys to snake_case
data = {"MyKey": {"InnerKey": "value"}}
expected_result = {"my_key": {"inner_key": "value"}}
result = recursively_convert_dict_keys(data, snakecase)
result = normalize_dict_keys(data, snakecase)
assert result == expected_result

# Test converting dictionary keys to PascalCase
data = {"my_key": {"inner_key": "value"}}
expected_result = {"MyKey": {"InnerKey": "value"}}
result = recursively_convert_dict_keys(data, pascalcase)
result = normalize_dict_keys(data, pascalcase)
assert result == expected_result


Expand Down

0 comments on commit 3a6478b

Please sign in to comment.