Skip to content

Commit

Permalink
Customize string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Oct 30, 2024
1 parent 8e0e0bf commit 6c8faaa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions target_postgres/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
from singer_sdk.connectors.sql import FullyQualifiedName


class JSONSchemaToPostgres(JSONSchemaToSQL):
"""Convert JSON Schema types to Postgres types."""

def __init__(self, *, content_encoding: bool = True) -> None:
"""Initialize the JSONSchemaToPostgres instance."""
super().__init__()
self.content_encoding = content_encoding

def handle_raw_string(self, schema):
"""Handle a raw string type."""
if self.content_encoding and schema.get("contentEncoding") == "base16":
return HexByteString()

return TEXT()


class PostgresConnector(SQLConnector):
"""Sets up SQL Alchemy, and other Postgres related stuff."""

Expand Down Expand Up @@ -242,7 +258,7 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
@cached_property
def jsonschema_to_sql(self) -> JSONSchemaToSQL:
"""Return a JSONSchemaToSQL instance with custom type handling."""
to_sql = JSONSchemaToSQL()
to_sql = JSONSchemaToPostgres(content_encoding=self.interpret_content_encoding)
to_sql.fallback_type = TEXT
to_sql.register_type_handler("integer", BIGINT)
to_sql.register_type_handler("object", JSONB)
Expand Down Expand Up @@ -323,23 +339,6 @@ def pick_individual_type(self, jsonschema_type: dict):
"""
if "null" in jsonschema_type["type"]:
return None
# if "integer" in jsonschema_type["type"]:
# return BIGINT()
# if "object" in jsonschema_type["type"]:
# return JSONB()
# if "array" in jsonschema_type["type"]:
# return self._handle_array_type(jsonschema_type)

# string formats
# if jsonschema_type.get("format") == "date-time":
# return TIMESTAMP()
# if jsonschema_type.get("format") == "uuid":
# return UUID()
if (
self.interpret_content_encoding
and jsonschema_type.get("contentEncoding") == "base16"
):
return HexByteString()

return self.jsonschema_to_sql.to_sql_type(jsonschema_type)

Expand Down

0 comments on commit 6c8faaa

Please sign in to comment.