Skip to content

Commit 587098f

Browse files
author
Sergio García Prado
authored
Merge pull request #305 from minos-framework/issue-150-config-v2
#150 - Add `ConfigV2`
2 parents 8e25ec7 + 7669030 commit 587098f

File tree

30 files changed

+978
-113
lines changed

30 files changed

+978
-113
lines changed

packages/core/minos-microservice-common/minos/common/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
__version__ = "0.5.3"
44

55
from .builders import (
6+
BuildableMixin,
67
Builder,
78
)
89
from .config import (
910
Config,
1011
ConfigV1,
12+
ConfigV2,
1113
MinosConfig,
1214
)
1315
from .database import (

packages/core/minos-microservice-common/minos/common/builders.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,34 @@ def build(self) -> Instance:
7070
"""
7171

7272

73+
Ins = TypeVar("Ins", bound="BuildableMixin")
74+
75+
76+
class BuildableMixin(SetupMixin):
77+
"""Buildable Mixin class."""
78+
79+
_builder_cls: type[Builder[Ins]]
80+
81+
@classmethod
82+
def _from_config(cls: type[Ins], config: Config, **kwargs) -> Ins:
83+
return cls.get_builder().new().with_config(config).with_kwargs(kwargs).build()
84+
85+
@classmethod
86+
def set_builder(cls: type[Ins], builder: type[Builder[Ins]]) -> None:
87+
"""Set a builder class.
88+
89+
:param builder: The builder class to be set.
90+
:return: This method does not return anything.
91+
"""
92+
cls._builder_cls = builder
93+
94+
@classmethod
95+
def get_builder(cls) -> type[Builder[Ins]]:
96+
"""Get the builder class.
97+
98+
:return: A ``Builder`` subclass.
99+
"""
100+
return cls._builder_cls
101+
102+
73103
B = TypeVar("B", bound=Builder)

packages/core/minos-microservice-common/minos/common/config/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
from .v1 import (
66
ConfigV1,
77
)
8+
from .v2 import (
9+
ConfigV2,
10+
)

packages/core/minos-microservice-common/minos/common/config/abc.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from collections.abc import (
1515
Callable,
1616
)
17+
from contextlib import (
18+
suppress,
19+
)
1720
from pathlib import (
1821
Path,
1922
)
@@ -45,9 +48,6 @@
4548
class Config(ABC):
4649
"""Config base class."""
4750

48-
_PARAMETERIZED_MAPPER: dict = dict()
49-
_ENVIRONMENT_MAPPER: dict = dict()
50-
5151
__slots__ = ("_file_path", "_data", "_with_environment", "_parameterized")
5252

5353
def __init__(self, path: Union[Path, str], with_environment: bool = True, **kwargs):
@@ -68,11 +68,15 @@ def __new__(cls, *args, **kwargs) -> Config:
6868
from .v1 import (
6969
ConfigV1,
7070
)
71+
from .v2 import (
72+
ConfigV2,
73+
)
7174

7275
version_mapper = defaultdict(
7376
lambda: ConfigV1,
7477
{
7578
1: ConfigV1,
79+
2: ConfigV2,
7680
},
7781
)
7882

@@ -185,6 +189,17 @@ def get_interfaces(self) -> dict[str, dict[str, Any]]:
185189
def _get_interfaces(self) -> dict[str, dict[str, Any]]:
186190
raise NotImplementedError
187191

192+
def get_pools(self) -> dict[str, type]:
193+
"""Get the pools value.
194+
195+
:return: A ``dict`` with pool names as keys and pools as values.
196+
"""
197+
return self._get_pools()
198+
199+
@abstractmethod
200+
def _get_pools(self) -> dict[str, type]:
201+
raise NotImplementedError
202+
188203
def get_routers(self) -> list[type]:
189204
"""Get the routers value.
190205
@@ -266,26 +281,41 @@ def get_by_key(self, key: str) -> Any:
266281
:param key: The key that identifies the value.
267282
:return: A value instance.
268283
"""
269-
if key in self._PARAMETERIZED_MAPPER and self._PARAMETERIZED_MAPPER[key] in self._parameterized:
270-
return self._parameterized[self._PARAMETERIZED_MAPPER[key]]
271284

272-
if self._with_environment and key in self._ENVIRONMENT_MAPPER and self._ENVIRONMENT_MAPPER[key] in os.environ:
273-
return os.environ[self._ENVIRONMENT_MAPPER[key]]
285+
def _fn(k: str, data: dict[str, Any], previous: str) -> Any:
286+
current, _sep, following = k.partition(".")
287+
full = f"{previous}.{current}".lstrip(".")
288+
289+
with suppress(KeyError):
290+
return self._parameterized[self._to_parameterized_variable(full)]
274291

275-
def _fn(k: str, data: dict[str, Any]) -> Any:
276-
current, _, following = k.partition(".")
292+
if self._with_environment:
293+
with suppress(KeyError):
294+
return os.environ[self._to_environment_variable(full)]
277295

278296
part = data[current]
279297
if not following:
280-
return part
298+
if not isinstance(part, dict):
299+
return part
281300

282-
return _fn(following, part)
301+
result = dict()
302+
for subpart in part:
303+
result[subpart] = _fn(subpart, part, full)
304+
return result
305+
306+
return _fn(following, part, full)
283307

284308
try:
285-
return _fn(key, self._data)
309+
return _fn(key, self._data, str())
286310
except Exception:
287311
raise MinosConfigException(f"{key!r} field is not defined on the configuration!")
288312

313+
def _to_parameterized_variable(self, key: str) -> str:
314+
raise KeyError
315+
316+
def _to_environment_variable(self, key: str) -> str:
317+
raise KeyError
318+
289319

290320
# noinspection PyUnusedLocal
291321
def _get_version(path: Union[str, Path], *args, **kwargs) -> int:

packages/core/minos-microservice-common/minos/common/config/v1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def _get_name(self) -> str:
112112
def _get_aggregate(self) -> dict[str, Any]:
113113
return {
114114
"entities": [self.get_type_by_key("service.aggregate")],
115+
"repositories": dict(),
115116
}
116117

117118
def _get_saga(self) -> dict[str, Any]:
@@ -203,6 +204,9 @@ def _get_services(self) -> list[type]:
203204

204205
return services
205206

207+
def _get_pools(self) -> dict[str, type]:
208+
return dict()
209+
206210
def _get_routers(self) -> list[type]:
207211
try:
208212
routers = self.get_by_key("routers")
@@ -279,3 +283,9 @@ def _get_discovery(self) -> dict[str, Any]:
279283
"host": self.get_by_key("discovery.host"),
280284
"port": self.get_by_key("discovery.port"),
281285
}
286+
287+
def _to_parameterized_variable(self, key: str) -> str:
288+
return self._PARAMETERIZED_MAPPER[key]
289+
290+
def _to_environment_variable(self, key: str) -> str:
291+
return self._ENVIRONMENT_MAPPER[key]

0 commit comments

Comments
 (0)