8000 Merge pull request #96: Don't clobber existing IDs with 'auto_id' · kennknowles/python-jsonpath-rw@6f5647b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f5647b

Browse files
authored
Merge pull request #96: Don't clobber existing IDs with 'auto_id'
2 parents b008a97 + 08231fb commit 6f5647b

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

jsonpath_rw/jsonpath.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,13 @@ def __init__(self, *fields):
430430
self.fields = fields
431431

432432
def get_field_datum(self, datum, field):
433-
if field == auto_id_field:
434-
return AutoIdForDatum(datum)
435-
else:
436-
try:
437-
field_value = datum.value[field] # Do NOT use `val.get(field)` since that confuses None as a value and None due to `get`
438-
return DatumInContext(value=field_value, path=Fields(field), context=datum)
439-
except (TypeError, KeyError, AttributeError):
440-
return None
433+
try:
434+
field_value = datum.value[field] # Do NOT use `val.get(field)` since that confuses None as a value and None due to `get`
435+
return DatumInContext(value=field_value, path=Fields(field), context=datum)
436+
except (TypeError, KeyError, AttributeError):
437+
if field == auto_id_field:
438+
return AutoIdForDatum(datum)
439+
return None
441440

442441
def reified_fields(self, datum):
443442
if '*' not in self.fields:

tests/test_jsonpath.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_fields_auto_id(self):
244244
('*.id',
245245
{'foo':{'id': 1},
246246
'baz': 2},
247-
set(['1', 'baz'])) ])
247+
set([1, 'baz'])) ])
248248

249249
def test_root_auto_id(self):
250250
jsonpath.auto_id_field = 'id'
@@ -267,6 +267,19 @@ def test_index_auto_id(self):
267267
self.check_cases([('[0].id', [42], ['[0]']),
268268
('[2].id', [34, 65, 29, 59], ['[2]'])])
269269

270+
def test_nested_index_auto_id(self):
271+
jsonpath.auto_id_field = "id"
272+
data = {
273+
"id": 1,
274+
"b": {'id': 'bid', 'name': 'bob'},
275+
"m": [
276+
{'a': 'a1'}, {'a': 'a2', 'id': 'a2id'}
277+
]
278+
}
279+
self.check_cases([('m.[1].id', data, ['a2id']),
280+
('m.[1].$.b.id', data, ['bid']),
281+
('m.[0].id', data, ['1.m.[0]'])])
282+
270283
def test_slice_auto_id(self):
271284
jsonpath.auto_id_field = "id"
272285
self.check_cases([ ('[*].id', [1, 2, 3], ['[0]', '[1]', '[2]']),
@@ -277,7 +290,7 @@ def test_child_auto_id(self):
277290
self.check_cases([('foo.baz.id', {'foo': {'baz': 3}}, ['foo.baz']),
278291
('foo.baz.id', {'foo': {'baz': [3]}}, ['foo.baz']),
279292
('foo.baz.id', {'foo': {'id': 'bizzle', 'baz': 3}}, ['bizzle.baz']),
280-
('foo.baz.id', {'foo': {'baz': {'id': 'hi'}}}, ['foo.hi']),
293+
('foo.baz.id', {'foo': {'baz': {'id': 'hi'}}}, ['hi']),
281294
('foo.baz.bizzle.id', {'foo': {'baz': {'bizzle': 5}}}, ['foo.baz.bizzle'])])
282295

283296
def test_descendants_auto_id(self):

0 commit comments

Comments
 (0)
0