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 a2f842a
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 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,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]

Check warning on line 1092 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1092

Added line #L1092 was not covered by tests


@_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]

Check warning on line 1097 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1097

Added line #L1097 was not covered by tests


@_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]

Check warning on line 1102 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1102

Added line #L1102 was not covered by tests


@_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]

Check warning on line 1112 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1112

Added line #L1112 was not covered by tests


@_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]

Check warning on line 1125 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1125

Added line #L1125 was not covered by tests


@_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]

Check warning on line 1130 in singer_sdk/typing.py

View check run for this annotation

Codecov / codecov/patch

singer_sdk/typing.py#L1130

Added line #L1130 was not covered by tests


def to_jsonschema_type(
from_type: str | sa.types.TypeEngine | type[sa.types.TypeEngine],
) -> dict:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a2f842a

Please sign in to comment.