Skip to content

Commit fad205e

Browse files
authored
[ROSIDL] Add toPlainObject() method for array messages (#1047)
This patch adds support for converting array messages to plain objects by introducing a helper function to detect typed arrays and updating relevant tests. Fix: #1046
1 parent f8fd620 commit fad205e

File tree

7 files changed

+18
-21
lines changed

7 files changed

+18
-21
lines changed

rosidl_gen/generator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rosidl-generator",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "Generate JavaScript object from ROS IDL(.msg) files",
55
"main": "index.js",
66
"authors": [

rosidl_gen/templates/message.dot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ class {{=arrayWrapper}} {
824824
get classType() {
825825
return {{=arrayWrapper}};
826826
}
827+
828+
toPlainObject(enableTypedArray) {
829+
return translator.toPlainObject(this, enableTypedArray);
830+
}
827831
}
828832

829833
{{? it.spec.constants != undefined && it.spec.constants.length}}

test/test-array-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ const deepEqual = require('deep-equal');
1919
const rclnodejs = require('../index.js');
2020
const translator = require('../rosidl_gen/message_translator.js');
2121
const arrayGen = require('./array_generator.js');
22-
23-
function isTypedArray(v) {
24-
return ArrayBuffer.isView(v) && !(v instanceof DataView);
25-
}
22+
const { isTypedArray } = require('./utils.js');
2623

2724
describe('rclnodejs message communication', function () {
2825
this.timeout(60 * 1000);

test/test-message-properties-validation.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,12 @@ describe('Rclnodejs message properities validation', function () {
175175
// Assign the property with a plain object.
176176
left[i] = testData.value[i];
177177
if (typeof left[i].toPlainObject === 'function') {
178-
assert.ok(deepEqual(testData.value[i], left[i].toPlainObject()));
179-
} else if (left[i].classType && left[i].classType.isROSArray) {
180-
left[i].data.forEach((value, index) => {
181-
assert.ok(
182-
deepEqual(testData.value[i][index], value.toPlainObject())
183-
);
184-
});
178+
assert.ok(
179+
deepEqual(
180+
testData.value[i],
181+
left[i].toPlainObject(/*enableTypedArray=*/ true)
182+
)
183+
);
185184
} else {
186185
assert.ok(deepEqual(testData.value[i], left[i]));
187186
}

test/test-message-translator-complex.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414

1515
'use strict';
1616

17-
const assert = require('assert');
1817
const rclnodejs = require('../index.js');
1918
const deepEqual = require('deep-equal');
2019

21-
function isTypedArray(v) {
22-
return ArrayBuffer.isView(v) && !(v instanceof DataView);
23-
}
24-
2520
describe('Rclnodejs message translation: complex types', function () {
2621
this.timeout(60 * 1000);
2722

test/test-message-translator-primitive.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
const rclnodejs = require('../index.js');
1818
const deepEqual = require('deep-equal');
1919
const arrayGen = require('./array_generator.js');
20-
21-
function isTypedArray(v) {
22-
return ArrayBuffer.isView(v) && !(v instanceof DataView);
23-
}
20+
const { isTypedArray } = require('./utils.js');
2421

2522
/* eslint-disable camelcase */
2623
/* eslint-disable indent */

test/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,15 @@ function createDelay(millis) {
8585
return new Promise((resolve) => setTimeout(resolve, millis));
8686
}
8787

88+
function isTypedArray(v) {
89+
return ArrayBuffer.isView(v) && !(v instanceof DataView);
90+
}
91+
8892
module.exports = {
8993
assertMember: assertMember,
9094
assertThrowsError: assertThrowsError,
9195
createDelay: createDelay,
9296
getAvailablePath: getAvailablePath,
9397
launchPythonProcess: launchPythonProcess,
98+
isTypedArray: isTypedArray,
9499
};

0 commit comments

Comments
 (0)