Skip to content

Commit

Permalink
Add custom PascalCase to snake_case alias generator (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored Jan 29, 2024
1 parent 86f8db9 commit f5de56a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion kpops/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("_", ".")
Expand Down
19 changes: 18 additions & 1 deletion tests/utils/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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

0 comments on commit f5de56a

Please sign in to comment.