Skip to content

Commit

Permalink
Implemented dump_value in mapper and some general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gurcuff91 committed May 20, 2024
1 parent a53d8b0 commit e503a28
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 39 deletions.
2 changes: 1 addition & 1 deletion mongotoy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ async def _fetch_by_id(self, value: typing.Any) -> T:
"""
# noinspection PyProtectedMember
return await self.filter(
self._document_cls.id == self._document_cls.id._field.mapper.validate(value)
self._document_cls.id == self._document_cls.id._field.mapper.__validate_value__(value)
)._fetch_one()

async def _count(self) -> int:
Expand Down
47 changes: 45 additions & 2 deletions mongotoy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def field(
index: expressions.IndexType = None,
sparse: bool = False,
unique: bool = False,
lt: typing.Any = None,
lte: typing.Any = None,
gt: typing.Any = None,
gte: typing.Any = None,
min_items: typing.Optional[int] = None,
max_items: typing.Optional[int] = None,
min_len: typing.Optional[int] = None,
max_len: typing.Optional[int] = None,
choices: typing.Optional[list[str]] = None,
**extra
) -> FieldOptions:
"""
Expand All @@ -54,12 +63,41 @@ def field(
index (IndexType, optional): Type of index for the field. Defaults to None.
sparse (bool, optional): Whether the index should be sparse. Defaults to False.
unique (bool, optional): Whether the index should be unique. Defaults to False.
lt (Any): Upper bound for the field value
lte (Any): Upper bound (inclusive) for the field value
gt (Any): Lower bound for the field value
gte (Any): Lower bound (inclusive) for the field value
min_items (int, optional): Minimum number of items for sequence fields
max_items (int, optional): Maximum number of items for sequence fields
min_len (int, optional): Minimum length for string fields
max_len (int, optional): Maximum length for string fields
choices (list[str], optional): List of allowed choices for string fields
extra: Extra configurations
Returns:
FieldOptions: Field descriptor.
"""
extra_configs = {**extra}
if lt is not None:
extra_configs['lt'] = lt
if lte is not None:
extra_configs['lte'] = lte
if gt is not None:
extra_configs['gt'] = gt
if gte is not None:
extra_configs['gte'] = gte
if min_items is not None:
extra_configs['min_items'] = min_items
if max_items is not None:
extra_configs['max_items'] = max_items
if min_len is not None:
extra_configs['min_len'] = min_len
if max_len is not None:
extra_configs['max_len'] = max_len
if choices:
extra_configs['choices'] = choices

return FieldOptions(
alias=alias,
id_field=id_field,
Expand All @@ -68,7 +106,7 @@ def field(
index=index,
sparse=sparse,
unique=unique,
extra=extra
extra=extra_configs
)


Expand Down Expand Up @@ -173,7 +211,10 @@ def __get__(self, instance, owner):
"""
if not instance:
return FieldProxy(self)
return instance.__data__.get(self.name, expressions.EmptyValue)
value = instance.__data__.get(self.name, expressions.EmptyValue)
if value in (expressions.EmptyValue, None):
return value
return self.mapper.dump_value(value)

def __set__(self, instance, value):
"""
Expand Down Expand Up @@ -223,7 +264,9 @@ def validate(self, value) -> typing.Any:
"""
try:
# Validate value
value = self.mapper(value)

# Check id value
if self.alias == '_id' and value is expressions.EmptyValue:
raise ValidationError([
Expand Down
Loading

0 comments on commit e503a28

Please sign in to comment.