typia 7.0.0-dev.20241019 → 7.0.0-dev.20241020

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.
@@ -59,9 +59,8 @@ var NumericRangeFactory_1 = require("../../factories/NumericRangeFactory");
59
59
  var ProtobufFactory_1 = require("../../factories/ProtobufFactory");
60
60
  var StatementFactory_1 = require("../../factories/StatementFactory");
61
61
  var TypeFactory_1 = require("../../factories/TypeFactory");
62
- var Metadata_1 = require("../../schemas/metadata/Metadata");
63
- var MetadataAtomic_1 = require("../../schemas/metadata/MetadataAtomic");
64
- var MetadataProperty_1 = require("../../schemas/metadata/MetadataProperty");
62
+ var MetadataMap_1 = require("../../schemas/metadata/MetadataMap");
63
+ var MetadataObjectType_1 = require("../../schemas/metadata/MetadataObjectType");
65
64
  var FeatureProgrammer_1 = require("../FeatureProgrammer");
66
65
  var IsProgrammer_1 = require("../IsProgrammer");
67
66
  var FunctionProgrammer_1 = require("../helpers/FunctionProgrammer");
@@ -107,7 +106,7 @@ var ProtobufEncodeProgrammer;
107
106
  ], typescript_1.default.factory.createTypeReferenceNode("Uint8Array"), undefined, typescript_1.default.factory.createBlock([
108
107
  callEncoder("sizer", typescript_1.default.factory.createNewExpression(props.context.importer.internal("ProtobufSizer"), undefined, [])),
109
108
  callEncoder("writer", typescript_1.default.factory.createNewExpression(props.context.importer.internal("ProtobufWriter"), undefined, [typescript_1.default.factory.createIdentifier("sizer")])),
110
- typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "buffer"), undefined, undefined)),
109
+ typescript_1.default.factory.createReturnStatement(callWriter("buffer")),
111
110
  ], true)),
112
111
  };
113
112
  };
@@ -121,6 +120,7 @@ var ProtobufEncodeProgrammer;
121
120
  });
122
121
  };
123
122
  var write_encoder = function (props) {
123
+ var _a, _b;
124
124
  var functors = props.collection
125
125
  .objects()
126
126
  .filter(function (obj) { return ProtobufUtil_1.ProtobufUtil.isStaticObject(obj); })
@@ -141,19 +141,6 @@ var ProtobufEncodeProgrammer;
141
141
  }),
142
142
  });
143
143
  });
144
- var main = decode({
145
- context: props.context,
146
- functor: props.functor,
147
- index: null,
148
- input: typescript_1.default.factory.createIdentifier("input"),
149
- metadata: props.metadata,
150
- explore: {
151
- source: "top",
152
- from: "top",
153
- tracable: false,
154
- postfix: "",
155
- },
156
- });
157
144
  return typescript_1.default.factory.createArrowFunction(undefined, [
158
145
  typescript_1.default.factory.createTypeParameterDeclaration(undefined, "Writer", props.context.importer.type({
159
146
  file: "typia/lib/internal/$IProtobufWriter.js",
@@ -162,108 +149,152 @@ var ProtobufEncodeProgrammer;
162
149
  ], [
163
150
  IdentifierFactory_1.IdentifierFactory.parameter("writer", typescript_1.default.factory.createTypeReferenceNode("Writer")),
164
151
  IdentifierFactory_1.IdentifierFactory.parameter("input"),
165
- ], typescript_1.default.factory.createTypeReferenceNode("Writer"), undefined, typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(props.functor.declareUnions()), false), __read(functors), false), __read(IsProgrammer_1.IsProgrammer.write_function_statements(props)), false), __read(main.statements), false), [
152
+ ], typescript_1.default.factory.createTypeReferenceNode("Writer"), undefined, typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(props.functor.declareUnions()), false), __read(functors), false), __read(IsProgrammer_1.IsProgrammer.write_function_statements(props)), false), [
153
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(props.functor.useLocal("".concat(PREFIX, "o").concat((_b = (_a = props.metadata.objects[0]) === null || _a === void 0 ? void 0 : _a.type.index) !== null && _b !== void 0 ? _b : 0))), [], [typescript_1.default.factory.createIdentifier("input")])),
166
154
  typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createIdentifier("writer")),
167
155
  ], false), true));
168
156
  };
169
157
  var write_object_function = function (props) {
170
- var index = 1;
171
158
  var body = props.object.properties
172
159
  .map(function (p) {
173
- var block = decode(__assign(__assign({}, props), { index: index, input: IdentifierFactory_1.IdentifierFactory.access(props.input, p.key.getSoleLiteral()), metadata: p.value }));
174
- index += ProtobufUtil_1.ProtobufUtil.size(p.value);
160
+ var block = decode_property({
161
+ context: props.context,
162
+ functor: props.functor,
163
+ explore: props.explore,
164
+ metadata: p.value,
165
+ protobuf: p.of_protobuf_,
166
+ input: IdentifierFactory_1.IdentifierFactory.access(props.input, p.key.getSoleLiteral()),
167
+ });
175
168
  return __spreadArray([
176
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createIdentifier("// property \"".concat(p.key.getSoleLiteral(), "\"")))
169
+ typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createIdentifier("// property ".concat(JSON.stringify(p.key.getSoleLiteral()), ": ").concat(p.value.getName())))
177
170
  ], __read(block.statements), false);
178
171
  })
179
172
  .flat();
180
173
  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));
181
174
  };
182
175
  /* -----------------------------------------------------------
183
- DECODERS
184
- ----------------------------------------------------------- */
185
- var decode = function (props) {
176
+ DECODER STATION
177
+ ----------------------------------------------------------- */
178
+ var decode_property = function (props) {
186
179
  var e_1, _a;
187
- var wrapper = props.metadata.isRequired() && props.metadata.nullable === false
188
- ? function (block) { return block; }
189
- : props.metadata.isRequired() === false &&
190
- props.metadata.nullable === true
191
- ? function (block) {
192
- return typescript_1.default.factory.createBlock([
193
- 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),
194
- ], true);
195
- }
196
- : props.metadata.isRequired() === false
197
- ? function (block) {
198
- return typescript_1.default.factory.createBlock([
199
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), props.input), block),
200
- ], true);
201
- }
202
- : function (block) {
203
- return typescript_1.default.factory.createBlock([
204
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), props.input), block),
205
- ], true);
206
- };
207
- // STARTS FROM ATOMIC TYPES
208
- // @todo
209
- var unions = [];
210
- var numbers = Array.from(ProtobufUtil_1.ProtobufUtil.getNumbers(props.metadata).keys());
211
- var bigints = Array.from(ProtobufUtil_1.ProtobufUtil.getBigints(props.metadata).keys());
212
- var _loop_1 = function (atom) {
213
- if (atom === "bool")
214
- unions.push({
215
- type: "bool",
180
+ var union = [];
181
+ var _loop_1 = function (schema) {
182
+ //----
183
+ // ATOMICS
184
+ //----
185
+ if (schema.type === "bool")
186
+ union.push({
216
187
  is: function () {
217
188
  return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("boolean"), typescript_1.default.factory.createTypeOfExpression(props.input));
218
189
  },
219
- value: function (index) {
190
+ value: function () {
220
191
  return decode_bool({
221
- index: index,
222
192
  input: props.input,
193
+ index: schema.index,
223
194
  });
224
195
  },
225
196
  });
226
- else if (atom === "int32" ||
227
- atom === "uint32" ||
228
- atom === "float" ||
229
- atom === "double")
230
- unions.push(decode_number({
231
- candidates: numbers,
232
- type: atom,
197
+ else if (schema.type === "bigint")
198
+ union.push(decode_bigint({
233
199
  input: props.input,
200
+ type: schema.name,
201
+ candidates: props.protobuf.union
202
+ .filter(function (s) { return s.type === "bigint"; })
203
+ .map(function (s) { return s.name; }),
204
+ index: schema.index,
234
205
  }));
235
- else if (atom === "int64" || atom === "uint64")
236
- if (numbers.some(function (n) { return n === atom; }))
237
- unions.push(decode_number({
238
- candidates: numbers,
239
- type: atom,
240
- input: props.input,
241
- }));
242
- else
243
- unions.push(decode_bigint({
244
- candidates: bigints,
245
- type: atom,
246
- input: props.input,
247
- }));
248
- else if (atom === "string")
249
- unions.push({
250
- type: "string",
206
+ else if (schema.type === "number")
207
+ union.push(decode_number({
208
+ input: props.input,
209
+ type: schema.name,
210
+ candidates: props.protobuf.union
211
+ .filter(function (s) { return s.type === "number"; })
212
+ .map(function (s) { return s.name; }),
213
+ index: schema.index,
214
+ }));
215
+ else if (schema.type === "string")
216
+ union.push({
251
217
  is: function () {
252
218
  return typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("string"), typescript_1.default.factory.createTypeOfExpression(props.input));
253
219
  },
254
- value: function (index) {
220
+ value: function () {
255
221
  return decode_bytes({
256
222
  method: "string",
257
- index: index,
223
+ index: schema.index,
224
+ input: props.input,
225
+ });
226
+ },
227
+ });
228
+ //----
229
+ // INSTANCES
230
+ //----
231
+ else if (schema.type === "bytes")
232
+ union.push({
233
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Uint8Array", props.input); },
234
+ value: function () {
235
+ return decode_bytes({
236
+ method: "bytes",
237
+ index: schema.index,
238
+ input: props.input,
239
+ });
240
+ },
241
+ });
242
+ else if (schema.type === "array")
243
+ union.push({
244
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isArray(props.input); },
245
+ value: function () {
246
+ return decode_array({
247
+ context: props.context,
248
+ functor: props.functor,
249
+ input: props.input,
250
+ schema: schema,
251
+ });
252
+ },
253
+ });
254
+ else if (schema.type === "map" && schema.map instanceof MetadataMap_1.MetadataMap) {
255
+ union.push({
256
+ is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Map", props.input); },
257
+ value: function () {
258
+ return decode_map({
259
+ context: props.context,
260
+ functor: props.functor,
261
+ schema: schema,
262
+ input: props.input,
263
+ });
264
+ },
265
+ });
266
+ }
267
+ var objectSchemas = props.protobuf.union
268
+ .filter(function (schema) { return schema.type === "object" || schema.type === "map"; })
269
+ .filter(function (schema) {
270
+ return schema.type === "object" ||
271
+ (schema.type === "map" && schema.map instanceof MetadataObjectType_1.MetadataObjectType);
272
+ });
273
+ if (objectSchemas.length !== 0)
274
+ union.push({
275
+ is: function () {
276
+ return ExpressionFactory_1.ExpressionFactory.isObject({
277
+ checkNull: true,
278
+ checkArray: false,
279
+ input: props.input,
280
+ });
281
+ },
282
+ value: function () {
283
+ return explore_objects({
284
+ context: props.context,
285
+ functor: props.functor,
286
+ level: 0,
287
+ schemas: objectSchemas,
288
+ explore: __assign(__assign({}, props.explore), { from: "object" }),
258
289
  input: props.input,
259
290
  });
260
291
  },
261
292
  });
262
293
  };
263
294
  try {
264
- for (var _b = __values(ProtobufUtil_1.ProtobufUtil.getAtomics(props.metadata)), _c = _b.next(); !_c.done; _c = _b.next()) {
265
- var _d = __read(_c.value, 1), atom = _d[0];
266
- _loop_1(atom);
295
+ for (var _b = __values(props.protobuf.union), _c = _b.next(); !_c.done; _c = _b.next()) {
296
+ var schema = _c.value;
297
+ _loop_1(schema);
267
298
  }
268
299
  }
269
300
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -273,147 +304,50 @@ var ProtobufEncodeProgrammer;
273
304
  }
274
305
  finally { if (e_1) throw e_1.error; }
275
306
  }
276
- // CONSIDER BYTES
277
- if (props.metadata.natives.length)
278
- unions.push({
279
- type: "bytes",
280
- is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Uint8Array", props.input); },
281
- value: function (index) {
282
- return decode_bytes({
283
- method: "bytes",
284
- index: index,
285
- input: props.input,
286
- });
287
- },
288
- });
289
- // CONSIDER ARRAYS
290
- if (props.metadata.arrays.length)
291
- unions.push({
292
- type: "array",
293
- is: function () { return ExpressionFactory_1.ExpressionFactory.isArray(props.input); },
294
- value: function (index) {
295
- return decode_array(__assign(__assign({}, props), { array: props.metadata.arrays[0], explore: __assign(__assign({}, props.explore), { from: "array" }), index: index }));
296
- },
297
- });
298
- // CONSIDER MAPS
299
- if (props.metadata.maps.length)
300
- unions.push({
301
- type: "map",
302
- is: function () { return ExpressionFactory_1.ExpressionFactory.isInstanceOf("Map", props.input); },
303
- value: function (index) {
304
- return decode_map(__assign(__assign({}, props), { index: index, entry: props.metadata.maps[0], explore: __assign(__assign({}, props.explore), { from: "array" }) }));
305
- },
306
- });
307
- // CONSIDER OBJECTS
308
- if (props.metadata.objects.length)
309
- unions.push({
310
- type: "object",
311
- is: function () {
312
- return ExpressionFactory_1.ExpressionFactory.isObject({
313
- checkNull: true,
314
- checkArray: false,
315
- input: props.input,
316
- });
317
- },
318
- value: function (index) {
319
- return explore_objects(__assign(__assign({}, props), { level: 0, index: index, objects: props.metadata.objects.map(function (o) { return o.type; }), explore: __assign(__assign({}, props.explore), { from: "object" }) }));
320
- },
321
- });
322
307
  // RETURNS
323
- if (unions.length === 1)
324
- return wrapper(unions[0].value(props.index));
325
- else
326
- return wrapper(iterate({
327
- context: props.context,
328
- functor: props.functor,
329
- index: props.index,
330
- unions: unions,
331
- expected: props.metadata.getName(),
332
- input: props.input,
333
- }));
334
- };
335
- var iterate = function (props) {
336
- return typescript_1.default.factory.createBlock([
337
- props.unions
308
+ var wrapper = props.metadata.isRequired() && props.metadata.nullable === false
309
+ ? function (block) { return block; }
310
+ : props.metadata.isRequired() === false &&
311
+ props.metadata.nullable === true
312
+ ? function (block) {
313
+ return typescript_1.default.factory.createBlock([
314
+ 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),
315
+ ], true);
316
+ }
317
+ : props.metadata.isRequired() === false
318
+ ? function (block) {
319
+ return typescript_1.default.factory.createBlock([
320
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createIdentifier("undefined"), props.input), block),
321
+ ], true);
322
+ }
323
+ : function (block) {
324
+ return typescript_1.default.factory.createBlock([
325
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(typescript_1.default.factory.createNull(), props.input), block),
326
+ ], true);
327
+ };
328
+ if (union.length === 1)
329
+ return wrapper(union[0].value());
330
+ return wrapper(typescript_1.default.factory.createBlock([
331
+ union
338
332
  .map(function (u, i) {
339
- return typescript_1.default.factory.createIfStatement(u.is(), u.value(props.index ? props.index + i : null), i === props.unions.length - 1
340
- ? create_throw_error(props)
333
+ return typescript_1.default.factory.createIfStatement(u.is(), u.value(), i === union.length - 1
334
+ ? create_throw_error({
335
+ context: props.context,
336
+ functor: props.functor,
337
+ input: props.input,
338
+ expected: props.metadata.getName(),
339
+ })
341
340
  : undefined);
342
341
  })
343
342
  .reverse()
344
343
  .reduce(function (a, b) {
345
344
  return typescript_1.default.factory.createIfStatement(b.expression, b.thenStatement, a);
346
345
  }),
347
- ], true);
348
- };
349
- var decode_map = function (props) {
350
- var each = __spreadArray(__spreadArray(__spreadArray([
351
- typescript_1.default.factory.createExpressionStatement(decode_tag({
352
- wire: 2 /* ProtobufWire.LEN */,
353
- index: props.index,
354
- })),
355
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "fork"), undefined, undefined))
356
- ], __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), [
357
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "ldelim"), undefined, undefined)),
358
- ], false);
359
- return typescript_1.default.factory.createBlock([
360
- typescript_1.default.factory.createForOfStatement(undefined, StatementFactory_1.StatementFactory.entry({
361
- key: "key",
362
- value: "value",
363
- }), props.input, typescript_1.default.factory.createBlock(each)),
364
- ], true);
365
- };
366
- var decode_object = function (props) {
367
- var top = props.object.properties[0];
368
- if (top.key.isSoleLiteral() === false)
369
- 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 () {
370
- var key = Metadata_1.Metadata.initialize();
371
- key.atomics.push(MetadataAtomic_1.MetadataAtomic.create({
372
- type: "string",
373
- tags: [],
374
- }));
375
- return key;
376
- })() })) }));
377
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray(__spreadArray([
378
- typescript_1.default.factory.createIdentifier("//".concat(props.index !== null ? " ".concat(props.index, " -> ") : "").concat(props.object.name))
379
- ], __read((props.index !== null
380
- ? [
381
- decode_tag({
382
- wire: 2 /* ProtobufWire.LEN */,
383
- index: props.index,
384
- }),
385
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "fork"), undefined, undefined),
386
- ]
387
- : [])), false), [
388
- typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(props.functor.useLocal("".concat(PREFIX, "o").concat(props.object.index))), [], [props.input])
389
- ], false), __read((props.index !== null
390
- ? [
391
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "ldelim"), undefined, undefined),
392
- ]
393
- : [])), false).map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
394
- };
395
- var decode_array = function (props) {
396
- var wire = get_standalone_wire(props.array.type.value);
397
- var forLoop = function (index) {
398
- 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 })));
399
- };
400
- var length = function (block) {
401
- return typescript_1.default.factory.createBlock([
402
- typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(ExpressionFactory_1.ExpressionFactory.number(0), IdentifierFactory_1.IdentifierFactory.access(props.input, "length")), block),
403
- ], true);
404
- };
405
- if (wire === 2 /* ProtobufWire.LEN */)
406
- return length(typescript_1.default.factory.createBlock([forLoop(props.index)], true));
407
- return length(typescript_1.default.factory.createBlock([
408
- typescript_1.default.factory.createExpressionStatement(decode_tag({
409
- wire: 2 /* ProtobufWire.LEN */,
410
- index: props.index,
411
- })),
412
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "fork"), undefined, undefined)),
413
- forLoop(null),
414
- typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "ldelim"), undefined, undefined)),
415
346
  ], true));
416
347
  };
348
+ /* -----------------------------------------------------------
349
+ ATOMIC DECODERS
350
+ ----------------------------------------------------------- */
417
351
  var decode_bool = function (props) {
418
352
  return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((props.index !== null
419
353
  ? [
@@ -423,109 +357,199 @@ var ProtobufEncodeProgrammer;
423
357
  }),
424
358
  ]
425
359
  : [])), false), [
426
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "bool"), undefined, [props.input]),
360
+ callWriter("bool", [props.input]),
427
361
  ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
428
362
  };
429
- var decode_number = function (props) { return ({
430
- type: props.type,
363
+ var decode_bigint = function (props) { return ({
431
364
  is: function () {
432
365
  return props.candidates.length === 1
433
- ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(props.input))
434
- : 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));
366
+ ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(props.input))
367
+ : 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));
435
368
  },
436
- value: function (index) {
437
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
369
+ value: function () {
370
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((props.index !== null
438
371
  ? [
439
372
  decode_tag({
440
- wire: get_numeric_wire(props.type),
441
- index: index,
373
+ wire: 0 /* ProtobufWire.VARIANT */,
374
+ index: props.index,
442
375
  }),
443
376
  ]
444
377
  : [])), false), [
445
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), props.type), undefined, [props.input]),
378
+ callWriter(props.type, [props.input]),
446
379
  ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
447
380
  },
448
381
  }); };
449
- var decode_bigint = function (props) { return ({
450
- type: props.type,
382
+ var decode_number = function (props) { return ({
451
383
  is: function () {
452
384
  return props.candidates.length === 1
453
- ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("bigint"), typescript_1.default.factory.createTypeOfExpression(props.input))
454
- : 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));
385
+ ? typescript_1.default.factory.createStrictEquality(typescript_1.default.factory.createStringLiteral("number"), typescript_1.default.factory.createTypeOfExpression(props.input))
386
+ : 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));
455
387
  },
456
- value: function (index) {
457
- return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((index !== null
388
+ value: function () {
389
+ return typescript_1.default.factory.createBlock(__spreadArray(__spreadArray([], __read((props.index !== null
458
390
  ? [
459
391
  decode_tag({
460
- wire: 0 /* ProtobufWire.VARIANT */,
461
- index: index,
392
+ wire: get_numeric_wire(props.type),
393
+ index: props.index,
462
394
  }),
463
395
  ]
464
396
  : [])), false), [
465
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), props.type), undefined, [props.input]),
397
+ callWriter(props.type, [props.input]),
466
398
  ], false).map(function (exp) { return typescript_1.default.factory.createExpressionStatement(exp); }), true);
467
399
  },
468
400
  }); };
401
+ var decode_container_value = function (props) {
402
+ if (props.schema.type === "bool")
403
+ return decode_bool({
404
+ input: props.input,
405
+ index: props.kind === "array" ? null : props.index,
406
+ });
407
+ else if (props.schema.type === "bigint")
408
+ return decode_bigint({
409
+ input: props.input,
410
+ type: props.schema.name,
411
+ candidates: [props.schema.name],
412
+ index: props.kind === "array" ? null : props.index,
413
+ }).value();
414
+ else if (props.schema.type === "number")
415
+ return decode_number({
416
+ input: props.input,
417
+ type: props.schema.name,
418
+ candidates: [props.schema.name],
419
+ index: props.kind === "array" ? null : props.index,
420
+ }).value();
421
+ else if (props.schema.type === "string" || props.schema.type === "bytes")
422
+ return decode_bytes({
423
+ method: props.schema.type,
424
+ input: props.input,
425
+ index: props.index,
426
+ });
427
+ return decode_object({
428
+ context: props.context,
429
+ functor: props.functor,
430
+ schema: props.schema,
431
+ input: props.input,
432
+ index: props.index,
433
+ });
434
+ };
435
+ /* -----------------------------------------------------------
436
+ INSTANCE DECODERS
437
+ ----------------------------------------------------------- */
469
438
  var decode_bytes = function (props) {
470
439
  return typescript_1.default.factory.createBlock([
471
440
  decode_tag({
472
441
  wire: 2 /* ProtobufWire.LEN */,
473
442
  index: props.index,
474
443
  }),
475
- typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), props.method), undefined, [props.input]),
444
+ callWriter(props.method, [props.input]),
476
445
  ].map(function (expr) { return typescript_1.default.factory.createExpressionStatement(expr); }), true);
477
446
  };
478
- var decode_tag = function (props) {
479
- return typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(WRITER(), "uint32"), undefined, [ExpressionFactory_1.ExpressionFactory.number((props.index << 3) | props.wire)]);
480
- };
481
- var get_standalone_wire = function (metadata) {
482
- if (metadata.arrays.length ||
483
- metadata.objects.length ||
484
- metadata.maps.length ||
485
- metadata.natives.length)
486
- return 2 /* ProtobufWire.LEN */;
487
- // @todo
488
- var v = ProtobufUtil_1.ProtobufUtil.getAtomics(metadata).keys().next().value;
489
- if (v === "string")
490
- return 2 /* ProtobufWire.LEN */;
491
- else if (v === "bool" ||
492
- v === "int32" ||
493
- v === "uint32" ||
494
- v === "int64" ||
495
- v === "uint64")
447
+ var decode_array = function (props) {
448
+ var value = props.schema.value;
449
+ var wire = (function () {
450
+ if (value.type === "object" ||
451
+ value.type === "bytes" ||
452
+ value.type === "string")
453
+ return 2 /* ProtobufWire.LEN */;
454
+ else if (value.type === "number" && value.name === "float")
455
+ return 5 /* ProtobufWire.I32 */;
496
456
  return 0 /* ProtobufWire.VARIANT */;
497
- else if (v === "float")
498
- return 5 /* ProtobufWire.I32 */;
499
- return 1 /* ProtobufWire.I64 */;
457
+ })();
458
+ var forLoop = function () {
459
+ 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_container_value({
460
+ kind: "array",
461
+ context: props.context,
462
+ functor: props.functor,
463
+ input: typescript_1.default.factory.createIdentifier("elem"),
464
+ index: props.schema.index,
465
+ schema: props.schema.value,
466
+ }));
467
+ };
468
+ var length = function (block) {
469
+ return typescript_1.default.factory.createBlock([
470
+ typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createStrictInequality(ExpressionFactory_1.ExpressionFactory.number(0), IdentifierFactory_1.IdentifierFactory.access(props.input, "length")), block),
471
+ ], true);
472
+ };
473
+ if (wire === 2 /* ProtobufWire.LEN */)
474
+ return length(typescript_1.default.factory.createBlock([forLoop()], true));
475
+ return length(typescript_1.default.factory.createBlock([
476
+ typescript_1.default.factory.createExpressionStatement(decode_tag({
477
+ wire: 2 /* ProtobufWire.LEN */,
478
+ index: props.schema.index,
479
+ })),
480
+ typescript_1.default.factory.createExpressionStatement(callWriter("fork")),
481
+ forLoop(),
482
+ typescript_1.default.factory.createExpressionStatement(callWriter("ldelim")),
483
+ ], true));
500
484
  };
501
- var get_numeric_wire = function (type) {
502
- return type === "double"
503
- ? 1 /* ProtobufWire.I64 */
504
- : type === "float"
505
- ? 5 /* ProtobufWire.I32 */
506
- : 0 /* ProtobufWire.VARIANT */;
485
+ var decode_object = function (props) {
486
+ return typescript_1.default.factory.createBlock([
487
+ decode_tag({
488
+ wire: 2 /* ProtobufWire.LEN */,
489
+ index: props.index,
490
+ }),
491
+ callWriter("fork"),
492
+ typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(props.functor.useLocal("".concat(PREFIX, "o").concat(props.schema.object.index))), [], [props.input]),
493
+ callWriter("ldelim"),
494
+ ].map(typescript_1.default.factory.createExpressionStatement), true);
495
+ };
496
+ var decode_map = function (props) {
497
+ var each = __spreadArray(__spreadArray(__spreadArray([
498
+ typescript_1.default.factory.createExpressionStatement(decode_tag({
499
+ wire: 2 /* ProtobufWire.LEN */,
500
+ index: props.schema.index,
501
+ })),
502
+ typescript_1.default.factory.createExpressionStatement(callWriter("fork"))
503
+ ], __read(decode_container_value({
504
+ kind: "map",
505
+ context: props.context,
506
+ functor: props.functor,
507
+ index: 1,
508
+ input: typescript_1.default.factory.createIdentifier("key"),
509
+ schema: props.schema.key,
510
+ }).statements), false), __read(decode_container_value({
511
+ kind: "map",
512
+ context: props.context,
513
+ functor: props.functor,
514
+ index: 2,
515
+ input: typescript_1.default.factory.createIdentifier("value"),
516
+ schema: props.schema.value,
517
+ }).statements), false), [
518
+ typescript_1.default.factory.createExpressionStatement(callWriter("ldelim")),
519
+ ], false);
520
+ return typescript_1.default.factory.createBlock([
521
+ typescript_1.default.factory.createForOfStatement(undefined, StatementFactory_1.StatementFactory.entry({
522
+ key: "key",
523
+ value: "value",
524
+ }), props.input, typescript_1.default.factory.createBlock(each)),
525
+ ], true);
507
526
  };
508
- /* -----------------------------------------------------------
509
- EXPLORERS
510
- ----------------------------------------------------------- */
511
527
  var explore_objects = function (props) {
512
- var _a;
513
- if (props.objects.length === 1)
514
- return decode_object({
515
- context: props.context,
516
- functor: props.functor,
517
- index: props.indexes
518
- ? props.indexes.get(props.objects[0])
519
- : props.index,
520
- input: props.input,
521
- object: props.objects[0],
522
- explore: props.explore,
523
- });
524
- var expected = "(".concat(props.objects.map(function (t) { return t.name; }).join(" | "), ")");
525
- // POSSIBLE TO SPECIALIZE?
526
- var specList = UnionPredicator_1.UnionPredicator.object(props.objects);
527
- var indexes = (_a = props.indexes) !== null && _a !== void 0 ? _a : new Map(props.objects.map(function (t, i) { return [t, props.index + i]; }));
528
- if (specList.length === 0) {
528
+ var out = function (schema) {
529
+ return schema.type === "object"
530
+ ? decode_object({
531
+ context: props.context,
532
+ functor: props.functor,
533
+ schema: schema,
534
+ index: schema.index,
535
+ input: props.input,
536
+ })
537
+ : decode_map({
538
+ context: props.context,
539
+ functor: props.functor,
540
+ schema: schema,
541
+ input: typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier("Object"), "entries"), undefined, [props.input]),
542
+ });
543
+ };
544
+ if (props.schemas.length === 1)
545
+ return out(props.schemas[0]);
546
+ var objects = props.schemas.map(function (s) {
547
+ return s.type === "map" ? s.map : s.object;
548
+ });
549
+ var expected = "(".concat(objects.map(function (t) { return t.name; }).join(" | "), ")");
550
+ var indexes = new WeakMap(objects.map(function (o, i) { return [o, props.schemas[i]]; }));
551
+ var specifications = UnionPredicator_1.UnionPredicator.object(objects);
552
+ if (specifications.length === 0) {
529
553
  var condition_1 = (0, decode_union_object_1.decode_union_object)({
530
554
  checker: function (v) {
531
555
  return IsProgrammer_1.IsProgrammer.decode_object({
@@ -536,16 +560,7 @@ var ProtobufEncodeProgrammer;
536
560
  explore: v.explore,
537
561
  });
538
562
  },
539
- decoder: function (v) {
540
- return ExpressionFactory_1.ExpressionFactory.selfCall(decode_object({
541
- context: props.context,
542
- functor: props.functor,
543
- index: indexes.get(v.object),
544
- input: v.input,
545
- object: v.object,
546
- explore: v.explore,
547
- }));
548
- },
563
+ decoder: function (v) { return ExpressionFactory_1.ExpressionFactory.selfCall(out(indexes.get(v.object))); },
549
564
  success: function (expr) { return expr; },
550
565
  escaper: function (v) {
551
566
  return create_throw_error({
@@ -556,14 +571,14 @@ var ProtobufEncodeProgrammer;
556
571
  });
557
572
  },
558
573
  input: props.input,
559
- objects: props.objects,
560
574
  explore: props.explore,
575
+ objects: objects,
561
576
  });
562
577
  return StatementFactory_1.StatementFactory.block(condition_1);
563
578
  }
564
- var remained = props.objects.filter(function (t) { return specList.find(function (s) { return s.object === t; }) === undefined; });
579
+ var remained = objects.filter(function (o) { return specifications.find(function (s) { return s.object === o; }) === undefined; });
565
580
  // DO SPECIALIZE
566
- var condition = specList
581
+ var condition = specifications
567
582
  .filter(function (spec) { return spec.property.key.getSoleLiteral() !== null; })
568
583
  .map(function (spec, i, array) {
569
584
  var key = spec.property.key.getSoleLiteral();
@@ -577,24 +592,16 @@ var ProtobufEncodeProgrammer;
577
592
  explore: __assign(__assign({}, props.explore), { tracable: false, postfix: IdentifierFactory_1.IdentifierFactory.postfix(key) }),
578
593
  })
579
594
  : ExpressionFactory_1.ExpressionFactory.isRequired(accessor);
580
- return typescript_1.default.factory.createIfStatement(pred, typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(decode_object({
581
- context: props.context,
582
- functor: props.functor,
583
- index: indexes.get(spec.object),
584
- input: props.input,
585
- object: spec.object,
586
- explore: props.explore,
587
- }))), i === array.length - 1
595
+ var schema = indexes.get(spec.object);
596
+ return typescript_1.default.factory.createIfStatement(pred, typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(out(schema))), i === array.length - 1
588
597
  ? remained.length
589
598
  ? typescript_1.default.factory.createExpressionStatement(ExpressionFactory_1.ExpressionFactory.selfCall(explore_objects({
590
599
  context: props.context,
591
600
  functor: props.functor,
592
601
  level: props.level + 1,
593
- index: props.index,
594
602
  input: props.input,
595
- objects: remained,
603
+ schemas: remained.map(function (r) { return indexes.get(r); }),
596
604
  explore: props.explore,
597
- indexes: indexes,
598
605
  })))
599
606
  : create_throw_error({
600
607
  context: props.context,
@@ -612,9 +619,21 @@ var ProtobufEncodeProgrammer;
612
619
  return typescript_1.default.factory.createBlock([condition], true);
613
620
  };
614
621
  /* -----------------------------------------------------------
615
- CONFIGURATIONS
616
- ----------------------------------------------------------- */
622
+ BACKGROUND FUNCTIONS
623
+ ----------------------------------------------------------- */
617
624
  var PREFIX = "$pe";
625
+ var decode_tag = function (props) {
626
+ return callWriter("uint32", [
627
+ ExpressionFactory_1.ExpressionFactory.number((props.index << 3) | props.wire),
628
+ ]);
629
+ };
630
+ var get_numeric_wire = function (type) {
631
+ return type === "double"
632
+ ? 1 /* ProtobufWire.I64 */
633
+ : type === "float"
634
+ ? 5 /* ProtobufWire.I32 */
635
+ : 0 /* ProtobufWire.VARIANT */;
636
+ };
618
637
  var create_throw_error = function (props) {
619
638
  return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(props.context.importer.internal("throwTypeGuardError"), [], [
620
639
  typescript_1.default.factory.createObjectLiteralExpression([
@@ -625,5 +644,7 @@ var ProtobufEncodeProgrammer;
625
644
  ]));
626
645
  };
627
646
  })(ProtobufEncodeProgrammer || (exports.ProtobufEncodeProgrammer = ProtobufEncodeProgrammer = {}));
628
- var WRITER = function () { return typescript_1.default.factory.createIdentifier("writer"); };
647
+ var callWriter = function (method, args) {
648
+ return typescript_1.default.factory.createCallExpression(IdentifierFactory_1.IdentifierFactory.access(typescript_1.default.factory.createIdentifier("writer"), method), undefined, args);
649
+ };
629
650
  //# sourceMappingURL=ProtobufEncodeProgrammer.js.map