Skip to content

Commit

Permalink
Support custom fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Oct 30, 2024
1 parent b67b565 commit e8351a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 21 additions & 1 deletion singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def __init__(self) -> None:
"ipv6": lambda _: sa.types.VARCHAR(45),
}

self._fallback_type = sa.types.VARCHAR

def _invoke_handler( # noqa: PLR6301
self,
handler: JSONtoSQLHandler,
Expand All @@ -256,6 +258,24 @@ def _invoke_handler( # noqa: PLR6301
return handler() # type: ignore[no-any-return]
return handler(schema)

@property
def fallback_type(self) -> type[sa.types.TypeEngine]:
"""Return the fallback type.
Returns:
The fallback type.
"""
return self._fallback_type

@fallback_type.setter
def fallback_type(self, value: type[sa.types.TypeEngine]) -> None:
"""Set the fallback type.
Args:
value: The new fallback type.
"""
self._fallback_type = value

def register_type_handler(self, json_type: str, handler: JSONtoSQLHandler) -> None:
"""Register a custom type handler.
Expand Down Expand Up @@ -385,7 +405,7 @@ def to_sql_type(self, schema: dict) -> sa.types.TypeEngine:
return sql_type

# Fallback
return sa.types.VARCHAR()
return self.fallback_type()


class SQLConnector: # noqa: PLR0904
Expand Down
7 changes: 7 additions & 0 deletions tests/core/test_connector_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,13 @@ def test_anyof_unknown(self, json_schema_to_sql: JSONSchemaToSQL):
result = json_schema_to_sql.to_sql_type(jsonschema_type)
assert isinstance(result, sa.types.VARCHAR)

def test_custom_fallback(self):
json_schema_to_sql = JSONSchemaToSQL()
json_schema_to_sql.fallback_type = sa.types.CHAR
jsonschema_type = {"cannot": "compute"}
result = json_schema_to_sql.to_sql_type(jsonschema_type)
assert isinstance(result, sa.types.CHAR)

@pytest.mark.parametrize(
"jsonschema_type,expected_type",
[
Expand Down

0 comments on commit e8351a8

Please sign in to comment.