Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added handling child lists #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,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
Loading