Merge pull request #33 from abloomston/nullObjs

Return empty result instead of crashing on index into None
This commit is contained in:
Kenn Knowles 2016-12-17 15:38:13 -08:00 committed by GitHub
commit 7ec055619e
2 changed files with 4 additions and 3 deletions

View File

@ -477,7 +477,7 @@ class Index(JSONPath):
JSONPath that matches indices of the current datum, or none if not large enough.
Concrete syntax is brackets.
WARNING: If the datum is not long enough, it will not crash but will not match anything.
WARNING: If the datum is None or not long enough, it will not crash but will not match anything.
NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
"""
@ -487,7 +487,7 @@ class Index(JSONPath):
def find(self, datum):
datum = DatumInContext.wrap(datum)
if len(datum.value) > self.index:
if datum.value and len(datum.value) > self.index:
return [DatumInContext(datum.value[self.index], path=self, context=datum)]
else:
return []

View File

@ -130,7 +130,8 @@ class TestJsonPath(unittest.TestCase):
self.check_cases([
('[0]', [42], [42]),
('[5]', [42], []),
('[2]', [34, 65, 29, 59], [29])
('[2]', [34, 65, 29, 59], [29]),
('[0]', None, [])
])
def test_slice_value(self):