Skip to content

Commit

Permalink
fix: env list
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo committed Sep 16, 2024
1 parent aced514 commit f4d19d3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
6 changes: 6 additions & 0 deletions dataconf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class EnvListOrderException(Exception):
pass


class EnvListFormatException(Exception):
"""Format exception."""

pass


class ParseException(Exception):
"""Parsing exception."""

Expand Down
9 changes: 5 additions & 4 deletions dataconf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Type
from typing import Union

from dataconf.exceptions import AmbiguousSubclassException
from dataconf.exceptions import AmbiguousSubclassException, EnvListFormatException
from dataconf.exceptions import EnvListOrderException
from dataconf.exceptions import MalformedConfigException
from dataconf.exceptions import MissingTypeException
Expand Down Expand Up @@ -333,6 +333,8 @@ def set_lens(p, focus, v):
if len(p) == 1:
# []x
if isinstance(focus, list):
if not isinstance(p[0], int):
raise EnvListFormatException
if p[0] != len(focus):
raise EnvListOrderException
focus.append(v)
Expand All @@ -346,9 +348,8 @@ def set_lens(p, focus, v):
if p[0] not in focus:
# []{x}
if isinstance(focus, list):
if p[0] != len(focus):
raise EnvListOrderException
focus.append({})
if p[0] == len(focus):
focus.append({})
# {}{x}
else:
focus[p[0]] = {}
Expand Down
33 changes: 32 additions & 1 deletion tests/test_env_vars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataconf.exceptions import EnvListOrderException
from dataconf.exceptions import EnvListFormatException, EnvListOrderException
from dataconf.exceptions import ParseException
from dataconf.utils import __env_vars_parse as env_vars_parse
import pytest
Expand Down Expand Up @@ -65,6 +65,37 @@ def test_ls_order(self) -> None:
with pytest.raises(EnvListOrderException):
env_vars_parse("P", env)

def test_ls_obj(self) -> None:
env = {
"P_A_0__A": "1",
"P_A_1__A": "2",
}
assert env_vars_parse("P", env) == dict(a=[dict(a="1"), dict(a="2")])

env = {
"P_A_0__A": "1",
"P_A_0__B": "2",
}
assert env_vars_parse("P", env) == dict(a=[dict(a="1", b="2")])

env = {
"P_A_0__A": "1",
"P_A_0__B": "2",
"P_A_1__A": "3",
"P_A_1__B": "4",
}
assert env_vars_parse("P", env) == dict(
a=[dict(a="1", b="2"), dict(a="3", b="4")]
)

def test_ls_wrong_obj(self) -> None:
env = {
"P_A_0_A": "1",
"P_A_1_A": "2",
}
with pytest.raises(EnvListFormatException):
env_vars_parse("P", env)

def test_number(self) -> None:
env = {
"A": "1",
Expand Down

0 comments on commit f4d19d3

Please sign in to comment.