forked from thriftrw/thriftrw-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.js
467 lines (405 loc) · 15.8 KB
/
struct.js
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* eslint max-len:[0, 120] */
/* eslint max-statements:[0, 99] */
/* eslint complexity:[0, 16] */
'use strict';
var assert = require('assert');
var bufrw = require('bufrw');
var RW = require('./rw');
var TYPE = require('./TYPE');
var NAMES = require('./names');
var errors = require('./errors');
var skipType = require('./skip').skipType;
var util = require('util');
var ThriftUnrecognizedException = require('./unrecognized-exception')
.ThriftUnrecognizedException;
var readType = require('./read').readType;
function ThriftField(def, struct) {
assert(def.isResult || def.id.value > 0,
'field identifier must be greater than 0' +
' for ' + JSON.stringify(def.name) +
' on ' + JSON.stringify(struct.name) +
' at ' + def.id.line + ':' + def.id.column
);
this.id = def.id.value;
this.name = def.name;
this.required = def.required;
this.optional = def.optional;
this.valueDefinition = def.valueType;
this.valueType = null;
this.defaultValueDefinition = def.defaultValue;
this.defaultValue = null;
this.constructDefaultValue = null;
this.annotations = def.annotations;
}
ThriftField.prototype.link = function link(model) {
this.valueType = model.resolve(this.valueDefinition);
assert(this.valueType, 'value type was defined, as returned by resolve');
};
ThriftField.prototype.linkValue = function linkValue(model) {
this.defaultValue = model.resolveValue(this.defaultValueDefinition);
};
function ThriftStruct(options) {
options = options || {};
this.name = null;
// Strict mode is on by default. Because we have strict opinions about Thrift.
this.strict = options.strict !== undefined ? options.strict : true;
// TODO bring in serviceName
this.fields = [];
this.fieldNames = [];
this.fieldsById = {};
this.fieldsByName = {};
this.isArgument = null;
this.isResult = null;
this.isException = options.isException || false;
this.Constructor = null;
this.surface = null;
this.rw = new this.RW(this);
this.thrift = null;
this.linked = false;
this.annotations = null;
}
ThriftStruct.prototype.name = 'struct';
ThriftStruct.prototype.typeid = TYPE.STRUCT;
ThriftStruct.prototype.RW = StructRW;
ThriftStruct.prototype.isUnion = false;
ThriftStruct.prototype.models = 'type';
ThriftStruct.prototype.toBuffer = function toBuffer(struct) {
return bufrw.toBuffer(this.rw, struct);
};
ThriftStruct.prototype.toBufferResult = function toBufferResult(struct) {
return bufrw.toBufferResult(this.rw, struct);
};
ThriftStruct.prototype.fromBuffer = function fromBuffer(buffer, offset) {
return bufrw.fromBuffer(this.rw, buffer, offset);
};
ThriftStruct.prototype.fromBufferResult = function fromBufferResult(buffer) {
return bufrw.fromBufferResult(this.rw, buffer);
};
ThriftStruct.prototype.compile = function compile(def, thrift) {
// Struct names must be valid JavaScript. If the Thrift name is not valid
// in JavaScript, it can be overridden with the js.name annotation.
this.name = def.annotations && def.annotations['js.name'] || def.id.name;
this.fullName = def.id.as || this.name;
this.isArgument = def.isArgument || false;
this.isResult = def.isResult || false;
this.thrift = thrift;
var fields = def.fields;
for (var index = 0; index < fields.length; index++) {
var fieldDef = fields[index];
var field = new ThriftField(fieldDef, this);
// Field names must be valid JavaScript. If the Thrift name is not
// valid in JavaScript, it can be overridden with the js.name
// annotation.
field.name = field.annotations && field.annotations['js.name'] || field.name;
this.fieldsById[field.id] = field;
this.fieldsByName[field.name] = field;
this.fieldNames[index] = field.name;
this.fields.push(field);
}
this.annotations = def.annotations;
};
ThriftStruct.prototype.link = function link(model) {
if (this.linked) {
return this;
}
this.linked = true;
var index;
// Link default values first since they're used by the constructor
for (index = 0; index < this.fields.length; index++) {
var field = this.fields[index];
field.linkValue(model);
// Evidently with Apache Thrift, arguments are always optional,
// regardless of how they are marked.
// They are optional in Go by virtue of defaulting to the zero value
// for their type, and it is not possible to distinguish a missing
// field from the zero value.
if (this.isArgument) {
if (this.thrift.allowOptionalArguments) {
// Once this flag is enabled, all ThriftRW language
// implementations agree that fields are optional unless marked
// required. If they are marked required, that contract is
// respected for both inbound and outbound messages.
if (!field.required && !field.optional && field.defaultValue === undefined) {
field.optional = true;
}
} else if (field.optional) {
// Until version 3.4.3, when we introduced the
// allowOptionalArguments opt-in, all arguments were always
// required. RPC handlers were written to depend on all
// argument fields being implicitly required.
assert.ok(false, 'no field of an argument struct may be marked ' +
'optional including ' + field.name + ' of ' + this.name + '; ' +
'consider new Thrift({allowOptionalArguments: true}).');
} else {
field.required = true;
}
}
// Validate field
if (this.strict) {
assert(
field.required || field.optional ||
field.defaultValue !== null && field.defaultValue !== undefined ||
this.isArgument || this.isResult || this.isUnion,
'every field must be marked optional, required, or have a default value on ' +
this.name + ' including "' + field.name + '" in strict mode'
);
}
}
this.Constructor = this.createConstructor(this.name, this.fields);
this.Constructor.rw = this.rw;
this.Constructor.fromBuffer = this.fromBuffer;
this.Constructor.fromBufferResult = this.fromBufferResult;
this.Constructor.toBuffer = this.toBuffer;
this.Constructor.toBufferResult = this.toBufferResult;
this.surface = this.Constructor;
// Link field types later since they may depend on the constructor existing
// first.
for (index = 0; index < this.fields.length; index++) {
this.fields[index].link(model);
}
if (this.isUnion) {
model.unions[this.name] = this.Constructor;
} else if (this.isException) {
model.exceptions[this.name] = this.Constructor;
} else {
model.structs[this.name] = this.Constructor;
}
// Alias if first character is not lower-case
if (!/^[a-z]/.test(this.name)) {
model[this.name] = this.Constructor;
}
return this;
};
ThriftStruct.prototype.validateStruct = function validateStruct(struct) {
// Validate required fields
for (var index = 0; index < this.fields.length; index++) {
var field = this.fields[index];
if (!field.required || field.defaultValue != null) {
continue;
}
var value = struct && struct[field.name];
var available = value !== null && value !== undefined;
if (!available) {
return errors.FieldRequiredError({
name: field.name,
id: field.id,
structName: this.name
});
}
}
return null;
};
// The following methods have alternate implementations for Exception and Union.
ThriftStruct.prototype.createConstructor = function createConstructor(name, fields) {
var source;
source = '(function thriftrw_' + name + '(options) {\n';
for (var index = 0; index < fields.length; index++) {
var field = fields[index];
source += ' this.' + field.name + ' = null;\n';
source += ' if (options && typeof options.' + field.name + ' !== "undefined") ' +
'{ this.' + field.name + ' = options.' + field.name + '; }\n';
if (field.defaultValue !== null) {
source += ' else { this.' + field.name +
' = ' + JSON.stringify(field.defaultValue) + '; }\n';
}
}
source += '})\n';
// eval is an operator that captures the lexical scope of the calling
// function and deoptimizes the lexical scope.
// By using eval in an expression context, it loses this second-class
// capability and becomes a first-class function.
// (0, eval) is one way to use eval in an expression context.
return (0, eval)(source);
};
ThriftStruct.prototype.create = function create() {
return new this.Constructor();
};
ThriftStruct.prototype.set = function set(struct, key, value) {
struct[key] = value;
};
ThriftStruct.prototype.finalize = function finalize(struct) {
return struct;
};
function StructRW(model) {
assert(model, 'model required');
this.model = model;
RW.call(this);
}
util.inherits(StructRW, RW);
StructRW.prototype.poolByteLength = function poolByteLength(destResult, struct) {
var length = 1; // stop:1
var result;
for (var index = 0; index < this.model.fields.length; index++) {
var field = this.model.fields[index];
var value = struct && struct[field.name];
var available = value !== null && value !== undefined;
if (!available && field.required) {
return destResult.reset(errors.FieldRequiredError({
name: field.name,
id: field.id,
structName: this.model.name,
what: struct
}));
}
if (!available) {
continue;
}
// TODO maybe suppress defaultValue on the wire
// typeid:1
// field.id:2
length += 3;
result = field.valueType.rw.poolByteLength(destResult, value);
// istanbul ignore if
if (result.err) {
return result;
}
length += result.length;
}
return destResult.reset(null, length);
};
StructRW.prototype.poolWriteInto = function poolWriteInto(destResult, struct, buffer, offset) {
var result;
for (var index = 0; index < this.model.fields.length; index++) {
var field = this.model.fields[index];
var value = struct && struct[field.name];
var available = value !== null && value !== undefined;
if (!available && field.required) {
return destResult.reset(errors.FieldRequiredError({
name: field.name,
id: field.id,
structName: this.model.name,
what: struct
}));
}
if (!available) {
continue;
}
// TODO maybe suppress defaultValue on the wire
result = bufrw.Int8.poolWriteInto(destResult, field.valueType.typeid, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
result = bufrw.Int16BE.poolWriteInto(destResult, field.id, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
result = field.valueType.rw.poolWriteInto(destResult, value, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
}
result = bufrw.Int8.poolWriteInto(destResult, TYPE.STOP, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
return destResult.reset(null, offset);
};
StructRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) {
var struct = this.model.create();
var result;
for (;;) {
result = bufrw.Int8.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
var typeid = result.value;
if (typeid === TYPE.STOP) {
break;
}
result = bufrw.Int16BE.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
var id = result.value;
// keep unrecognized files from the future if it could be an
// unrecognized exception.
if (!this.model.fieldsById[id] && this.model.isResult) {
result = readType(destResult, buffer, offset, typeid);
// result = skipType(buffer, offset, typeid);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
this.model.set(
struct,
'failure',
new ThriftUnrecognizedException(result.value)
);
continue;
}
// skip unrecognized fields from THE FUTURE
if (!this.model.fieldsById[id]) {
result = skipType(destResult, buffer, offset, typeid);
// istanbul ignore if
if (result.err) {
return result;
}
offset = result.offset;
continue;
}
var field = this.model.fieldsById[id];
if (
field.valueType.typeid !== typeid &&
field.valueType.altTypeid !== typeid // deprecated, see set.js
) {
return destResult.reset(errors.UnexpectedFieldValueTypeidError({
fieldId: id,
fieldName: field.name,
structName: this.model.name,
typeid: typeid,
typeName: NAMES[typeid],
expectedTypeid: field.valueType.typeid,
expectedTypeName: NAMES[field.valueType.typeid]
}), offset);
}
result = field.valueType.rw.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (result.err) {
return destResult.reset(result.err, offset);
}
offset = result.offset;
// TODO promote return error of set to a ReadResult error
this.model.set(struct, field.name, result.value);
}
// Validate required fields
var err = this.model.validateStruct(struct);
if (err) {
return destResult.reset(err, offset);
}
return destResult.reset(null, offset, this.model.finalize(struct));
};
module.exports.ThriftField = ThriftField;
module.exports.ThriftStruct = ThriftStruct;
module.exports.StructRW = StructRW;