Skip to content

Commit c2c0e71

Browse files
authored
Merge pull request #245 from python-ellar/custom_resolver_gen
Custom resolver gen
2 parents ac7621c + bebfdd6 commit c2c0e71

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

ellar/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
"""Ellar - Python ASGI web framework for building fast, efficient, and scalable RESTful APIs and server-side applications."""
22

3-
__version__ = "0.8.3"
3+
__version__ = "0.8.4"
4+

ellar/common/params/args/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
}
5050

5151

52-
def add_get_resolver_generator(
52+
def add_resolver_generator(
5353
param: t.Type[params.ParamFieldInfo],
5454
resolver_gen: t.Type[BulkArgsResolverGenerator],
5555
) -> None:

ellar/common/params/params.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ def __init__(
115115
def create_resolver(
116116
self, model_field: ModelField
117117
) -> t.Union[BaseRouteParameterResolver, IRouteParameterResolver]:
118+
"""
119+
Create a resolver for a given model field.
120+
121+
This method is responsible for creating the appropriate resolver for a given
122+
model field.
123+
It checks if the model field has been split into many sub-models that are referred to as MultipleResolvers.
124+
And then creates an appropriate resolver for each of them.
125+
126+
Args:
127+
model_field (ModelField): The model field to create a resolver for.
128+
129+
Returns:
130+
BaseRouteParameterResolver: The created resolver.
131+
"""
118132
multiple_resolvers = model_field.field_info.json_schema_extra.pop( # type:ignore[union-attr]
119133
MULTI_RESOLVER_KEY, None
120134
)

ellar/common/params/resolvers/base.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313

1414
class ResolverResult(t.NamedTuple):
15+
"""
16+
A named tuple containing the resolved value, any errors, and the raw data.
17+
"""
18+
1519
data: t.Optional[t.Any]
1620
errors: t.Optional[t.List[t.Dict[str, t.Any]]]
1721
raw_data: t.Dict[str, t.Any]
@@ -27,12 +31,29 @@ class IRouteParameterResolver(ABC, metaclass=ABCMeta):
2731
@abstractmethod
2832
@t.no_type_check
2933
async def resolve(self, *args: t.Any, **kwargs: t.Any) -> ResolverResult:
30-
"""Resolve handle"""
34+
"""
35+
Resolves the value of the parameter during request processing.
36+
37+
Args:
38+
*args: Additional positional arguments.
39+
**kwargs: Additional keyword arguments.
40+
41+
Returns:
42+
`ResolverResult`: A named tuple containing the resolved value, any errors, and the raw data.
43+
"""
3144

3245
@abstractmethod
3346
@t.no_type_check
3447
def create_raw_data(self, data: t.Any) -> t.Dict:
35-
"""Essential for debugging"""
48+
"""
49+
Creates the raw data for the parameter.
50+
51+
Args:
52+
data: The resolved value of the parameter.
53+
54+
Returns:
55+
`dict`: A dictionary containing the raw data.
56+
"""
3657

3758

3859
class BaseRouteParameterResolver(IRouteParameterResolver, ABC):
@@ -42,10 +63,12 @@ def __init__(self, model_field: ModelField, *args: t.Any, **kwargs: t.Any) -> No
4263
)
4364

4465
def create_raw_data(self, data: t.Any) -> t.Dict:
45-
"""Essential for debugging"""
4666
return {self.model_field.name: data}
4767

4868
def assert_field_info(self) -> None:
69+
"""
70+
Asserts that the field info is of the correct type.
71+
"""
4972
from .. import params
5073

5174
assert isinstance(
@@ -69,4 +92,13 @@ async def resolve(self, *args: t.Any, **kwargs: t.Any) -> ResolverResult:
6992
@abstractmethod
7093
@t.no_type_check
7194
async def resolve_handle(self, *args: t.Any, **kwargs: t.Any) -> ResolverResult:
72-
"""resolver action"""
95+
"""
96+
Resolves the value of the parameter during request processing.
97+
98+
Args:
99+
*args: Additional positional arguments.
100+
**kwargs: Additional keyword arguments.
101+
102+
Returns:
103+
`ResolverResult`: A named tuple containing the resolved value, any errors, and the raw data.
104+
"""

0 commit comments

Comments
 (0)