From 6cae62507e471578b8eba6154bec7a80a4f6a909 Mon Sep 17 00:00:00 2001 From: mhidalgo-bdai <144129882+mhidalgo-bdai@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:27:46 -0300 Subject: [PATCH] Preserve empty Protobuf messages upon ROS 2 conversions (#56) Signed-off-by: Michel Hidalgo --- .../output/templates/conversions.py.jinja | 2 +- proto2ros_tests/proto/test.proto | 4 ++++ .../test/generated/AnyCommandOneOfCommands.msg | 2 ++ proto2ros_tests/test/generated/AnyCommandSit.msg | 1 + proto2ros_tests/test/test_proto2ros.py | 16 ++++++++++++++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 proto2ros_tests/test/generated/AnyCommandSit.msg diff --git a/proto2ros/proto2ros/output/templates/conversions.py.jinja b/proto2ros/proto2ros/output/templates/conversions.py.jinja index 5c14038..e51c97b 100644 --- a/proto2ros/proto2ros/output/templates/conversions.py.jinja +++ b/proto2ros/proto2ros/output/templates/conversions.py.jinja @@ -251,7 +251,7 @@ def convert_{{ spec.base_type | string | as_python_identifier }}_message_to_{{ s {%- endif -%} {%- endfor -%} {% else -%}{#- Handle empty message. #} - pass + proto_msg.SetInParent() {%- endif %} diff --git a/proto2ros_tests/proto/test.proto b/proto2ros_tests/proto/test.proto index 72c02f2..0997c48 100644 --- a/proto2ros_tests/proto/test.proto +++ b/proto2ros_tests/proto/test.proto @@ -94,9 +94,13 @@ message AnyCommand { // Jump height. float height = 1; } + // Sit command. + message Sit {} + oneof commands { Walk walk = 1; Jump jump = 2; + Sit sit = 3; } } diff --git a/proto2ros_tests/test/generated/AnyCommandOneOfCommands.msg b/proto2ros_tests/test/generated/AnyCommandOneOfCommands.msg index 7239d50..3a51122 100644 --- a/proto2ros_tests/test/generated/AnyCommandOneOfCommands.msg +++ b/proto2ros_tests/test/generated/AnyCommandOneOfCommands.msg @@ -2,8 +2,10 @@ int8 COMMANDS_NOT_SET=0 int8 COMMANDS_WALK_SET=1 int8 COMMANDS_JUMP_SET=2 +int8 COMMANDS_SIT_SET=3 proto2ros_tests/AnyCommandWalk walk proto2ros_tests/AnyCommandJump jump +proto2ros_tests/AnyCommandSit sit int8 commands_choice # deprecated int8 which diff --git a/proto2ros_tests/test/generated/AnyCommandSit.msg b/proto2ros_tests/test/generated/AnyCommandSit.msg new file mode 100644 index 0000000..fcb5a5a --- /dev/null +++ b/proto2ros_tests/test/generated/AnyCommandSit.msg @@ -0,0 +1 @@ +# Sit command. diff --git a/proto2ros_tests/test/test_proto2ros.py b/proto2ros_tests/test/test_proto2ros.py index 5ad262e..71e2924 100644 --- a/proto2ros_tests/test/test_proto2ros.py +++ b/proto2ros_tests/test/test_proto2ros.py @@ -78,6 +78,7 @@ def test_one_of_messages() -> None: ros_any_command = proto2ros_tests.msg.AnyCommand() convert(proto_any_command, ros_any_command) walk_set = proto2ros_tests.msg.AnyCommandOneOfCommands.COMMANDS_WALK_SET + assert ros_any_command.commands.which == walk_set assert ros_any_command.commands.commands_choice == walk_set assert ros_any_command.commands.walk.distance == proto_any_command.walk.distance assert ros_any_command.commands.walk.speed == proto_any_command.walk.speed @@ -89,6 +90,21 @@ def test_one_of_messages() -> None: assert other_proto_any_command.walk.speed == proto_any_command.walk.speed +def test_one_of_empty_messages() -> None: + proto_any_command = test_pb2.AnyCommand() + proto_any_command.sit.SetInParent() + + ros_any_command = proto2ros_tests.msg.AnyCommand() + convert(proto_any_command, ros_any_command) + sit_set = proto2ros_tests.msg.AnyCommandOneOfCommands.COMMANDS_SIT_SET + assert ros_any_command.commands.which == sit_set + assert ros_any_command.commands.commands_choice == sit_set + + other_proto_any_command = test_pb2.AnyCommand() + convert(ros_any_command, other_proto_any_command) + assert other_proto_any_command.WhichOneof("commands") == "sit" + + def test_messages_with_map_field() -> None: proto_diagnostic = test_pb2.Diagnostic() proto_diagnostic.severity = test_pb2.Diagnostic.Severity.FATAL