1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Protocol Buffer Field Descriptor class.
*/
goog.provide('goog.proto2.FieldDescriptor');
goog.require('goog.asserts');
goog.require('goog.string');
/**
* A class which describes a field in a Protocol Buffer 2 Message.
*
* @param {function(new:goog.proto2.Message)} messageType Constructor for the
* message class to which the field described by this class belongs.
* @param {number|string} tag The field's tag index.
* @param {Object} metadata The metadata about this field that will be used
* to construct this descriptor.
*
* @constructor
* @final
*/
goog.proto2.FieldDescriptor = function(messageType, tag, metadata) {
/**
* The message type that contains the field that this
* descriptor describes.
* @private {function(new:goog.proto2.Message)}
*/
this.parent_ = messageType;
// Ensure that the tag is numeric.
goog.asserts.assert(goog.string.isNumeric(tag));
/**
* The field's tag number.
* @private {number}
*/
this.tag_ = /** @type {number} */ (tag);
/**
* The field's name.
* @private {string}
*/
this.name_ = metadata.name;
/** @type {goog.proto2.FieldDescriptor.FieldType} */
metadata.fieldType;
/** @type {*} */
metadata.repeated;
/** @type {*} */
metadata.required;
/** @type {*} */
metadata.packed;
/**
* If true, this field is a packed field.
* @private {boolean}
*/
this.isPacked_ = !!metadata.packed;
/**
* If true, this field is a repeating field.
* @private {boolean}
*/
this.isRepeated_ = !!metadata.repeated;
/**
* If true, this field is required.
* @private {boolean}
*/
this.isRequired_ = !!metadata.required;
/**
* The field type of this field.
* @private {goog.proto2.FieldDescriptor.FieldType}
*/
this.fieldType_ = metadata.fieldType;
/**
* If this field is a primitive: The native (ECMAScript) type of this field.
* If an enumeration: The enumeration object.
* If a message or group field: The Message function.
* @private {Function}
*/
this.nativeType_ = metadata.type;
/**
* Is it permissible on deserialization to convert between numbers and
* well-formed strings? Is true for 64-bit integral field types and float and
* double types, false for all other field types.
* @private {boolean}
*/
this.deserializationConversionPermitted_ = false;
switch (this.fieldType_) {
case goog.proto2.FieldDescriptor.FieldType.INT64:
case goog.proto2.FieldDescriptor.FieldType.UINT64:
case goog.proto2.FieldDescriptor.FieldType.FIXED64:
case goog.proto2.FieldDescriptor.FieldType.SFIXED64:
case goog.proto2.FieldDescriptor.FieldType.SINT64:
case goog.proto2.FieldDescriptor.FieldType.FLOAT:
case goog.proto2.FieldDescriptor.FieldType.DOUBLE:
this.deserializationConversionPermitted_ = true;
break;
}
/**
* The default value of this field, if different from the default, default
* value.
* @private {*}
*/
this.defaultValue_ = metadata.defaultValue;
};
/**
* An enumeration defining the possible field types.
* Should be a mirror of that defined in descriptor.h.
*
* @enum {number}
*/
goog.proto2.FieldDescriptor.FieldType = {
DOUBLE: 1,
FLOAT: 2,
INT64: 3,
UINT64: 4,
INT32: 5,
FIXED64: 6,
FIXED32: 7,
BOOL: 8,
STRING: 9,
GROUP: 10,
MESSAGE: 11,
BYTES: 12,
UINT32: 13,
ENUM: 14,
SFIXED32: 15,
SFIXED64: 16,
SINT32: 17,
SINT64: 18
};
/**
* Returns the tag of the field that this descriptor represents.
*
* @return {number} The tag number.
*/
goog.proto2.FieldDescriptor.prototype.getTag = function() {
return this.tag_;
};
/**
* Returns the descriptor describing the message that defined this field.
* @return {!goog.proto2.Descriptor} The descriptor.
*/
goog.proto2.FieldDescriptor.prototype.getContainingType = function() {
// Generated JS proto_library messages have getDescriptor() method which can
// be called with or without an instance.
return this.parent_.prototype.getDescriptor();
};
/**
* Returns the name of the field that this descriptor represents.
* @return {string} The name.
*/
goog.proto2.FieldDescriptor.prototype.getName = function() {
return this.name_;
};
/**
* Returns the default value of this field.
* @return {*} The default value.
*/
goog.proto2.FieldDescriptor.prototype.getDefaultValue = function() {
if (this.defaultValue_ === undefined) {
// Set the default value based on a new instance of the native type.
// This will be (0, false, "") for (number, boolean, string) and will
// be a new instance of a group/message if the field is a message type.
var nativeType = this.nativeType_;
if (nativeType === Boolean) {
this.defaultValue_ = false;
} else if (nativeType === Number) {
this.defaultValue_ = 0;
} else if (nativeType === String) {
if (this.deserializationConversionPermitted_) {
// This field is a 64 bit integer represented as a string.
this.defaultValue_ = '0';
} else {
this.defaultValue_ = '';
}
} else {
return new nativeType;
}
}
return this.defaultValue_;
};
/**
* Returns the field type of the field described by this descriptor.
* @return {goog.proto2.FieldDescriptor.FieldType} The field type.
*/
goog.proto2.FieldDescriptor.prototype.getFieldType = function() {
return this.fieldType_;
};
/**
* Returns the native (i.e. ECMAScript) type of the field described by this
* descriptor.
*
* @return {Object} The native type.
*/
goog.proto2.FieldDescriptor.prototype.getNativeType = function() {
return this.nativeType_;
};
/**
* Returns true if simple conversions between numbers and strings are permitted
* during deserialization for this field.
*
* @return {boolean} Whether conversion is permitted.
*/
goog.proto2.FieldDescriptor.prototype.deserializationConversionPermitted =
function() {
return this.deserializationConversionPermitted_;
};
/**
* Returns the descriptor of the message type of this field. Only valid
* for fields of type GROUP and MESSAGE.
*
* @return {!goog.proto2.Descriptor} The message descriptor.
*/
goog.proto2.FieldDescriptor.prototype.getFieldMessageType = function() {
// Generated JS proto_library messages have getDescriptor() method which can
// be called with or without an instance.
var messageClass =
/** @type {function(new:goog.proto2.Message)} */ (this.nativeType_);
return messageClass.prototype.getDescriptor();
};
/**
* @return {boolean} True if the field stores composite data or repeated
* composite data (message or group).
*/
goog.proto2.FieldDescriptor.prototype.isCompositeType = function() {
return this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.MESSAGE ||
this.fieldType_ == goog.proto2.FieldDescriptor.FieldType.GROUP;
};
/**
* Returns whether the field described by this descriptor is packed.
* @return {boolean} Whether the field is packed.
*/
goog.proto2.FieldDescriptor.prototype.isPacked = function() {
return this.isPacked_;
};
/**
* Returns whether the field described by this descriptor is repeating.
* @return {boolean} Whether the field is repeated.
*/
goog.proto2.FieldDescriptor.prototype.isRepeated = function() {
return this.isRepeated_;
};
/**
* Returns whether the field described by this descriptor is required.
* @return {boolean} Whether the field is required.
*/
goog.proto2.FieldDescriptor.prototype.isRequired = function() {
return this.isRequired_;
};
/**
* Returns whether the field described by this descriptor is optional.
* @return {boolean} Whether the field is optional.
*/
goog.proto2.FieldDescriptor.prototype.isOptional = function() {
return !this.isRepeated_ && !this.isRequired_;
};
|