-
Notifications
You must be signed in to change notification settings - Fork 4
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 parameter_match check, to support non-normalized data #90
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""Evaluators.""" | ||
import re | ||
from typing import Any, Mapping, Dict, Tuple | ||
from typing import Any, Mapping, Dict, Tuple, List | ||
from deepdiff import DeepDiff | ||
from .utils.diff_helpers import get_diff_iterables_items, fix_deepdiff_key_names | ||
from .operator import Operator | ||
|
@@ -36,7 +36,7 @@ def diff_generator(pre_result: Any, post_result: Any) -> Dict: | |
return fix_deepdiff_key_names(result) | ||
|
||
|
||
def parameter_evaluator(values: Mapping, parameters: Mapping, mode: str) -> Dict: | ||
def parameter_evaluator(values: List[Dict], parameters: Mapping, mode: str) -> Dict: | ||
"""Parameter Match evaluator engine. | ||
|
||
Args: | ||
|
@@ -51,41 +51,41 @@ def parameter_evaluator(values: Mapping, parameters: Mapping, mode: str) -> Dict | |
Dictionary with all the items that have some value not matching the expectations from parameters | ||
""" | ||
if not isinstance(values, list): | ||
raise TypeError("Something went wrong during jmespath parsing. 'values' must be of type List.") | ||
raise TypeError("'values' must be of type List.") | ||
|
||
result = {} | ||
for value in values: | ||
for index, value in enumerate(values): | ||
# value: {'7.7.7.7': {'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals | ||
if not isinstance(value, dict): | ||
raise TypeError( | ||
"Something went wrong during jmespath parsing. ", | ||
f"'value' ({value}) must be of type Dict, and it's {type(value)}", | ||
) | ||
raise TypeError(f"'value' ({value}) must be of type Dict, and it's {type(value)}") | ||
|
||
result_item = {} | ||
|
||
# TODO: Why the 'value' dict has always ONE single element? we have to explain | ||
# inner_key: '7.7.7.7' | ||
inner_key = list(value.keys())[0] | ||
# inner_value: [{'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals'}] | ||
inner_value = list(value.values())[0] | ||
# When data has been normalized with $key$, get inner key and value | ||
if len(value) == 1: | ||
# inner_key: '7.7.7.7' | ||
inner_key = list(value.keys())[0] | ||
# inner_value: [{'peerAddress': '7.7.7.7', 'localAsn': '65130.1101', 'linkType': 'externals'}] | ||
value = list(value.values())[0] | ||
else: | ||
inner_key = index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the value in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An element from the top for loop, where values is a function argument. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If data normalisation is detected, then reassign value from inner element. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe this answering to my question above (?) |
||
|
||
for parameter_key, parameter_value in parameters.items(): | ||
if mode == "match" and inner_value[parameter_key] != parameter_value: | ||
result_item[parameter_key] = inner_value[parameter_key] | ||
elif mode == "no-match" and inner_value[parameter_key] == parameter_value: | ||
result_item[parameter_key] = inner_value[parameter_key] | ||
if mode == "match" and value[parameter_key] != parameter_value: | ||
result_item[parameter_key] = value[parameter_key] | ||
elif mode == "no-match" and value[parameter_key] == parameter_value: | ||
result_item[parameter_key] = value[parameter_key] | ||
|
||
if result_item: | ||
result[inner_key] = result_item | ||
|
||
return result | ||
|
||
|
||
def regex_evaluator(values: Mapping, regex_expression: str, mode: str) -> Dict: | ||
def regex_evaluator(values: List[Dict[Any, Dict]], regex_expression: str, mode: str) -> Dict: | ||
"""Regex Match evaluator engine.""" | ||
# values: [{'7.7.7.7': {'peerGroup': 'EVPN-OVERLAY-SPINE'}}] | ||
# parameter: {'regex': '.*UNDERLAY.*', 'mode': 'include'} | ||
# parameter: {'regex': '.*UNDERLAY.*', 'mode': 'match'} | ||
result = {} | ||
if not isinstance(values, list): | ||
raise TypeError("Something went wrong during JMSPath parsing. 'values' must be of type List.") | ||
|
@@ -94,10 +94,10 @@ def regex_evaluator(values: Mapping, regex_expression: str, mode: str) -> Dict: | |
for founded_value in item.values(): | ||
for value in founded_value.values(): | ||
match_result = re.search(regex_expression, value) | ||
# Fail if there is not regex match | ||
# Fail if there is no regex match for "match" mode | ||
if mode == "match" and not match_result: | ||
result.update(item) | ||
# Fail if there is regex match | ||
# Fail if there is regex match for "no-match" mode. | ||
elif mode == "no-match" and match_result: | ||
result.update(item) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jmespath parsing is now optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question around this. If JMSPATH is now optional, how the library will understand when is required and when is not?