From 3f13e0c3b03e0d6df9d4dbbf1875719f44a673b8 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Krauch Date: Wed, 24 Apr 2024 15:13:18 -0300 Subject: [PATCH] ROS2: Normalize `byte` as `uint8` instead of `int8` (#49) ### Changelog ### Description In [ROS2](https://docs.ros.org/en/iron/Concepts/Basic/About-Interfaces.html#field-types), `byte` fields should be treated as `uint8` types: > | Type name | C++ | Python | DDS type | > |-----------|---------|-----------------|----------| > | byte | uint8_t | builtins.bytes* | octet | Prior to this change we were treating `byte` fields as `int8` which causes errors when parsing a byte field with a default value (e.g. `255`) which is out of the range of a int8. --- src/buildRos2Type.ts | 2 +- src/parse.ros2.test.ts | 34 ++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/buildRos2Type.ts b/src/buildRos2Type.ts index 0c0c08f..e7d971b 100644 --- a/src/buildRos2Type.ts +++ b/src/buildRos2Type.ts @@ -200,7 +200,7 @@ function normalizeType(type: string): string { case "char": return "uint8"; case "byte": - return "int8"; + return "uint8"; case "builtin_interfaces/Time": case "builtin_interfaces/msg/Time": return "time"; diff --git a/src/parse.ros2.test.ts b/src/parse.ros2.test.ts index 99b43e7..c186577 100644 --- a/src/parse.ros2.test.ts +++ b/src/parse.ros2.test.ts @@ -187,7 +187,7 @@ describe("parseMessageDefinition", () => { isArray: false, isComplex: false, name: "y", - type: "int8", + type: "uint8", }, ], name: undefined, @@ -803,35 +803,35 @@ uint32 line`; { definitions: [ { - type: "int8", + type: "uint8", name: "DEBUG", isConstant: true, value: 10, valueText: "10", }, { - type: "int8", + type: "uint8", name: "INFO", isConstant: true, value: 20, valueText: "20", }, { - type: "int8", + type: "uint8", name: "WARN", isConstant: true, value: 30, valueText: "30", }, { - type: "int8", + type: "uint8", name: "ERROR", isConstant: true, value: 40, valueText: "40", }, { - type: "int8", + type: "uint8", name: "FATAL", isConstant: true, value: 50, @@ -1176,4 +1176,26 @@ string<=10[<=5] up_to_five_strings_up_to_ten_characters_each }, ]); }); + + it("handles bounded byte field with default value", () => { + // Can be found in /opt/ros//share/test_msgs/msg/BoundedSequences.msg + const messageDefinition = ` + byte[<=3] byte_values_default [0, 1, 255] + `; + const types = parse(messageDefinition, { ros2: true }); + expect(types).toEqual([ + { + definitions: [ + { + type: "uint8", + isArray: true, + name: "byte_values_default", + isComplex: false, + defaultValue: [0, 1, 255], + arrayUpperBound: 3, + }, + ], + }, + ]); + }); });