Skip to content

Commit

Permalink
refactor: Override superclass to_jsonschema in `PostgresSQLToJSONSc…
Browse files Browse the repository at this point in the history
…hema`
  • Loading branch information
edgarrmondragon committed Nov 28, 2024
1 parent 7e7d194 commit 4b3ac81
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions tap_postgres/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from collections.abc import Iterable, Mapping

from singer_sdk.helpers.types import Context
from sqlalchemy.dialects import postgresql
from sqlalchemy.engine import Engine
from sqlalchemy.engine.reflection import Inspector

Expand All @@ -44,7 +43,12 @@ def __init__(self, dates_as_string: bool, json_as_object: bool, *args, **kwargs)
self.dates_as_string = dates_as_string
self.json_as_object = json_as_object

@SQLToJSONSchema.to_jsonschema.register # type: ignore[attr-defined]
@functools.singledispatchmethod
def to_jsonschema(self, column_type: Any) -> dict:
"""Customize the JSON Schema for Postgres types."""
return super().to_jsonschema(column_type)

@to_jsonschema.register
def array_to_jsonschema(self, column_type: postgresql.ARRAY) -> dict:
"""Override the default mapping for NUMERIC columns.
Expand All @@ -55,21 +59,21 @@ def array_to_jsonschema(self, column_type: postgresql.ARRAY) -> dict:
"items": self.to_jsonschema(column_type.item_type),
}

@SQLToJSONSchema.to_jsonschema.register # type: ignore[attr-defined]
@to_jsonschema.register
def json_to_jsonschema(self, column_type: postgresql.JSON) -> dict:
"""Override the default mapping for JSON and JSONB columns."""
if self.json_as_object:
return {"type": ["object", "null"]}
return {"type": ["string", "number", "integer", "array", "object", "boolean"]}

@SQLToJSONSchema.to_jsonschema.register # type: ignore[attr-defined]
@to_jsonschema.register
def datetime_to_jsonschema(self, column_type: sqlalchemy.types.DateTime) -> dict:
"""Override the default mapping for DATETIME columns."""
if self.dates_as_string:
return {"type": ["string", "null"]}
return super().datetime_to_jsonschema(column_type)

@SQLToJSONSchema.to_jsonschema.register # type: ignore[attr-defined]
@to_jsonschema.register
def date_to_jsonschema(self, column_type: sqlalchemy.types.Date) -> dict:
"""Override the default mapping for DATE columns."""
if self.dates_as_string:
Expand Down

0 comments on commit 4b3ac81

Please sign in to comment.