Skip to content

Commit

Permalink
added handling child lists
Browse files Browse the repository at this point in the history
  • Loading branch information
joul87 committed Jul 6, 2024
1 parent 9b764d6 commit a26fd54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions config/_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from contextlib import suppress
from typing import Tuple

Expand Down Expand Up @@ -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]:
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a26fd54

Please sign in to comment.