Skip to content
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

non-reversible case conversion #1204

Open
Eraldo opened this issue Jun 6, 2020 · 2 comments
Open

non-reversible case conversion #1204

Eraldo opened this issue Jun 6, 2020 · 2 comments
Labels

Comments

@Eraldo
Copy link

Eraldo commented Jun 6, 2020

  • What is the current behavior?
>>> from graphene.utils.str_converters import to_snake_case
>>> from graphene.utils.str_converters import to_camel_case

>>> to_camel_case("my_variable_1")
'myVariable1'

>>> to_snake_case("myVariable1")
'my_variable1'
  • What is the expected behavior?
>>> from graphene.utils.str_converters import to_snake_case
>>> from graphene.utils.str_converters import to_camel_case

>>> to_camel_case("my_variable_1")
'myVariable1'

>>> to_snake_case("myVariable1")
'my_variable_1'
  • What is the motivation / use case for changing the behavior?

Being able to convert objects/properties back and forth between server and client.
Also useful for introspecting and debugging use cases.

In my particular case, I receive an object on my server that the client got from the api and I want to associate it with the source which I now need an extra mapping for since the conversion is one-directional.

  • Please tell us about your environment:

Version: graphene-django 2.10.0

  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

This is where in graphene it is happening: #1204

My current workaround is to add an underscore in case of trailing numbers:

input = re.sub(r'(.*)(\d+)$', r'\1_\2', input) 

I have 2 solution suggestions:

  1. Adding an underscore in case of trailing numbers.
  2. Not removing the underscore in case of trailing numbers (like _1).
@Eraldo Eraldo added the 🐛 bug label Jun 6, 2020
@abhushansahu
Copy link

For your use-case, I believe this can also work.

re.sub("([a-z]+)(([A-Z][0-9])*)", r"\1_\2", name)

@Eraldo
Copy link
Author

Eraldo commented Jun 6, 2020

For your use-case, I believe this can also work.

re.sub("([a-z]+)(([A-Z][0-9])*)", r"\1_\2", name)

Thanks @abhushansahu for the comment.

This adds an underscore before each digit.
E.g.:

In [6]: re.sub(r'(.*)(\d+)$', r"\1_\2", "http2Module1").lower()                 
Out[6]: 'http2module_1'

In [7]: re.sub("([a-z]+)(([A-Z][0-9])*)", r"\1_\2", "http2Module1").lower()     
Out[7]: 'http_2module_1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants