Skip to content
This repository was archived by the owner on Aug 25, 2022. It is now read-only.

Commit 6a69fc6

Browse files
authored
1 parent cb8ec62 commit 6a69fc6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

target_postgres/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
from tempfile import NamedTemporaryFile
1414

1515
import pkg_resources
16-
from jsonschema.validators import Draft4Validator
16+
from jsonschema import Draft4Validator, FormatChecker
17+
from decimal import Decimal
1718
import singer
1819
from target_postgres.db_sync import DbSync
1920

2021
logger = singer.get_logger()
2122

2223

24+
def float_to_decimal(value):
25+
'''Walk the given data structure and turn all instances of float into
26+
double.'''
27+
if isinstance(value, float):
28+
return Decimal(str(value))
29+
if isinstance(value, list):
30+
return [float_to_decimal(child) for child in value]
31+
if isinstance(value, dict):
32+
return {k: float_to_decimal(v) for k, v in value.items()}
33+
return value
34+
2335
def emit_state(state):
2436
if state is not None:
2537
line = json.dumps(state)
@@ -65,7 +77,7 @@ def persist_lines(config, lines):
6577
stream = o['stream']
6678

6779
# Validate record
68-
validators[stream].validate(o['record'])
80+
validators[stream].validate(float_to_decimal(o['record']))
6981

7082
sync = stream_to_sync[stream]
7183

@@ -93,7 +105,8 @@ def persist_lines(config, lines):
93105
raise Exception("Line is missing required key 'stream': {}".format(line))
94106
stream = o['stream']
95107
schemas[stream] = o
96-
validators[stream] = Draft4Validator(o['schema'])
108+
schema = float_to_decimal(o['schema'])
109+
validators[stream] = Draft4Validator(schema, format_checker=FormatChecker())
97110
if 'key_properties' not in o:
98111
raise Exception("key_properties field is required")
99112
key_properties[stream] = o['key_properties']

0 commit comments

Comments
 (0)