Path: blob/trunk/third_party/closure/goog/proto2/descriptor.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Protocol Buffer (Message) Descriptor class.16*/1718goog.provide('goog.proto2.Descriptor');19goog.provide('goog.proto2.Metadata');2021goog.require('goog.array');22goog.require('goog.asserts');23goog.require('goog.object');24goog.require('goog.string');252627/**28* @typedef {{name: (string|undefined),29* fullName: (string|undefined),30* containingType: (goog.proto2.Message|undefined)}}31*/32goog.proto2.Metadata;33343536/**37* A class which describes a Protocol Buffer 2 Message.38*39* @param {function(new:goog.proto2.Message)} messageType Constructor for40* the message class that this descriptor describes.41* @param {!goog.proto2.Metadata} metadata The metadata about the message that42* will be used to construct this descriptor.43* @param {Array<!goog.proto2.FieldDescriptor>} fields The fields of the44* message described by this descriptor.45*46* @constructor47* @final48*/49goog.proto2.Descriptor = function(messageType, metadata, fields) {5051/**52* @type {function(new:goog.proto2.Message)}53* @private54*/55this.messageType_ = messageType;5657/**58* @type {?string}59* @private60*/61this.name_ = metadata.name || null;6263/**64* @type {?string}65* @private66*/67this.fullName_ = metadata.fullName || null;6869/**70* @type {goog.proto2.Message|undefined}71* @private72*/73this.containingType_ = metadata.containingType;7475/**76* The fields of the message described by this descriptor.77* @type {!Object<number, !goog.proto2.FieldDescriptor>}78* @private79*/80this.fields_ = {};8182for (var i = 0; i < fields.length; i++) {83var field = fields[i];84this.fields_[field.getTag()] = field;85}86};878889/**90* Returns the name of the message, if any.91*92* @return {?string} The name.93*/94goog.proto2.Descriptor.prototype.getName = function() {95return this.name_;96};979899/**100* Returns the full name of the message, if any.101*102* @return {?string} The name.103*/104goog.proto2.Descriptor.prototype.getFullName = function() {105return this.fullName_;106};107108109/**110* Returns the descriptor of the containing message type or null if none.111*112* @return {goog.proto2.Descriptor} The descriptor.113*/114goog.proto2.Descriptor.prototype.getContainingType = function() {115if (!this.containingType_) {116return null;117}118119return this.containingType_.getDescriptor();120};121122123/**124* Returns the fields in the message described by this descriptor ordered by125* tag.126*127* @return {!Array<!goog.proto2.FieldDescriptor>} The array of field128* descriptors.129*/130goog.proto2.Descriptor.prototype.getFields = function() {131/**132* @param {!goog.proto2.FieldDescriptor} fieldA First field.133* @param {!goog.proto2.FieldDescriptor} fieldB Second field.134* @return {number} Negative if fieldA's tag number is smaller, positive135* if greater, zero if the same.136*/137function tagComparator(fieldA, fieldB) {138return fieldA.getTag() - fieldB.getTag();139}140141var fields = goog.object.getValues(this.fields_);142goog.array.sort(fields, tagComparator);143144return fields;145};146147148/**149* Returns the fields in the message as a key/value map, where the key is150* the tag number of the field. DO NOT MODIFY THE RETURNED OBJECT. We return151* the actual, internal, fields map for performance reasons, and changing the152* map can result in undefined behavior of this library.153*154* @return {!Object<number, !goog.proto2.FieldDescriptor>} The field map.155*/156goog.proto2.Descriptor.prototype.getFieldsMap = function() {157return this.fields_;158};159160161/**162* Returns the field matching the given name, if any. Note that163* this method searches over the *original* name of the field,164* not the camelCase version.165*166* @param {string} name The field name for which to search.167*168* @return {goog.proto2.FieldDescriptor} The field found, if any.169*/170goog.proto2.Descriptor.prototype.findFieldByName = function(name) {171var valueFound = goog.object.findValue(172this.fields_,173function(field, key, obj) { return field.getName() == name; });174175return /** @type {goog.proto2.FieldDescriptor} */ (valueFound) || null;176};177178179/**180* Returns the field matching the given tag number, if any.181*182* @param {number|string} tag The field tag number for which to search.183*184* @return {goog.proto2.FieldDescriptor} The field found, if any.185*/186goog.proto2.Descriptor.prototype.findFieldByTag = function(tag) {187goog.asserts.assert(goog.string.isNumeric(tag));188return this.fields_[parseInt(tag, 10)] || null;189};190191192/**193* Creates an instance of the message type that this descriptor194* describes.195*196* @return {!goog.proto2.Message} The instance of the message.197*/198goog.proto2.Descriptor.prototype.createMessageInstance = function() {199return new this.messageType_;200};201202203