Merge pull request #274 from dprogm/feature/snake_case_path_conversion

Provide a path conversion from snake_case to dashed-case
This commit is contained in:
Adam Liddell 2023-09-07 15:37:05 +01:00 committed by GitHub
commit 3cf9c7ec36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 0 deletions

View File

@ -178,6 +178,46 @@ def php_path(s):
"""
return "/".join([capitalize(c) for c in s.split("/")])
def snake_case(s):
"""
Convert a path string from camelCase to snake_case.
Args:
s (string): The input string to be converted.
Returns:
(string): The converted string.
"""
converted = ""
is_last_lower_case = False
for char in s.elems():
if char.isupper():
if is_last_lower_case:
converted += "_"+char
is_last_lower_case = False
continue
elif char.islower() or char.isdigit():
is_last_lower_case = True
converted += char
continue
is_last_lower_case = False
converted += char
return converted.lower()
def dasherize(s):
"""
Convert a path string from snake_case to dashed-case.
Args:
s (string): The input string to be converted.
Returns:
(string): The converted string.
"""
return snake_case(s).replace("_", "-")
def get_output_filename(src_file, pattern, proto_info):
"""
Build the predicted filename for file generated by the given plugin.
@ -249,6 +289,12 @@ def get_output_filename(src_file, pattern, proto_info):
filename = pattern.replace("{basename|rust_keyword}", rust_keyword(basename))
elif "{protopath|rust_keyword}" in pattern:
filename = pattern.replace("{basename|rust_keyword}", rust_keyword(protopath))
elif "{protopath|dasherize}" in pattern:
filename = pattern.replace("{protopath|dasherize}", "/".join([
# Dasherize only the file name
protopath_partitions[0],
dasherize(protopath_partitions[2]),
]))
else:
filename += basename + pattern