diff --git a/config/_config.py b/config/_config.py index 872a00f..38bb017 100644 --- a/config/_config.py +++ b/config/_config.py @@ -1,3 +1,4 @@ +import logging from contextlib import suppress from typing import Tuple @@ -43,20 +44,29 @@ def merge_list(config: dict) -> None: for key, it in keys.items(): for i in range(it + 1): + value = config[f"{key}[{i}]"] if key not in config: - config.update({key: [config[f"{key}[{i}]"]]}) + config.update({key: [value]}) config.pop(f"{key}[{i}]") else: - config[key].append(config[f"{key}[{i}]"]) + config[key].append(value) config.pop(f"{key}[{i}]") +def _fix_list_items(values: list) -> None: + for item in values: + if isinstance(item, dict): + _merge(item) + + def _merge(config: dict) -> None: merge_list(config) for k, v in config.items(): merge_list(config[k]) if isinstance(v, dict): _merge(v) + elif isinstance(v, list): + _fix_list_items(v) def _fix_key(key_str: str) -> Tuple[str, bool]: diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 13cecb9..6385e7e 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -46,6 +46,14 @@ {"examples.three.one[0]": "one", "examples.three.one[1]": "thow"}, {"examples": {"three": {"one": ["one", "thow"]}}}, ), + ( + { + "main_list[0]": {"child_list[0]": {"property-name": "example0"}}, + }, + { + "main_list": [{'child_list': [{'property-name': 'example0'}]}], + }, + ), ], ) def test_to_dict(data, expected):