colander_data_converter.base.utils module
- class colander_data_converter.base.utils.BaseModelMerger(strategy=MergingStrategy.OVERWRITE)[source]
Bases:
object
A utility class for merging
pydantic.BaseModel
instances with configurable strategies.This class provides functionality to merge fields from a source BaseModel into a destination BaseModel, handling both regular model fields and extra attributes. Fields containing ObjectReference types are automatically excluded from merging and reported as unprocessed.
The merger supports two strategies:
PRESERVE
: Only merge fields if the destination field is empty or NoneOVERWRITE
: Always merge fields from source to destination
Fields are merged based on type compatibility and field constraints. Extra attributes are automatically converted to strings when stored in the attributes dictionary (if supported by the destination model).
Example
>>> from pydantic import BaseModel >>> class SourceModel(BaseModel): ... name: str ... age: int ... attributes: dict = {} >>> class DestinationModel(BaseModel): ... name: str ... age: int ... city: str = "Unknown" ... attributes: dict = {} >>> source = SourceModel(name="Alice", age=30) >>> destination = DestinationModel(name="Bob", age=25) >>> merger = BaseModelMerger(strategy=MergingStrategy.OVERWRITE) >>> unprocessed = merger.merge(source, destination) >>> print(destination.name) Alice >>> print(destination.age) 30 >>> print(destination.city) Unknown
Note
Fields with
ObjectReference
types are never merged and are reported as unprocessedFrozen fields cannot be modified and will be reported as unprocessed
Complex types (list, dict, tuple, set) in extra attributes are not supported
Extra attributes are converted to strings when stored
- merge(source, destination, ignored_fields=None)[source]
Merge all compatible fields from the source object into the destination object.
This method iterates through all fields in the source object and attempts to merge them into the destination object. It handles both regular object fields and extra attributes dictionary if supported.
- Parameters:
source (BaseModel) – The source model to merge from
destination (BaseModel) – The destination model to merge to
ignored_fields (List[str], optional) – List of field names to skip during merging
- Returns:
A list of field names that could not be processed during the merge operation. Fields containing ObjectReference types are automatically added to this list.
- Return type:
List[str]
- merge_field(destination, field_name, field_value, ignored_fields=None)[source]
Merge a single field from source to destination model.
This method handles the logic for merging individual fields, including type checking, field existence validation, and attribute handling. It processes both regular model fields and extra attributes based on the destination model’s capabilities and field constraints.
Note
The method follows these rules:
Skips fields listed in ignored_fields
Skips empty/None field values
For fields not in the destination model schema: stores as string in attributes dict (if supported) unless the value is a complex type
For schema fields: merges only if type-compatible, not frozen, not containing ObjectReference, and destination is empty (
PRESERVE
) or strategy isOVERWRITE
- Parameters:
- Returns:
True if the field was processed (successfully merged or handled), False if the field could not be processed
- Return type: