field.js
11.2 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"use strict";
module.exports = Field;
// extends ReflectionObject
var ReflectionObject = require("./object");
((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
var Enum = require("./enum"),
types = require("./types"),
util = require("./util");
var Type; // cyclic
var ruleRe = /^required|optional|repeated$/;
/**
* Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
* @name Field
* @classdesc Reflected message field.
* @extends FieldBase
* @constructor
* @param {string} name Unique name within its namespace
* @param {number} id Unique id within its namespace
* @param {string} type Value type
* @param {string|Object.<string,*>} [rule="optional"] Field rule
* @param {string|Object.<string,*>} [extend] Extended type if different from parent
* @param {Object.<string,*>} [options] Declared options
*/
/**
* Constructs a field from a field descriptor.
* @param {string} name Field name
* @param {IField} json Field descriptor
* @returns {Field} Created field
* @throws {TypeError} If arguments are invalid
*/
Field.fromJSON = function fromJSON(name, json) {
return new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
};
/**
* Not an actual constructor. Use {@link Field} instead.
* @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
* @exports FieldBase
* @extends ReflectionObject
* @constructor
* @param {string} name Unique name within its namespace
* @param {number} id Unique id within its namespace
* @param {string} type Value type
* @param {string|Object.<string,*>} [rule="optional"] Field rule
* @param {string|Object.<string,*>} [extend] Extended type if different from parent
* @param {Object.<string,*>} [options] Declared options
* @param {string} [comment] Comment associated with this field
*/
function Field(name, id, type, rule, extend, options, comment) {
if (util.isObject(rule)) {
comment = extend;
options = rule;
rule = extend = undefined;
} else if (util.isObject(extend)) {
comment = options;
options = extend;
extend = undefined;
}
ReflectionObject.call(this, name, options);
if (!util.isInteger(id) || id < 0)
throw TypeError("id must be a non-negative integer");
if (!util.isString(type))
throw TypeError("type must be a string");
if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
throw TypeError("rule must be a string rule");
if (extend !== undefined && !util.isString(extend))
throw TypeError("extend must be a string");
/**
* Field rule, if any.
* @type {string|undefined}
*/
this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
/**
* Field type.
* @type {string}
*/
this.type = type; // toJSON
/**
* Unique field id.
* @type {number}
*/
this.id = id; // toJSON, marker
/**
* Extended type if different from parent.
* @type {string|undefined}
*/
this.extend = extend || undefined; // toJSON
/**
* Whether this field is required.
* @type {boolean}
*/
this.required = rule === "required";
/**
* Whether this field is optional.
* @type {boolean}
*/
this.optional = !this.required;
/**
* Whether this field is repeated.
* @type {boolean}
*/
this.repeated = rule === "repeated";
/**
* Whether this field is a map or not.
* @type {boolean}
*/
this.map = false;
/**
* Message this field belongs to.
* @type {Type|null}
*/
this.message = null;
/**
* OneOf this field belongs to, if any,
* @type {OneOf|null}
*/
this.partOf = null;
/**
* The field type's default value.
* @type {*}
*/
this.typeDefault = null;
/**
* The field's default value on prototypes.
* @type {*}
*/
this.defaultValue = null;
/**
* Whether this field's value should be treated as a long.
* @type {boolean}
*/
this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
/**
* Whether this field's value is a buffer.
* @type {boolean}
*/
this.bytes = type === "bytes";
/**
* Resolved type if not a basic type.
* @type {Type|Enum|null}
*/
this.resolvedType = null;
/**
* Sister-field within the extended type if a declaring extension field.
* @type {Field|null}
*/
this.extensionField = null;
/**
* Sister-field within the declaring namespace if an extended field.
* @type {Field|null}
*/
this.declaringField = null;
/**
* Internally remembers whether this field is packed.
* @type {boolean|null}
* @private
*/
this._packed = null;
/**
* Comment for this field.
* @type {string|null}
*/
this.comment = comment;
}
/**
* Determines whether this field is packed. Only relevant when repeated and working with proto2.
* @name Field#packed
* @type {boolean}
* @readonly
*/
Object.defineProperty(Field.prototype, "packed", {
get: function() {
// defaults to packed=true if not explicity set to false
if (this._packed === null)
this._packed = this.getOption("packed") !== false;
return this._packed;
}
});
/**
* @override
*/
Field.prototype.setOption = function setOption(name, value, ifNotSet) {
if (name === "packed") // clear cached before setting
this._packed = null;
return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
};
/**
* Field descriptor.
* @interface IField
* @property {string} [rule="optional"] Field rule
* @property {string} type Field type
* @property {number} id Field id
* @property {Object.<string,*>} [options] Field options
*/
/**
* Extension field descriptor.
* @interface IExtensionField
* @extends IField
* @property {string} extend Extended type
*/
/**
* Converts this field to a field descriptor.
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
* @returns {IField} Field descriptor
*/
Field.prototype.toJSON = function toJSON(toJSONOptions) {
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
return util.toObject([
"rule" , this.rule !== "optional" && this.rule || undefined,
"type" , this.type,
"id" , this.id,
"extend" , this.extend,
"options" , this.options,
"comment" , keepComments ? this.comment : undefined
]);
};
/**
* Resolves this field's type references.
* @returns {Field} `this`
* @throws {Error} If any reference cannot be resolved
*/
Field.prototype.resolve = function resolve() {
if (this.resolved)
return this;
if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
if (this.resolvedType instanceof Type)
this.typeDefault = null;
else // instanceof Enum
this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
}
// use explicitly set default value if present
if (this.options && this.options["default"] != null) {
this.typeDefault = this.options["default"];
if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
this.typeDefault = this.resolvedType.values[this.typeDefault];
}
// remove unnecessary options
if (this.options) {
if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
delete this.options.packed;
if (!Object.keys(this.options).length)
this.options = undefined;
}
// convert to internal data type if necesssary
if (this.long) {
this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
/* istanbul ignore else */
if (Object.freeze)
Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
} else if (this.bytes && typeof this.typeDefault === "string") {
var buf;
if (util.base64.test(this.typeDefault))
util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
else
util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
this.typeDefault = buf;
}
// take special care of maps and repeated fields
if (this.map)
this.defaultValue = util.emptyObject;
else if (this.repeated)
this.defaultValue = util.emptyArray;
else
this.defaultValue = this.typeDefault;
// ensure proper value on prototype
if (this.parent instanceof Type)
this.parent.ctor.prototype[this.name] = this.defaultValue;
return ReflectionObject.prototype.resolve.call(this);
};
/**
* Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
* @typedef FieldDecorator
* @type {function}
* @param {Object} prototype Target prototype
* @param {string} fieldName Field name
* @returns {undefined}
*/
/**
* Field decorator (TypeScript).
* @name Field.d
* @function
* @param {number} fieldId Field id
* @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
* @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
* @param {T} [defaultValue] Default value
* @returns {FieldDecorator} Decorator function
* @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
*/
Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
// submessage: decorate the submessage and use its name as the type
if (typeof fieldType === "function")
fieldType = util.decorateType(fieldType).name;
// enum reference: create a reflected copy of the enum and keep reuseing it
else if (fieldType && typeof fieldType === "object")
fieldType = util.decorateEnum(fieldType).name;
return function fieldDecorator(prototype, fieldName) {
util.decorateType(prototype.constructor)
.add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
};
};
/**
* Field decorator (TypeScript).
* @name Field.d
* @function
* @param {number} fieldId Field id
* @param {Constructor<T>|string} fieldType Field type
* @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
* @returns {FieldDecorator} Decorator function
* @template T extends Message<T>
* @variation 2
*/
// like Field.d but without a default value
// Sets up cyclic dependencies (called in index-light)
Field._configure = function configure(Type_) {
Type = Type_;
};