Skip to content

Add new features #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/references/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Here a flask extension, ``ioc.extra.flask.di.Extension``

# load an external file defining services
loader = ioc.loader.YamlLoader()
loader.load("%s/resources/config/flask.yml" % path, container_builder)
loader.load("%s/config/flask.yml" % path, container_builder)

# set default parameters into the container to be reuse by the container builder
# or by external services
Expand Down Expand Up @@ -125,4 +125,4 @@ shirka configuration defined inside the ``config.yml`` file::
base_url: '/shirka/api'
data_dir: '%base_dir%/data'

So through some configuration, the user can configure how the Flask action will be expose ``/shirka/api``.
So through some configuration, the user can configure how the Flask action will be expose ``/shirka/api``.
23 changes: 23 additions & 0 deletions ioc/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

import importlib, inspect, re, logging

HAS_PYDANTIC = False
try:
from pydantic import BaseModel as PydanticBaseModel
HAS_PYDANTIC = True
except ImportError:
pass

class Reference(object):
def __init__(self, id: str, method: Optional[str] = None) -> None:
self.id = id
Expand Down Expand Up @@ -102,6 +109,22 @@ def __init__(self, logger: Optional[logging.Logger] = None) -> None:
self.stack: list[str] = []

def _resolve(self, parameter: Any, parameter_holder: ParameterHolder) -> Any:
if HAS_PYDANTIC and isinstance(parameter, PydanticBaseModel):
# If the parameter is a Pydantic model, resolve its fields
def walk_and_modify(model):
for field in model.__fields__:
value = getattr(model, field)
if isinstance(value, PydanticBaseModel):
parameter = walk_and_modify(value) # Recursive call for nested models
else:
parameter = self.resolve(value, parameter_holder)

setattr(model, field, parameter)

return model

return walk_and_modify(parameter)

if isinstance(parameter, (tuple)):
parameter = list(parameter)
for key in get_keys(parameter):
Expand Down
1 change: 1 addition & 0 deletions ioc/extra/jinja2/di.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def load(self, config, container_builder):
mapping[name] = jinja2.FileSystemLoader([
"%s/resources/%s/templates" % (container_builder.parameters.get('project.root_folder'), name),
"%s/resources/templates" % os.path.dirname(importlib.import_module(name).__file__),
"%s/templates" % os.path.dirname(importlib.import_module(name).__file__),
])

container_builder.parameters.set("ioc.extra.jinja2.loader_mapping", mapping)
Expand Down
3 changes: 2 additions & 1 deletion ioc/extra/locator/di.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def load(self, config, container_builder):
for extension in extensions:
arguments = [[
Definition('ioc.locator.FileSystemLocator', arguments=["%s/resources/%s" % (container_builder.parameters.get('project.root_folder'), extension)]),
Definition('ioc.locator.PackageLocator', arguments=[extension], kwargs={'package_path': 'resources'})
Definition('ioc.locator.PackageLocator', arguments=[extension], kwargs={'package_path': 'resources'}),
Definition('ioc.locator.PackageLocator', arguments=[extension], kwargs={'package_path': ''})
]]

locator_map[extension] = Definition('ioc.locator.ChoiceLocator', arguments=arguments)
Expand Down