Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDK: improve TypeTransformer to convert simple types to array of simple types #16636

Merged
merged 11 commits into from
Sep 15, 2022
3 changes: 3 additions & 0 deletions airbyte-cdk/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.1.86
- Improve TypeTransformer to convert simple types to array of simple types

## 0.1.85
- Make TypeTransformer more robust to incorrect incoming records

Expand Down
6 changes: 6 additions & 0 deletions airbyte-cdk/python/airbyte_cdk/sources/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

logger = logging.getLogger("airbyte")

simple_types = {"string": str, "number": float, "integer": int, "boolean": bool, "null": type(None)}


class TransformConfig(Flag):
"""
Expand Down Expand Up @@ -114,6 +116,10 @@ def default_convert(original_item: Any, subschema: Dict[str, Any]) -> Any:
if isinstance(original_item, str):
return strtobool(original_item) == 1
return bool(original_item)
elif target_type == "array":
item_types = set(subschema.get("items", {}).get("type", set()))
if item_types.issubset(simple_types) and type(original_item) in simple_types.values():
return [original_item]
except (ValueError, TypeError):
return original_item
return original_item
Expand Down
2 changes: 1 addition & 1 deletion airbyte-cdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="airbyte-cdk",
version="0.1.85",
version="0.1.86",
description="A framework for writing Airbyte Connectors.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
40 changes: 38 additions & 2 deletions airbyte-cdk/python/unit_tests/sources/utils/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,44 @@
# Array without items and value is not an array
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": "12"},
{"value": "12"},
"'12' is not of type 'array'",
{"value": ["12"]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": 12},
{"value": [12]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": None},
{"value": [None]},
None,
),
(
{"type": "object", "properties": {"value": {"type": ["null", "array"]}}},
{"value": None},
{"value": None},
None,
),
(
{"type": "object", "properties": {"value": {"type": ["array"], "items": {"type": ["string"]}}}},
{"value": 10},
{"value": ["10"]},
None,
),
(
{"type": "object", "properties": {"value": {"type": ["array"], "items": {"type": ["object"]}}}},
{"value": "string"},
{"value": "string"},
"'string' is not of type 'array'",
),
(
{"type": "object", "properties": {"value": {"type": ["array"], "items": {"type": ["object"]}}}},
{"value": {"key": "value"}},
{"value": {"key": "value"}},
"{'key': 'value'} is not of type 'array'",
),
(
# Schema root object is not an object, no convertion should happen
Expand Down