Allows jsonpath in sort, not just ID
This commit is contained in:
parent
c9703d5682
commit
94fe4d3562
|
@ -32,7 +32,11 @@ class ExtendedJsonPathLexer(lexer.JsonPathLexer):
|
|||
|
||||
t_FILTER_VALUE = r'\w+'
|
||||
t_FILTER_OP = r'(==?|<=|>=|!=|<|>)'
|
||||
t_SORT_DIRECTION = r'(/|\\)'
|
||||
|
||||
def t_SORT_DIRECTION(self, t):
|
||||
r',?\s*(/|\\)'
|
||||
t.value = t.value[-1]
|
||||
return t
|
||||
|
||||
def t_ID(self, t):
|
||||
r'@?[a-zA-Z_][a-zA-Z0-9_@\-]*'
|
||||
|
@ -94,17 +98,16 @@ class ExtentedJsonPathParser(parser.JsonPathParser):
|
|||
p[0] = jsonpath_rw.Child(p[1], p[3])
|
||||
|
||||
def p_sort(self, p):
|
||||
"sort : SORT_DIRECTION ID"
|
||||
field = jsonpath_rw.Fields(p[2])
|
||||
p[0] = (field, p[1] != '/')
|
||||
"sort : SORT_DIRECTION jsonpath"
|
||||
p[0] = (p[2], p[1] != "/")
|
||||
|
||||
def p_sorts_sort(self, p):
|
||||
"sorts : sort"
|
||||
p[0] = [p[1]]
|
||||
|
||||
def p_sorts_comma(self, p):
|
||||
"sorts : sorts ',' sorts"
|
||||
p[0] = p[1] + p[3]
|
||||
"sorts : sorts sorts"
|
||||
p[0] = p[1] + p[2]
|
||||
|
||||
def p_jsonpath_sort(self, p):
|
||||
"jsonpath : jsonpath '[' sorts ']'"
|
||||
|
@ -115,6 +118,9 @@ class ExtentedJsonPathParser(parser.JsonPathParser):
|
|||
"jsonpath : '@'"
|
||||
p[0] = jsonpath_rw.This()
|
||||
|
||||
precedence = ([
|
||||
] + parser.JsonPathParser.precedence)
|
||||
|
||||
|
||||
def parse(path, debug=False):
|
||||
return ExtentedJsonPathParser(debug=debug).parse(path)
|
||||
|
|
|
@ -20,6 +20,7 @@ Tests for `jsonpath_rw_ext` module.
|
|||
"""
|
||||
|
||||
from jsonpath_rw import jsonpath # For setting the global auto_id_field flag
|
||||
from jsonpath_rw import lexer # For setting the global auto_id_field flag
|
||||
from oslotest import base
|
||||
import testscenarios
|
||||
|
||||
|
@ -95,10 +96,10 @@ class TestJsonpath_rw_ext(testscenarios.WithScenarios,
|
|||
target=[{'cat': 2, 'cow': 1},
|
||||
{'cat': 1, 'cow': 2},
|
||||
{'cat': 3, 'cow': 3}])),
|
||||
('sort2', dict(string='objects[\\cat]',
|
||||
('sort2', dict(string='objects[\cat]',
|
||||
data={'objects': [{'cat': 2}, {'cat': 1}, {'cat': 3}]},
|
||||
target=[{'cat': 3}, {'cat': 2}, {'cat': 1}])),
|
||||
('sort3', dict(string='objects[/cow,\\cat]',
|
||||
('sort3', dict(string='objects[/cow,\cat]',
|
||||
data={'objects': [{'cat': 1, 'cow': 2},
|
||||
{'cat': 2, 'cow': 1},
|
||||
{'cat': 3, 'cow': 1},
|
||||
|
@ -107,6 +108,23 @@ class TestJsonpath_rw_ext(testscenarios.WithScenarios,
|
|||
{'cat': 2, 'cow': 1},
|
||||
{'cat': 1, 'cow': 2},
|
||||
{'cat': 3, 'cow': 3}])),
|
||||
('sort4', dict(string='objects[/cat.cow]',
|
||||
data={'objects': [{'cat': {'dog': 1, 'cow': 2}},
|
||||
{'cat': {'dog': 2, 'cow': 1}},
|
||||
{'cat': {'dog': 3, 'cow': 3}}]},
|
||||
target=[{'cat': {'dog': 2, 'cow': 1}},
|
||||
{'cat': {'dog': 1, 'cow': 2}},
|
||||
{'cat': {'dog': 3, 'cow': 3}}])),
|
||||
('sort5_twofields', dict(string='objects[/cat.(cow,bow)]',
|
||||
data={'objects':
|
||||
[{'cat': {'dog': 1, 'bow': 3}},
|
||||
{'cat': {'dog': 2, 'cow': 1}},
|
||||
{'cat': {'dog': 2, 'bow': 2}},
|
||||
{'cat': {'dog': 3, 'cow': 2}}]},
|
||||
target=[{'cat': {'dog': 2, 'cow': 1}},
|
||||
{'cat': {'dog': 2, 'bow': 2}},
|
||||
{'cat': {'dog': 3, 'cow': 2}},
|
||||
{'cat': {'dog': 1, 'bow': 3}}])),
|
||||
|
||||
('real_life_example1', dict(
|
||||
string="payload.metrics[?(@.name='cpu.frequency')].value",
|
||||
|
|
Loading…
Reference in New Issue