descriptor.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2008 The Closure Library Authors. 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. * @fileoverview Protocol Buffer (Message) Descriptor class.
  16. */
  17. goog.provide('goog.proto2.Descriptor');
  18. goog.provide('goog.proto2.Metadata');
  19. goog.require('goog.array');
  20. goog.require('goog.asserts');
  21. goog.require('goog.object');
  22. goog.require('goog.string');
  23. /**
  24. * @typedef {{name: (string|undefined),
  25. * fullName: (string|undefined),
  26. * containingType: (goog.proto2.Message|undefined)}}
  27. */
  28. goog.proto2.Metadata;
  29. /**
  30. * A class which describes a Protocol Buffer 2 Message.
  31. *
  32. * @param {function(new:goog.proto2.Message)} messageType Constructor for
  33. * the message class that this descriptor describes.
  34. * @param {!goog.proto2.Metadata} metadata The metadata about the message that
  35. * will be used to construct this descriptor.
  36. * @param {Array<!goog.proto2.FieldDescriptor>} fields The fields of the
  37. * message described by this descriptor.
  38. *
  39. * @constructor
  40. * @final
  41. */
  42. goog.proto2.Descriptor = function(messageType, metadata, fields) {
  43. /**
  44. * @type {function(new:goog.proto2.Message)}
  45. * @private
  46. */
  47. this.messageType_ = messageType;
  48. /**
  49. * @type {?string}
  50. * @private
  51. */
  52. this.name_ = metadata.name || null;
  53. /**
  54. * @type {?string}
  55. * @private
  56. */
  57. this.fullName_ = metadata.fullName || null;
  58. /**
  59. * @type {goog.proto2.Message|undefined}
  60. * @private
  61. */
  62. this.containingType_ = metadata.containingType;
  63. /**
  64. * The fields of the message described by this descriptor.
  65. * @type {!Object<number, !goog.proto2.FieldDescriptor>}
  66. * @private
  67. */
  68. this.fields_ = {};
  69. for (var i = 0; i < fields.length; i++) {
  70. var field = fields[i];
  71. this.fields_[field.getTag()] = field;
  72. }
  73. };
  74. /**
  75. * Returns the name of the message, if any.
  76. *
  77. * @return {?string} The name.
  78. */
  79. goog.proto2.Descriptor.prototype.getName = function() {
  80. return this.name_;
  81. };
  82. /**
  83. * Returns the full name of the message, if any.
  84. *
  85. * @return {?string} The name.
  86. */
  87. goog.proto2.Descriptor.prototype.getFullName = function() {
  88. return this.fullName_;
  89. };
  90. /**
  91. * Returns the descriptor of the containing message type or null if none.
  92. *
  93. * @return {goog.proto2.Descriptor} The descriptor.
  94. */
  95. goog.proto2.Descriptor.prototype.getContainingType = function() {
  96. if (!this.containingType_) {
  97. return null;
  98. }
  99. return this.containingType_.getDescriptor();
  100. };
  101. /**
  102. * Returns the fields in the message described by this descriptor ordered by
  103. * tag.
  104. *
  105. * @return {!Array<!goog.proto2.FieldDescriptor>} The array of field
  106. * descriptors.
  107. */
  108. goog.proto2.Descriptor.prototype.getFields = function() {
  109. /**
  110. * @param {!goog.proto2.FieldDescriptor} fieldA First field.
  111. * @param {!goog.proto2.FieldDescriptor} fieldB Second field.
  112. * @return {number} Negative if fieldA's tag number is smaller, positive
  113. * if greater, zero if the same.
  114. */
  115. function tagComparator(fieldA, fieldB) {
  116. return fieldA.getTag() - fieldB.getTag();
  117. }
  118. var fields = goog.object.getValues(this.fields_);
  119. goog.array.sort(fields, tagComparator);
  120. return fields;
  121. };
  122. /**
  123. * Returns the fields in the message as a key/value map, where the key is
  124. * the tag number of the field. DO NOT MODIFY THE RETURNED OBJECT. We return
  125. * the actual, internal, fields map for performance reasons, and changing the
  126. * map can result in undefined behavior of this library.
  127. *
  128. * @return {!Object<number, !goog.proto2.FieldDescriptor>} The field map.
  129. */
  130. goog.proto2.Descriptor.prototype.getFieldsMap = function() {
  131. return this.fields_;
  132. };
  133. /**
  134. * Returns the field matching the given name, if any. Note that
  135. * this method searches over the *original* name of the field,
  136. * not the camelCase version.
  137. *
  138. * @param {string} name The field name for which to search.
  139. *
  140. * @return {goog.proto2.FieldDescriptor} The field found, if any.
  141. */
  142. goog.proto2.Descriptor.prototype.findFieldByName = function(name) {
  143. var valueFound = goog.object.findValue(
  144. this.fields_,
  145. function(field, key, obj) { return field.getName() == name; });
  146. return /** @type {goog.proto2.FieldDescriptor} */ (valueFound) || null;
  147. };
  148. /**
  149. * Returns the field matching the given tag number, if any.
  150. *
  151. * @param {number|string} tag The field tag number for which to search.
  152. *
  153. * @return {goog.proto2.FieldDescriptor} The field found, if any.
  154. */
  155. goog.proto2.Descriptor.prototype.findFieldByTag = function(tag) {
  156. goog.asserts.assert(goog.string.isNumeric(tag));
  157. return this.fields_[parseInt(tag, 10)] || null;
  158. };
  159. /**
  160. * Creates an instance of the message type that this descriptor
  161. * describes.
  162. *
  163. * @return {!goog.proto2.Message} The instance of the message.
  164. */
  165. goog.proto2.Descriptor.prototype.createMessageInstance = function() {
  166. return new this.messageType_;
  167. };