src/utils/messageGeneration/messages.js
function buildMessageClass(msgSpec) {

  function Message(values) {
    if (!(this instanceof Message)) {
      return new Message(values);
    }

    var that = this;

    if (msgSpec.fields) {
      msgSpec.fields.forEach(function(field) {
        if (!field.isBuiltin) {
          // sub-message class
          // is it an array?
          if (values && typeof values[field.name] != "undefined") {
            // values provided
            if (field.isArray) {
              that[field.name] = values[field.name].map(function(value) {
                  return new (getMessageFromRegistry(field.baseType, 'msg'))(value);
                });
            } else {
              that[field.name] =
                new (getMessageFromRegistry(field.baseType, 'msg'))(values[field.name]);
            }
          } else {
            // use defaults
            if (field.isArray) {
              // it's an array
              const length = field.arrayLen || 0;
              that[field.name] = new Array(length).fill(new (getMessageFromRegistry(field.baseType, 'msg'))());
            } else {
              that[field.name] = new (getMessageFromRegistry(field.baseType, 'msg'))();
            }
          }
        } else {
          // simple type
          that[field.name] =
            (values && typeof values[field.name] != "undefined") ?
             values[field.name] :
             (field.value || fieldsUtil.getDefaultValue(field.type));
        }
      });
    }
  };

  Message.messageType = msgSpec.getFullMessageName();
  // TODO: bring these back?
  // Message.packageName = details.packageName;
  // Message.messageName = Message.prototype.messageName = details.messageName;
  // Message.md5         = Message.prototype.md5         = details.md5;
  const md5Sum = msgSpec.getMd5sum();
  Message.md5sum = function() {
    return md5Sum;
  };
  Message.Constants = (() => {
      const ret = {};
      msgSpec.constants.forEach((constant) => {
        ret[constant.name.toUpperCase()] = constant.value;
      });
    return ret;
  })();
  Message.fields = msgSpec.fields;
  Message.serialize = function(obj, buffer, offset) {
      serializeInnerMessage(msgSpec, obj, buffer, offset);
    };
  Message.deserialize = function(buffer) {
    var message = new Message();

    message = deserializeInnerMessage(msgSpec, message, buffer, [0]);

    return message;
  };
  Message.getMessageSize = function(msg) { return fieldsUtil.getMessageSize(msg, msgSpec); };

  const fullMsgDefinition = msgSpec.computeFullText();
  Message.messageDefinition = function() { return fullMsgDefinition; };
  Message.datatype = function() { return msgSpec.getFullMessageName(); };

  return Message;
}
function buildMessageClass(msgSpec) {

  function Message(values) {
    if (!(this instanceof Message)) {
      return new Message(values);
    }

    var that = this;

    if (msgSpec.fields) {
      msgSpec.fields.forEach(function(field) {
        if (!field.isBuiltin) {
          // sub-message class
          // is it an array?
          if (values && typeof values[field.name] != "undefined") {
            // values provided
            if (field.isArray) {
              that[field.name] = values[field.name].map(function(value) {
                  return new (getMessageFromRegistry(field.baseType, 'msg'))(value);
                });
            } else {
              that[field.name] =
                new (getMessageFromRegistry(field.baseType, 'msg'))(values[field.name]);
            }
          } else {
            // use defaults
            if (field.isArray) {
              // it's an array
              const length = field.arrayLen || 0;
              that[field.name] = new Array(length).fill(new (getMessageFromRegistry(field.baseType, 'msg'))());
            } else {
              that[field.name] = new (getMessageFromRegistry(field.baseType, 'msg'))();
            }
          }
        } else {
          // simple type
          that[field.name] =
            (values && typeof values[field.name] != "undefined") ?
             values[field.name] :
             (field.value || fieldsUtil.getDefaultValue(field.type));
        }
      });
    }
  };

  Message.messageType = msgSpec.getFullMessageName();
  // TODO: bring these back?
  // Message.packageName = details.packageName;
  // Message.messageName = Message.prototype.messageName = details.messageName;
  // Message.md5         = Message.prototype.md5         = details.md5;
  const md5Sum = msgSpec.getMd5sum();
  Message.md5sum = function() {
    return md5Sum;
  };
  Message.Constants = (() => {
      const ret = {};
      msgSpec.constants.forEach((constant) => {
        ret[constant.name.toUpperCase()] = constant.value;
      });
    return ret;
  })();
  Message.fields = msgSpec.fields;
  Message.serialize = function(obj, buffer, offset) {
      serializeInnerMessage(msgSpec, obj, buffer, offset);
    };
  Message.deserialize = function(buffer) {
    var message = new Message();

    message = deserializeInnerMessage(msgSpec, message, buffer, [0]);

    return message;
  };
  Message.getMessageSize = function(msg) { return fieldsUtil.getMessageSize(msg, msgSpec); };

  const fullMsgDefinition = msgSpec.computeFullText();
  Message.messageDefinition = function() { return fullMsgDefinition; };
  Message.datatype = function() { return msgSpec.getFullMessageName(); };

  return Message;
}
function buildMessageClass ( msgSpec ) { function Message ( values ) { if ( ! ( this instanceof Message ) ) { return new Message ( values ) ; } var that = this ; if ( msgSpec . fields ) { msgSpec . fields . forEach ( function ( field ) { if ( ! field . isBuiltin ) { // sub-message class // is it an array? if ( values && typeof values [ field . name ] != "undefined" ) { // values provided if ( field . isArray ) { that [ field . name ] = values [ field . name ] . map ( function ( value ) { return new ( getMessageFromRegistry ( field . baseType , 'msg' ) ) ( value ) ; } ) ; } else { that [ field . name ] = new ( getMessageFromRegistry ( field . baseType , 'msg' ) ) ( values [ field . name ] ) ; } } else { // use defaults if ( field . isArray ) { // it's an array const length = field . arrayLen || 0 ; that [ field . name ] = new Array ( length ) . fill ( new ( getMessageFromRegistry ( field . baseType , 'msg' ) ) ( ) ) ; } else { that [ field . name ] = new ( getMessageFromRegistry ( field . baseType , 'msg' ) ) ( ) ; } } } else { // simple type that [ field . name ] = ( values && typeof values [ field . name ] != "undefined" ) ? values [ field . name ] : ( field . value || fieldsUtil . getDefaultValue ( field . type ) ) ; } } ) ; } } ; Message . messageType = msgSpec . getFullMessageName ( ) ; // TODO: bring these back? // Message.packageName = details.packageName; // Message.messageName = Message.prototype.messageName = details.messageName; // Message.md5         = Message.prototype.md5         = details.md5; const md5Sum = msgSpec . getMd5sum ( ) ; Message . md5sum = function ( ) { return md5Sum ; } ; Message . Constants = ( ( ) => { const ret = { } ; msgSpec . constants . forEach ( ( constant ) => { ret [ constant . name . toUpperCase ( ) ] = constant . value ; } ) ; return ret ; } ) ( ) ; Message . fields = msgSpec . fields ; Message . serialize = function ( obj , buffer , offset ) { serializeInnerMessage ( msgSpec , obj , buffer , offset ) ; } ; Message . deserialize = function ( buffer ) { var message = new Message ( ) ; message = deserializeInnerMessage ( msgSpec , message , buffer , [ 0 ] ) ; return message ; } ; Message . getMessageSize = function ( msg ) { return fieldsUtil . getMessageSize ( msg , msgSpec ) ; } ; const fullMsgDefinition = msgSpec . computeFullText ( ) ; Message . messageDefinition = function ( ) { return fullMsgDefinition ; } ; Message . datatype = function ( ) { return msgSpec . getFullMessageName ( ) ; } ; return Message ; }
Construct the class definition for the given message type. The
resulting class holds the data and has the methods required for
use with ROS, incl. serialization, deserialization, and md5sum.
Construct the class definition for the given message type . The resulting class holds the data and has the methods required for use with ROS incl . serialization deserialization and md5sum .