-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
feat(generator): support custom default casing for client properties #877
Open
lxxonx
wants to merge
14
commits into
RobertCraigie:main
Choose a base branch
from
lxxonx:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
07984ae
chore: prisma client default element as snake case
lxxonx e0faebc
test: snapshot update
lxxonx 9bf0e6c
Merge remote-tracking branch 'upstream/main'
lxxonx 85283cc
case support
lxxonx 1c5acee
fix: revert casing tests to original ones
lxxonx 5e41aff
Merge remote-tracking branch 'origin/case-support'
lxxonx 321114d
chore(reference): add reference url for casing functions
lxxonx 50409a7
chore(models): add exhaustive match for client casing
lxxonx 008deed
test(client casing): add casing util tests
lxxonx 90baefd
chore(pre-commit.ci): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ece064a
fix(Mixed_Case): supporting Mixed_Case to snake_case
lxxonx e04e4ec
Merge branch 'Mixed_Case-to-snake_case'
lxxonx fe24621
chore(pre-commit.ci): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ca47ce7
Merge remote-tracking branch 'upstream/main'
lxxonx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
import re | ||
import shutil | ||
from typing import TYPE_CHECKING, Any, Dict, List, Union, TypeVar, Iterator | ||
from pathlib import Path | ||
|
@@ -122,3 +123,37 @@ def clean_multiline(string: str) -> str: | |
assert string, 'Expected non-empty string' | ||
lines = string.splitlines() | ||
return '\n'.join([dedent(lines[0]), *lines[1:]]) | ||
|
||
|
||
# https://github.com/nficano/humps/blob/master/humps/main.py | ||
|
||
ACRONYM_RE = re.compile(r'([A-Z\d]+)(?=[A-Z\d]|$)') | ||
PASCAL_RE = re.compile(r'([^\-_]+)') | ||
SPLIT_RE = re.compile(r'([\-_]*[A-Z][^A-Z]*[\-_]*)') | ||
UNDERSCORE_RE = re.compile(r'(?<=[^\-_])[\-_]+[^\-_]') | ||
|
||
|
||
def to_snake_case(input_str: str) -> str: | ||
if to_camel_case(input_str) == input_str or to_pascal_case(input_str) == input_str: # if camel case or pascal case | ||
input_str = ACRONYM_RE.sub(lambda m: m.group(0).title(), input_str) | ||
input_str = '_'.join(s for s in SPLIT_RE.split(input_str) if s) | ||
return input_str.lower() | ||
else: | ||
input_str = re.sub(r'[^a-zA-Z0-9]', '_', input_str) | ||
input_str = input_str.lower().strip('_') | ||
|
||
return input_str | ||
|
||
|
||
def to_camel_case(input_str: str) -> str: | ||
if len(input_str) != 0 and not input_str[:2].isupper(): | ||
input_str = input_str[0].lower() + input_str[1:] | ||
return UNDERSCORE_RE.sub(lambda m: m.group(0)[-1].upper(), input_str) | ||
|
||
|
||
def to_pascal_case(input_str: str) -> str: | ||
def _replace_fn(match: re.Match[str]) -> str: | ||
return match.group(1)[0].upper() + match.group(1)[1:] | ||
|
||
input_str = to_camel_case(PASCAL_RE.sub(_replace_fn, input_str)) | ||
return input_str[0].upper() + input_str[1:] if len(input_str) != 0 else input_str | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: where did you get these functions from? did you come up with it yourself? I'm only asking because, if you copied these from somewhere, it would be great to add a link to that place in case these need to be updated in the future. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would be great to add test cases for all these functions!