diff --git a/kpops/utils/pydantic.py b/kpops/utils/pydantic.py index 10c4b9415..b47df8125 100644 --- a/kpops/utils/pydantic.py +++ b/kpops/utils/pydantic.py @@ -4,7 +4,6 @@ import humps from pydantic import BaseModel, ConfigDict, Field -from pydantic.alias_generators import to_snake from pydantic.fields import FieldInfo from pydantic_settings import PydanticBaseSettingsSource from typing_extensions import TypeVar, override @@ -24,6 +23,11 @@ def to_dash(s: str) -> str: return humps.depascalize(s).lower().replace("_", "-") +def to_snake(s: str) -> str: + """Convert PascalCase to snake_case.""" + return humps.depascalize(s).lower() + + def to_dot(s: str) -> str: """Convert snake_case to dot.notation.""" return s.replace("_", ".") diff --git a/tests/utils/test_pydantic.py b/tests/utils/test_pydantic.py index 5503492db..53e7fb29f 100644 --- a/tests/utils/test_pydantic.py +++ b/tests/utils/test_pydantic.py @@ -1,6 +1,6 @@ import pytest -from kpops.utils.pydantic import to_dash +from kpops.utils.pydantic import to_dash, to_snake @pytest.mark.parametrize( @@ -12,7 +12,24 @@ ("ExampleACRONYMComponent", "example-acronym-component"), ("ComponentWithACRONYM", "component-with-acronym"), ("ComponentWithDIGIT00", "component-with-digit00"), + ("S3Test", "s3-test"), ], ) def test_to_dash(input: str, expected: str): assert to_dash(input) == expected + + +@pytest.mark.parametrize( + ("input", "expected"), + [ + ("BaseDefaultsComponent", "base_defaults_component"), + ("ACRONYM", "acronym"), + ("ACRONYMComponent", "acronym_component"), + ("ExampleACRONYMComponent", "example_acronym_component"), + ("ComponentWithACRONYM", "component_with_acronym"), + ("ComponentWithDIGIT00", "component_with_digit00"), + ("S3Test", "s3_test"), # NOTE: this one fails with Pydantic's to_snake util + ], +) +def test_to_snake(input: str, expected: str): + assert to_snake(input) == expected