|
| 1 | +"""Dict-like object reducers for merging state with field-specific reducers.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from pydantic import BaseModel |
| 6 | +from uipath.platform.attachments import Attachment |
| 7 | + |
| 8 | + |
| 9 | +def add_job_attachments( |
| 10 | + left: dict[str, Attachment], right: dict[str, Attachment] |
| 11 | +) -> dict[str, Attachment]: |
| 12 | + """Merge attachment dictionaries, with right values taking precedence. |
| 13 | +
|
| 14 | + This reducer function merges two dictionaries of attachments by UUID string. |
| 15 | + If the same UUID exists in both dictionaries, the value from 'right' takes precedence. |
| 16 | +
|
| 17 | + Args: |
| 18 | + left: Existing dictionary of attachments keyed by UUID string |
| 19 | + right: New dictionary of attachments to merge |
| 20 | +
|
| 21 | + Returns: |
| 22 | + Merged dictionary with right values overriding left values for duplicate keys |
| 23 | + """ |
| 24 | + if not right: |
| 25 | + return left |
| 26 | + |
| 27 | + if not left: |
| 28 | + return right |
| 29 | + |
| 30 | + return {**left, **right} |
| 31 | + |
| 32 | + |
| 33 | +def merge_objects(left: Any, right: Any) -> Any: |
| 34 | + """Merge a Pydantic model with another model or dict, with right values taking precedence. |
| 35 | +
|
| 36 | + Applies field-specific reducers from annotation metadata when merging values. |
| 37 | +
|
| 38 | + Args: |
| 39 | + left: Existing Pydantic BaseModel instance |
| 40 | + right: New Pydantic BaseModel instance or dict to merge |
| 41 | +
|
| 42 | + Returns: |
| 43 | + New Pydantic model instance with merged values |
| 44 | +
|
| 45 | + Raises: |
| 46 | + TypeError: If left is not a Pydantic BaseModel or right is not a BaseModel or dict |
| 47 | + """ |
| 48 | + if not right: |
| 49 | + return left |
| 50 | + |
| 51 | + if not left: |
| 52 | + return right |
| 53 | + |
| 54 | + # validate input types |
| 55 | + if not isinstance(left, BaseModel): |
| 56 | + raise TypeError("Left object must be a Pydantic BaseModel") |
| 57 | + |
| 58 | + if not isinstance(right, (BaseModel, dict)): |
| 59 | + raise TypeError("Right object must be a Pydantic BaseModel or dict") |
| 60 | + |
| 61 | + model_fields = type(left).model_fields |
| 62 | + merged_values = {} |
| 63 | + |
| 64 | + for field_name in model_fields: |
| 65 | + merged_values[field_name] = getattr(left, field_name) |
| 66 | + |
| 67 | + for field_name in model_fields: |
| 68 | + if isinstance(right, BaseModel): |
| 69 | + if hasattr(right, field_name): |
| 70 | + right_value = getattr(right, field_name) |
| 71 | + else: |
| 72 | + continue # field not present in right |
| 73 | + else: |
| 74 | + # right is dict |
| 75 | + if field_name not in right: |
| 76 | + continue # field not present in right |
| 77 | + right_value = right[field_name] |
| 78 | + |
| 79 | + field_info = model_fields[field_name] |
| 80 | + left_value = merged_values[field_name] |
| 81 | + |
| 82 | + # apply reducer if defined |
| 83 | + if field_info.metadata and callable(field_info.metadata[0]): |
| 84 | + reducer_func = field_info.metadata[0] |
| 85 | + merged_values[field_name] = reducer_func(left_value, right_value) |
| 86 | + else: |
| 87 | + merged_values[field_name] = right_value |
| 88 | + |
| 89 | + # return new model instance with merged values |
| 90 | + return type(left)(**merged_values) |
0 commit comments