python_dto_mappers.utils

Mapping utilities for partial updates and chaining.

Functions

extract_changed_fields(→ Dict[str, Any])

Extract only changed fields for partial updates (PATCH).

chain_map(→ Any)

Chain multiple mappings in sequence.

Module Contents

python_dto_mappers.utils.extract_changed_fields(original: pydantic.BaseModel, update_dto: pydantic.BaseModel, exclude: Set[str] | None = None) Dict[str, Any]

Extract only changed fields for partial updates (PATCH).

Compares original object with update DTO and returns only fields that have changed. Useful for HTTP PATCH operations.

Args:

original: Original object update_dto: Update DTO with new values exclude: Fields to exclude from comparison

Returns:

Dictionary with only changed fields

Example:
>>> original = User(id=1, name="John", email="john@example.com")
>>> update = UserUpdateDTO(name="Jane", email="john@example.com")
>>> changed = extract_changed_fields(original, update)
>>> # {'name': 'Jane'}
python_dto_mappers.utils.chain_map(source: Any, through: List[Type]) Any

Chain multiple mappings in sequence.

Applies multiple type transformations in order, useful for ORM → Domain → DTO conversions.

Args:

source: Source object to map through: List of target types to map through

Returns:

Final mapped object

Example:
>>> # ORM → Domain → DTO
>>> dto = chain_map(
...     orm_user,
...     through=[DomainUser, UserDTO]
... )