From e51e8dcb261563956c58002e719a388aa57d7d2f 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 | 45 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py index 6bf8d9527..53ea63e4b 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,48 @@ 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 + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_datetime(sa_type: sa.types.DateTime) -> dict: # noqa: ARG001 + return DateTimeType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_date(sa_type: sa.types.Date) -> dict: # noqa: ARG001 + return DateType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_integer(sa_type: sa.types.Integer) -> dict: # noqa: ARG001 + return IntegerType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_float(sa_type: sa.types.Numeric) -> dict: # noqa: ARG001 + return NumberType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_string(sa_type: sa.types.String) -> dict: + if sa_type.length: + return StringType(max_length=sa_type.length).type_dict + return StringType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_boolean(sa_type: sa.types.Boolean) -> dict: # noqa: ARG001 + return BooleanType.type_dict + + +@_sa_type_to_jsonschema.register +def _sa_type_to_jsonschema_time(sa_type: sa.types.Variant) -> dict: # noqa: ARG001 + return TimeType.type_dict + + def to_jsonschema_type( from_type: str | sa.types.TypeEngine | type[sa.types.TypeEngine], ) -> dict: @@ -1122,7 +1165,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,