-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
🏗️ Python CDK: add schema transformer class #6139
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d63db55
Python CDK: add schema transformer class
9938d11
Fix review comments
17add3b
Fix review comments
1fbf534
Fix review comments
8d2c950
Fix review comments
fc81b4c
Merge remote-tracking branch 'origin/master' into drezchykov/cdk-tran…
daeb1bb
Merge remote-tracking branch 'origin/master' into drezchykov/cdk-tran…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
airbyte-cdk/python/airbyte_cdk/sources/utils/transform.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 Airbyte | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
from distutils.util import strtobool | ||
from enum import Flag, auto | ||
from typing import Any, Callable, Dict | ||
|
||
from airbyte_cdk.logger import AirbyteLogger | ||
from jsonschema import Draft7Validator, validators | ||
|
||
logger = AirbyteLogger() | ||
|
||
|
||
class TransformConfig(Flag): | ||
""" | ||
TypeTransformer class config. Configs can be combined using bitwise or operator e.g. | ||
``` | ||
TransformConfig.DefaultSchemaNormalization | TransformConfig.CustomSchemaNormalization | ||
``` | ||
""" | ||
|
||
# No action taken, default behaviour. Cannot be combined with any other options. | ||
NoTransform = auto() | ||
avida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Applies default type casting with default_convert method which converts | ||
# values by applying simple type casting to specified jsonschema type. | ||
DefaultSchemaNormalization = auto() | ||
# Allow registering custom type transformation callback. Can be combined | ||
# with DefaultSchemaNormalization. In this case default type casting would | ||
# be applied before custom one. | ||
CustomSchemaNormalization = auto() | ||
|
||
|
||
class TypeTransformer: | ||
""" | ||
Class for transforming object before output. | ||
""" | ||
|
||
_custom_normalizer: Callable[[Any, Dict[str, Any]], Any] = None | ||
|
||
def __init__(self, config: TransformConfig): | ||
""" | ||
Initialize TypeTransformer instance. | ||
:param config Transform config that would be applied to object | ||
""" | ||
if TransformConfig.NoTransform in config and config != TransformConfig.NoTransform: | ||
raise Exception("NoTransform option cannot be combined with other flags.") | ||
self._config = config | ||
all_validators = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. piggybacking on jsonschema native validators is a clever idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, Im proud of it :) |
||
key: self.__get_normalizer(key, orig_validator) | ||
for key, orig_validator in Draft7Validator.VALIDATORS.items() | ||
# Do not validate field we do not transform for maximum performance. | ||
if key in ["type", "array", "$ref", "properties", "items"] | ||
} | ||
self._normalizer = validators.create(meta_schema=Draft7Validator.META_SCHEMA, validators=all_validators) | ||
|
||
def registerCustomTransform(self, normalization_callback: Callable[[Any, Dict[str, Any]], Any]) -> Callable: | ||
""" | ||
Register custom normalization callback. | ||
:param normalization_callback function to be used for value | ||
normalization. Takes original value and part type schema. Should return | ||
normalized value. See docs/connector-development/cdk-python/schemas.md | ||
for details. | ||
:return Same callbeck, this is usefull for using registerCustomTransform function as decorator. | ||
""" | ||
if TransformConfig.CustomSchemaNormalization not in self._config: | ||
avida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise Exception("Please set TransformConfig.CustomSchemaNormalization config before registering custom normalizer") | ||
self._custom_normalizer = normalization_callback | ||
return normalization_callback | ||
|
||
def __normalize(self, original_item: Any, subschema: Dict[str, Any]) -> Any: | ||
""" | ||
Applies different transform function to object's field according to config. | ||
:param original_item original value of field. | ||
:param subschema part of the jsonschema containing field type/format data. | ||
:return Final field value. | ||
""" | ||
if TransformConfig.DefaultSchemaNormalization in self._config: | ||
original_item = self.default_convert(original_item, subschema) | ||
|
||
if self._custom_normalizer: | ||
original_item = self._custom_normalizer(original_item, subschema) | ||
return original_item | ||
htrueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def default_convert(original_item: Any, subschema: Dict[str, Any]) -> Any: | ||
""" | ||
Default transform function that is used when TransformConfig.DefaultSchemaNormalization flag set. | ||
:param original_item original value of field. | ||
:param subschema part of the jsonschema containing field type/format data. | ||
:return transformed field value. | ||
""" | ||
target_type = subschema.get("type") | ||
if original_item is None and "null" in target_type: | ||
return None | ||
if isinstance(target_type, list): | ||
# jsonschema type could either be a single string or array of type | ||
# strings. In case if there is some disambigous and more than one | ||
# type (except null) do not do any conversion and return original | ||
# value. If type array has one type and null i.e. {"type": | ||
# ["integer", "null"]}, convert value to specified type. | ||
target_type = [t for t in target_type if t != "null"] | ||
if len(target_type) != 1: | ||
return original_item | ||
target_type = target_type[0] | ||
try: | ||
if target_type == "string": | ||
return str(original_item) | ||
elif target_type == "number": | ||
return float(original_item) | ||
elif target_type == "integer": | ||
return int(original_item) | ||
elif target_type == "boolean": | ||
if isinstance(original_item, str): | ||
return strtobool(original_item) == 1 | ||
return bool(original_item) | ||
avida marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except ValueError: | ||
return original_item | ||
return original_item | ||
|
||
def __get_normalizer(self, schema_key: str, original_validator: Callable): | ||
""" | ||
Traverse through object fields using native jsonschema validator and apply normalization function. | ||
:param schema_key related json schema key that currently being validated/normalized. | ||
:original_validator: native jsonschema validator callback. | ||
""" | ||
|
||
def normalizator(validator_instance: Callable, val: Any, instance: Any, schema: Dict[str, Any]): | ||
""" | ||
Jsonschema validator callable it uses for validating instance. We | ||
override default Draft7Validator to perform value transformation | ||
before validation take place. We do not take any action except | ||
logging warn if object does not conform to json schema, just using | ||
jsonschema algorithm to traverse through object fields. | ||
Look | ||
https://python-jsonschema.readthedocs.io/en/stable/creating/?highlight=validators.create#jsonschema.validators.create | ||
validators parameter for detailed description. | ||
: | ||
""" | ||
|
||
def resolve(subschema): | ||
if "$ref" in subschema: | ||
_, resolved = validator_instance.resolver.resolve(subschema["$ref"]) | ||
return resolved | ||
return subschema | ||
|
||
if schema_key == "type" and instance is not None: | ||
if "object" in val and isinstance(instance, dict): | ||
for k, subschema in schema.get("properties", {}).items(): | ||
if k in instance: | ||
subschema = resolve(subschema) | ||
instance[k] = self.__normalize(instance[k], subschema) | ||
elif "array" in val and isinstance(instance, list): | ||
subschema = schema.get("items", {}) | ||
subschema = resolve(subschema) | ||
for index, item in enumerate(instance): | ||
instance[index] = self.__normalize(item, subschema) | ||
# Running native jsonschema traverse algorithm after field normalization is done. | ||
yield from original_validator(validator_instance, val, instance, schema) | ||
|
||
return normalizator | ||
|
||
def transform(self, record: Dict[str, Any], schema: Dict[str, Any]): | ||
""" | ||
Normalize and validate according to config. | ||
:param record record instance for normalization/transformation. All modification are done by modifing existent object. | ||
:schema object's jsonschema for normalization. | ||
""" | ||
if TransformConfig.NoTransform in self._config: | ||
return | ||
normalizer = self._normalizer(schema) | ||
for e in normalizer.iter_errors(record): | ||
""" | ||
just calling normalizer.validate() would throw an exception on | ||
first validation occurences and stop processing rest of schema. | ||
""" | ||
logger.warn(e.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am conflicted between providing this by default in the source itself and therefore increasing the scope of the source vs. just telling the user they are responsible for using this helper class.
How do you think about this decision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it would be a lot of duplicate code like
instead of just
I would like to do this in source code to keep connectors code cleaner. Also connector's developer may forget about get_json_schema caching and this would reduce performance A LOT.