From a2f842a6955632b5dbae57befc953797f78d7caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Wed, 21 Aug 2024 14:59:05 -0600 Subject: [PATCH] feat: (WIP) Let developers more easily override SQL column type to JSON schema mapping --- singer_sdk/typing.py | 46 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py index 6bf8d95278..be5bc58777 100644 --- a/singer_sdk/typing.py +++ b/singer_sdk/typing.py @@ -52,6 +52,7 @@ from __future__ import annotations +import functools import json import typing as t @@ -1086,6 +1087,49 @@ def __iter__(self) -> t.Iterator[Property]: return self.wrapped.values().__iter__() +@functools.singledispatch +def _sa_type_to_jsonschema(sa_type: sa.types.TypeEngine) -> dict: # noqa: ARG001 + return StringType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_datetime(sa_type: sa.types.DateTime) -> dict: # noqa: ARG001 + return DateTimeType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_date(sa_type: sa.types.Date) -> dict: # noqa: ARG001 + return DateType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_integer(sa_type: sa.types.Integer) -> dict: # noqa: ARG001 + return IntegerType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_float(sa_type: sa.types.Numeric) -> dict: # noqa: ARG001 + return NumberType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_string(sa_type: sa.types.String) -> dict: # noqa: ARG001 + # TODO: Enable support for maxLength. + # if sa_type.length: + # return StringType(max_length=sa_type.length).type_dict # noqa: ERA001 + return StringType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_boolean(sa_type: sa.types.Boolean) -> dict: # noqa: ARG001 + return BooleanType.type_dict # type: ignore[no-any-return] + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_time(sa_type: sa.types.Variant) -> dict: # noqa: ARG001 + return TimeType.type_dict # type: ignore[no-any-return] + + def to_jsonschema_type( from_type: str | sa.types.TypeEngine | type[sa.types.TypeEngine], ) -> dict: @@ -1122,7 +1166,7 @@ def to_jsonschema_type( if isinstance(from_type, str): type_name = from_type elif isinstance(from_type, sa.types.TypeEngine): - type_name = type(from_type).__name__ + return _sa_type_to_jsonschema(from_type) elif isinstance(from_type, type) and issubclass( from_type, sa.types.TypeEngine,