|
| 1 | +// Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const loader = require('./interface_loader.js'); |
| 18 | +const { toPlainObject } = require('../rosidl_gen/message_translator.js'); |
| 19 | +const { TypeValidationError } = require('./errors.js'); |
| 20 | + |
| 21 | +/** |
| 22 | + * A utility class for inspecting ROS 2 message structure without using loader.loadInterface directly. |
| 23 | + * Provides access to message schema, field names, and default values. |
| 24 | + */ |
| 25 | +class MessageIntrospector { |
| 26 | + #typeClass; |
| 27 | + #typeName; |
| 28 | + #defaultsCache; |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a new MessageIntrospector for a ROS 2 message type. |
| 32 | + * @param {string} typeName - The full message type name (e.g., 'geometry_msgs/msg/Twist') |
| 33 | + * @throws {TypeValidationError} If typeName is not a non-empty string |
| 34 | + * @throws {Error} If the message type cannot be loaded |
| 35 | + */ |
| 36 | + constructor(typeName) { |
| 37 | + if (!typeName || typeof typeName !== 'string') { |
| 38 | + throw new TypeValidationError('typeName', typeName, 'non-empty string', { |
| 39 | + entityType: 'MessageIntrospector', |
| 40 | + }); |
| 41 | + } |
| 42 | + this.#typeName = typeName; |
| 43 | + this.#typeClass = loader.loadInterface(typeName); |
| 44 | + this.#defaultsCache = null; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the full message type name. |
| 49 | + * @returns {string} The message type name (e.g., 'geometry_msgs/msg/Twist') |
| 50 | + */ |
| 51 | + get typeName() { |
| 52 | + return this.#typeName; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the underlying ROS message class. |
| 57 | + * @returns {Function} The message type class constructor |
| 58 | + */ |
| 59 | + get typeClass() { |
| 60 | + return this.#typeClass; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the field names of the message. |
| 65 | + * @returns {string[]} Array of field names |
| 66 | + */ |
| 67 | + get fields() { |
| 68 | + const def = this.#typeClass.ROSMessageDef; |
| 69 | + return def.fields.map((f) => f.name); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the ROSMessageDef schema for the message type. |
| 74 | + * @returns {object} The message definition schema |
| 75 | + */ |
| 76 | + get schema() { |
| 77 | + return this.#typeClass.ROSMessageDef; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the default values for all fields. |
| 82 | + * Creates a new instance of the message and converts it to a plain object. |
| 83 | + * Result is cached for performance. |
| 84 | + * @returns {object} A plain object with all default values |
| 85 | + */ |
| 86 | + get defaults() { |
| 87 | + if (this.#defaultsCache === null) { |
| 88 | + const instance = new this.#typeClass(); |
| 89 | + this.#defaultsCache = toPlainObject(instance); |
| 90 | + } |
| 91 | + return this.#deepClone(this.#defaultsCache); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Deep clone an object. |
| 96 | + * @param {any} obj - Object to clone |
| 97 | + * @returns {any} Cloned object |
| 98 | + * @private |
| 99 | + */ |
| 100 | + #deepClone(obj) { |
| 101 | + if (obj === null || typeof obj !== 'object') { |
| 102 | + return obj; |
| 103 | + } |
| 104 | + |
| 105 | + if (Array.isArray(obj)) { |
| 106 | + return obj.map((item) => this.#deepClone(item)); |
| 107 | + } |
| 108 | + |
| 109 | + if (ArrayBuffer.isView(obj) && !(obj instanceof DataView)) { |
| 110 | + return obj.slice(); |
| 111 | + } |
| 112 | + |
| 113 | + const cloned = {}; |
| 114 | + for (const key in obj) { |
| 115 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 116 | + cloned[key] = this.#deepClone(obj[key]); |
| 117 | + } |
| 118 | + } |
| 119 | + return cloned; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +module.exports = MessageIntrospector; |
0 commit comments