From 819ef5c9541888f9a123afd6f447f63d4c59bfa6 Mon Sep 17 00:00:00 2001 From: rnetser Date: Tue, 30 Jul 2024 13:30:00 +0300 Subject: [PATCH 1/3] Fix camel case --- scripts/resource/class_generator.py | 12 ++++++++++++ scripts/resource/tests/test_class_generator.py | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/resource/class_generator.py b/scripts/resource/class_generator.py index 5274f99ea0..fc42e8a10f 100644 --- a/scripts/resource/class_generator.py +++ b/scripts/resource/class_generator.py @@ -173,9 +173,21 @@ def convert_camel_case_to_snake_case(string_: str) -> str: if following_capital_chars: formatted_str += f"_{char.lower()}" last_capital_char = True + + # The 2 letters in the string; uppercase char followed by lowercase char + elif idx + 1 == str_len_for_idx_check: + formatted_str += f"{''.join(string_[idx:]).lower()}" + break + + # The last word in the string; uppercase followed by multiple lowercase chars + elif (remaining_str := "".join(string_[idx:])).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 diff --git a/scripts/resource/tests/test_class_generator.py b/scripts/resource/tests/test_class_generator.py index 40b749e1b1..a0aee2f085 100644 --- a/scripts/resource/tests/test_class_generator.py +++ b/scripts/resource/tests/test_class_generator.py @@ -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): From 8a3900a60234d8288184b027520775a2552c147d Mon Sep 17 00:00:00 2001 From: rnetser Date: Tue, 30 Jul 2024 13:43:08 +0300 Subject: [PATCH 2/3] add exmaple --- scripts/resource/class_generator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/resource/class_generator.py b/scripts/resource/class_generator.py index fc42e8a10f..660d7fd3c9 100644 --- a/scripts/resource/class_generator.py +++ b/scripts/resource/class_generator.py @@ -174,12 +174,14 @@ def convert_camel_case_to_snake_case(string_: str) -> str: formatted_str += f"_{char.lower()}" last_capital_char = True - # The 2 letters in the string; uppercase char followed by lowercase char + # The 2 letters in the string; uppercase char followed by lowercase char. + # Example: `clusterIPs`, handle `Ps` at this point elif idx + 1 == str_len_for_idx_check: formatted_str += f"{''.join(string_[idx:]).lower()}" break # The last word in the string; uppercase followed by multiple lowercase chars + # Example: `dataVolumeTTLSeconds`, handle `Seconds` at this point elif (remaining_str := "".join(string_[idx:])).istitle(): formatted_str += f"_{remaining_str.lower()}" break From a309a3594d8dccde73a6be63c839baa86b17d0cb Mon Sep 17 00:00:00 2001 From: rnetser Date: Tue, 30 Jul 2024 13:55:41 +0300 Subject: [PATCH 3/3] fix code --- scripts/resource/class_generator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/resource/class_generator.py b/scripts/resource/class_generator.py index 660d7fd3c9..9f1861ca9d 100644 --- a/scripts/resource/class_generator.py +++ b/scripts/resource/class_generator.py @@ -173,16 +173,18 @@ 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 - elif idx + 1 == str_len_for_idx_check: - formatted_str += f"{''.join(string_[idx:]).lower()}" + 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 := "".join(string_[idx:])).istitle(): + elif remaining_str.istitle(): formatted_str += f"_{remaining_str.lower()}" break