fix(shared): allow list settings (#1837)
This commit is contained in:
parent
bc6b43c43c
commit
a4caf4782b
|
@ -2,7 +2,7 @@ import sys
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from os import environ
|
from os import environ
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, Optional, Union
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
@ -63,11 +63,18 @@ class BaseConfig(BaseModel):
|
||||||
env_name = (env_prefix + field.name).upper()
|
env_name = (env_prefix + field.name).upper()
|
||||||
|
|
||||||
if field.is_complex():
|
if field.is_complex():
|
||||||
children = self._get_fields_from_env(
|
children: Union[List[Any], Dict[str, Any]] = []
|
||||||
env_name,
|
|
||||||
env_delimiter,
|
if field.sub_fields:
|
||||||
field.type_.__fields__,
|
if env_name in environ:
|
||||||
)
|
children = [v.strip() for v in environ[env_name].split(",")]
|
||||||
|
|
||||||
|
else:
|
||||||
|
children = self._get_fields_from_env(
|
||||||
|
env_name,
|
||||||
|
env_delimiter,
|
||||||
|
field.type_.__fields__,
|
||||||
|
)
|
||||||
|
|
||||||
if len(children) != 0:
|
if len(children) != 0:
|
||||||
result[field.name] = children
|
result[field.name] = children
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from os import environ
|
from os import environ
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
@ -11,17 +12,21 @@ from libretime_shared.config import BaseConfig, DatabaseConfig, RabbitMQConfig
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
class FixtureConfig(BaseConfig):
|
class FixtureConfig(BaseConfig):
|
||||||
api_key: str
|
api_key: str
|
||||||
|
allowed_hosts: List[str] = []
|
||||||
database: DatabaseConfig
|
database: DatabaseConfig
|
||||||
rabbitmq: RabbitMQConfig = RabbitMQConfig()
|
rabbitmq: RabbitMQConfig = RabbitMQConfig()
|
||||||
|
|
||||||
|
|
||||||
FIXTURE_CONFIG_RAW = """
|
FIXTURE_CONFIG_RAW = """
|
||||||
api_key: "f3bf04fc"
|
api_key: "f3bf04fc"
|
||||||
|
allowed_hosts:
|
||||||
|
- example.com
|
||||||
|
- sub.example.com
|
||||||
|
|
||||||
# Comment !
|
# Comment !
|
||||||
database:
|
database:
|
||||||
host: "localhost"
|
host: "localhost"
|
||||||
port: 5432
|
port: 5432
|
||||||
|
|
||||||
ignored: "ignored"
|
ignored: "ignored"
|
||||||
"""
|
"""
|
||||||
|
@ -45,6 +50,7 @@ def test_base_config(tmp_path: Path):
|
||||||
config = FixtureConfig(filepath=config_filepath)
|
config = FixtureConfig(filepath=config_filepath)
|
||||||
|
|
||||||
assert config.api_key == "f3bf04fc"
|
assert config.api_key == "f3bf04fc"
|
||||||
|
assert config.allowed_hosts == ["example.com", "sub.example.com"]
|
||||||
assert config.database.host == "localhost"
|
assert config.database.host == "localhost"
|
||||||
assert config.database.port == 8888
|
assert config.database.port == 8888
|
||||||
assert config.rabbitmq.host == "changed"
|
assert config.rabbitmq.host == "changed"
|
||||||
|
@ -53,12 +59,20 @@ def test_base_config(tmp_path: Path):
|
||||||
# Optional model: loading default values (rabbitmq)
|
# Optional model: loading default values (rabbitmq)
|
||||||
with mock.patch.dict(environ, {}):
|
with mock.patch.dict(environ, {}):
|
||||||
config = FixtureConfig(filepath=config_filepath)
|
config = FixtureConfig(filepath=config_filepath)
|
||||||
|
assert config.allowed_hosts == ["example.com", "sub.example.com"]
|
||||||
assert config.rabbitmq.host == "localhost"
|
assert config.rabbitmq.host == "localhost"
|
||||||
assert config.rabbitmq.port == 5672
|
assert config.rabbitmq.port == 5672
|
||||||
|
|
||||||
# Optional model: overriding using environment (rabbitmq)
|
# Optional model: overriding using environment (rabbitmq)
|
||||||
with mock.patch.dict(environ, {"LIBRETIME_RABBITMQ_HOST": "changed"}):
|
with mock.patch.dict(
|
||||||
|
environ,
|
||||||
|
{
|
||||||
|
"LIBRETIME_RABBITMQ_HOST": "changed",
|
||||||
|
"LIBRETIME_ALLOWED_HOSTS": "example.com, changed.example.com",
|
||||||
|
},
|
||||||
|
):
|
||||||
config = FixtureConfig(filepath=config_filepath)
|
config = FixtureConfig(filepath=config_filepath)
|
||||||
|
assert config.allowed_hosts == ["example.com", "changed.example.com"]
|
||||||
assert config.rabbitmq.host == "changed"
|
assert config.rabbitmq.host == "changed"
|
||||||
assert config.rabbitmq.port == 5672
|
assert config.rabbitmq.port == 5672
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue