Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate detection issue #288

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions iac_validate/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,13 @@ def merge_list_item(
for dest_item in destination:
match = True
comparison = False
unique_source = False
unique_dest = False
for k, v in source_item.items():
if isinstance(v, dict) or isinstance(v, list):
continue
if k in dest_item and v == dest_item[k]:
comparison = True
continue
if k not in dest_item:
unique_source = True
continue
comparison = True
match = False
Expand All @@ -117,11 +114,10 @@ def merge_list_item(
comparison = True
continue
if k not in source_item:
unique_dest = True
continue
comparison = True
match = False
if comparison and match and not (unique_source and unique_dest):
if comparison and match:
merge_dict(source_item, dest_item, merge_list_items)
return
elif source_item in destination:
Expand All @@ -145,10 +141,11 @@ def merge_dict(
merge_dict(value, node, merge_list_items)
elif isinstance(value, list):
if key not in destination:
destination[key] = []
destination[key] = value[:]
if isinstance(destination[key], list):
for i in value:
merge_list_item(i, destination[key], merge_list_items)
if i not in destination[key]:
merge_list_item(i, destination[key], merge_list_items)
else:
destination[key] = value
return destination
9 changes: 7 additions & 2 deletions tests/unit/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def test_merge_dict():
destination = {}
yaml.merge_dict(source, destination)
assert destination == source
# make sure that merge_dict will not remove duplicate entries
source = {"root": {"duplicates": [{"name": "abc"}, {"name": "abc"}]}}
destination = {}
yaml.merge_dict(source, destination)
assert destination == source


def test_merge_list_item():
Expand Down Expand Up @@ -105,9 +110,9 @@ def test_merge_list_item():
]
yaml.merge_list_item(source_item, destination)
assert destination == result
# not merge matching dict list items with extra dst and src primitive attribute
# merge matching dict list items with extra dst and src primitive attribute
destination = [{"name": "abc", "name2": "def"}]
source_item = {"name": "abc", "name3": "ghi"}
result = [{"name": "abc", "name2": "def"}, {"name": "abc", "name3": "ghi"}]
result = [{"name": "abc", "name2": "def", "name3": "ghi"}]
yaml.merge_list_item(source_item, destination)
assert destination == result
Loading