Skip to content

Commit a419a51

Browse files
author
Mehdi Abaakouk
committed
Add len extention to the json parser
This extends the jsonpath_rw parser with customisation for gabbi. This first one is `len`
1 parent 6d761d4 commit a419a51

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

gabbi/case.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import unittest
3030
from unittest import case
3131

32-
import jsonpath_rw
3332
import six
3433
from six.moves.urllib import parse as urlparse
3534
import wsgi_intercept
3635

36+
from gabbi import json_parser
3737
from gabbi import utils
3838

3939
REPLACERS = [
@@ -193,7 +193,7 @@ def extract_json_path_value(data, path):
193193
194194
The input data is a Python datastructre, not a JSON string.
195195
"""
196-
path_expr = jsonpath_rw.parse(path)
196+
path_expr = json_parser.parse(path)
197197
matches = [match.value for match in path_expr.find(data)]
198198
try:
199199
return matches[0]

gabbi/gabbits_intercept/data.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tests:
2525
response_json_paths:
2626
$[0]: 1
2727
$[1]: 2
28+
$.`len`: 2
2829

2930
- name: load json file
3031
url: /

gabbi/json_parser.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
14+
import jsonpath_rw
15+
16+
17+
class Len(jsonpath_rw.JSONPath):
18+
"""The JSONPath referring to the len of the current object.
19+
20+
Concrete syntax is '`len`'.
21+
"""
22+
23+
def find(self, datum):
24+
datum = jsonpath_rw.DatumInContext.wrap(datum)
25+
try:
26+
value = len(datum.value)
27+
except TypeError:
28+
return []
29+
else:
30+
return [jsonpath_rw.DatumInContext(value,
31+
context=None,
32+
path=Len())]
33+
34+
def __eq__(self, other):
35+
return isinstance(other, Len)
36+
37+
def __str__(self):
38+
return '`len`'
39+
40+
def __repr__(self):
41+
return 'Len()'
42+
43+
44+
class GabbiJsonPathParser(jsonpath_rw.parser.JsonPathParser):
45+
"""Custom gabbi LALR-parser for JsonPath"""
46+
47+
def p_jsonpath_named_operator(self, p):
48+
"jsonpath : NAMED_OPERATOR"
49+
if p[1] == 'len':
50+
p[0] = Len()
51+
else:
52+
super(GabbiJsonPathParser, self).p_jsonpath_named_operator(p)
53+
54+
55+
def parse(path):
56+
return GabbiJsonPathParser().parse(path)

0 commit comments

Comments
 (0)