-
Notifications
You must be signed in to change notification settings - Fork 4
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 cdk): add KeyToSnakeCase transformation (#178)
- Loading branch information
Showing
7 changed files
with
548 additions
and
384 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
68 changes: 68 additions & 0 deletions
68
airbyte_cdk/sources/declarative/transformations/keys_to_snake_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,68 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import re | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, List, Optional | ||
|
||
import unidecode | ||
|
||
from airbyte_cdk.sources.declarative.transformations import RecordTransformation | ||
from airbyte_cdk.sources.types import Config, StreamSlice, StreamState | ||
|
||
|
||
@dataclass | ||
class KeysToSnakeCaseTransformation(RecordTransformation): | ||
token_pattern: re.Pattern[str] = re.compile( | ||
r"[A-Z]+[a-z]*|[a-z]+|\d+|(?P<NoToken>[^a-zA-Z\d]+)" | ||
) | ||
|
||
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._transform_record(record) | ||
record.clear() | ||
record.update(transformed_record) | ||
|
||
def _transform_record(self, record: Dict[str, Any]) -> Dict[str, Any]: | ||
transformed_record = {} | ||
for key, value in record.items(): | ||
transformed_key = self.process_key(key) | ||
transformed_value = value | ||
|
||
if isinstance(value, dict): | ||
transformed_value = self._transform_record(value) | ||
|
||
transformed_record[transformed_key] = transformed_value | ||
return transformed_record | ||
|
||
def process_key(self, key: str) -> str: | ||
key = self.normalize_key(key) | ||
tokens = self.tokenize_key(key) | ||
tokens = self.filter_tokens(tokens) | ||
return self.tokens_to_snake_case(tokens) | ||
|
||
def normalize_key(self, key: str) -> str: | ||
return unidecode.unidecode(key) | ||
|
||
def tokenize_key(self, key: str) -> List[str]: | ||
tokens = [] | ||
for match in self.token_pattern.finditer(key): | ||
token = match.group(0) if match.group("NoToken") is None else "" | ||
tokens.append(token) | ||
return tokens | ||
|
||
def filter_tokens(self, tokens: List[str]) -> List[str]: | ||
if len(tokens) >= 3: | ||
tokens = tokens[:1] + [t for t in tokens[1:-1] if t] + tokens[-1:] | ||
if tokens and tokens[0].isdigit(): | ||
tokens.insert(0, "") | ||
return tokens | ||
|
||
def tokens_to_snake_case(self, tokens: List[str]) -> str: | ||
return "_".join(token.lower() for token in tokens) |
Oops, something went wrong.