|
28 | 28 | T = TypeVar("T") |
29 | 29 | ClassifierFunction = Callable[[Type[T]], bool] |
30 | 30 | SpecFunction = Callable[[Type[T]], Iterable[str]] |
31 | | -FieldsMap = Optional[Dict[str, Any]] |
| 31 | +FieldsMap = Optional[Dict[str, Union[Callable[[S], Any],Any]]] |
32 | 32 |
|
33 | 33 |
|
34 | 34 | def _try_get_field_value( |
35 | 35 | field_name: str, original_obj: Any, custom_mapping: FieldsMap |
36 | 36 | ) -> Tuple[bool, Any]: |
37 | | - if field_name in (custom_mapping or {}): |
| 37 | + if field_name in (custom_mapping or {}): # type: ignore [index] |
| 38 | + if callable(custom_mapping[field_name]): # type: ignore [index] |
| 39 | + return True, custom_mapping[field_name](original_obj) # type: ignore [index] |
38 | 40 | return True, custom_mapping[field_name] # type: ignore [index] |
39 | 41 | if hasattr(original_obj, field_name): |
40 | 42 | return True, getattr(original_obj, field_name) |
@@ -184,7 +186,8 @@ def map( |
184 | 186 | obj (object): Source object to map to `target class`. |
185 | 187 | skip_none_values (bool, optional): Skip None values when creating `target class` obj. Defaults to False. |
186 | 188 | fields_mapping (FieldsMap, optional): Custom mapping. |
187 | | - Specify dictionary in format {"field_name": value_object}. Defaults to None. |
| 189 | + Specify dictionary in format {"field_name": value_object | lambda soure_obj}. Can take a lamdba |
| 190 | + funtion as argument, that will get the source_cls as argument. Defaults to None. |
188 | 191 | use_deepcopy (bool, optional): Apply deepcopy to all child objects when copy from source to target object. |
189 | 192 | Defaults to True. |
190 | 193 |
|
|
0 commit comments