mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Moving comparision functions to compare.py from common.py (#53946)
This commit is contained in:
parent
014cb73694
commit
dcc4e0f220
1 changed files with 54 additions and 0 deletions
|
@ -6,6 +6,8 @@
|
|||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils.six import iteritems
|
||||
|
||||
|
||||
def cmp_simple_list(want, have):
|
||||
if want is None:
|
||||
|
@ -28,3 +30,55 @@ def cmp_str_with_none(want, have):
|
|||
return None
|
||||
if want != have:
|
||||
return want
|
||||
|
||||
|
||||
def compare_complex_list(want, have):
|
||||
"""Performs a complex list comparison
|
||||
|
||||
A complex list is a list of dictionaries
|
||||
|
||||
Args:
|
||||
want (list): List of dictionaries to compare with second parameter.
|
||||
have (list): List of dictionaries compare with first parameter.
|
||||
|
||||
Returns:
|
||||
bool:
|
||||
"""
|
||||
if want == [] and have is None:
|
||||
return None
|
||||
if want is None:
|
||||
return None
|
||||
w = []
|
||||
h = []
|
||||
for x in want:
|
||||
tmp = [(str(k), str(v)) for k, v in iteritems(x)]
|
||||
w += tmp
|
||||
for x in have:
|
||||
tmp = [(str(k), str(v)) for k, v in iteritems(x)]
|
||||
h += tmp
|
||||
if set(w) == set(h):
|
||||
return None
|
||||
else:
|
||||
return want
|
||||
|
||||
|
||||
def compare_dictionary(want, have):
|
||||
"""Performs a dictionary comparison
|
||||
|
||||
Args:
|
||||
want (dict): Dictionary to compare with second parameter.
|
||||
have (dict): Dictionary to compare with first parameter.
|
||||
|
||||
Returns:
|
||||
bool:
|
||||
"""
|
||||
if want == {} and have is None:
|
||||
return None
|
||||
if want is None:
|
||||
return None
|
||||
w = [(str(k), str(v)) for k, v in iteritems(want)]
|
||||
h = [(str(k), str(v)) for k, v in iteritems(have)]
|
||||
if set(w) == set(h):
|
||||
return None
|
||||
else:
|
||||
return want
|
||||
|
|
Loading…
Reference in a new issue