Skip to content

Commit

Permalink
Fix camel case (#1979)
Browse files Browse the repository at this point in the history
* Fix camel case

* add exmaple

* fix code
  • Loading branch information
rnetser committed Jul 30, 2024
1 parent 0e028d6 commit c8b9dd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
16 changes: 16 additions & 0 deletions scripts/resource/class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,25 @@ def convert_camel_case_to_snake_case(string_: str) -> str:
if following_capital_chars:
formatted_str += f"_{char.lower()}"
last_capital_char = True
continue

remaining_str = "".join(string_[idx:])
# The 2 letters in the string; uppercase char followed by lowercase char.
# Example: `clusterIPs`, handle `Ps` at this point
if idx + 1 == str_len_for_idx_check:
formatted_str += remaining_str.lower()
break

# The last word in the string; uppercase followed by multiple lowercase chars
# Example: `dataVolumeTTLSeconds`, handle `Seconds` at this point
elif remaining_str.istitle():
formatted_str += f"_{remaining_str.lower()}"
break

else:
formatted_str += char.lower()
last_capital_char = True

else:
formatted_str += char.lower()
last_capital_char = True
Expand Down
15 changes: 13 additions & 2 deletions scripts/resource/tests/test_class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,21 @@ def test_parse_explain(tmpdir_factory, kind, debug_file, result_file):
),
pytest.param("XMLHttpRequest", "xml_http_request", id="combined_uppercase_word_is_first"),
pytest.param(
"additionalCORSAllowedOS", "additional_cors_allowed_os", id="combined_uppercase_word_in_the_middle"
"additionalCORSAllowedOS",
"additional_cors_allowed_os",
id="combined_uppercase_word_in_the_middle",
),
pytest.param("hostIPC", "host_ipc", id="combined_uppercase_word_is_last"),
pytest.param("clusterIPs", "cluster_ips", id="combined_uppercase_word_is_last_ends_with_lowercase"),
pytest.param(
"clusterIPs",
"cluster_ips",
id="combined_uppercase_word_is_last_ends_with_one_lowercase_char",
),
pytest.param(
"dataVolumeTTLSeconds",
"data_volume_ttl_seconds",
id="combined_uppercase_word_followed_by_uppercase_word_is_last_ends_with_lowercase",
),
],
)
def test_convert_camel_case_to_snake_case(camel_case_str, expected):
Expand Down

0 comments on commit c8b9dd7

Please sign in to comment.