-
-
Notifications
You must be signed in to change notification settings - Fork 81
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?
Conversation
Thanks! Unfortunately, this is a breaking change so I'd like to first introduce this using a config option, e.g. generator client {
provider = "prisma-client-py"
client_casing = "snake_case"
} I'm also a little hesitant to introduce this new dependency, would it be feasible to do this without that? or at least using a popular package for changing case? It's also worth noting that longer term, I want to support customising this more freely, e.g. (structure still TBD) /// @Python(instance_name: 'user_foo')
model UserFoo {
// ...
} |
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.
Haven't fully reviewed yet but love the direction!
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 comment
The 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.
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 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!
src/prisma/generator/models.py
Outdated
if isinstance(config, Config) and config.client_casing == ClientCasing.camel_case: | ||
return to_camel_case(self.name) | ||
elif isinstance(config, Config) and config.client_casing == ClientCasing.pascal_case: | ||
return to_pascal_case(self.name) | ||
elif isinstance(config, Config) and config.client_casing == ClientCasing.snake_case: | ||
return to_snake_case(self.name) | ||
return self.name.lower() |
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
if value == EngineType.binary:
return value
elif value == EngineType.dataproxy: # pragma: no cover
raise ValueError('Prisma Client Python does not support the Prisma Data Proxy yet.')
elif value == EngineType.library: # pragma: no cover
raise ValueError('Prisma Client Python does not support native engine bindings yet.')
else: # pragma: no cover
assert_never(value)
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
FYI I cherry picked your change case util functions into a separate PR as I needed them for something else, #918. I tried to rebase your PR for you to resolve the merge conflicts but it looks like I don't have permission to push to your fork, could you open a separate PR with edit perms and from a different branch? (i.e. a branch that isn't |
New PR has opened with edit permission |
Change Summary
this code generate prisma class attributes like
prisma.userprofile
, which is sometimes very hard to readi suggest having default attribute name as
prisma.user_profile
with usingjinja2_strcase
package.Checklist
Agreement
By submitting this pull request, I confirm that you can use, modify, copy and redistribute this contribution, under the terms of your choice.