-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automate tablename generation from camelCase
This commit introduces a method to automatically convert the class name from camelCase to snake_case for database table names. This enhancement addresses inconsistencies in naming conventions and improves maintainability, allowing developers to focus on writing class definitions without worrying about table naming. - Implemented `__tablename__` using `camel2snake` for automatic conversion - Added utility functions for converting between camelCase and snake_case - Created a test to verify the correct generation of table names Generated-by: aiautocommit
- Loading branch information
1 parent
33905a9
commit 193f839
Showing
5 changed files
with
53 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Lifted from: https://github.com/fastapiutils/fastapi-utils/blob/master/fastapi_utils/camelcase.py | ||
""" | ||
|
||
import re | ||
|
||
|
||
def snake2camel(snake: str, start_lower: bool = False) -> str: | ||
""" | ||
Converts a snake_case string to camelCase. | ||
The `start_lower` argument determines whether the first letter in the generated camelcase should | ||
be lowercase (if `start_lower` is True), or capitalized (if `start_lower` is False). | ||
""" | ||
|
||
camel = snake.title() | ||
camel = re.sub("([0-9A-Za-z])_(?=[0-9A-Z])", lambda m: m.group(1), camel) | ||
if start_lower: | ||
camel = re.sub("(^_*[A-Z])", lambda m: m.group(1).lower(), camel) | ||
return camel | ||
|
||
|
||
def camel2snake(camel: str) -> str: | ||
""" | ||
Converts a camelCase string to snake_case. | ||
""" | ||
snake = re.sub(r"([a-zA-Z])([0-9])", lambda m: f"{m.group(1)}_{m.group(2)}", camel) | ||
snake = re.sub(r"([a-z0-9])([A-Z])", lambda m: f"{m.group(1)}_{m.group(2)}", snake) | ||
return snake.lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from activemodel import BaseModel | ||
|
||
|
||
class TestTable(BaseModel): | ||
id: int | ||
|
||
|
||
def test_table_name(): | ||
assert TestTable.__tablename__ == "test_table" |