Skip to content

Commit

Permalink
feat: (WIP) Let developers more easily override SQL column type to JS…
Browse files Browse the repository at this point in the history
…ON schema mapping
  • Loading branch information
edgarrmondragon committed Aug 21, 2024
1 parent b619b0d commit e51e8dc
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

from __future__ import annotations

import functools
import json
import typing as t

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e51e8dc

Please sign in to comment.