Skip to content

Commit cf61b6d

Browse files
authored
Fix #31 Fixes start of arrayslice (#32)
This makes sure start of arrayslice is actually 0 before we start the processing of arrayslice subscripts. Signed-off-by: David Brown <dmlb2000@gmail.com>
1 parent c6d4dd4 commit cf61b6d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

jsonpath2/subscripts/arrayslice.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def match(self, root_value: object, current_value: object) -> Generator[MatchDat
3535
"""Match an array slice between values."""
3636
if isinstance(current_value, Sequence) and not isinstance(current_value, str):
3737
start = None if (self.start is None) else (
38-
self.start + (len(current_value) if (self.start < 0) else 0))
38+
self.start + ((
39+
len(current_value) if abs(self.start) < len(current_value) else abs(self.start)
40+
) if (self.start < 0) else 0))
3941

4042
end = None if (self.end is None) else (
4143
self.end + (len(current_value) if (self.end < 0) else 0))

tests/arrayslice_subscript_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ def test_arrayslice_not_list(self):
7575
root_value = None
7676
self.assertTrue(not isinstance(root_value, list))
7777
self.assertEqual([], list(subscript.match(root_value, root_value)))
78+
79+
def test_arrayslice8(self):
80+
"""Test the arrayslice with configuration 1000 (base 2)."""
81+
subscript = ArraySliceSubscript(-15, None, None)
82+
self.assertEqual('-15:', subscript.tojsonpath())
83+
self.assertEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], list(map(
84+
lambda match_data: match_data.current_value, subscript.match(self.root_value, self.current_value))))
85+
86+
def test_arrayslice9(self):
87+
"""Test the arrayslice with configuration 1001 (base 2)."""
88+
subscript = ArraySliceSubscript(None, -15, None)
89+
self.assertEqual(':-15', subscript.tojsonpath())
90+
self.assertEqual([], list(map(
91+
lambda match_data: match_data.current_value, subscript.match(self.root_value, self.current_value))))

0 commit comments

Comments
 (0)