diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 31863f1..4be6fe6 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -6,21 +6,31 @@ def jmspath_value_parser(path: str): """ Get the JMSPath value path from 'path'. + Two combinations are possible based on where reference key is defined. See example below. + Args: path: "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]" + path: "result[0].$vrfs$.default.peerList[*].[peerAddress, prefixesReceived]" + Return: "result[0].vrfs.default.peerList[*].[prefixesReceived]" + "result[0].vrfs.default.peerList[*].[peerAddress, prefixesReceived]" """ - regex_match_value = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) - - if not regex_match_value: - return path - # $peers$. --> peers - regex_normalized_value = re.search(r"\$.*\$", regex_match_value.group()) - if regex_normalized_value: - normalized_value = regex_match_value.group().split("$")[1] - return path.replace(regex_normalized_value.group(), normalized_value) - + regex_ref_key = re.compile(r"\$.*\$\.|\$.*\$,|,\$.*\$") + regex_match_ref_key = regex_ref_key.search(path) + path_suffix = path.split(".")[-1] + + if regex_match_ref_key: + if regex_ref_key.search(path_suffix): + # [$peerAddress$,prefixesReceived] --> [prefixesReceived] + reference_key = regex_match_ref_key.group() + return path.replace(reference_key, "") + + # result[0].$vrfs$.default... --> result[0].vrfs.default.... + regex_normalized_value = re.search(r"\$.*\$", regex_match_ref_key.group()) + if regex_normalized_value: + normalized_value = regex_match_ref_key.group().split("$")[1] + return path.replace(regex_normalized_value.group(), normalized_value) return path diff --git a/tests/test_jmspath_parsers.py b/tests/test_jmspath_parsers.py index 7d225f3..a54c683 100644 --- a/tests/test_jmspath_parsers.py +++ b/tests/test_jmspath_parsers.py @@ -6,15 +6,15 @@ value_parser_case_1 = ( "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]", - "result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[prefixesReceived]", ) value_parser_case_2 = ( "result[0].vrfs.default.peerList[*].[peerAddress,$prefixesReceived$]", - "result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[peerAddress]", ) value_parser_case_3 = ( "result[0].vrfs.default.peerList[*].[interfaceCounters,$peerAddress$,prefixesReceived]", - "result[0].vrfs.default.peerList[*].[interfaceCounters,peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[interfaceCounters,prefixesReceived]", ) value_parser_case_4 = ( "result[0].$vrfs$.default.peerList[*].[peerAddress,prefixesReceived]",