From c272bf635b3872beaaa5e92dcaabd52453f0ecb1 Mon Sep 17 00:00:00 2001 From: Michael Beardsworth Date: Tue, 25 Mar 2025 15:11:45 -0700 Subject: [PATCH] Use snake_case for generated proto files. From the Protocol Buffers style guide: "Files should be named lower_snake_case.proto." (https://protobuf.dev/programming-guides/style/) --- rosidl_adapter_proto/bin/rosidl_adapter_proto | 5 +++-- .../cmake/rosidl_adapt_proto_interfaces.cmake | 8 +++++--- rosidl_adapter_proto/resource/idl.proto.em | 2 +- rosidl_adapter_proto/rosidl_adapter_proto/__init__.py | 11 ++++++++--- .../test/test_adapter_proto_message.py | 6 +++--- .../rosidl_typesupport_protobuf/__init__.py | 2 +- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/rosidl_adapter_proto/bin/rosidl_adapter_proto b/rosidl_adapter_proto/bin/rosidl_adapter_proto index bf49b23..1a9eaa3 100644 --- a/rosidl_adapter_proto/bin/rosidl_adapter_proto +++ b/rosidl_adapter_proto/bin/rosidl_adapter_proto @@ -23,7 +23,7 @@ import sys import pathlib import os -from rosidl_cmake import read_generator_arguments +from rosidl_cmake import read_generator_arguments, convert_camel_case_to_lower_case_underscore from rosidl_adapter_proto import generate_proto from rosidl_adapter_proto import compile_proto @@ -65,10 +65,11 @@ def main(argv=sys.argv[1:]): assert len(idl_parts) == 2 idl_rel_path = pathlib.Path(idl_parts[1]) idl_stem = idl_rel_path.stem + proto_stem = convert_camel_case_to_lower_case_underscore(idl_stem) generated_file = os.path.join( generator_args['output_dir'], str(idl_rel_path.parent), - idl_stem + ".proto" + proto_stem + ".proto" ) proto_files.append(str(pathlib.Path(generated_file).resolve())) diff --git a/rosidl_adapter_proto/cmake/rosidl_adapt_proto_interfaces.cmake b/rosidl_adapter_proto/cmake/rosidl_adapt_proto_interfaces.cmake index 2f187e6..b5ae827 100644 --- a/rosidl_adapter_proto/cmake/rosidl_adapt_proto_interfaces.cmake +++ b/rosidl_adapter_proto/cmake/rosidl_adapt_proto_interfaces.cmake @@ -62,9 +62,11 @@ foreach(_abs_idl_file ${rosidl_generate_interfaces_ABS_IDL_FILES}) get_filename_component(_parent_folder "${_abs_idl_file}" DIRECTORY) get_filename_component(_parent_folder "${_parent_folder}" NAME) get_filename_component(_idl_name "${_abs_idl_file}" NAME_WE) - list(APPEND rosidl_adapter_proto_GENERATED_CPP "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name}.pb.cc") - list(APPEND rosidl_adapter_proto_GENERATED_H "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name}.pb.h") - list(APPEND rosidl_adapter_proto_GENERATED_PROTO "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name}.proto") + string_camel_case_to_lower_case_underscore("${_idl_name}" _idl_name_lower) + + list(APPEND rosidl_adapter_proto_GENERATED_CPP "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name_lower}.pb.cc") + list(APPEND rosidl_adapter_proto_GENERATED_H "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name_lower}.pb.h") + list(APPEND rosidl_adapter_proto_GENERATED_PROTO "${rosidl_adapter_proto_OUTPUT_DIR}/${_parent_folder}/${_idl_name_lower}.proto") endforeach() add_custom_command( diff --git a/rosidl_adapter_proto/resource/idl.proto.em b/rosidl_adapter_proto/resource/idl.proto.em index 7537cbe..57e8535 100644 --- a/rosidl_adapter_proto/resource/idl.proto.em +++ b/rosidl_adapter_proto/resource/idl.proto.em @@ -124,4 +124,4 @@ for action in content.get_elements_of_type(Action): interface_path=interface_path, action=action, ) -}@ \ No newline at end of file +}@ diff --git a/rosidl_adapter_proto/rosidl_adapter_proto/__init__.py b/rosidl_adapter_proto/rosidl_adapter_proto/__init__.py index a76db58..ae013ab 100644 --- a/rosidl_adapter_proto/rosidl_adapter_proto/__init__.py +++ b/rosidl_adapter_proto/rosidl_adapter_proto/__init__.py @@ -19,7 +19,7 @@ import subprocess import zlib -from rosidl_cmake import generate_files +from rosidl_cmake import generate_files, convert_camel_case_to_lower_case_underscore import rosidl_parser.definition as rosidl # A postfix for the protobuf package name / the c++ namespace @@ -83,7 +83,7 @@ def compute_proto_field_number(variable_name): def to_proto_import(namespaced_type): assert isinstance(namespaced_type, rosidl.NamespacedType) - return '/'.join(namespaced_type.namespaces + [namespaced_type.name]) + '.proto' + return '/'.join(namespaced_type.namespaces + [convert_camel_case_to_lower_case_underscore(namespaced_type.name)]) + '.proto' def collect_proto_imports(rosidl_message): @@ -108,7 +108,12 @@ def generate_proto(generator_arguments_file): mapping = { 'idl.proto.em': '%s.proto', } - generate_files(generator_arguments_file, mapping, keep_case=True) + generate_files( + generator_arguments_file, + mapping, + # Don't keep case - we want snake case + keep_case=False + ) return 0 diff --git a/rosidl_adapter_proto/test/test_adapter_proto_message.py b/rosidl_adapter_proto/test/test_adapter_proto_message.py index dca1c54..b5540d1 100644 --- a/rosidl_adapter_proto/test/test_adapter_proto_message.py +++ b/rosidl_adapter_proto/test/test_adapter_proto_message.py @@ -88,7 +88,7 @@ def test_message_proto_generated(message_idl_file): proto_file_name = IdlLocator( pathlib.Path(__file__).parent, - pathlib.Path('msg') / 'BoolTest.proto') + pathlib.Path('msg') / 'bool_test.proto') assert search_word(proto_file_name.get_absolute_path(), member.name) is True assert search_word(proto_file_name.get_absolute_path(), str(field_number)) is True @@ -150,7 +150,7 @@ def test_to_proto_import(message_idl_file): messages = message_idl_file.content.get_elements_of_type(Message) namespace_type = messages[0].structure.namespaced_type proto_import = to_proto_import(namespace_type) - assert proto_import == 'rosidl_adapter_proto/Bool.proto' + assert proto_import == 'rosidl_adapter_proto/bool.proto' def test_to_proto_import_invalid_argument(): @@ -164,7 +164,7 @@ def test_collect_proto_import(message_idl_file): for message in messages: proto_import_set.update(collect_proto_imports(message)) for proto_file in proto_import_set: - assert proto_file == 'rosidl_adapter_proto/Bool.proto' + assert proto_file == 'rosidl_adapter_proto/bool.proto' def test_collect_proto_import_invalid_argument(): diff --git a/rosidl_typesupport_protobuf/rosidl_typesupport_protobuf/__init__.py b/rosidl_typesupport_protobuf/rosidl_typesupport_protobuf/__init__.py index c3e690d..e4d4f95 100644 --- a/rosidl_typesupport_protobuf/rosidl_typesupport_protobuf/__init__.py +++ b/rosidl_typesupport_protobuf/rosidl_typesupport_protobuf/__init__.py @@ -80,7 +80,7 @@ def ros_message_functions_header_c_from_namespace(namespace, name): def protobuf_message_header(package_name, interface_path): include_parts = [package_name] + list(interface_path.parents[0].parts) - include_prefix = interface_path.stem + include_prefix = convert_camel_case_to_lower_case_underscore(interface_path.stem) return '/'.join(include_parts + [include_prefix + '.pb.h'])