8000 Support auto id and paths well for `$` operator · 0xdecaf/python-jsonpath-rw@fe2f97f · GitHub
[go: up one dir, main page]

Skip to content

Commit fe2f97f

Browse files
committed
Support auto id and paths well for $ operator
1 parent 991e0f6 commit fe2f97f

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

jsonpath_rw/jsonpath.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def child(self, child):
3333
"""
3434
Equivalent to Child(self, next) but with some canonicalization
3535
"""
36-
if isinstance(self, This):
36+
if isinstance(self, This) or isinstance(self, Root):
3737
return child
3838
elif isinstance(child, This):
3939
return self
40+
elif isinstance(child, Root):
41+
return child
4042
else:
4143
return Child(self, child)
4244

@@ -165,10 +167,10 @@ class Root(JSONPath):
165167

166168
def find(self, data):
167169
if not isinstance(data, DatumInContext):
168-
return [DatumInContext(data, path=This(), context=None)]
170+
return [DatumInContext(data, path=Root(), context=None)]
169171
else:
170172
if data.context is None:
171-
return data
173+
return [DatumInContext(data.value, context=None, path=Root())]
172174
else:
173175
return Root().find(data.context)
174176

@@ -190,7 +192,7 @@ class This(JSONPath):
190192
"""
191193

192194
def find(self, datum):
193-
return DatumInContext.wrap(datum)
195+
return [DatumInContext.wrap(datum)]
194196

195197
def update(self, data, val):
196198
return val

tests/test_jsonpath.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ def test_fields_value(self):
108108
jsonpath.auto_id_field = 'id'
109109
self.check_cases([ ('*', {'foo': 1, 'baz': 2}, set([1, 2, '@'])) ])
110110

111+
def test_root_value(self):
112+
jsonpath.auto_id_field = None
113+
self.check_cases([
114+
('$', {'foo': 'baz'}, [{'foo':'baz'}]),
115+
('foo.$', {'foo': 'baz'}, [{'foo':'baz'}]),
116+
('foo.$.foo', {'foo': 'baz'}, ['baz']),
117+
])
118+
119+
def test_this_value(self):
120+
jsonpath.auto_id_field = None
121+
self.check_cases([
122+
('@', {'foo': 'baz'}, [{'foo':'baz'}]),
123+
('foo.@', {'foo': 'baz'}, ['baz']),
124+
('foo.@.baz', {'foo': {'baz': 3}}, [3]),
125+
])
126+
111127
def test_index_value(self):
112128
self.check_cases([
113129
('[0]', [42], [42]),
@@ -167,6 +183,22 @@ def test_fields_paths(self):
167183
jsonpath.auto_id_field = 'id'
168184
self.check_paths([ ('*', {'foo': 1, 'baz': 2}, set(['foo', 'baz', 'id'])) ])
169185

186+
def test_root_paths(self):
187+
jsonpath.auto_id_field = None
188+
self.check_paths([
189+
('$', {'foo': 'baz'}, ['$']),
190+
('foo.$', {'foo': 'baz'}, ['$']),
191+
('foo.$.foo', {'foo': 'baz'}, ['foo']),
192+
])
193+
194+
def test_this_paths(self):
195+
jsonpath.auto_id_field = None
196+
self.check_paths([
197+
('@', {'foo': 'baz'}, ['@']),
198+
('foo.@', {'foo': 'baz'}, ['foo']),
199+
('foo.@.baz', {'foo': {'baz': 3}}, ['foo.baz']),
200+
])
201+
170202
def test_index_paths(self):
171203
self.check_paths([('[0]', [42], ['[0]']),
172204
('[2]', [34, 65, 29, 59], ['[2]'])])
@@ -197,6 +229,22 @@ def test_fields_auto_id(self):
197229
'baz': 2},
198230
set(['1', 'baz'])) ])
199231

232+
def test_root_auto_id(self):
233+
jsonpath.auto_id_field = 'id'
234+
self.check_cases([
235+
('$.id', {'foo': 'baz'}, ['$']), # This is a wonky case that is not that interesting
236+
('foo.$.id', {'foo': 'baz', 'id': 'bizzle'}, ['bizzle']),
237+
('foo.$.baz.id', {'foo': 4, 'baz': 3}, ['baz']),
238+
])
239+
240+
def test_this_auto_id(self):
241+
jsonpath.auto_id_field = 'id'
242+
self.check_cases([
243+
('id', {'foo': 'baz'}, ['@']), # This is, again, a wonky case that is not that interesting
244+
('foo.@.id', {'foo': 'baz'}, ['foo']),
245+
('foo.@.baz.id', {'foo': {'baz': 3}}, ['foo.baz']),
246+
])
247+
200248
def test_index_auto_id(self):
201249
jsonpath.auto_id_field = "id"
202250
self.check_cases([('[0].id', [42], ['[0]']),

0 commit comments

Comments
 (0)
0