8000 don't clobber IDs if they exist · steveknoblock/python-jsonpath-rw@08231fb · GitHub
[go: up one dir, main page]

Skip to content

Commit 08231fb

Browse files
committed
don't clobber IDs if they exist
If a field exists with the name of the `auto_id_field` it should be used as is without converting it to an AutoID. The auto ID functionality should only kick in if and ID field does not exist.
1 parent 749d123 commit 08231fb

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
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: 4 additions & 4 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'
@@ -276,8 +276,8 @@ def test_nested_index_auto_id(self):
276276
{'a': 'a1'}, {'a': 'a2', 'id': 'a2id'}
277277
]
278278
}
279-
self.check_cases([('m.[1].id', data, ['1.m.a2id']),
280-
('m.[1].$.b.id', data, ['1.bid']),
279+
self.check_cases([('m.[1].id', data, ['a2id']),
280+
('m.[1].$.b.id', data, ['bid']),
281281
('m.[0].id', data, ['1.m.[0]'])])
282282

283283
def test_slice_auto_id(self):
@@ -290,7 +290,7 @@ def test_child_auto_id(self):
290290
self.check_cases([('foo.baz.id', {'foo': {'baz': 3}}, ['foo.baz']),
291291
('foo.baz.id', {'foo': {'baz': [3]}}, ['foo.baz']),
292292
('foo.baz.id', {'foo': {'id': 'bizzle', 'baz': 3}}, ['bizzle.baz']),
293-
('foo.baz.id', {'foo': {'baz': {'id': 'hi'}}}, ['foo.hi']),
293+
('foo.baz.id', {'foo': {'baz': {'id': 'hi'}}}, ['hi']),
294294
('foo.baz.bizzle.id', {'foo': {'baz': {'bizzle': 5}}}, ['foo.baz.bizzle'])])
295295

296296
def test_descendants_auto_id(self):

0 commit comments

Comments
 (0)
0