|
19 | 19 | logger = logging.getLogger(__name__) |
20 | 20 |
|
21 | 21 |
|
| 22 | +def convert_floats_to_decimal(obj): |
| 23 | + """ |
| 24 | + Recursively convert float values to Decimal for DynamoDB compatibility. |
| 25 | +
|
| 26 | + Args: |
| 27 | + obj: Object that may contain float values |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Object with floats converted to Decimal |
| 31 | + """ |
| 32 | + if isinstance(obj, float): |
| 33 | + return Decimal(str(obj)) |
| 34 | + elif isinstance(obj, dict): |
| 35 | + return {key: convert_floats_to_decimal(value) for key, value in obj.items()} |
| 36 | + elif isinstance(obj, list): |
| 37 | + return [convert_floats_to_decimal(item) for item in obj] |
| 38 | + else: |
| 39 | + return obj |
| 40 | + |
| 41 | + |
22 | 42 | class DocumentDynamoDBService: |
23 | 43 | """ |
24 | 44 | Service for interacting directly with DynamoDB to manage Documents. |
@@ -203,11 +223,15 @@ def _document_to_update_expressions( |
203 | 223 | if section.confidence_threshold_alerts: |
204 | 224 | alerts_data = [] |
205 | 225 | for alert in section.confidence_threshold_alerts: |
206 | | - alert_data = { |
207 | | - "attributeName": alert.get("attribute_name"), |
208 | | - "confidence": alert.get("confidence"), |
209 | | - "confidenceThreshold": alert.get("confidence_threshold"), |
210 | | - } |
| 226 | + alert_data = convert_floats_to_decimal( |
| 227 | + { |
| 228 | + "attributeName": alert.get("attribute_name"), |
| 229 | + "confidence": alert.get("confidence"), |
| 230 | + "confidenceThreshold": alert.get( |
| 231 | + "confidence_threshold" |
| 232 | + ), |
| 233 | + } |
| 234 | + ) |
211 | 235 | alerts_data.append(alert_data) |
212 | 236 | section_data["ConfidenceThresholdAlerts"] = alerts_data |
213 | 237 |
|
@@ -242,6 +266,8 @@ def _document_to_update_expressions( |
242 | 266 | expression_values[":SummaryReportUri"] = document.summary_report_uri |
243 | 267 |
|
244 | 268 | update_expression = "SET " + ", ".join(set_expressions) |
| 269 | + # Convert any float values to Decimal for DynamoDB compatibility |
| 270 | + expression_values = convert_floats_to_decimal(expression_values) |
245 | 271 |
|
246 | 272 | return update_expression, expression_names, expression_values |
247 | 273 |
|
|
0 commit comments