-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major redesign of the SPARQLModelAdapter class. Add a code path in ModelBindingsMapper for handling list type fields (other than list[BaseModel]) that triggers array collection behavior. Move ModelBindingsMapper into its own module. Closes #57, #81.
- Loading branch information
Showing
8 changed files
with
223 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from rdfproxy.adapter import SPARQLModelAdapter # noqa: F401 | ||
from rdfproxy.mapper import ModelBindingsMapper # noqa: F401 | ||
from rdfproxy.utils._types import SPARQLBinding # noqa: F401 | ||
from rdfproxy.utils.models import Page # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"""ModelBindingsMapper: Functionality for mapping binding maps to a Pydantic model.""" | ||
|
||
from collections.abc import Iterator | ||
from typing import Any, Generic, get_args | ||
|
||
from pydantic import BaseModel | ||
from rdfproxy.utils._types import _TModelInstance | ||
from rdfproxy.utils.utils import ( | ||
_collect_values_from_bindings, | ||
_get_group_by, | ||
_get_key_from_metadata, | ||
_list_basemodel_p, | ||
_list_p, | ||
) | ||
|
||
|
||
class ModelBindingsMapper(Generic[_TModelInstance]): | ||
"""Utility class for mapping flat bindings to a (potentially nested) Pydantic model.""" | ||
|
||
def __init__(self, model: type[_TModelInstance], *bindings: dict): | ||
self.model = model | ||
self.bindings = bindings | ||
self._contexts = [] | ||
|
||
def get_models(self) -> list[_TModelInstance]: | ||
"""Generate a list of (potentially nested) Pydantic models based on (flat) bindings.""" | ||
return self._get_unique_models(self.model, self.bindings) | ||
|
||
def _get_unique_models(self, model, bindings): | ||
"""Call the mapping logic and collect unique and non-empty models.""" | ||
models = [] | ||
for _bindings in bindings: | ||
_model = model(**dict(self._generate_binding_pairs(model, **_bindings))) | ||
|
||
if any(_model.model_dump().values()) and (_model not in models): | ||
models.append(_model) | ||
|
||
return models | ||
|
||
def _get_group_by(self, model, kwargs) -> str: | ||
"""Get the group_by value from a model and register it in self._contexts.""" | ||
group_by: str = _get_group_by(model, kwargs) | ||
|
||
if group_by not in self._contexts: | ||
self._contexts.append(group_by) | ||
|
||
return group_by | ||
|
||
def _generate_binding_pairs( | ||
self, | ||
model: type[BaseModel], | ||
**kwargs, | ||
) -> Iterator[tuple[str, Any]]: | ||
"""Generate an Iterator[tuple] projection of the bindings needed for model instantation.""" | ||
for k, v in model.model_fields.items(): | ||
if _list_basemodel_p(v.annotation): | ||
group_by: str = self._get_group_by(model, kwargs) | ||
group_model, *_ = get_args(v.annotation) | ||
|
||
applicable_bindings = filter( | ||
lambda x: (x[group_by] == kwargs[group_by]) | ||
and (x[self._contexts[0]] == kwargs[self._contexts[0]]), | ||
self.bindings, | ||
) | ||
|
||
value = self._get_unique_models(group_model, applicable_bindings) | ||
|
||
elif _list_p(v.annotation): | ||
group_by: str = self._get_group_by(model, kwargs) | ||
applicable_bindings = filter( | ||
lambda x: x[group_by] == kwargs[group_by], | ||
self.bindings, | ||
) | ||
|
||
binding_key: str = _get_key_from_metadata(v) or k | ||
value = _collect_values_from_bindings(binding_key, applicable_bindings) | ||
|
||
elif isinstance(v.annotation, type(BaseModel)): | ||
nested_model = v.annotation | ||
value = nested_model( | ||
**dict(self._generate_binding_pairs(nested_model, **kwargs)) | ||
) | ||
else: | ||
binding_key: str = _get_key_from_metadata(v) or k | ||
value = kwargs.get(binding_key, v.default) | ||
|
||
yield k, value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.