Skip to content

feat(low-code): camel_case_to_snake_case macros #617

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions airbyte_cdk/sources/declarative/interpolation/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import builtins
import datetime
import re
import typing
from typing import Optional, Union
from urllib.parse import quote_plus
Expand Down Expand Up @@ -194,6 +195,18 @@ def sanitize_url(value: str) -> str:
return sanitization_strategy(value)


def camel_case_to_snake_case(value: str) -> str:
"""
Converts CamelCase strings to snake_case format

Usage:
`"{{ camel_case_to_snake_case('CamelCase') }}"`
:param value: string to convert from CamelCase to snake_case
:return: snake_case formatted string
"""
return re.sub(r"(?<!^)(?=[A-Z])", "_", value).lower()


_macros_list = [
now_utc,
today_utc,
Expand All @@ -206,5 +219,6 @@ def sanitize_url(value: str) -> str:
today_with_timezone,
str_to_datetime,
sanitize_url,
camel_case_to_snake_case,
]
macros = {f.__name__: f for f in _macros_list}
26 changes: 26 additions & 0 deletions unit_tests/sources/declarative/interpolation/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
("test_day_delta", "day_delta", True),
("test_format_datetime", "format_datetime", True),
("test_duration", "duration", True),
("test_camel_case_to_snake_case", "camel_case_to_snake_case", True),
("test_not_a_macro", "thisisnotavalidmacro", False),
],
)
Expand Down Expand Up @@ -249,3 +250,28 @@ def test_sanitize_url(test_name, input_value, expected_output):
sanitize_url = macros["sanitize_url"]
actual_output = sanitize_url(input_value)
assert actual_output == expected_output


@pytest.mark.parametrize(
"value, expected_value",
[
(
"CamelCase",
"camel_case",
),
(
"snake_case",
"snake_case",
),
(
"CamelCasesnake_case",
"camel_casesnake_case",
),
(
"CamelCase_snake_case",
"camel_case_snake_case",
),
],
)
def test_camel_case_to_snake_case(value, expected_value):
assert macros["camel_case_to_snake_case"](value) == expected_value
Loading