Skip to content

Commit cc93e62

Browse files
darynaishchenkooctavia-squidington-iii
andauthored
feat(low-code): camel_case_to_snake_case macros (#617)
Co-authored-by: octavia-squidington-iii <contact@airbyte.com>
1 parent 2f8310e commit cc93e62

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎airbyte_cdk/sources/declarative/interpolation/macros.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import builtins
66
import datetime
7+
import re
78
import typing
89
from typing import Optional, Union
910
from urllib.parse import quote_plus
@@ -194,6 +195,18 @@ def sanitize_url(value: str) -> str:
194195
return sanitization_strategy(value)
195196

196197

198+
def camel_case_to_snake_case(value: str) -> str:
199+
"""
200+
Converts CamelCase strings to snake_case format
201+
202+
Usage:
203+
`"{{ camel_case_to_snake_case('CamelCase') }}"`
204+
:param value: string to convert from CamelCase to snake_case
205+
:return: snake_case formatted string
206+
"""
207+
return re.sub(r"(?<!^)(?=[A-Z])", "_", value).lower()
208+
209+
197210
_macros_list = [
198211
now_utc,
199212
today_utc,
@@ -206,5 +219,6 @@ def sanitize_url(value: str) -> str:
206219
today_with_timezone,
207220
str_to_datetime,
208221
sanitize_url,
222+
camel_case_to_snake_case,
209223
]
210224
macros = {f.__name__: f for f in _macros_list}

‎unit_tests/sources/declarative/interpolation/test_macros.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
("test_day_delta", "day_delta", True),
2020
("test_format_datetime", "format_datetime", True),
2121
("test_duration", "duration", True),
22+
("test_camel_case_to_snake_case", "camel_case_to_snake_case", True),
2223
("test_not_a_macro", "thisisnotavalidmacro", False),
2324
],
2425
)
@@ -249,3 +250,28 @@ def test_sanitize_url(test_name, input_value, expected_output):
249250
sanitize_url = macros["sanitize_url"]
250251
actual_output = sanitize_url(input_value)
251252
assert actual_output == expected_output
253+
254+
255+
@pytest.mark.parametrize(
256+
"value, expected_value",
257+
[
258+
(
259+
"CamelCase",
260+
"camel_case",
261+
),
262+
(
263+
"snake_case",
264+
"snake_case",
265+
),
266+
(
267+
"CamelCasesnake_case",
268+
"camel_casesnake_case",
269+
),
270+
(
271+
"CamelCase_snake_case",
272+
"camel_case_snake_case",
273+
),
274+
],
275+
)
276+
def test_camel_case_to_snake_case(value, expected_value):
277+
assert macros["camel_case_to_snake_case"](value) == expected_value

0 commit comments

Comments
 (0)