1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-13 15:57:14 +03:00

resolved edgecase with dotted keys

This commit is contained in:
A_D 2021-08-10 16:18:23 +02:00
parent df200d2c22
commit 9255c4fa96
No known key found for this signature in database
GPG Key ID: 4BE9EB7DF45076C4
2 changed files with 23 additions and 2 deletions

View File

@ -113,7 +113,25 @@ def _deep_apply(target: UPDATABLE_DATA, path: str, to_set=None, delete=False):
# it exists on this level, dont go further
break
key, _, path = path.partition('.')
elif isinstance(current, Mapping) and any('.' in k and path.startswith(k) for k in current.keys()):
# there is a dotted key in here that can be used for this
# if theres a dotted key in here (must be a mapping), use that if we can
keys = current.keys()
for k in filter(lambda x: '.' in x, keys):
if path.startswith(k):
key = k
path = path.removeprefix(k)
# we assume that the `.` here is for "accessing" the next key.
if path[0] == '.':
path = path[1:]
if len(path) == 0:
path = key
break
else:
key, _, path = path.partition('.')
if isinstance(current, Mapping):
current = current[key] # type: ignore # I really dont know at this point what you want from me mypy.

View File

@ -72,7 +72,10 @@ def test_get_int(input: str, expected: Optional[int]) -> None:
({'depth': {'is': 'important'}}, 'depth.is', '', 'nonexistent', {'depth': {'is': 'nonexistent'}}),
([{'test': ['stuff']}], '0.test.0', '', 'things', [{'test': ['things']}]),
(({'test': {'with': ['a', 'tuple']}},), '0.test.with.0', 'delete', '', ({'test': {'with': ['tuple']}},)),
({'test': ['with a', {'set', 'of', 'stuff'}]}, 'test.1', 'delete', '', {'test': ['with a']})
({'test': ['with a', {'set', 'of', 'stuff'}]}, 'test.1', 'delete', '', {'test': ['with a']}),
({'keys.can.have.': 'dots!'}, 'keys.can.have.', '', '.s!', {'keys.can.have.': '.s!'}),
({'multilevel.keys': {'with.dots': False}}, 'multilevel.keys.with.dots',
'', True, {'multilevel.keys': {'with.dots': True}})
],
)
def test_deep_get(source: UPDATABLE_DATA, key: str, action: str, to_set: Any, result: UPDATABLE_DATA) -> None: