graphql-python / graphene

GraphQL framework for Python
http://graphene-python.org/
MIT License
8.09k stars 824 forks source link

non-reversible case conversion #1204

Open Eraldo opened 4 years ago

Eraldo commented 4 years ago

to_camel_case("my_variable_1") 'myVariable1'

to_snake_case("myVariable1") 'my_variable1'

to_camel_case("my_variable_1") 'myVariable1'

to_snake_case("myVariable1") 'my_variable_1'

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.

Version: graphene-django 2.10.0

This is where in graphene it is happening: https://github.com/graphql-python/graphene/issues/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).
abhushansahu commented 4 years ago

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

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

Eraldo commented 4 years ago

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'