智慧水务管理系统 - 精河县供水工程综合管理平台

message.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. module.exports = Message;
  3. var util = require("./util/minimal");
  4. /**
  5. * Constructs a new message instance.
  6. * @classdesc Abstract runtime message.
  7. * @constructor
  8. * @param {Properties<T>} [properties] Properties to set
  9. * @property {Array.<Uint8Array>} [$unknowns] Unknown fields preserved while decoding when enabled
  10. * @template T extends object = object
  11. */
  12. function Message(properties) {
  13. // not used internally
  14. if (properties)
  15. for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
  16. if (properties[keys[i]] != null && keys[i] !== "__proto__")
  17. this[keys[i]] = properties[keys[i]];
  18. }
  19. /**
  20. * Reference to the reflected type.
  21. * @name Message.$type
  22. * @type {Type}
  23. * @readonly
  24. */
  25. /**
  26. * Reference to the reflected type.
  27. * @name Message#$type
  28. * @type {Type}
  29. * @readonly
  30. */
  31. /**
  32. * Creates a new message of this type using the specified properties.
  33. * @param {Object.<string,*>} [properties] Properties to set
  34. * @returns {T} Message instance
  35. * @template T extends Message<T>
  36. * @this Constructor<T>
  37. */
  38. Message.create = function create(properties) {
  39. return this.$type.create(properties);
  40. };
  41. /**
  42. * Encodes a message of this type.
  43. * @param {T|Object.<string,*>} message Message to encode
  44. * @param {Writer} [writer] Writer to use
  45. * @returns {Writer} Writer
  46. * @template T extends Message<T>
  47. * @this Constructor<T>
  48. */
  49. Message.encode = function encode(message, writer) {
  50. return this.$type.encode(message, writer);
  51. };
  52. /**
  53. * Encodes a message of this type preceeded by its length as a varint.
  54. * @param {T|Object.<string,*>} message Message to encode
  55. * @param {Writer} [writer] Writer to use
  56. * @returns {Writer} Writer
  57. * @template T extends Message<T>
  58. * @this Constructor<T>
  59. */
  60. Message.encodeDelimited = function encodeDelimited(message, writer) {
  61. return this.$type.encodeDelimited(message, writer);
  62. };
  63. /**
  64. * Decodes a message of this type.
  65. * @name Message.decode
  66. * @function
  67. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  68. * @returns {T} Decoded message
  69. * @template T extends Message<T>
  70. * @this Constructor<T>
  71. */
  72. Message.decode = function decode(reader) {
  73. return this.$type.decode(reader);
  74. };
  75. /**
  76. * Decodes a message of this type preceeded by its length as a varint.
  77. * @name Message.decodeDelimited
  78. * @function
  79. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  80. * @returns {T} Decoded message
  81. * @template T extends Message<T>
  82. * @this Constructor<T>
  83. */
  84. Message.decodeDelimited = function decodeDelimited(reader) {
  85. return this.$type.decodeDelimited(reader);
  86. };
  87. /**
  88. * Verifies a message of this type.
  89. * @name Message.verify
  90. * @function
  91. * @param {Object.<string,*>} message Plain object to verify
  92. * @returns {string|null} `null` if valid, otherwise the reason why it is not
  93. */
  94. Message.verify = function verify(message) {
  95. return this.$type.verify(message);
  96. };
  97. /**
  98. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  99. * @param {Object.<string,*>} object Plain object
  100. * @returns {T} Message instance
  101. * @template T extends Message<T>
  102. * @this Constructor<T>
  103. */
  104. Message.fromObject = function fromObject(object) {
  105. return this.$type.fromObject(object);
  106. };
  107. /**
  108. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  109. * @param {T} message Message instance
  110. * @param {IConversionOptions} [options] Conversion options
  111. * @returns {Object.<string,*>} Plain object
  112. * @template T extends Message<T>
  113. * @this Constructor<T>
  114. */
  115. Message.toObject = function toObject(message, options) {
  116. return this.$type.toObject(message, options);
  117. };
  118. /**
  119. * Converts this message to JSON.
  120. * @returns {Object.<string,*>} JSON object
  121. */
  122. Message.prototype.toJSON = function toJSON() {
  123. return this.$type.toObject(this, util.toJSONOptions);
  124. };