This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code for template definition normalization
This commit refactors the code responsible for normalizing template definitions by implementing improved type annotations, better function organization, and enhanced error handling. Key changes include: 1. Refactoring the to_snake_case function to use the str.replace method for kebab-case to snake_case conversion. 2. Improving the cast_value_to_global function to handle different types of input values, including lists and IP addresses, and utilizing IPv4Address and IPv6Address from the ipaddress module. 3. Enhancing the transform_dict function to handle nested dictionaries and lists, casting leaf values to global types recursively. 4. Updating the template_definition_normalization function to utilize the refactored transform_dict function for key transformation and value normalization. These changes improve code readability, maintainability, and robustness, providing a more efficient and reliable solution for template definition normalization.
- Loading branch information
1 parent
146508f
commit 88a53e1
Showing
16 changed files
with
307 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 0 additions & 80 deletions
80
catalystwan/models/configuration/feature_profile/converters/feature_template/normalizator.py
This file was deleted.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
catalystwan/models/configuration/feature_profile/sdwan/system/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import unittest | ||
from ipaddress import IPv4Address, IPv6Address | ||
from typing import List | ||
|
||
from catalystwan.api.configuration_groups.parcel import Global | ||
from catalystwan.utils.config_migration.converters.feature_template import template_definition_normalization | ||
|
||
|
||
class TestNormalizer(unittest.TestCase): | ||
def setUp(self): | ||
self.template_values = { | ||
"key-one": "Simple string !@#$%^&*()-=[';/.,`~]", | ||
"keyone": "Simplestring!@#$%^&*()-=[';/.,`~]", | ||
"bool-value-as-string": "true", | ||
"boolvalueasstring": "false", | ||
"simple-int": 1, | ||
"simpleint": 333333331231, | ||
"simple-ints-in-list": [1, 2, 4, 5, 6, 7, 8, 9], | ||
"simple-int-in-list": [1], | ||
"simplestringsinlist": ["1232132", "!@#$%^&*()-=[';/.,`~]", ""], | ||
"objects-in-list": [ | ||
{"color": "lte", "hello-interval": 300000, "pmtu-discovery": "false"}, | ||
{"color": "mpls", "pmtu-discovery": "false"}, | ||
{"color": "biz-internet"}, | ||
{"color": "public-internet"}, | ||
], | ||
"nested-objects": [{"next-hop": [{"distance": 1}]}], | ||
"ipv4-address": "10.0.0.2", | ||
"ipv6addr": "2000:0:2:3::", | ||
} | ||
self.expected_result = { | ||
"key_one": Global[str](value="Simple string !@#$%^&*()-=[';/.,`~]"), | ||
"keyone": Global[str](value="Simplestring!@#$%^&*()-=[';/.,`~]"), | ||
"bool_value_as_string": Global[bool](value=True), | ||
"boolvalueasstring": Global[bool](value=False), | ||
"simple_int": Global[int](value=1), | ||
"simpleint": Global[int](value=333333331231), | ||
"simple_ints_in_list": Global[List[int]](value=[1, 2, 4, 5, 6, 7, 8, 9]), | ||
"simple_int_in_list": Global[List[int]](value=[1]), | ||
"simplestringsinlist": Global[List[str]](value=["1232132", "!@#$%^&*()-=[';/.,`~]", ""]), | ||
"objects_in_list": [ | ||
{ | ||
"color": Global[str](value="lte"), | ||
"hello_interval": Global[int](value=300000), | ||
"pmtu_discovery": Global[bool](value=False), | ||
}, | ||
{"color": Global[str](value="mpls"), "pmtu_discovery": Global[bool](value=False)}, | ||
{"color": Global[str](value="biz-internet")}, | ||
{"color": Global[str](value="public-internet")}, | ||
], | ||
"nested_objects": [{"next_hop": [{"distance": Global[int](value=1)}]}], | ||
"ipv4_address": Global[IPv4Address](value=IPv4Address("10.0.0.2")), | ||
"ipv6addr": Global[IPv6Address](value=IPv6Address("2000:0:2:3::")), | ||
} | ||
|
||
def test_normalizer(self): | ||
# Arrange | ||
expected_result = self.expected_result | ||
# Act | ||
returned_result = template_definition_normalization(self.template_values) | ||
# Assert | ||
assert expected_result == returned_result | ||
|
||
def test_super_nested(self): | ||
# Arrange | ||
super_nested_input = { | ||
"super_nested": {"level1": {"level2": {"level3": {"key_one": "value_one", "key_two": "value_two"}}}} | ||
} | ||
expected_result = { | ||
"super_nested": { | ||
"level1": { | ||
"level2": { | ||
"level3": {"key_one": Global[str](value="value_one"), "key_two": Global[str](value="value_two")} | ||
} | ||
} | ||
} | ||
} | ||
|
||
# Act | ||
returned_result = template_definition_normalization(super_nested_input) | ||
|
||
# Assert | ||
assert expected_result == returned_result |
Oops, something went wrong.