Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added handling child lists
Browse files Browse the repository at this point in the history
joul87 committed Jul 8, 2024

Verified

This commit was signed with the committer’s verified signature.
cjdenio Caleb Denio
1 parent 9b764d6 commit 4f010d2
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

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

0 comments on commit 4f010d2

Please sign in to comment.