Skip to content

Commit

Permalink
Fix conversion (#76)
Browse files Browse the repository at this point in the history
* Force fix

* Force fix

* Only do decimalc

* Fix

* Fix
  • Loading branch information
BTheunissen authored Nov 21, 2023
1 parent 2201d4e commit e85db19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "shaped-target-clickhouse"
version = "0.1.8"
version = "0.1.9"
description = "`target-clickhouse` is a Singer target for clickhouse, built with the Meltano Singer SDK."
readme = "README.md"
authors = ["Ben Theunissen"]
Expand Down
28 changes: 25 additions & 3 deletions target_clickhouse/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import decimal
from collections.abc import MutableMapping
from typing import Any, Iterable

import jsonschema.exceptions as jsonschema_exceptions
Expand Down Expand Up @@ -173,15 +175,35 @@ def _pre_validate_for_string_type(self, record: dict) -> dict:
for key, value in record.items():
# Checking if the schema expects a string for this key.
expected_type = self.schema.get("properties", {}).get(key, {}).get("type")
if expected_type == "string" and not isinstance(value, str):
if "string" in expected_type and not isinstance(value, str):
# Convert the value to string if it's not already a string.
record[key] = (
json.dumps(value)
json.dumps(record[key])
if isinstance(value, (dict, list)) else str(value)
)
if self.logger:
self.logger.debug(
f"Converted field {key} to string: {record[key]}",
)

return record
return self._convert_decimal_to_float(record)

def _convert_decimal_to_float(self, obj):
"""Recursively convert all Decimal values in a dictionary to floats.
Args:
obj: The input object (dictionary, list, or any other data type).
Returns:
The object with all Decimal values converted to strings.
"""
if isinstance(obj, MutableMapping):
for key, value in obj.items():
obj[key] = self._convert_decimal_to_float(value)
elif isinstance(obj, list):
for i, item in enumerate(obj):
obj[i] = self._convert_decimal_to_float(item)
elif isinstance(obj, decimal.Decimal):
return float(obj)

return obj

0 comments on commit e85db19

Please sign in to comment.