Skip to content

Commit

Permalink
Refactor out recursion for parsing bad records (#74)
Browse files Browse the repository at this point in the history
* fix recursion

* fix recursion
  • Loading branch information
BTheunissen authored Nov 21, 2023
1 parent 0dee7ef commit f335db7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 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.6"
version = "0.1.7"
description = "`target-clickhouse` is a Singer target for clickhouse, built with the Meltano Singer SDK."
readme = "README.md"
authors = ["Ben Theunissen"]
Expand Down
30 changes: 15 additions & 15 deletions target_clickhouse/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ def activate_version(self, new_version: int) -> None:
conn.execute(query)


# Override record validation implementation to parse objects that can be stringified
# into string fields, ie. numeric or JSON.
def _validate_and_parse(self, record: dict) -> dict:
"""Validate or repair the record, parsing to python-native types as needed.
Expand All @@ -149,17 +147,19 @@ def _validate_and_parse(self, record: dict) -> dict:
Returns:
Validated record.
"""
try:
self._validator.validate(record)
except jsonschema_exceptions.ValidationError as e:
record = handle_validation_error(record, e, self.logger)
return self._validate_and_parse(record)

self._parse_timestamps_in_record(
record=record,
schema=self.schema,
treatment=self.datetime_error_treatment,
)
validation_error = True
while validation_error:
try:
self._validator.validate(record)
self._parse_timestamps_in_record(
record=record,
schema=self.schema,
treatment=self.datetime_error_treatment,
)
validation_error = False
except jsonschema_exceptions.ValidationError as e:
record = handle_validation_error(record, e, self.logger)

return record


Expand All @@ -172,8 +172,8 @@ def handle_validation_error(record,
f"Received non valid record for types 'string', {e.path}, "
f"attempting conversion for record, {record}",
)

# get the parent key path to the problematic value.
# Get the parent key path to the problematic value.
record = record.copy()
parent_key = e.path[0]
problem_value = record[parent_key]

Expand Down

0 comments on commit f335db7

Please sign in to comment.