Repository: networktocode/jdiff (language: python)

Issue:
Parsed value includes reference key value
Taking the following JMSPath expression `result[0].vrfs.default.peerList[*].[$peerAddress$,localAsn,linkType]` we should have a parsed output which includes only `localAsn` and `linkType`.
We also get instead the reference key

```
[{'7.7.7.7': {'peerAddress': '7.7.7.7', 'localAsn': '65130.1010', 'linkType': 'the road to seniority'}}, {'10.1.0.0': {'peerAddress': '10.1.0.0', 'localAsn': '65130.8888', 'linkType': 'external'}}, {'10.2.0.0': {'peerAddress': '10.2.0.0', 'localAsn': '65130.1100', 'linkType': 'external'}}, {'10.64.207.255': {'peerAddress': '10.64.207.255', 'localAsn': '65130.1100', 'linkType': 'external'}}]
```



Discussion / hints:
Issue:

- if `$ref_key$` is defined at the end of the expression or within square brackets, the ref key must be removed.
- if `$ref_key$` is defined anywhere else, the ref key must be parsed but has to stay.


Fix patch:
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
 
 


Test patch:
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]",