Skip to content

Commit

Permalink
Provide a path conversion from snake_case to dashed-case
Browse files Browse the repository at this point in the history
  • Loading branch information
dprogm committed Aug 6, 2023
1 parent 7064b28 commit 4f282ad
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4f282ad

Please sign in to comment.