Skip to content

Commit

Permalink
Make index expressions in flatten expression consistent
Browse files Browse the repository at this point in the history
The behavior is now consistent with wildcards.
  • Loading branch information
jamesls committed Nov 19, 2013
1 parent 5bc4de6 commit b4dc9e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
39 changes: 17 additions & 22 deletions jmespath/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class WildcardIndex(AST):
"""
def search(self, value):
return _MultiMatch(value)
return _Projection(value)

def pretty_print(self, indent=''):
return "%sIndex(*)" % indent
Expand All @@ -166,7 +166,7 @@ class WildcardValues(AST):
"""
def search(self, value):
try:
return _MultiMatch(value.values())
return _Projection(value.values())
except AttributeError:
return None

Expand All @@ -185,15 +185,15 @@ def search(self, value):
merged_list.extend(element)
else:
merged_list.append(element)
return _FlattenedMultiMatch(merged_list)
return _Projection(merged_list)
else:
return _FlattenedMultiMatch(value)
return _Projection(value)

def pretty_print(self, indent=''):
return "%sListElements()" % indent


class _BaseMultiMatch(list):
class _Projection(list):
def __init__(self, elements):
self.extend(elements)

Expand All @@ -210,6 +210,18 @@ def get(self, value):
results.append(result)
return results

def get_index(self, index):
matches = []
for el in self:
if not isinstance(el, list):
continue
try:
matches.append(el[index])
except (IndexError, TypeError):
pass
if matches:
return self.__class__(matches)

def multi_get(self, nodes):
results = self.__class__([])
for element in self:
Expand All @@ -235,23 +247,6 @@ def multi_get_list(self, nodes):
return results


class _MultiMatch(_BaseMultiMatch):
def get_index(self, index):
matches = []
for el in self:
try:
matches.append(el[index])
except (IndexError, TypeError):
pass
if matches:
return self.__class__(matches)


class _FlattenedMultiMatch(_BaseMultiMatch):
def __init__(self, elements):
self.extend(elements)


class ORExpression(AST):
def __init__(self, first, remaining):
self.first = first
Expand Down
22 changes: 3 additions & 19 deletions tests/compliance/indices.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,36 +213,20 @@
"cases": [
{
"expression": "foo[][0]",
"result": ["one", "two"]
"result": ["one", "three", "five", "seven", "nine", "ten"]
},
{
"expression": "foo[][1]",
"result": ["three", "four"]
"result": ["two", "four", "six", "eight"]
},
{
"expression": "foo[][0][0]",
"result": "one"
"result": null
},
{
"expression": "foo[][1][0]",
"result": "three"
},
{
"expression": "foo[][0][1]",
"result": "two"
},
{
"expression": "foo[][1][1]",
"result": "four"
},
{
"expression": "foo[][2][2]",
"result": null
},
{
"expression": "foo[][0][0][0]",
"result": null
},
{
"expression": "foo[][0][0][100]",
"result": null
Expand Down
3 changes: 2 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def setUp(self):
def test_merge_with_indices(self):
parsed = self.parser.parse('foo[][0]')
match = parsed.search(self.data)
self.assertEqual(match, ["one", "two"])
self.assertEqual(match, ["one", "three", "five", "seven",
"nine", "ten"])

def test_trailing_merged_operator(self):
parsed = self.parser.parse('foo[]')
Expand Down

0 comments on commit b4dc9e1

Please sign in to comment.