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

Issue 94 & 91 #100

Merged
merged 4 commits into from
Apr 20, 2023
Merged
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
18 changes: 12 additions & 6 deletions jdiff/utils/jmespath_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def jmespath_refkey_parser(path: str):
if regex_match_anchor and not element.startswith("[") and not element.endswith("]"):
splitted_jmespath = splitted_jmespath[:number]

return ".".join(splitted_jmespath)
return ".".join(splitted_jmespath) or "@"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #91



def associate_key_of_my_value(paths: str, wanted_value: List) -> List:
Expand All @@ -87,13 +87,19 @@ def associate_key_of_my_value(paths: str, wanted_value: List) -> List:

final_list = []

for items in wanted_value:
if len(items) != len(my_key_value_list):
raise ValueError("Key's value len != from value len")
if not all(isinstance(item, list) for item in wanted_value) and len(my_key_value_list) == 1:
for item in wanted_value:
temp_dict = {my_key_value_list[0]: item}
final_list.append(temp_dict)
Comment on lines +90 to +93
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #94


temp_dict = {my_key_value_list[my_index]: my_value for my_index, my_value in enumerate(items)}
else:
for items in wanted_value:
if len(items) != len(my_key_value_list):
raise ValueError("Key's value len != from value len")

temp_dict = {my_key_value_list[my_index]: my_value for my_index, my_value in enumerate(items)}

final_list.append(temp_dict)
final_list.append(temp_dict)

return final_list

Expand Down
54 changes: 54 additions & 0 deletions tests/test_get_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ def test_jmspath_return_none(jmspath):
"",
marks=pytest.mark.xfail(reason="Jmespath issue - path returns empty list."),
),
("global.peers.*.is_enabled", [True, True, False, True]),
(
"global.peers.$*$.is_enabled",
[
{"10.1.0.0": {"is_enabled": True}},
{"10.2.0.0": {"is_enabled": True}},
{"10.64.207.255": {"is_enabled": False}},
{"7.7.7.7": {"is_enabled": True}},
],
),
(
"global.peers.$*$.[is_enabled]",
[
{"10.1.0.0": {"is_enabled": True}},
{"10.2.0.0": {"is_enabled": True}},
{"10.64.207.255": {"is_enabled": False}},
{"7.7.7.7": {"is_enabled": True}},
],
),
]


Expand All @@ -83,3 +102,38 @@ def test_extract_data_from_json(jmspath, expected_value):
value = extract_data_from_json(data=data, path=jmspath)

assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)


test_cases_top_key_anchor = [
("$*$.is_enabled", [{".local.": {"is_enabled": True}}, {".local..0": {"is_enabled": True}}]),
("$*$.is_up", [{".local.": {"is_up": True}}, {".local..0": {"is_up": True}}]),
]


@pytest.mark.parametrize("jmspath, expected_value", test_cases_top_key_anchor)
def test_top_key_anchor(jmspath, expected_value):
"""Test JMSPath return value for anchoring the top key."""
data = {
".local.": {
"description": "",
"is_enabled": True,
"is_up": True,
"last_flapped": -1,
"mac_address": "Unspecified",
"mtu": 0,
"speed": -1,
},
".local..0": {
"description": "",
"is_enabled": True,
"is_up": True,
"last_flapped": -1,
"mac_address": "Unspecified",
"mtu": 0,
"speed": -1,
},
}

value = extract_data_from_json(data=data, path=jmspath)

assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)