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