Skip to content

Commit

Permalink
fix access to nested config
Browse files Browse the repository at this point in the history
  • Loading branch information
amenezes committed Aug 28, 2022
1 parent b2ae715 commit 4c57438
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .cfenv import CFenv
from .spring import ConfigClient, config_client, create_config_client

__version__ = "1.0.2"
__version__ = "1.0.3"
__all__ = [
"__version__",
"ConfigClient",
Expand Down
38 changes: 32 additions & 6 deletions config/_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
from typing import Tuple


Expand All @@ -12,6 +13,7 @@ def to_dict(config: dict) -> dict:
else:
tconfig = {ksub: tconfig}
merge_dict(final_config, tconfig)
_merge(final_config)
return final_config


Expand All @@ -23,16 +25,40 @@ def merge_dict(primary_config: dict, secondary_config: dict) -> dict:
elif k in secondary_config:
primary_config[k] = secondary_config[k]
for k, v in secondary_config.items():
k, is_list = _fix_key(k)
if k not in primary_config and is_list is False:
if k not in primary_config:
primary_config.update({k: v})
elif k not in primary_config and is_list:
primary_config.update({k: [v]})
elif k in primary_config and is_list:
primary_config[k].append(v)
return primary_config


def merge_list(config: dict) -> None:
keys = {}
with suppress(AttributeError):
for k in config.keys():
key, is_list = _fix_key(k)
if is_list:
if key not in keys:
keys[key] = 0
else:
keys[key] += 1

for key, it in keys.items():
for i in range(it + 1):
if key not in config:
config.update({key: [config[f"{key}[{i}]"]]})
config.pop(f"{key}[{i}]")
else:
config[key].append(config[f"{key}[{i}]"])
config.pop(f"{key}[{i}]")


def _merge(config: dict):
merge_list(config)
for k, v in config.items():
merge_list(config[k])
if isinstance(v, dict):
_merge(v)


def _fix_key(key_str: str) -> Tuple[str, bool]:
"""Check if a dictionary key has array values.
Expand Down
67 changes: 63 additions & 4 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import pytest

from config._config import merge_dict, to_dict
from config._config import merge_dict, merge_list, to_dict


@pytest.mark.parametrize(
"data, expected",
[
({"python.cache.timeout": 10}, {"python": {"cache": {"timeout": 10}}}),
({"health.config.enabled": False}, {"health": {"config": {"enabled": False}}}),
(
{
"info": {
"description": "simple description",
"url": "http://localhost",
"docs": "http://localhost/docs",
},
"app": {"password": "234"},
"example[0]": 1,
"example[1]": 2,
"examples.one[0]": 1,
"examples.one[1]": 2,
"examples.one[2]": 3,
"examples.two[0]": 1,
"examples.two[1]": 2,
"examples.three.one[0]": 1,
"examples.three.one[1]": 2,
"examples.three.two.one[0]": 1,
},
{
"info": {
"description": "simple description",
"url": "http://localhost",
"docs": "http://localhost/docs",
},
"app": {"password": "234"},
"examples": {
"three": {"two": {"one": [1]}, "one": [1, 2]},
"one": [1, 2, 3],
"two": [1, 2],
},
"example": [1, 2],
},
),
],
)
def test_to_dict(client, data, expected):
def test_to_dict(data, expected):
assert to_dict(data) == expected


def test_merge_dict(client):
def test_merge_dict():
first = {
"info": {"description": "simple description", "docs": "http://localhost/docs"}
}
Expand All @@ -31,6 +65,31 @@ def test_merge_dict(client):
"docs": "http://localhost/docs",
},
"app": {"password": "234"},
"example": [1, 2],
"example[0]": 1,
"example[1]": 2,
}
assert merge_dict(first, second) == expected


def test_merge_list():
config = {
"info": {
"description": "simple description",
"url": "http://localhost",
"docs": "http://localhost/docs",
},
"app": {"password": "234"},
"example[0]": 1,
"example[1]": 2,
}
expected_config = {
"info": {
"description": "simple description",
"url": "http://localhost",
"docs": "http://localhost/docs",
},
"app": {"password": "234"},
"example": [1, 2],
}
merge_list(config)
assert config == expected_config

0 comments on commit 4c57438

Please sign in to comment.