Skip to content

Commit 3b924a1

Browse files
author
Bob Strahan
committed
Add Float to Decimal conversion
1 parent b2aa8b6 commit 3b924a1

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

lib/idp_common_pkg/idp_common/dynamodb/service.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919
logger = logging.getLogger(__name__)
2020

2121

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+
2242
class DocumentDynamoDBService:
2343
"""
2444
Service for interacting directly with DynamoDB to manage Documents.
@@ -203,11 +223,15 @@ def _document_to_update_expressions(
203223
if section.confidence_threshold_alerts:
204224
alerts_data = []
205225
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+
)
211235
alerts_data.append(alert_data)
212236
section_data["ConfidenceThresholdAlerts"] = alerts_data
213237

@@ -242,6 +266,8 @@ def _document_to_update_expressions(
242266
expression_values[":SummaryReportUri"] = document.summary_report_uri
243267

244268
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)
245271

246272
return update_expression, expression_names, expression_values
247273

0 commit comments

Comments
 (0)