* Fix prefix bug
This commit is contained in:
Andres Morales 2024-09-26 17:02:11 -06:00 коммит произвёл GitHub
Родитель 14c29939cc
Коммит 518d8bc9e8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 31 добавлений и 10 удалений

Просмотреть файл

@ -156,30 +156,31 @@ def _load_config(
value = None
for source in sources:
source_prefix = field_prefix
if (
source.prefix is not None
and prefix_annotation is not None
and field_prefix == prefix_annotation.prefix
and source_prefix == prefix_annotation.prefix
) or (source.prefix is not None and inner):
# Add the source prefix to the prefix annotated field
field_prefix = f"{source.prefix}.{field_prefix}"
elif source.prefix is not None and field_prefix != "":
source_prefix = f"{source.prefix}.{source_prefix}"
elif source.prefix is not None and source_prefix != "":
# Replace the root prefix with the source prefix
without_root_prefix = ".".join(field_prefix.split(".")[1:])
field_prefix = (
without_root_prefix = ".".join(source_prefix.split(".")[1:])
source_prefix = (
f"{source.prefix}.{without_root_prefix}"
if without_root_prefix != ""
else source.prefix
)
elif source.prefix is not None and field_prefix == "":
elif source.prefix is not None and source_prefix == "":
# Use the source prefix as the field prefix
field_prefix = source.prefix
source_prefix = source.prefix
try:
if value and update_annotation:
_value = source.get_value(
name,
field_type,
field_prefix,
source_prefix,
source_alias.get(type(source)),
parser_annotation,
)
@ -188,7 +189,7 @@ def _load_config(
value = source.get_value(
name,
field_type,
field_prefix,
source_prefix,
source_alias.get(type(source)),
parser_annotation,
)

Просмотреть файл

@ -1,6 +1,6 @@
[tool.poetry]
name = "essex-config"
version = "0.0.4"
version = "0.0.5"
description = "Python library for creating configuration objects that read from various sources, including files, environment variables, and Azure Key Vault."
authors = ["Andres Morales <andresmor@microsoft.com>"]
readme = "README.md"

Просмотреть файл

@ -0,0 +1 @@
test_value: True

Просмотреть файл

@ -14,6 +14,7 @@ from essex_config.field_annotations import Alias, Parser, Prefixed, Updatable
from essex_config.sources import Source
from essex_config.sources.args_source import ArgSource
from essex_config.sources.env_source import EnvSource
from essex_config.sources.file_source import FileSource
from essex_config.sources.utils import json_list_parser, plain_text_list_parser
T = TypeVar("T")
@ -501,3 +502,21 @@ def test_parsing_env_variables_with_dotenv_file():
assert basic_config.escaped_template_str == "${DO_NOT_REPLACE}"
assert type(basic_config) == BasicConfiguration
def test_use_file_source_after_env_with_prefix():
class BasicConfiguration(BaseModel):
test_value: bool
yaml_file = (Path(__file__).parent / ".." / "test.yaml").resolve()
basic_config = load_config(
BasicConfiguration,
sources=[
EnvSource(prefix="TEST"),
FileSource(file_path=yaml_file),
],
)
assert basic_config.test_value
assert type(basic_config) == BasicConfiguration