-
Notifications
You must be signed in to change notification settings - Fork 30
feat(low-code cdk): add flatten fields #181
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
airbyte_cdk/sources/declarative/transformations/flatten_fields.py
This file contains hidden or 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,50 @@ | ||
| # | ||
| # Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from airbyte_cdk.sources.declarative.transformations import RecordTransformation | ||
| from airbyte_cdk.sources.types import Config, StreamSlice, StreamState | ||
|
|
||
|
|
||
| @dataclass | ||
| class FlattenFields(RecordTransformation): | ||
| def transform( | ||
| self, | ||
| record: Dict[str, Any], | ||
| config: Optional[Config] = None, | ||
| stream_state: Optional[StreamState] = None, | ||
| stream_slice: Optional[StreamSlice] = None, | ||
| ) -> None: | ||
| transformed_record = self.flatten_record(record) | ||
| record.clear() | ||
| record.update(transformed_record) | ||
|
|
||
| def flatten_record(self, record: Dict[str, Any]) -> Dict[str, Any]: | ||
| stack = [(record, "_")] | ||
| transformed_record: Dict[str, Any] = {} | ||
| force_with_parent_name = False | ||
|
|
||
| while stack: | ||
| current_record, parent_key = stack.pop() | ||
|
|
||
| if isinstance(current_record, dict): | ||
| for current_key, value in current_record.items(): | ||
| new_key = ( | ||
| f"{parent_key}.{current_key}" | ||
| if (current_key in transformed_record or force_with_parent_name) | ||
| else current_key | ||
| ) | ||
| stack.append((value, new_key)) | ||
|
|
||
| elif isinstance(current_record, list): | ||
| for i, item in enumerate(current_record): | ||
| force_with_parent_name = True | ||
| stack.append((item, f"{parent_key}.{i}")) | ||
|
|
||
| else: | ||
| transformed_record[parent_key] = current_record | ||
|
|
||
| return transformed_record | ||
54 changes: 54 additions & 0 deletions
54
unit_tests/sources/declarative/transformations/test_flatten_fields.py
This file contains hidden or 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,54 @@ | ||
| # | ||
| # Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| import pytest | ||
|
|
||
| from airbyte_cdk.sources.declarative.transformations.flatten_fields import ( | ||
| FlattenFields, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_record, expected_output", | ||
| [ | ||
| ({"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Doe"}), | ||
| ({"123Number": 123, "456Another123": 456}, {"123Number": 123, "456Another123": 456}), | ||
| ( | ||
| { | ||
| "NestedRecord": {"FirstName": "John", "LastName": "Doe"}, | ||
| "456Another123": 456, | ||
| }, | ||
| { | ||
| "FirstName": "John", | ||
| "LastName": "Doe", | ||
| "456Another123": 456, | ||
| }, | ||
| ), | ||
| ( | ||
| {"ListExample": [{"A": "a"}, {"A": "b"}]}, | ||
| {"ListExample.0.A": "a", "ListExample.1.A": "b"}, | ||
| ), | ||
| ( | ||
| { | ||
| "MixedCase123": { | ||
| "Nested": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}] | ||
| }, | ||
| "SimpleKey": "SimpleValue", | ||
| }, | ||
| { | ||
| "Nested.0.Key.Value": "test1", | ||
| "Nested.1.Key.Value": "test2", | ||
| "SimpleKey": "SimpleValue", | ||
| }, | ||
| ), | ||
| ( | ||
| {"List": ["Item1", "Item2", "Item3"]}, | ||
| {"List.0": "Item1", "List.1": "Item2", "List.2": "Item3"}, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_flatten_fields(input_record, expected_output): | ||
| flattener = FlattenFields() | ||
| flattener.transform(input_record) | ||
| assert input_record == expected_output | ||
lazebnyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.