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

add lowerCamelCase #105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ The `my_key` field would not have its data changed:

{"myKey": {"do_not_change": 1}}

=============
Use lower camel case style
=============

You can also specify lowerCamelCase behaviour, in which first letter will be always in lower case.
For example:

.. code-block:: python

data = {"_some_key": 1}

Would become:

.. code-block:: python

{"someKey": 1}

By default, the package not uses lowerCamelCase behaviour. To use it, specify it in your django settings file.

.. code-block:: python

REST_FRAMEWORK = {
# ...
'JSON_UNDERSCOREIZE': {
'lower_camel_case': True,
},
# ...
}

=============
Running Tests
=============
Expand Down
2 changes: 1 addition & 1 deletion djangorestframework_camel_case/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
DEFAULTS = {
"RENDERER_CLASS": "rest_framework.renderers.JSONRenderer",
"PARSER_CLASS": "rest_framework.parsers.JSONParser",
"JSON_UNDERSCOREIZE": {"no_underscore_before_number": False, "ignore_fields": None},
"JSON_UNDERSCOREIZE": {"no_underscore_before_number": False, "ignore_fields": None, "lower_camel_case": False},
}

# List of settings that may be in string import notation.
Expand Down
15 changes: 5 additions & 10 deletions djangorestframework_camel_case/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@

from rest_framework.utils.serializer_helpers import ReturnDict

camelize_re = re.compile(r"[a-z0-9]?_[a-z0-9]")


def underscore_to_camel(match):
group = match.group()
if len(group) == 3:
return group[0] + group[2].upper()
else:
return group[1].upper()
camelize_re = re.compile("_[a-z0-9]")


def camelize(data, **options):
# Handle lazy translated strings.
ignore_fields = options.get("ignore_fields") or ()
lower_camel_case = options.get("lower_camel_case") or False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lower_camel_case = options.get("lower_camel_case") or False
lower_camel_case = options.get("lower_camel_case", False)

Можно так

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

принято, поменял

if isinstance(data, Promise):
data = force_str(data)
if isinstance(data, dict):
Expand All @@ -34,7 +27,9 @@ def camelize(data, **options):
if isinstance(key, Promise):
key = force_str(key)
if isinstance(key, str) and "_" in key:
new_key = re.sub(camelize_re, underscore_to_camel, key)
new_key = re.sub(camelize_re, lambda match: match.group()[1].upper(), key)
if new_key and lower_camel_case:
new_key = f'{new_key[0].lower()}{new_key[1:]}'
else:
new_key = key
if key not in ignore_fields and new_key not in ignore_fields:
Expand Down
29 changes: 29 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_under_to_camel_keys(self):
"mix_123123a_and_letters": 7,
"mix_123123aa_and_letters_complex": 8,
"no_underscore_before123": 9,
"": 10,
}
output = {
"twoWord": 1,
Expand All @@ -49,9 +50,37 @@ def test_under_to_camel_keys(self):
"mix123123aAndLetters": 7,
"mix123123aaAndLettersComplex": 8,
"noUnderscoreBefore123": 9,
"": 10,
}
self.assertEqual(camelize(data), output)

def test_under_to_camel_keys_with_lower_camel_case(self):
data = {
"_two_word": 1,
"_long_key_with_many_underscores": 2,
"_only_1_key": 3,
"_only_one_letter_a": 4,
"_b_only_one_letter": 5,
"_only_c_letter": 6,
"_mix_123123a_and_letters": 7,
"_mix_123123aa_and_letters_complex": 8,
"_no_underscore_before123": 9,
"": 10,
}
output = {
"twoWord": 1,
"longKeyWithManyUnderscores": 2,
"only1Key": 3,
"onlyOneLetterA": 4,
"bOnlyOneLetter": 5,
"onlyCLetter": 6,
"mix123123aAndLetters": 7,
"mix123123aaAndLettersComplex": 8,
"noUnderscoreBefore123": 9,
"": 10,
}
self.assertEqual(camelize(data, lower_camel_case=True), output)

def test_tuples(self):
data = {"multiple_values": (1, 2), "data": [1, 3, 4]}
output = {"multipleValues": [1, 2], "data": [1, 3, 4]}
Expand Down