Skip to content

Commit adadda2

Browse files
committed
added handling child lists
1 parent 9b764d6 commit adadda2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

config/_config.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,29 @@ def merge_list(config: dict) -> None:
4343

4444
for key, it in keys.items():
4545
for i in range(it + 1):
46+
value = config[f"{key}[{i}]"]
4647
if key not in config:
47-
config.update({key: [config[f"{key}[{i}]"]]})
48+
config.update({key: [value]})
4849
config.pop(f"{key}[{i}]")
4950
else:
50-
config[key].append(config[f"{key}[{i}]"])
51+
config[key].append(value)
5152
config.pop(f"{key}[{i}]")
5253

5354

55+
def _fix_list_items(values: list) -> None:
56+
for item in values:
57+
if isinstance(item, dict):
58+
_merge(item)
59+
60+
5461
def _merge(config: dict) -> None:
5562
merge_list(config)
5663
for k, v in config.items():
5764
merge_list(config[k])
5865
if isinstance(v, dict):
5966
_merge(v)
67+
elif isinstance(v, list):
68+
_fix_list_items(v)
6069

6170

6271
def _fix_key(key_str: str) -> Tuple[str, bool]:

tests/unit/test_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
{"examples.three.one[0]": "one", "examples.three.one[1]": "thow"},
4747
{"examples": {"three": {"one": ["one", "thow"]}}},
4848
),
49+
(
50+
{
51+
"main_list[0]": {"child_list[0]": {"property-name": "example0"}},
52+
},
53+
{
54+
"main_list": [{'child_list': [{'property-name': 'example0'}]}],
55+
},
56+
),
4957
],
5058
)
5159
def test_to_dict(data, expected):

0 commit comments

Comments
 (0)