Skip to content

Commit

Permalink
Preserve empty Protobuf messages upon ROS 2 conversions (#56)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
mhidalgo-bdai authored Jan 19, 2024
1 parent 540822c commit 6cae625
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion proto2ros/proto2ros/output/templates/conversions.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}


Expand Down
4 changes: 4 additions & 0 deletions proto2ros_tests/proto/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 2 additions & 0 deletions proto2ros_tests/test/generated/AnyCommandOneOfCommands.msg
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions proto2ros_tests/test/generated/AnyCommandSit.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Sit command.
16 changes: 16 additions & 0 deletions proto2ros_tests/test/test_proto2ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6cae625

Please sign in to comment.