Path: blob/trunk/third_party/closure/goog/proto2/fielddescriptor.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 Field Descriptor class.16*/1718goog.provide('goog.proto2.FieldDescriptor');1920goog.require('goog.asserts');21goog.require('goog.string');22232425/**26* A class which describes a field in a Protocol Buffer 2 Message.27*28* @param {function(new:goog.proto2.Message)} messageType Constructor for the29* message class to which the field described by this class belongs.30* @param {number|string} tag The field's tag index.31* @param {Object} metadata The metadata about this field that will be used32* to construct this descriptor.33*34* @constructor35* @final36*/37goog.proto2.FieldDescriptor = function(messageType, tag, metadata) {38/**39* The message type that contains the field that this40* descriptor describes.41* @private {function(new:goog.proto2.Message)}42*/43this.parent_ = messageType;4445// Ensure that the tag is numeric.46goog.asserts.assert(goog.string.isNumeric(tag));4748/**49* The field's tag number.50* @private {number}51*/52this.tag_ = /** @type {number} */ (tag);5354/**55* The field's name.56* @private {string}57*/58this.name_ = metadata.name;5960/** @type {goog.proto2.FieldDescriptor.FieldType} */61metadata.fieldType;6263/** @type {*} */64metadata.repeated;6566/** @type {*} */67metadata.required;6869/** @type {*} */70metadata.packed;7172/**73* If true, this field is a packed field.74* @private {boolean}75*/76this.isPacked_ = !!metadata.packed;7778/**79* If true, this field is a repeating field.80* @private {boolean}81*/82this.isRepeated_ = !!metadata.repeated;8384/**85* If true, this field is required.86* @private {boolean}87*/88this.isRequired_ = !!metadata.required;8990/**91* The field type of this field.92* @private {goog.proto2.FieldDescriptor.FieldType}93*/94this.fieldType_ = metadata.fieldType;9596/**97* If this field is a primitive: The native (ECMAScript) type of this field.98* If an enumeration: The enumeration object.99* If a message or group field: The Message function.100* @private {Function}101*/102this.nativeType_ = metadata.type;103104/**105* Is it permissible on deserialization to convert between numbers and106* well-formed strings? Is true for 64-bit integral field types and float and107* double types, false for all other field types.108* @private {boolean}109*/110this.deserializationConversionPermitted_ = false;111112switch (this.fieldType_) {113case goog.proto2.FieldDescriptor.FieldType.INT64:114case goog.proto2.FieldDescriptor.FieldType.UINT64:115case goog.proto2.FieldDescriptor.FieldType.FIXED64:116case goog.proto2.FieldDescriptor.FieldType.SFIXED64:117case goog.proto2.FieldDescriptor.FieldType.SINT64:118case goog.proto2.FieldDescriptor.FieldType.FLOAT:119case goog.proto2.FieldDescriptor.FieldType.DOUBLE:120this.deserializationConversionPermitted_ = true;121break;122}123124/**125* The default value of this field, if different from the default, default126* value.127* @private {*}128*/129this.defaultValue_ = metadata.defaultValue;130};131132133/**134* An enumeration defining the possible field types.135* Should be a mirror of that defined in descriptor.h.136*137* @enum {number}138*/139goog.proto2.FieldDescriptor.FieldType = {140DOUBLE: 1,141FLOAT: 2,142INT64: 3,143UINT64: 4,144INT32: 5,145FIXED64: 6,146FIXED32: 7,147BOOL: 8,148STRING: 9,149GROUP: 10,150MESSAGE: 11,151BYTES: 12,152UINT32: 13,153ENUM: 14,154SFIXED32: 15,155SFIXED64: 16,156SINT32: 17,157SINT64: 18158};159160161/**162* Returns the tag of the field that this descriptor represents.163*164* @return {number} The tag number.165*/166goog.proto2.FieldDescriptor.prototype.getTag = function() {167return this.tag_;168};169170171/**172* Returns the descriptor describing the message that defined this field.173* @return {!goog.proto2.Descriptor} The descriptor.174*/175goog.proto2.FieldDescriptor.prototype.getContainingType = function() {176// Generated JS proto_library messages have getDescriptor() method which can177// be called with or without an instance.178return this.parent_.prototype.getDescriptor();179};180181182/**183* Returns the name of the field that this descriptor represents.184* @return {string} The name.185*/186goog.proto2.FieldDescriptor.prototype.getName = function() {187return this.name_;188};189190191/**192* Returns the default value of this field.193* @return {*} The default value.194*/195goog.proto2.FieldDescriptor.prototype.getDefaultValue = function() {196if (this.defaultValue_ === undefined) {197// Set the default value based on a new instance of the native type.198// This will be (0, false, "") for (number, boolean, string) and will199// be a new instance of a group/message if the field is a message type.200var nativeType = this.nativeType_;201if (nativeType === Boolean) {202this.defaultValue_ = false;203} else if (nativeType === Number) {204this.defaultValue_ = 0;205} else if (nativeType === String) {206if (this.deserializationConversionPermitted_) {207// This field is a 64 bit integer represented as a string.208this.defaultValue_ = '0';209} else {210this.defaultValue_ = '';211}212} else {213return new nativeType;214}215}216217return this.defaultValue_;218};219220221/**222* Returns the field type of the field described by this descriptor.223* @return {goog.proto2.FieldDescriptor.FieldType} The field type.224*/225goog.proto2.FieldDescriptor.prototype.getFieldType = function() {226return this.fieldType_;227};228229230/**231* Returns the native (i.e. ECMAScript) type of the field described by this232* descriptor.233*234* @return {Object} The native type.235*/236goog.proto2.FieldDescriptor.prototype.getNativeType = function() {237return this.nativeType_;238};239240241/**242* Returns true if simple conversions between numbers and strings are permitted243* during deserialization for this field.244*245* @return {boolean} Whether conversion is permitted.246*/247goog.proto2.FieldDescriptor.prototype.deserializationConversionPermitted =248function() {249return this.deserializationConversionPermitted_;250};251252253/**254* Returns the descriptor of the message type of this field. Only valid255* for fields of type GROUP and MESSAGE.256*257* @return {!goog.proto2.Descriptor} The message descriptor.258*/259goog.proto2.FieldDescriptor.prototype.getFieldMessageType = function() {260// Generated JS proto_library messages have getDescriptor() method which can261// be called with or without an instance.262var messageClass =263/** @type {function(new:goog.proto2.Message)} */ (this.nativeType_);264return messageClass.prototype.getDescriptor();265};266267268/**269* @return {boolean} True if the field stores composite data or repeated270* composite data (message or group).271*/272goog.proto2.FieldDescriptor.prototype.isCompositeType = function() {273return this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||274this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.GROUP;275};276277278/**279* Returns whether the field described by this descriptor is packed.280* @return {boolean} Whether the field is packed.281*/282goog.proto2.FieldDescriptor.prototype.isPacked = function() {283return this.isPacked_;284};285286287/**288* Returns whether the field described by this descriptor is repeating.289* @return {boolean} Whether the field is repeated.290*/291goog.proto2.FieldDescriptor.prototype.isRepeated = function() {292return this.isRepeated_;293};294295296/**297* Returns whether the field described by this descriptor is required.298* @return {boolean} Whether the field is required.299*/300goog.proto2.FieldDescriptor.prototype.isRequired = function() {301return this.isRequired_;302};303304305/**306* Returns whether the field described by this descriptor is optional.307* @return {boolean} Whether the field is optional.308*/309goog.proto2.FieldDescriptor.prototype.isOptional = function() {310return !this.isRepeated_ && !this.isRequired_;311};312313314