python_dto_mappers.decorators

Decorators for auto-mapping (syntactic sugar over AutoMapper).

Functions

auto_map(→ Callable)

Decorator to auto-generate mapping for matching fields.

field_transform(→ Callable)

Mark a method as a field transformation.

Module Contents

python_dto_mappers.decorators.auto_map(source_type: Type, target_type: Type, exclude: Set[str] | None = None) Callable

Decorator to auto-generate mapping for matching fields.

Uses AutoMapper engine under the hood. Custom transforms can be defined with transform_{field_name} methods on the class.

Args:

source_type: Source model type target_type: Target model type exclude: Set of field names to exclude from auto-mapping

Returns:

Class decorator function

Example:
>>> @auto_map(UserEntity, UserDTO, exclude={'id'})
... class UserMapper:
...     def transform_name(self, source):
...         return source.name.upper()
python_dto_mappers.decorators.field_transform(field_name: str) Callable

Mark a method as a field transformation.

Args:

field_name: Name of the field this transforms

Returns:

Method decorator

Example:
>>> @field_transform("created_at")
... def transform_date(self, source):
...     return source.created_at.isoformat()