-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
07984ae
e0faebc
9bf0e6c
85283cc
1c5acee
5e41aff
321114d
50409a7
008deed
90baefd
ece064a
e04e4ec
fe24621
ca47ce7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,29 @@ def clean_multiline(string: str) -> str: | |
assert string, 'Expected non-empty string' | ||
lines = string.splitlines() | ||
return '\n'.join([dedent(lines[0]), *lines[1:]]) | ||
|
||
|
||
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'(?<=[^\-_])[\-_]+[^\-_]') | ||
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. nit: would be great to add test cases for all these functions! |
||
|
||
|
||
def to_snake_case(input_str: str) -> str: | ||
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() | ||
|
||
|
||
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. |
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: it would be great for this to exhaustively match the enum, you can do this by using our
assert_never()
helper function. Here's an example from another part of the codebase