Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed multipleOf validation issue: convert float to Decimal #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions target_postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
from tempfile import TemporaryFile

import pkg_resources
from jsonschema.validators import Draft4Validator
from jsonschema import Draft4Validator, FormatChecker
from decimal import Decimal
import singer
from target_postgres.db_sync import DbSync

logger = singer.get_logger()


def float_to_decimal(value):
'''Walk the given data structure and turn all instances of float into
double.'''
if isinstance(value, float):
return Decimal(str(value))
if isinstance(value, list):
return [float_to_decimal(child) for child in value]
if isinstance(value, dict):
return {k: float_to_decimal(v) for k, v in value.items()}
return value

def emit_state(state):
if state is not None:
line = json.dumps(state)
Expand Down Expand Up @@ -65,7 +77,7 @@ def persist_lines(config, lines):
stream = o['stream']

# Validate record
validators[stream].validate(o['record'])
validators[stream].validate(float_to_decimal(o['record']))

sync = stream_to_sync[stream]

Expand Down Expand Up @@ -93,7 +105,8 @@ def persist_lines(config, lines):
raise Exception("Line is missing required key 'stream': {}".format(line))
stream = o['stream']
schemas[stream] = o
validators[stream] = Draft4Validator(o['schema'])
schema = float_to_decimal(o['schema'])
validators[stream] = Draft4Validator(schema, format_checker=FormatChecker())
if 'key_properties' not in o:
raise Exception("key_properties field is required")
key_properties[stream] = o['key_properties']
Expand Down