Skip to content

Commit 890746b

Browse files
committed
fix kv get_tree for objects within lists
1 parent 3969a63 commit 890746b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

jupyterhub_traefik_proxy/kv_proxy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,22 @@ def by_depth(item):
319319
key_path = key.split(sep)
320320
d = tree
321321
for parent_key, key in zip(key_path[:-1], key_path[1:]):
322-
if parent_key not in d:
322+
if parent_key.isdigit():
323+
parent_key = int(parent_key)
324+
if isinstance(d, dict) and parent_key not in d:
323325
# create container
324326
if key.isdigit():
325327
# integer keys mean it's a list
326328
d[parent_key] = []
327329
else:
328330
d[parent_key] = {}
331+
elif isinstance(d, list):
332+
if key.isdigit():
333+
# integer keys mean it's a list
334+
next_d = []
335+
else:
336+
next_d = {}
337+
d.append(next_d)
329338
# walk down to the next level
330339
d = d[parent_key]
331340
if isinstance(d, list):

tests/test_kv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ def test_flatten_dict_error(orig, expected):
8181
"key/deeper",
8282
{"anddeeper": "false"},
8383
),
84+
(
85+
[("key/0/x", "true"), ("key/1/y", "false")],
86+
"",
87+
{"key": [{"x": "true"}, {"y": "false"}]},
88+
),
8489
],
8590
)
8691
def test_unflatten_dict(flat, root_key, expected):

0 commit comments

Comments
 (0)