-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(low-code): added keys replace transformation (#183)
Co-authored-by: octavia-squidington-iii <[email protected]>
- Loading branch information
1 parent
3ee710d
commit 3344441
Showing
5 changed files
with
251 additions
and
0 deletions.
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
61 changes: 61 additions & 0 deletions
61
airbyte_cdk/sources/declarative/transformations/keys_replace_transformation.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,61 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from dataclasses import InitVar, dataclass | ||
from typing import Any, Dict, Mapping, Optional | ||
|
||
from airbyte_cdk import InterpolatedString | ||
from airbyte_cdk.sources.declarative.transformations import RecordTransformation | ||
from airbyte_cdk.sources.types import Config, StreamSlice, StreamState | ||
|
||
|
||
@dataclass | ||
class KeysReplaceTransformation(RecordTransformation): | ||
""" | ||
Transformation that applies keys names replacement. | ||
Example usage: | ||
- type: KeysReplace | ||
old: " " | ||
new: "_" | ||
Result: | ||
from: {"created time": ..., "customer id": ..., "user id": ...} | ||
to: {"created_time": ..., "customer_id": ..., "user_id": ...} | ||
""" | ||
|
||
old: str | ||
new: str | ||
parameters: InitVar[Mapping[str, Any]] | ||
|
||
def __post_init__(self, parameters: Mapping[str, Any]) -> None: | ||
self._old = InterpolatedString.create(self.old, parameters=parameters) | ||
self._new = InterpolatedString.create(self.new, parameters=parameters) | ||
|
||
def transform( | ||
self, | ||
record: Dict[str, Any], | ||
config: Optional[Config] = None, | ||
stream_state: Optional[StreamState] = None, | ||
stream_slice: Optional[StreamSlice] = None, | ||
) -> None: | ||
if config is None: | ||
config = {} | ||
|
||
kwargs = {"record": record, "stream_state": stream_state, "stream_slice": stream_slice} | ||
old_key = str(self._old.eval(config, **kwargs)) | ||
new_key = str(self._new.eval(config, **kwargs)) | ||
|
||
def _transform(data: Dict[str, Any]) -> Dict[str, Any]: | ||
result = {} | ||
for key, value in data.items(): | ||
updated_key = key.replace(old_key, new_key) | ||
if isinstance(value, dict): | ||
result[updated_key] = _transform(value) | ||
else: | ||
result[updated_key] = value | ||
return result | ||
|
||
transformed_record = _transform(record) | ||
record.clear() | ||
record.update(transformed_record) |
112 changes: 112 additions & 0 deletions
112
unit_tests/sources/declarative/transformations/test_keys_replace_transformation.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,112 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
import pytest | ||
|
||
from airbyte_cdk.sources.declarative.transformations.keys_replace_transformation import ( | ||
KeysReplaceTransformation, | ||
) | ||
|
||
_ANY_VALUE = -1 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
[ | ||
"input_record", | ||
"config", | ||
"stream_state", | ||
"stream_slice", | ||
"keys_replace_config", | ||
"expected_record", | ||
], | ||
[ | ||
pytest.param( | ||
{"date time": _ANY_VALUE, "customer id": _ANY_VALUE}, | ||
{}, | ||
{}, | ||
{}, | ||
{"old": " ", "new": "_"}, | ||
{"date_time": _ANY_VALUE, "customer_id": _ANY_VALUE}, | ||
id="simple keys replace config", | ||
), | ||
pytest.param( | ||
{ | ||
"customer_id": 111111, | ||
"customer_name": "MainCustomer", | ||
"field_1_111111": _ANY_VALUE, | ||
"field_2_111111": _ANY_VALUE, | ||
}, | ||
{}, | ||
{}, | ||
{}, | ||
{"old": '{{ record["customer_id"] }}', "new": '{{ record["customer_name"] }}'}, | ||
{ | ||
"customer_id": 111111, | ||
"customer_name": "MainCustomer", | ||
"field_1_MainCustomer": _ANY_VALUE, | ||
"field_2_MainCustomer": _ANY_VALUE, | ||
}, | ||
id="keys replace config uses values from record", | ||
), | ||
pytest.param( | ||
{"customer_id": 111111, "field_1_111111": _ANY_VALUE, "field_2_111111": _ANY_VALUE}, | ||
{}, | ||
{}, | ||
{"customer_name": "MainCustomer"}, | ||
{"old": '{{ record["customer_id"] }}', "new": '{{ stream_slice["customer_name"] }}'}, | ||
{ | ||
"customer_id": 111111, | ||
"field_1_MainCustomer": _ANY_VALUE, | ||
"field_2_MainCustomer": _ANY_VALUE, | ||
}, | ||
id="keys replace config uses values from slice", | ||
), | ||
pytest.param( | ||
{"customer_id": 111111, "field_1_111111": _ANY_VALUE, "field_2_111111": _ANY_VALUE}, | ||
{"customer_name": "MainCustomer"}, | ||
{}, | ||
{}, | ||
{"old": '{{ record["customer_id"] }}', "new": '{{ config["customer_name"] }}'}, | ||
{ | ||
"customer_id": 111111, | ||
"field_1_MainCustomer": _ANY_VALUE, | ||
"field_2_MainCustomer": _ANY_VALUE, | ||
}, | ||
id="keys replace config uses values from config", | ||
), | ||
pytest.param( | ||
{ | ||
"date time": _ANY_VALUE, | ||
"user id": _ANY_VALUE, | ||
"customer": { | ||
"customer name": _ANY_VALUE, | ||
"customer id": _ANY_VALUE, | ||
"contact info": {"email": _ANY_VALUE, "phone number": _ANY_VALUE}, | ||
}, | ||
}, | ||
{}, | ||
{}, | ||
{}, | ||
{"old": " ", "new": "_"}, | ||
{ | ||
"customer": { | ||
"contact_info": {"email": _ANY_VALUE, "phone_number": _ANY_VALUE}, | ||
"customer_id": _ANY_VALUE, | ||
"customer_name": _ANY_VALUE, | ||
}, | ||
"date_time": _ANY_VALUE, | ||
"user_id": _ANY_VALUE, | ||
}, | ||
id="simple keys replace config with nested fields in record", | ||
), | ||
], | ||
) | ||
def test_transform( | ||
input_record, config, stream_state, stream_slice, keys_replace_config, expected_record | ||
): | ||
KeysReplaceTransformation( | ||
old=keys_replace_config["old"], new=keys_replace_config["new"], parameters={} | ||
).transform( | ||
record=input_record, config=config, stream_state=stream_state, stream_slice=stream_slice | ||
) | ||
assert input_record == expected_record |