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

feat(low-code cdk): add KeyToSnakeCase transformation #178

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

lazebnyi
Copy link
Contributor

@lazebnyi lazebnyi commented Dec 17, 2024

What

Some sources on the road to manifest only require the transformation of their schema keys' values to snake case.

How

Add a KeyToSnakeCase transformation to convert all record keys to snake case.

Example:

FirstName -> first_name
lastName -> last_name
123Number -> _123_number
456Another123 -> _456_another_123
hello@world-> hello_world
test#case -> test_case
MixedUPCase123 -> mixed_upcase_123
lowercaseAnd123 -> lowercase_and_123
Café -> cafe
Naïve -> naive
This is a full sentence -> this_is_a_full_sentence
Another full sentence with more words -> another_full_sentence_with_more_words

Summary by CodeRabbit

  • New Features

    • Introduced a new transformation, KeysToSnakeCase, for converting record keys to snake_case format.
    • Updated the schema to include this transformation in the data processing pipeline.
  • Bug Fixes

    • Improved unit tests for key transformations to ensure accurate output verification.
  • Tests

    • Added a new test suite for the KeysToSnakeCaseTransformation class, covering various key formats and scenarios.
  • Chores

    • Added a new dependency, Unidecode, to the project requirements.

@lazebnyi lazebnyi requested a review from maxi297 December 17, 2024 16:51
@github-actions github-actions bot added the enhancement New feature or request label Dec 17, 2024
Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces a new transformation definition called KeysToSnakeCase, which renames all keys in a record to snake_case format. This transformation is added to the existing schema under the definitions section. The transformations property in the DeclarativeStream definition is updated to reference this new transformation. Additionally, a new class KeysToSnakeCaseTransformation is implemented to handle the transformation logic, and a new test file is created to validate its functionality. A dependency on the Unidecode library is also added to support key normalization.

Changes

File Change Summary
airbyte_cdk/sources/declarative/declarative_component_schema.yaml Added KeysToSnakeCase definition to schema transformations; updated DeclarativeStream to reference KeysToSnakeCase
airbyte_cdk/sources/declarative/models/declarative_component_schema.py Introduced KeysToSnakeCase class and updated DeclarativeStream transformations
airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py New implementation of KeysToSnakeCaseTransformation with key processing methods
pyproject.toml Added Unidecode dependency (v^1.3)
unit_tests/sources/declarative/transformations/test_keys_to_snake_transformation.py New test file for KeysToSnakeCaseTransformation with comprehensive test cases

Sequence Diagram

sequenceDiagram
    participant Record
    participant KeysToSnakeCaseTransformation
    participant Normalizer
    participant Tokenizer
    participant SnakeCaseConverter

    Record->>KeysToSnakeCaseTransformation: transform()
    KeysToSnakeCaseTransformation->>Normalizer: normalize_key()
    Normalizer-->>KeysToSnakeCaseTransformation: normalized key
    KeysToSnakeCaseTransformation->>Tokenizer: tokenize_key()
    Tokenizer-->>KeysToSnakeCaseTransformation: key tokens
    KeysToSnakeCaseTransformation->>SnakeCaseConverter: tokens_to_snake_case()
    SnakeCaseConverter-->>KeysToSnakeCaseTransformation: snake_case key
    KeysToSnakeCaseTransformation->>Record: update with transformed keys
Loading

The sequence diagram illustrates the key transformation process, showing how a record's keys are processed through normalization, tokenization, and conversion to snake_case format.

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
unit_tests/sources/declarative/transformations/test_keys_to_snake_transformation.py (1)

14-45: Great test coverage! Would you consider adding a few more edge cases?

The parametrized tests cover a good range of scenarios. What do you think about adding tests for:

  • Nested dictionaries with mixed-case keys
  • Empty string keys
  • None values in dictionaries

Here's a suggestion for additional test cases:

(
    {"outer": {"InnerKey": _ANY_VALUE}, "NestedObj": {"AnotherLevel": _ANY_VALUE}},
    {"outer": {"inner_key": _ANY_VALUE}, "nested_obj": {"another_level": _ANY_VALUE}},
),
(
    {"": _ANY_VALUE, "ValidKey": _ANY_VALUE},
    {"": _ANY_VALUE, "valid_key": _ANY_VALUE},
),
(
    {"Key": None},
    {"key": None},
),
airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py (2)

14-14: Consider making the regex pattern more efficient?

The current pattern works but requires multiple alternations. What do you think about simplifying it to:

TOKEN_PATTERN = re.compile(r'[A-Z][a-z]*|[a-z]+|\d+|[^a-zA-Z\d]+')

This achieves the same result with less complexity and better readability, wdyt?


52-57: The filter_tokens logic could use some clarification

The current implementation is a bit cryptic. Could we add a docstring explaining:

  1. Why we're limiting to 3 tokens
  2. Why we're keeping first and last tokens
  3. Why we're adding an empty token before numbers

Example:

def filter_tokens(self, tokens: list) -> list:
    """Filter and modify token list based on specific rules:
    1. For 3+ tokens, keep first and last tokens but filter empty ones in between
    2. Prefix numeric tokens with underscore by adding empty token
    
    Args:
        tokens: List of string tokens
    Returns:
        Filtered and modified token list
    """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 216cd43 and 453580d.

⛔ Files ignored due to path filters (1)
  • poetry.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (3 hunks)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2 hunks)
  • airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py (1 hunks)
  • pyproject.toml (1 hunks)
  • unit_tests/sources/declarative/transformations/test_keys_to_lower_transformation.py (1 hunks)
  • unit_tests/sources/declarative/transformations/test_keys_to_snake_transformation.py (1 hunks)
🔇 Additional comments (6)
unit_tests/sources/declarative/transformations/test_keys_to_lower_transformation.py (1)

15-15: LGTM! Nice cleanup of the assertion.

The simplified assertion improves readability while maintaining the same test coverage.

airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py (1)

42-43: LGTM! Good use of unidecode for normalization.

The normalization approach using unidecode is solid and handles international characters well.

pyproject.toml (1)

83-83: LGTM!

The addition of the Unidecode dependency with a caret version constraint is appropriate for the new key transformation feature.

airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2)

713-716: LGTM!

The KeyToSnakeCase class follows the established pattern for transformation classes with appropriate type literal and optional parameters.


1662-1670: LGTM!

The addition of KeyToSnakeCase to the transformations Union type is well-integrated with the existing transformation options.

airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

1842-1854: LGTM!

The KeyToSnakeCase schema definition is well-structured and follows the established pattern for transformation components. The title and description clearly communicate its purpose.

Copy link
Contributor

@maxi297 maxi297 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks Serhii

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

1843-1843: Should we change the title to 'Keys to Snake Case' for consistency?

Currently, the title is 'Key to Snake Case'. To be consistent with 'Keys to Lower Case', perhaps we should rename it to 'Keys to Snake Case'. Wdyt?

Apply this diff to update the title:

-        title: Key to Snake Case
+        title: Keys to Snake Case
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 453580d and 7319974.

📒 Files selected for processing (4)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (3 hunks)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2 hunks)
  • airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py (1 hunks)
  • unit_tests/sources/declarative/transformations/test_keys_to_snake_transformation.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • unit_tests/sources/declarative/transformations/test_keys_to_snake_transformation.py
🔇 Additional comments (4)
airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py (2)

21-30: Consider returning a new record instead of modifying in place

Currently, the transform method modifies the input record in place, which could lead to unexpected behavior if the same dictionary is used elsewhere. Would you consider returning a new transformed record instead? Wdyt?


32-40: Consider adding support for lists in nested structures

The current implementation handles nested dictionaries but does not process lists of dictionaries. Would you consider adding recursive support for lists to ensure all nested keys are transformed? Wdyt?

airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2)

713-716: Looks good to me!

The KeysToSnakeCase class is defined consistently, and the implementation aligns with the existing transformation classes.


1662-1670: KeysToSnakeCase correctly added to transformations list

The KeysToSnakeCase transformation is properly included in the transformations union type, ensuring it can be utilized in declarative streams.

@lazebnyi lazebnyi merged commit adef1e8 into main Dec 18, 2024
20 checks passed
@lazebnyi lazebnyi deleted the lazebnyi/add-keys-to-snake-transformation branch December 18, 2024 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants