typia 7.0.0-dev.20240921 → 7.0.0-dev.20240923

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -73,26 +73,31 @@ var ProtobufEncodeProgrammer;
73
73
  ProtobufEncodeProgrammer.decompose = function (props) {
74
74
  var _a;
75
75
  var collection = new MetadataCollection_1.MetadataCollection();
76
- var meta = ProtobufFactory_1.ProtobufFactory.metadata({
76
+ var metadata = ProtobufFactory_1.ProtobufFactory.metadata({
77
77
  method: props.modulo.getText(),
78
78
  checker: props.context.checker,
79
79
  transformer: props.context.transformer,
80
80
  collection: collection,
81
81
  type: props.type,
82
82
  });
83
- var callEncoder = function (writer) { return function (factory) {
83
+ var callEncoder = function (writer, factory) {
84
84
  return StatementFactory_1.StatementFactory.constant(writer, typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("encoder"), undefined, [factory, typescript_1.default.factory.createIdentifier("input")]));
85
- }; };
85
+ };
86
86
  return {
87
87
  functions: {
88
- encoder: StatementFactory_1.StatementFactory.constant(props.importer.useLocal("encoder"), write_encoder(props.context)(props.importer)(collection)(meta)),
88
+ encoder: StatementFactory_1.StatementFactory.constant(props.importer.useLocal("encoder"), write_encoder({
89
+ context: props.context,
90
+ importer: props.importer,
91
+ collection: collection,
92
+ metadata: metadata,
93
+ })),
89
94
  },
90
95
  statements: [],
91
96
  arrow: typescript_1.default.factory.createArrowFunction(undefined, undefined, [
92
97
  IdentifierFactory_1.IdentifierFactory.parameter("input", typescript_1.default.factory.createTypeReferenceNode((_a = props.name) !== null && _a !== void 0 ? _a : TypeFactory_1.TypeFactory.getFullName(props.context.checker)(props.type))),
93
98
  ], typescript_1.default.factory.createTypeReferenceNode("Uint8Array"), undefined, typescript_1.default.factory.createBlock([
94
- callEncoder("sizer")(typescript_1.default.factory.createNewExpression(props.importer.use("Sizer"), undefined, [])),
95
- callEncoder("writer")(typescript_1.default.factory.createNewExpression(props.importer.use("Writer"), undefined, [typescript_1.default.factory.createIdentifier("sizer")])),
99
+ callEncoder("sizer", typescript_1.default.factory.createNewExpression(props.importer.use("Sizer"), undefined, [])),
100
+ callEncoder("writer", typescript_1.default.factory.createNewExpression(props.importer.use("Writer"), undefined, [typescript_1.default.factory.createIdentifier("sizer")])),
96
101
  typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("buffer"), undefined, undefined)),
97
102
  ], true)),
98
103
  };
@@ -106,335 +111,357 @@ var ProtobufEncodeProgrammer;
106
111
  result: result,
107
112
  });
108
113
  };
109
- var write_encoder = function (project) {
110
- return function (importer) {
111
- return function (collection) {
112
- return function (meta) {
113
- var functors = collection
114
- .objects()
115
- .filter(function (obj) { return ProtobufUtil_1.ProtobufUtil.isStaticObject(obj); })
116
- .map(function (obj) {
117
- return StatementFactory_1.StatementFactory.constant("".concat(PREFIX, "o").concat(obj.index), write_object_function(project)(importer)(typescript_1.default.factory.createIdentifier("input"), obj, {
118
- source: "function",
119
- from: "object",
120
- tracable: false,
121
- postfix: "",
122
- }));
123
- });
124
- var main = decode(project)(importer)(null)(typescript_1.default.factory.createIdentifier("input"), meta, {
125
- source: "top",
126
- from: "top",
127
- tracable: false,
128
- postfix: "",
129
- });
130
- return typescript_1.default.factory.createArrowFunction(undefined, undefined, [
131
- IdentifierFactory_1.IdentifierFactory.parameter("writer"),
132
- IdentifierFactory_1.IdentifierFactory.parameter("input"),
133
- ], TypeFactory_1.TypeFactory.keyword("any"), undefined, typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(importer.declareUnions()), false), __read(functors), false), __read(IsProgrammer_1.IsProgrammer.write_function_statements(project)(importer)(collection)), false), __read(main.statements), false), [
134
- typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createIdentifier("writer")),
135
- ], false), true));
136
- };
137
- };
138
- };
114
+ var write_encoder = function (props) {
115
+ var functors = props.collection
116
+ .objects()
117
+ .filter(function (obj) { return ProtobufUtil_1.ProtobufUtil.isStaticObject(obj); })
118
+ .map(function (object) {
119
+ return StatementFactory_1.StatementFactory.constant("".concat(PREFIX, "o").concat(object.index), write_object_function({
120
+ context: props.context,
121
+ importer: props.importer,
122
+ input: typescript_1.default.factory.createIdentifier("input"),
123
+ object: object,
124
+ explore: {
125
+ source: "function",
126
+ from: "object",
127
+ tracable: false,
128
+ postfix: "",
129
+ },
130
+ }));
131
+ });
132
+ var main = decode({
133
+ context: props.context,
134
+ importer: props.importer,
135
+ index: null,
136
+ input: typescript_1.default.factory.createIdentifier("input"),
137
+ metadata: props.metadata,
138
+ explore: {
139
+ source: "top",
140
+ from: "top",
141
+ tracable: false,
142
+ postfix: "",
143
+ },
144
+ });
145
+ return typescript_1.default.factory.createArrowFunction(undefined, undefined, [
146
+ IdentifierFactory_1.IdentifierFactory.parameter("writer"),
147
+ IdentifierFactory_1.IdentifierFactory.parameter("input"),
148
+ ], TypeFactory_1.TypeFactory.keyword("any"), undefined, typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(props.importer.declareUnions()), false), __read(functors), false), __read(IsProgrammer_1.IsProgrammer.write_function_statements(props.context)(props.importer)(props.collection)), false), __read(main.statements), false), [
149
+ typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createIdentifier("writer")),
150
+ ], false), true));
139
151
  };
140
- var write_object_function = function (project) {
141
- return function (importer) {
142
- return function (input, obj, explore) {
143
- var index = 1;
144
- var body = obj.properties
145
- .map(function (p) {
146
- var block = decode(project)(importer)(index)(IdentifierFactory_1.IdentifierFactory.access(input)(p.key.getSoleLiteral()), p.value, explore);
147
- index += ProtobufUtil_1.ProtobufUtil.size(p.value);
148
- return __spreadArray([
149
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createIdentifier("// property \"".concat(p.key.getSoleLiteral(), "\"")))
150
- ], __read(block.statements), false);
151
- })
152
- .flat();
153
- return typescript_1.default.factory.createArrowFunction(undefined, undefined, [IdentifierFactory_1.IdentifierFactory.parameter("input")], TypeFactory_1.TypeFactory.keyword("any"), undefined, typescript_1.default.factory.createBlock(body, true));
154
- };
155
- };
152
+ var write_object_function = function (props) {
153
+ var index = 1;
154
+ var body = props.object.properties
155
+ .map(function (p) {
156
+ var block = decode(__assign(__assign({}, props), { index: index, input: IdentifierFactory_1.IdentifierFactory.access(props.input)(p.key.getSoleLiteral()), metadata: p.value }));
157
+ index += ProtobufUtil_1.ProtobufUtil.size(p.value);
158
+ return __spreadArray([
159
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createIdentifier("// property \"".concat(p.key.getSoleLiteral(), "\"")))
160
+ ], __read(block.statements), false);
161
+ })
162
+ .flat();
163
+ return typescript_1.default.factory.createArrowFunction(undefined, undefined, [IdentifierFactory_1.IdentifierFactory.parameter("input")], TypeFactory_1.TypeFactory.keyword("any"), undefined, typescript_1.default.factory.createBlock(body, true));
156
164
  };
157
165
  /* -----------------------------------------------------------
158
166
  DECODERS
159
167
  ----------------------------------------------------------- */
160
- var decode = function (project) {
161
- return function (importer) {
162
- return function (index) {
163
- return function (input, meta, explore) {
164
- var e_1, _a;
165
- var wrapper = meta.isRequired() && meta.nullable === false
166
- ? function (block) { return block; }
167
- : meta.isRequired() === false && meta.nullable === true
168
- ? function (block) {
169
- return typescript_1.default.factory.createBlock([
170
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), input), typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), input)), block),
171
- ], true);
172
- }
173
- : meta.isRequired() === false
174
- ? function (block) {
175
- return typescript_1.default.factory.createBlock([
176
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), input), block),
177
- ], true);
178
- }
179
- : function (block) {
180
- return typescript_1.default.factory.createBlock([
181
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), input), block),
182
- ], true);
183
- };
184
- // STARTS FROM ATOMIC TYPES
185
- var unions = [];
186
- var numbers = ProtobufUtil_1.ProtobufUtil.getNumbers(meta);
187
- var bigints = ProtobufUtil_1.ProtobufUtil.getBigints(meta);
188
- var _loop_1 = function (atom) {
189
- if (atom === "bool")
190
- unions.push({
191
- type: "bool",
192
- is: function () {
193
- return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("boolean"), typescript_1.default.factory.createTypeOfExpression(input));
194
- },
195
- value: function (index) { return decode_bool(index)(input); },
196
- });
197
- else if (atom === "int32" ||
198
- atom === "uint32" ||
199
- atom === "float" ||
200
- atom === "double")
201
- unions.push(decode_number(numbers)(atom)(input));
202
- else if (atom === "int64" || atom === "uint64")
203
- if (numbers.some(function (n) { return n === atom; }))
204
- unions.push(decode_number(numbers)(atom)(input));
205
- else
206
- unions.push(decode_bigint(bigints)(atom)(input));
207
- else if (atom === "string")
208
- unions.push({
209
- type: "string",
210
- is: function () {
211
- return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("string"), typescript_1.default.factory.createTypeOfExpression(input));
212
- },
213
- value: function (index) { return decode_bytes("string")(index)(input); },
214
- });
215
- };
216
- try {
217
- for (var _b = __values(ProtobufUtil_1.ProtobufUtil.getAtomics(meta)), _c = _b.next(); !_c.done; _c = _b.next()) {
218
- var atom = _c.value;
219
- _loop_1(atom);
220
- }
221
- }
222
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
223
- finally {
224
- try {
225
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
226
- }
227
- finally { if (e_1) throw e_1.error; }
228
- }
229
- // CONSIDER BYTES
230
- if (meta.natives.length)
231
- unions.push({
232
- type: "bytes",
233
- is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Uint8Array")(input); },
234
- value: function (index) { return decode_bytes("bytes")(index)(input); },
235
- });
236
- // CONSIDER ARRAYS
237
- if (meta.arrays.length)
238
- unions.push({
239
- type: "array",
240
- is: function () { return ExpressionFactory_1.ExpressionFactory.isArray(input); },
241
- value: function (index) {
242
- return decode_array(project)(importer)(index)(input, meta.arrays[0], __assign(__assign({}, explore), { from: "array" }));
243
- },
244
- });
245
- // CONSIDER MAPS
246
- if (meta.maps.length)
247
- unions.push({
248
- type: "map",
249
- is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Map")(input); },
250
- value: function (index) {
251
- return decode_map(project)(importer)(index)(input, meta.maps[0], __assign(__assign({}, explore), { from: "array" }));
252
- },
253
- });
254
- // CONSIDER OBJECTS
255
- if (meta.objects.length)
256
- unions.push({
257
- type: "object",
258
- is: function () {
259
- return ExpressionFactory_1.ExpressionFactory.isObject({
260
- checkNull: true,
261
- checkArray: false,
262
- })(input);
263
- },
264
- value: function (index) {
265
- return explore_objects(project)(importer)(0)(index)(input, meta.objects, __assign(__assign({}, explore), { from: "object" }));
266
- },
267
- });
268
- // RETURNS
269
- if (unions.length === 1)
270
- return wrapper(unions[0].value(index));
271
- else
272
- return wrapper(iterate(importer)(index)(unions)(meta.getName())(input));
273
- };
274
- };
275
- };
276
- };
277
- var iterate = function (importer) {
278
- return function (index) {
279
- return function (unions) {
280
- return function (expected) {
281
- return function (input) {
282
- return typescript_1.default.factory.createBlock([
283
- unions
284
- .map(function (u, i) {
285
- return typescript_1.default.factory.createIfStatement(u.is(), u.value(index ? index + i : null), i === unions.length - 1
286
- ? create_throw_error(importer)(expected)(input)
287
- : undefined);
288
- })
289
- .reverse()
290
- .reduce(function (a, b) {
291
- return typescript_1.default.factory.createIfStatement(b.expression, b.thenStatement, a);
292
- }),
293
- ], true);
294
- };
295
- };
296
- };
297
- };
298
- };
299
- var decode_map = function (project) {
300
- return function (importer) {
301
- return function (index) {
302
- return function (input, map, explore) {
303
- var each = __spreadArray(__spreadArray(__spreadArray([
304
- typescript_1.default.factory.createExpressionStatement(decode_tag(2 /* ProtobufWire.LEN */)(index)),
305
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined))
306
- ], __read(decode(project)(importer)(1)(typescript_1.default.factory.createIdentifier("key"), map.key, explore).statements), false), __read(decode(project)(importer)(2)(typescript_1.default.factory.createIdentifier("value"), map.value, explore).statements), false), [
307
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined)),
308
- ], false);
168
+ var decode = function (props) {
169
+ var e_1, _a;
170
+ var wrapper = props.metadata.isRequired() && props.metadata.nullable === false
171
+ ? function (block) { return block; }
172
+ : props.metadata.isRequired() === false &&
173
+ props.metadata.nullable === true
174
+ ? function (block) {
309
175
  return typescript_1.default.factory.createBlock([
310
- typescript_1.default.factory.createForOfStatement(undefined, StatementFactory_1.StatementFactory.entry("key")("value"), input, typescript_1.default.factory.createBlock(each)),
176
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), props.input), typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), props.input)), block),
311
177
  ], true);
312
- };
313
- };
314
- };
315
- };
316
- var decode_object = function (project) {
317
- return function (importer) {
318
- return function (index) {
319
- return function (input, object, explore) {
320
- var top = object.properties[0];
321
- if (top.key.isSoleLiteral() === false)
322
- return decode_map(project)(importer)(index)(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("Object.entries"), [], [input]), MetadataProperty_1.MetadataProperty.create(__assign(__assign({}, top), { key: (function () {
323
- var key = Metadata_1.Metadata.initialize();
324
- key.atomics.push(MetadataAtomic_1.MetadataAtomic.create({
325
- type: "string",
326
- tags: [],
327
- }));
328
- return key;
329
- })() })), explore);
330
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray([
331
- typescript_1.default.factory.createIdentifier("//".concat(index !== null ? " ".concat(index, " -> ") : "").concat(object.name))
332
- ], __read((index !== null
333
- ? [
334
- decode_tag(2 /* ProtobufWire.LEN */)(index),
335
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined),
336
- ]
337
- : [])), false), [
338
- typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(importer.useLocal("".concat(PREFIX, "o").concat(object.index))), [], [input])
339
- ], false), __read((index !== null
340
- ? [
341
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined),
342
- ]
343
- : [])), false).map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
344
- };
345
- };
346
- };
347
- };
348
- var decode_array = function (project) {
349
- return function (importer) {
350
- return function (index) {
351
- return function (input, array, explore) {
352
- var wire = get_standalone_wire(array.type.value);
353
- var forLoop = function (index) {
354
- return typescript_1.default.factory.createForOfStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([typescript_1.default.factory.createVariableDeclaration("elem")], typescript_1.default.NodeFlags.Const), input, decode(project)(importer)(index)(typescript_1.default.factory.createIdentifier("elem"), array.type.value, explore));
355
- };
356
- var length = function (block) {
178
+ }
179
+ : props.metadata.isRequired() === false
180
+ ? function (block) {
181
+ return typescript_1.default.factory.createBlock([
182
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), props.input), block),
183
+ ], true);
184
+ }
185
+ : function (block) {
357
186
  return typescript_1.default.factory.createBlock([
358
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(ExpressionFactory_1.ExpressionFactory.number(0), IdentifierFactory_1.IdentifierFactory.access(input)("length")), block),
187
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), props.input), block),
359
188
  ], true);
360
189
  };
361
- if (wire === 2 /* ProtobufWire.LEN */)
362
- return length(typescript_1.default.factory.createBlock([forLoop(index)], true));
363
- return length(typescript_1.default.factory.createBlock([
364
- typescript_1.default.factory.createExpressionStatement(decode_tag(2 /* ProtobufWire.LEN */)(index)),
365
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined)),
366
- forLoop(null),
367
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined)),
368
- ], true));
369
- };
370
- };
190
+ // STARTS FROM ATOMIC TYPES
191
+ var unions = [];
192
+ var numbers = ProtobufUtil_1.ProtobufUtil.getNumbers(props.metadata);
193
+ var bigints = ProtobufUtil_1.ProtobufUtil.getBigints(props.metadata);
194
+ var _loop_1 = function (atom) {
195
+ if (atom === "bool")
196
+ unions.push({
197
+ type: "bool",
198
+ is: function () {
199
+ return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("boolean"), typescript_1.default.factory.createTypeOfExpression(props.input));
200
+ },
201
+ value: function (index) {
202
+ return decode_bool({
203
+ index: index,
204
+ input: props.input,
205
+ });
206
+ },
207
+ });
208
+ else if (atom === "int32" ||
209
+ atom === "uint32" ||
210
+ atom === "float" ||
211
+ atom === "double")
212
+ unions.push(decode_number({
213
+ candidates: numbers,
214
+ type: atom,
215
+ input: props.input,
216
+ }));
217
+ else if (atom === "int64" || atom === "uint64")
218
+ if (numbers.some(function (n) { return n === atom; }))
219
+ unions.push(decode_number({
220
+ candidates: numbers,
221
+ type: atom,
222
+ input: props.input,
223
+ }));
224
+ else
225
+ unions.push(decode_bigint({
226
+ candidates: bigints,
227
+ type: atom,
228
+ input: props.input,
229
+ }));
230
+ else if (atom === "string")
231
+ unions.push({
232
+ type: "string",
233
+ is: function () {
234
+ return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("string"), typescript_1.default.factory.createTypeOfExpression(props.input));
235
+ },
236
+ value: function (index) {
237
+ return decode_bytes({
238
+ method: "string",
239
+ index: index,
240
+ input: props.input,
241
+ });
242
+ },
243
+ });
371
244
  };
372
- };
373
- var decode_bool = function (index) { return function (input) {
374
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null ? [decode_tag(0 /* ProtobufWire.VARIANT */)(index)] : [])), false), [
375
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("bool"), undefined, [input]),
376
- ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
377
- }; };
378
- var decode_number = function (candidates) {
379
- return function (type) {
380
- return function (input) { return ({
381
- type: type,
382
- is: function () {
383
- return candidates.length === 1
384
- ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(input))
385
- : typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(input)), NumericRangeFactory_1.NumericRangeFactory.number(type)(input));
245
+ try {
246
+ for (var _b = __values(ProtobufUtil_1.ProtobufUtil.getAtomics(props.metadata)), _c = _b.next(); !_c.done; _c = _b.next()) {
247
+ var atom = _c.value;
248
+ _loop_1(atom);
249
+ }
250
+ }
251
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
252
+ finally {
253
+ try {
254
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
255
+ }
256
+ finally { if (e_1) throw e_1.error; }
257
+ }
258
+ // CONSIDER BYTES
259
+ if (props.metadata.natives.length)
260
+ unions.push({
261
+ type: "bytes",
262
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Uint8Array")(props.input); },
263
+ value: function (index) {
264
+ return decode_bytes({
265
+ method: "bytes",
266
+ index: index,
267
+ input: props.input,
268
+ });
386
269
  },
270
+ });
271
+ // CONSIDER ARRAYS
272
+ if (props.metadata.arrays.length)
273
+ unions.push({
274
+ type: "array",
275
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isArray(props.input); },
387
276
  value: function (index) {
388
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
389
- ? [decode_tag(get_numeric_wire(type))(index)]
390
- : [])), false), [
391
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(type), undefined, [input]),
392
- ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
277
+ return decode_array(__assign(__assign({}, props), { array: props.metadata.arrays[0], explore: __assign(__assign({}, props.explore), { from: "array" }), index: index }));
393
278
  },
394
- }); };
395
- };
396
- };
397
- var decode_bigint = function (candidates) {
398
- return function (type) {
399
- return function (input) { return ({
400
- type: type,
279
+ });
280
+ // CONSIDER MAPS
281
+ if (props.metadata.maps.length)
282
+ unions.push({
283
+ type: "map",
284
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Map")(props.input); },
285
+ value: function (index) {
286
+ return decode_map(__assign(__assign({}, props), { index: index, entry: props.metadata.maps[0], explore: __assign(__assign({}, props.explore), { from: "array" }) }));
287
+ },
288
+ });
289
+ // CONSIDER OBJECTS
290
+ if (props.metadata.objects.length)
291
+ unions.push({
292
+ type: "object",
401
293
  is: function () {
402
- return candidates.length === 1
403
- ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(input))
404
- : typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(input)), NumericRangeFactory_1.NumericRangeFactory.bigint(type)(input));
294
+ return ExpressionFactory_1.ExpressionFactory.isObject({
295
+ checkNull: true,
296
+ checkArray: false,
297
+ })(props.input);
405
298
  },
406
299
  value: function (index) {
407
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
408
- ? [decode_tag(0 /* ProtobufWire.VARIANT */)(index)]
409
- : [])), false), [
410
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(type), undefined, [input]),
411
- ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
300
+ return explore_objects(__assign(__assign({}, props), { level: 0, index: index, objects: props.metadata.objects, explore: __assign(__assign({}, props.explore), { from: "object" }) }));
412
301
  },
413
- }); };
414
- };
302
+ });
303
+ // RETURNS
304
+ if (unions.length === 1)
305
+ return wrapper(unions[0].value(props.index));
306
+ else
307
+ return wrapper(iterate({
308
+ importer: props.importer,
309
+ index: props.index,
310
+ unions: unions,
311
+ expected: props.metadata.getName(),
312
+ input: props.input,
313
+ }));
415
314
  };
416
- var decode_bytes = function (method) {
417
- return function (index) {
418
- return function (input) {
419
- return typescript_1.default.factory.createBlock([
420
- decode_tag(2 /* ProtobufWire.LEN */)(index),
421
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(method), undefined, [input]),
422
- ].map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
423
- };
424
- };
315
+ var iterate = function (props) {
316
+ return typescript_1.default.factory.createBlock([
317
+ props.unions
318
+ .map(function (u, i) {
319
+ return typescript_1.default.factory.createIfStatement(u.is(), u.value(props.index ? props.index + i : null), i === props.unions.length - 1
320
+ ? create_throw_error(props)
321
+ : undefined);
322
+ })
323
+ .reverse()
324
+ .reduce(function (a, b) {
325
+ return typescript_1.default.factory.createIfStatement(b.expression, b.thenStatement, a);
326
+ }),
327
+ ], true);
425
328
  };
426
- var decode_tag = function (wire) {
427
- return function (index) {
428
- return typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("uint32"), undefined, [ExpressionFactory_1.ExpressionFactory.number((index << 3) | wire)]);
329
+ var decode_map = function (props) {
330
+ var each = __spreadArray(__spreadArray(__spreadArray([
331
+ typescript_1.default.factory.createExpressionStatement(decode_tag({
332
+ wire: 2 /* ProtobufWire.LEN */,
333
+ index: props.index,
334
+ })),
335
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined))
336
+ ], __read(decode(__assign(__assign({}, props), { index: 1, input: typescript_1.default.factory.createIdentifier("key"), metadata: props.entry.key })).statements), false), __read(decode(__assign(__assign({}, props), { index: 2, input: typescript_1.default.factory.createIdentifier("value"), metadata: props.entry.value })).statements), false), [
337
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined)),
338
+ ], false);
339
+ return typescript_1.default.factory.createBlock([
340
+ typescript_1.default.factory.createForOfStatement(undefined, StatementFactory_1.StatementFactory.entry("key")("value"), props.input, typescript_1.default.factory.createBlock(each)),
341
+ ], true);
342
+ };
343
+ var decode_object = function (props) {
344
+ var top = props.object.properties[0];
345
+ if (top.key.isSoleLiteral() === false)
346
+ return decode_map(__assign(__assign({}, props), { index: props.index, input: typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier("Object.entries"), [], [props.input]), entry: MetadataProperty_1.MetadataProperty.create(__assign(__assign({}, top), { key: (function () {
347
+ var key = Metadata_1.Metadata.initialize();
348
+ key.atomics.push(MetadataAtomic_1.MetadataAtomic.create({
349
+ type: "string",
350
+ tags: [],
351
+ }));
352
+ return key;
353
+ })() })) }));
354
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray([
355
+ typescript_1.default.factory.createIdentifier("//".concat(props.index !== null ? " ".concat(props.index, " -> ") : "").concat(props.object.name))
356
+ ], __read((props.index !== null
357
+ ? [
358
+ decode_tag({
359
+ wire: 2 /* ProtobufWire.LEN */,
360
+ index: props.index,
361
+ }),
362
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined),
363
+ ]
364
+ : [])), false), [
365
+ typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(props.importer.useLocal("".concat(PREFIX, "o").concat(props.object.index))), [], [props.input])
366
+ ], false), __read((props.index !== null
367
+ ? [
368
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined),
369
+ ]
370
+ : [])), false).map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
371
+ };
372
+ var decode_array = function (props) {
373
+ var wire = get_standalone_wire(props.array.type.value);
374
+ var forLoop = function (index) {
375
+ return typescript_1.default.factory.createForOfStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([typescript_1.default.factory.createVariableDeclaration("elem")], typescript_1.default.NodeFlags.Const), props.input, decode(__assign(__assign({}, props), { input: typescript_1.default.factory.createIdentifier("elem"), index: index, metadata: props.array.type.value })));
376
+ };
377
+ var length = function (block) {
378
+ return typescript_1.default.factory.createBlock([
379
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(ExpressionFactory_1.ExpressionFactory.number(0), IdentifierFactory_1.IdentifierFactory.access(props.input)("length")), block),
380
+ ], true);
429
381
  };
382
+ if (wire === 2 /* ProtobufWire.LEN */)
383
+ return length(typescript_1.default.factory.createBlock([forLoop(props.index)], true));
384
+ return length(typescript_1.default.factory.createBlock([
385
+ typescript_1.default.factory.createExpressionStatement(decode_tag({
386
+ wire: 2 /* ProtobufWire.LEN */,
387
+ index: props.index,
388
+ })),
389
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("fork"), undefined, undefined)),
390
+ forLoop(null),
391
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("ldelim"), undefined, undefined)),
392
+ ], true));
393
+ };
394
+ var decode_bool = function (props) {
395
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((props.index !== null
396
+ ? [
397
+ decode_tag({
398
+ wire: 0 /* ProtobufWire.VARIANT */,
399
+ index: props.index,
400
+ }),
401
+ ]
402
+ : [])), false), [
403
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("bool"), undefined, [props.input]),
404
+ ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
405
+ };
406
+ var decode_number = function (props) { return ({
407
+ type: props.type,
408
+ is: function () {
409
+ return props.candidates.length === 1
410
+ ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(props.input))
411
+ : typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(props.input)), NumericRangeFactory_1.NumericRangeFactory.number(props.type)(props.input));
412
+ },
413
+ value: function (index) {
414
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
415
+ ? [
416
+ decode_tag({
417
+ wire: get_numeric_wire(props.type),
418
+ index: index,
419
+ }),
420
+ ]
421
+ : [])), false), [
422
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(props.type), undefined, [props.input]),
423
+ ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
424
+ },
425
+ }); };
426
+ var decode_bigint = function (props) { return ({
427
+ type: props.type,
428
+ is: function () {
429
+ return props.candidates.length === 1
430
+ ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(props.input))
431
+ : typescript_1.default.factory.createLogicalAnd(typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(props.input)), NumericRangeFactory_1.NumericRangeFactory.bigint(props.type)(props.input));
432
+ },
433
+ value: function (index) {
434
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
435
+ ? [
436
+ decode_tag({
437
+ wire: 0 /* ProtobufWire.VARIANT */,
438
+ index: index,
439
+ }),
440
+ ]
441
+ : [])), false), [
442
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(props.type), undefined, [props.input]),
443
+ ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
444
+ },
445
+ }); };
446
+ var decode_bytes = function (props) {
447
+ return typescript_1.default.factory.createBlock([
448
+ decode_tag({
449
+ wire: 2 /* ProtobufWire.LEN */,
450
+ index: props.index,
451
+ }),
452
+ typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())(props.method), undefined, [props.input]),
453
+ ].map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
454
+ };
455
+ var decode_tag = function (props) {
456
+ return typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER())("uint32"), undefined, [ExpressionFactory_1.ExpressionFactory.number((props.index << 3) | props.wire)]);
430
457
  };
431
- var get_standalone_wire = function (meta) {
432
- if (meta.arrays.length ||
433
- meta.objects.length ||
434
- meta.maps.length ||
435
- meta.natives.length)
458
+ var get_standalone_wire = function (metadata) {
459
+ if (metadata.arrays.length ||
460
+ metadata.objects.length ||
461
+ metadata.maps.length ||
462
+ metadata.natives.length)
436
463
  return 2 /* ProtobufWire.LEN */;
437
- var v = ProtobufUtil_1.ProtobufUtil.getAtomics(meta)[0];
464
+ var v = ProtobufUtil_1.ProtobufUtil.getAtomics(metadata)[0];
438
465
  if (v === "string")
439
466
  return 2 /* ProtobufWire.LEN */;
440
467
  else if (v === "bool" ||
@@ -457,67 +484,96 @@ var ProtobufEncodeProgrammer;
457
484
  /* -----------------------------------------------------------
458
485
  EXPLORERS
459
486
  ----------------------------------------------------------- */
460
- var explore_objects = function (project) {
461
- return function (importer) {
462
- return function (level) {
463
- return function (index) {
464
- return function (input, targets, explore, indexes) {
465
- if (targets.length === 1)
466
- return decode_object(project)(importer)(indexes ? indexes.get(targets[0]) : index)(input, targets[0], explore);
467
- var expected = "(".concat(targets.map(function (t) { return t.name; }).join(" | "), ")");
468
- // POSSIBLE TO SPECIALIZE?
469
- var specList = UnionPredicator_1.UnionPredicator.object(targets);
470
- indexes !== null && indexes !== void 0 ? indexes : (indexes = new Map(targets.map(function (t, i) { return [t, index + i]; })));
471
- if (specList.length === 0) {
472
- var condition_1 = (0, decode_union_object_1.decode_union_object)(IsProgrammer_1.IsProgrammer.decode_object(project)(importer))(function (i, o, e) {
473
- return ExpressionFactory_1.ExpressionFactory.selfCall(decode_object(project)(importer)(indexes.get(o))(i, o, e));
474
- })(function (expr) { return expr; })(function (value, expected) {
475
- return create_throw_error(importer)(expected)(value);
476
- })(input, targets, explore);
477
- return StatementFactory_1.StatementFactory.block(condition_1);
478
- }
479
- var remained = targets.filter(function (t) { return specList.find(function (s) { return s.object === t; }) === undefined; });
480
- // DO SPECIALIZE
481
- var condition = specList
482
- .filter(function (spec) { return spec.property.key.getSoleLiteral() !== null; })
483
- .map(function (spec, i, array) {
484
- var key = spec.property.key.getSoleLiteral();
485
- var accessor = IdentifierFactory_1.IdentifierFactory.access(input)(key);
486
- var pred = spec.neighbour
487
- ? IsProgrammer_1.IsProgrammer.decode(project)(importer)(accessor, spec.property.value, __assign(__assign({}, explore), { tracable: false, postfix: IdentifierFactory_1.IdentifierFactory.postfix(key) }))
488
- : ExpressionFactory_1.ExpressionFactory.isRequired(accessor);
489
- return typescript_1.default.factory.createIfStatement(pred, typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(decode_object(project)(importer)(indexes.get(spec.object))(input, spec.object, explore))), i === array.length - 1
490
- ? remained.length
491
- ? typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(explore_objects(project)(importer)(level + 1)(index)(input, remained, explore, indexes)))
492
- : create_throw_error(importer)(expected)(input)
493
- : undefined);
494
- })
495
- .reverse()
496
- .reduce(function (a, b) {
497
- return typescript_1.default.factory.createIfStatement(b.expression, b.thenStatement, a);
498
- });
499
- // RETURNS WITH CONDITIONS
500
- return typescript_1.default.factory.createBlock([condition], true);
501
- };
502
- };
503
- };
504
- };
487
+ var explore_objects = function (props) {
488
+ var _a;
489
+ if (props.objects.length === 1)
490
+ return decode_object({
491
+ context: props.context,
492
+ importer: props.importer,
493
+ index: props.indexes
494
+ ? props.indexes.get(props.objects[0])
495
+ : props.index,
496
+ input: props.input,
497
+ object: props.objects[0],
498
+ explore: props.explore,
499
+ });
500
+ var expected = "(".concat(props.objects.map(function (t) { return t.name; }).join(" | "), ")");
501
+ // POSSIBLE TO SPECIALIZE?
502
+ var specList = UnionPredicator_1.UnionPredicator.object(props.objects);
503
+ var indexes = (_a = props.indexes) !== null && _a !== void 0 ? _a : new Map(props.objects.map(function (t, i) { return [t, props.index + i]; }));
504
+ if (specList.length === 0) {
505
+ var condition_1 = (0, decode_union_object_1.decode_union_object)(IsProgrammer_1.IsProgrammer.decode_object(props.context)(props.importer))(function (input, object, explore) {
506
+ return ExpressionFactory_1.ExpressionFactory.selfCall(decode_object({
507
+ context: props.context,
508
+ importer: props.importer,
509
+ index: indexes.get(object),
510
+ input: input,
511
+ object: object,
512
+ explore: explore,
513
+ }));
514
+ })(function (expr) { return expr; })(function (input, expected) {
515
+ return create_throw_error({
516
+ importer: props.importer,
517
+ expected: expected,
518
+ input: input,
519
+ });
520
+ })(props.input, props.objects, props.explore);
521
+ return StatementFactory_1.StatementFactory.block(condition_1);
522
+ }
523
+ var remained = props.objects.filter(function (t) { return specList.find(function (s) { return s.object === t; }) === undefined; });
524
+ // DO SPECIALIZE
525
+ var condition = specList
526
+ .filter(function (spec) { return spec.property.key.getSoleLiteral() !== null; })
527
+ .map(function (spec, i, array) {
528
+ var key = spec.property.key.getSoleLiteral();
529
+ var accessor = IdentifierFactory_1.IdentifierFactory.access(props.input)(key);
530
+ var pred = spec.neighbour
531
+ ? IsProgrammer_1.IsProgrammer.decode(props.context)(props.importer)(accessor, spec.property.value, __assign(__assign({}, props.explore), { tracable: false, postfix: IdentifierFactory_1.IdentifierFactory.postfix(key) }))
532
+ : ExpressionFactory_1.ExpressionFactory.isRequired(accessor);
533
+ return typescript_1.default.factory.createIfStatement(pred, typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(decode_object({
534
+ context: props.context,
535
+ importer: props.importer,
536
+ index: indexes.get(spec.object),
537
+ input: props.input,
538
+ object: spec.object,
539
+ explore: props.explore,
540
+ }))), i === array.length - 1
541
+ ? remained.length
542
+ ? typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(explore_objects({
543
+ context: props.context,
544
+ importer: props.importer,
545
+ level: props.level + 1,
546
+ index: props.index,
547
+ input: props.input,
548
+ objects: remained,
549
+ explore: props.explore,
550
+ indexes: indexes,
551
+ })))
552
+ : create_throw_error({
553
+ importer: props.importer,
554
+ input: props.input,
555
+ expected: expected,
556
+ })
557
+ : undefined);
558
+ })
559
+ .reverse()
560
+ .reduce(function (a, b) {
561
+ return typescript_1.default.factory.createIfStatement(b.expression, b.thenStatement, a);
562
+ });
563
+ // RETURNS WITH CONDITIONS
564
+ return typescript_1.default.factory.createBlock([condition], true);
505
565
  };
506
566
  /* -----------------------------------------------------------
507
567
  CONFIGURATIONS
508
568
  ----------------------------------------------------------- */
509
569
  var PREFIX = "$pe";
510
- var create_throw_error = function (importer) {
511
- return function (expected) {
512
- return function (value) {
513
- return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(importer.use("throws"), [], [
514
- typescript_1.default.factory.createObjectLiteralExpression([
515
- typescript_1.default.factory.createPropertyAssignment("expected", typescript_1.default.factory.createStringLiteral(expected)),
516
- typescript_1.default.factory.createPropertyAssignment("value", value),
517
- ], true),
518
- ]));
519
- };
520
- };
570
+ var create_throw_error = function (props) {
571
+ return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(props.importer.use("throws"), [], [
572
+ typescript_1.default.factory.createObjectLiteralExpression([
573
+ typescript_1.default.factory.createPropertyAssignment("expected", typescript_1.default.factory.createStringLiteral(props.expected)),
574
+ typescript_1.default.factory.createPropertyAssignment("value", props.input),
575
+ ], true),
576
+ ]));
521
577
  };
522
578
  })(ProtobufEncodeProgrammer || (exports.ProtobufEncodeProgrammer = ProtobufEncodeProgrammer = {}));
523
579
  var WRITER = function () { return typescript_1.default.factory.createIdentifier("writer"); };