typia 5.0.0-dev.20230814 → 5.0.0-dev.20230818

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.
Files changed (72) hide show
  1. package/lib/factories/ExpressionFactory.d.ts +1 -0
  2. package/lib/factories/ExpressionFactory.js +33 -0
  3. package/lib/factories/ExpressionFactory.js.map +1 -1
  4. package/lib/factories/StatementFactory.d.ts +2 -0
  5. package/lib/factories/StatementFactory.js +11 -0
  6. package/lib/factories/StatementFactory.js.map +1 -1
  7. package/lib/functional/$ProtobufReader.d.ts +33 -0
  8. package/lib/functional/$ProtobufReader.js +155 -0
  9. package/lib/functional/$ProtobufReader.js.map +1 -0
  10. package/lib/functional/$ProtobufSizer.d.ts +36 -0
  11. package/lib/functional/$ProtobufSizer.js +105 -0
  12. package/lib/functional/$ProtobufSizer.js.map +1 -0
  13. package/lib/functional/$ProtobufWriter.d.ts +45 -0
  14. package/lib/functional/$ProtobufWriter.js +107 -0
  15. package/lib/functional/$ProtobufWriter.js.map +1 -0
  16. package/lib/functional/IProtobufWriter.d.ts +15 -0
  17. package/lib/functional/IProtobufWriter.js +3 -0
  18. package/lib/functional/IProtobufWriter.js.map +1 -0
  19. package/lib/functional/Namespace.js +13 -6
  20. package/lib/functional/Namespace.js.map +1 -1
  21. package/lib/programmers/FeatureProgrammer.js +2 -2
  22. package/lib/programmers/FeatureProgrammer.js.map +1 -1
  23. package/lib/programmers/helpers/CloneJoiner.js +1 -7
  24. package/lib/programmers/helpers/CloneJoiner.js.map +1 -1
  25. package/lib/programmers/helpers/ProtobufWire.d.ts +16 -12
  26. package/lib/programmers/helpers/RandomRanger.js +1 -1
  27. package/lib/programmers/helpers/RandomRanger.js.map +1 -1
  28. package/lib/programmers/json/JsonStringifyProgrammer.js +3 -0
  29. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  30. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +269 -127
  31. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  32. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +8 -2
  33. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  34. package/lib/protobuf.js +23 -9
  35. package/lib/protobuf.js.map +1 -1
  36. package/lib/schemas/metadata/MetadataObject.js +4 -0
  37. package/lib/schemas/metadata/MetadataObject.js.map +1 -1
  38. package/lib/transformers/CallExpressionTransformer.js +0 -4
  39. package/lib/transformers/CallExpressionTransformer.js.map +1 -1
  40. package/lib/transformers/features/json/JsonApplicationTransformer.js +6 -3
  41. package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
  42. package/package.json +1 -1
  43. package/src/factories/ExpressionFactory.ts +20 -0
  44. package/src/factories/StatementFactory.ts +32 -0
  45. package/src/functional/$ProtobufReader.ts +189 -0
  46. package/src/functional/$ProtobufSizer.ts +144 -0
  47. package/src/functional/$ProtobufWriter.ts +152 -0
  48. package/src/functional/IProtobufWriter.ts +18 -0
  49. package/src/functional/Namespace.ts +14 -7
  50. package/src/programmers/FeatureProgrammer.ts +2 -2
  51. package/src/programmers/helpers/CloneJoiner.ts +1 -25
  52. package/src/programmers/helpers/ProtobufWire.ts +18 -12
  53. package/src/programmers/helpers/RandomRanger.ts +1 -1
  54. package/src/programmers/json/JsonStringifyProgrammer.ts +8 -0
  55. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +544 -201
  56. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +4 -1
  57. package/src/protobuf.ts +23 -29
  58. package/src/schemas/metadata/MetadataObject.ts +10 -0
  59. package/src/transformers/CallExpressionTransformer.ts +0 -4
  60. package/src/transformers/features/json/JsonApplicationTransformer.ts +12 -3
  61. package/lib/programmers/protobuf/ProtobufSizeProgrammer.d.ts +0 -5
  62. package/lib/programmers/protobuf/ProtobufSizeProgrammer.js +0 -409
  63. package/lib/programmers/protobuf/ProtobufSizeProgrammer.js.map +0 -1
  64. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.d.ts +0 -3
  65. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js +0 -10
  66. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js.map +0 -1
  67. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.d.ts +0 -3
  68. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js +0 -10
  69. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js.map +0 -1
  70. package/src/programmers/protobuf/ProtobufSizeProgrammer.ts +0 -716
  71. package/src/transformers/features/protobuf/CreateProtobufSizeTransformer.ts +0 -9
  72. package/src/transformers/features/protobuf/ProtobufSizeTransformer.ts +0 -9
@@ -1,5 +1,6 @@
1
1
  import ts from "typescript";
2
2
 
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
3
4
  import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
5
  import { MetadataCollection } from "../../factories/MetadataCollection";
5
6
  import { ProtobufFactory } from "../../factories/ProtobufFactory";
@@ -36,25 +37,39 @@ export namespace ProtobufEncodeProgrammer {
36
37
  project.checker,
37
38
  )(collection)(type);
38
39
 
39
- // @todo
40
- meta;
41
- write_object_function_body;
42
- write_map_function_body;
43
- explore_objects;
44
- explore_arrays;
40
+ const callEncoder =
41
+ (writer: string) => (factory: ts.NewExpression) =>
42
+ StatementFactory.constant(
43
+ writer,
44
+ ts.factory.createCallExpression(
45
+ ts.factory.createIdentifier("encoder"),
46
+ undefined,
47
+ [factory],
48
+ ),
49
+ );
45
50
 
46
51
  const block: ts.Statement[] = [
47
52
  StatementFactory.constant(
48
- "writer",
53
+ "encoder",
54
+ write_encoder(project)(importer)(collection)(meta),
55
+ ),
56
+ callEncoder("sizer")(
49
57
  ts.factory.createNewExpression(
50
- importer.use("Writer"),
58
+ importer.use("Sizer"),
51
59
  undefined,
60
+ [],
61
+ ),
62
+ ),
63
+ callEncoder("writer")(
64
+ ts.factory.createNewExpression(
65
+ importer.use("Writer"),
52
66
  undefined,
67
+ [ts.factory.createIdentifier("sizer")],
53
68
  ),
54
69
  ),
55
70
  ts.factory.createReturnStatement(
56
71
  ts.factory.createCallExpression(
57
- IdentifierFactory.access(WRITER())("finish"),
72
+ IdentifierFactory.access(WRITER())("buffer"),
58
73
  undefined,
59
74
  undefined,
60
75
  ),
@@ -83,62 +98,95 @@ export namespace ProtobufEncodeProgrammer {
83
98
  );
84
99
  };
85
100
 
86
- const write_object_function_body =
101
+ const write_encoder =
87
102
  (project: IProject) =>
88
- (config: FeatureProgrammer.IConfig) =>
89
103
  (importer: FunctionImporter) =>
90
- (
91
- input: ts.Expression,
92
- obj: MetadataObject,
93
- explore: FeatureProgrammer.IExplore,
94
- tags: IMetadataTag[],
95
- ): ts.Block => {
96
- const top: MetadataProperty = obj.properties[0]!;
97
- if (top.key.isSoleLiteral() === false)
98
- return write_map_function_body(project)(config)(importer)(
99
- input,
100
- top,
101
- explore,
102
- tags,
103
- );
104
- let index: number = 1;
105
- return ts.factory.createBlock(
106
- obj.properties
107
- .map((property) => {
108
- const statements: ts.NodeArray<ts.Statement> = decode(
109
- project,
110
- )(config)(importer)(index)(
111
- input,
112
- property.value,
113
- explore,
114
- property.tags,
115
- ).statements;
116
- index += property.value.binarySize();
117
- return statements;
118
- })
119
- .flat(),
120
- true,
104
+ (collection: MetadataCollection) =>
105
+ (meta: Metadata): ts.ArrowFunction =>
106
+ ts.factory.createArrowFunction(
107
+ undefined,
108
+ undefined,
109
+ [IdentifierFactory.parameter("writer")],
110
+ TypeFactory.keyword("any"),
111
+ undefined,
112
+ ts.factory.createBlock(
113
+ [
114
+ ...collection
115
+ .objects()
116
+ .filter((obj) => obj._Messagable())
117
+ .map((obj) =>
118
+ StatementFactory.constant(
119
+ `${PREFIX}o${obj.index}`,
120
+ write_object_function(project)(importer)(
121
+ ts.factory.createIdentifier("input"),
122
+ obj,
123
+ {
124
+ source: "function",
125
+ from: "object",
126
+ tracable: false,
127
+ postfix: "",
128
+ },
129
+ ),
130
+ ),
131
+ ),
132
+ ...decode(project)(importer)(null)(
133
+ ts.factory.createIdentifier("input"),
134
+ meta,
135
+ {
136
+ source: "top",
137
+ from: "top",
138
+ tracable: false,
139
+ postfix: "",
140
+ },
141
+ [],
142
+ ).statements,
143
+ ts.factory.createReturnStatement(
144
+ ts.factory.createIdentifier("writer"),
145
+ ),
146
+ ],
147
+ true,
148
+ ),
121
149
  );
122
- };
123
150
 
124
- const write_map_function_body =
151
+ const write_object_function =
125
152
  (project: IProject) =>
126
- (config: FeatureProgrammer.IConfig) =>
127
153
  (importer: FunctionImporter) =>
128
154
  (
129
155
  input: ts.Expression,
130
- entry: Metadata.Entry,
156
+ obj: MetadataObject,
131
157
  explore: FeatureProgrammer.IExplore,
132
- tags: IMetadataTag[],
133
- ): ts.Block => {
134
- project;
135
- config;
136
- importer;
137
- input;
138
- entry;
139
- explore;
140
- tags;
141
- return ts.factory.createBlock([], true);
158
+ ): ts.ArrowFunction => {
159
+ let index: number = 1;
160
+ const body: ts.Statement[] = obj.properties
161
+ .map((p) => {
162
+ const block = decode(project)(importer)(index)(
163
+ IdentifierFactory.access(input)(
164
+ p.key.getSoleLiteral()!,
165
+ ),
166
+ p.value,
167
+ explore,
168
+ p.tags,
169
+ );
170
+ index += p.value.binarySize();
171
+ return [
172
+ ts.factory.createExpressionStatement(
173
+ ts.factory.createIdentifier(
174
+ `// property "${p.key.getSoleLiteral()!}"`,
175
+ ),
176
+ ),
177
+ ...block.statements,
178
+ ];
179
+ })
180
+ .flat();
181
+
182
+ return ts.factory.createArrowFunction(
183
+ undefined,
184
+ undefined,
185
+ [IdentifierFactory.parameter("input")],
186
+ TypeFactory.keyword("any"),
187
+ undefined,
188
+ ts.factory.createBlock(body, true),
189
+ );
142
190
  };
143
191
 
144
192
  /* -----------------------------------------------------------
@@ -146,7 +194,6 @@ export namespace ProtobufEncodeProgrammer {
146
194
  ----------------------------------------------------------- */
147
195
  const decode =
148
196
  (project: IProject) =>
149
- (config: FeatureProgrammer.IConfig) =>
150
197
  (importer: FunctionImporter) =>
151
198
  (index: number | null) =>
152
199
  (
@@ -155,6 +202,61 @@ export namespace ProtobufEncodeProgrammer {
155
202
  explore: FeatureProgrammer.IExplore,
156
203
  tags: IMetadataTag[],
157
204
  ): ts.Block => {
205
+ const wrapper: (block: ts.Block) => ts.Block =
206
+ meta.isRequired() && meta.nullable === false
207
+ ? (block) => block
208
+ : meta.isRequired() === false && meta.nullable === false
209
+ ? (block) =>
210
+ ts.factory.createBlock(
211
+ [
212
+ ts.factory.createIfStatement(
213
+ ts.factory.createLogicalAnd(
214
+ ts.factory.createInequality(
215
+ ts.factory.createIdentifier(
216
+ "undefined",
217
+ ),
218
+ input,
219
+ ),
220
+ ts.factory.createInequality(
221
+ ts.factory.createNull(),
222
+ input,
223
+ ),
224
+ ),
225
+ block,
226
+ ),
227
+ ],
228
+ true,
229
+ )
230
+ : meta.isRequired() === false
231
+ ? (block) =>
232
+ ts.factory.createBlock(
233
+ [
234
+ ts.factory.createIfStatement(
235
+ ts.factory.createInequality(
236
+ ts.factory.createIdentifier(
237
+ "undefined",
238
+ ),
239
+ input,
240
+ ),
241
+ block,
242
+ ),
243
+ ],
244
+ true,
245
+ )
246
+ : (block) =>
247
+ ts.factory.createBlock(
248
+ [
249
+ ts.factory.createIfStatement(
250
+ ts.factory.createInequality(
251
+ ts.factory.createNull(),
252
+ input,
253
+ ),
254
+ block,
255
+ ),
256
+ ],
257
+ true,
258
+ );
259
+
158
260
  // STARTS FROM ATOMIC TYPES
159
261
  const unions: IUnion[] = ProtobufUtil.atomics(meta).map((type) => ({
160
262
  type,
@@ -164,134 +266,330 @@ export namespace ProtobufEncodeProgrammer {
164
266
  ts.factory.createStringLiteral(type),
165
267
  ts.factory.createTypeOfExpression(input),
166
268
  ),
167
- value: () => decode_atomic(input, type, tags),
269
+ value: () => decode_atomic(index!)(input, type, tags),
168
270
  }));
169
271
 
170
- project;
171
- config;
172
- importer;
173
- index;
174
- explore;
175
- unions;
272
+ // CONSIDER BYTES
273
+ if (meta.natives.length)
274
+ unions.push({
275
+ type: "bytes",
276
+ is: () =>
277
+ ExpressionFactory.isInstanceOf("Uint8Array")(input),
278
+ value: () => decode_bytes("bytes")(index!)(input),
279
+ });
280
+
281
+ // CONSIDER MAPS
282
+ if (meta.maps.length)
283
+ unions.push({
284
+ type: "map",
285
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
286
+ value: () =>
287
+ decode_map(project)(importer)(index!)(
288
+ input,
289
+ meta.maps[0]!,
290
+ {
291
+ ...explore,
292
+ from: "array",
293
+ },
294
+ tags,
295
+ ),
296
+ });
176
297
 
177
298
  // CONSIDER ARRAYS
299
+ if (meta.arrays.length)
300
+ unions.push({
301
+ type: "array",
302
+ is: () => ExpressionFactory.isArray(input),
303
+ value: () =>
304
+ explore_arrays(project)(importer)(index!)(
305
+ input,
306
+ meta.arrays,
307
+ {
308
+ ...explore,
309
+ from: "array",
310
+ },
311
+ tags,
312
+ ),
313
+ });
178
314
 
179
315
  // CONSIDER OBJECTS
316
+ if (meta.objects.length)
317
+ unions.push({
318
+ type: "object",
319
+ is: () =>
320
+ ExpressionFactory.isObject({
321
+ checkNull: true,
322
+ checkArray: false,
323
+ })(input),
324
+ value: () =>
325
+ explore_objects(project)(index)(importer)(
326
+ input,
327
+ meta,
328
+ {
329
+ ...explore,
330
+ from: "object",
331
+ },
332
+ tags,
333
+ ),
334
+ });
180
335
 
181
- // CONSIDER MAPS
182
-
183
- return ts.factory.createBlock([]);
336
+ // RETURNS
337
+ if (unions.length === 1) return wrapper(unions[0]!.value(index));
338
+ else
339
+ return wrapper(
340
+ iterate(importer)(index)(unions)(meta.getName())(input),
341
+ );
184
342
  };
185
343
 
186
- const decode_object =
344
+ const iterate =
345
+ (importer: FunctionImporter) =>
346
+ (index: number | null) =>
347
+ (unions: IUnion[]) =>
348
+ (expected: string) =>
349
+ (input: ts.Expression) =>
350
+ ts.factory.createBlock(
351
+ [
352
+ ...unions.map((u) =>
353
+ ts.factory.createIfStatement(u.is(), u.value(index)),
354
+ ),
355
+ create_throw_error(importer)(expected)(input),
356
+ ],
357
+ true,
358
+ );
359
+
360
+ const decode_map =
187
361
  (project: IProject) =>
188
- (config: FeatureProgrammer.IConfig) =>
189
362
  (importer: FunctionImporter) =>
363
+ (index: number) =>
190
364
  (
191
365
  input: ts.Expression,
192
- object: MetadataObject,
366
+ map: Metadata.Entry,
193
367
  explore: FeatureProgrammer.IExplore,
368
+ tags: IMetadataTag[],
194
369
  ): ts.Block => {
195
- project;
196
- config;
197
- importer;
198
- input;
199
- object;
200
- explore;
201
-
370
+ const each: ts.Statement[] = [
371
+ ts.factory.createExpressionStatement(
372
+ decode_tag(ProtobufWire.LEN)(index),
373
+ ),
374
+ ts.factory.createExpressionStatement(
375
+ ts.factory.createCallExpression(
376
+ IdentifierFactory.access(WRITER())("fork"),
377
+ undefined,
378
+ undefined,
379
+ ),
380
+ ),
381
+ ...decode(project)(importer)(1)(
382
+ ts.factory.createIdentifier("key"),
383
+ map.key,
384
+ explore,
385
+ [],
386
+ ).statements,
387
+ ...decode(project)(importer)(2)(
388
+ ts.factory.createIdentifier("value"),
389
+ map.value,
390
+ explore,
391
+ tags,
392
+ ).statements,
393
+ ts.factory.createExpressionStatement(
394
+ ts.factory.createCallExpression(
395
+ IdentifierFactory.access(WRITER())("ldelim"),
396
+ undefined,
397
+ undefined,
398
+ ),
399
+ ),
400
+ ];
202
401
  return ts.factory.createBlock(
203
- [].map((exp) => ts.factory.createExpressionStatement(exp)),
402
+ [
403
+ ts.factory.createForOfStatement(
404
+ undefined,
405
+ StatementFactory.entry("key")("value"),
406
+ input,
407
+ ts.factory.createBlock(each),
408
+ ),
409
+ ],
204
410
  true,
205
411
  );
206
412
  };
207
413
 
208
- const decode_array =
414
+ const decode_object =
209
415
  (project: IProject) =>
210
- (config: FeatureProgrammer.IConfig) =>
211
416
  (importer: FunctionImporter) =>
212
- (index: number) =>
417
+ (index: number | null) =>
213
418
  (
214
419
  input: ts.Expression,
215
- array: MetadataArray,
420
+ object: MetadataObject,
216
421
  explore: FeatureProgrammer.IExplore,
217
422
  tags: IMetadataTag[],
218
- ): ts.Block =>
219
- ts.factory.createBlock(
423
+ ): ts.Block => {
424
+ const top: MetadataProperty = object.properties[0]!;
425
+ if (top.key.isSoleLiteral() === false)
426
+ return decode_map(project)(importer)(index!)(
427
+ input,
428
+ top,
429
+ explore,
430
+ tags,
431
+ );
432
+ return ts.factory.createBlock(
220
433
  [
221
434
  ...(index !== null
222
435
  ? [
223
- ts.factory.createExpressionStatement(
224
- decode_tag(ProtobufWire.LEN)(index),
436
+ decode_tag(ProtobufWire.LEN)(index),
437
+ ts.factory.createCallExpression(
438
+ IdentifierFactory.access(WRITER())("fork"),
439
+ undefined,
440
+ undefined,
225
441
  ),
226
442
  ]
227
443
  : []),
228
- ts.factory.createForOfStatement(
229
- undefined,
230
- ts.factory.createVariableDeclarationList(
231
- [ts.factory.createVariableDeclaration("elem")],
232
- undefined,
233
- ),
234
- input,
235
- decode(project)(config)(importer)(index)(
236
- ts.factory.createIdentifier("elem"),
237
- array.value,
238
- explore,
239
- tags,
444
+ ts.factory.createCallExpression(
445
+ ts.factory.createIdentifier(
446
+ `${PREFIX}o${object.index}`,
240
447
  ),
448
+ [],
449
+ [input],
241
450
  ),
242
- ],
451
+ ...(index !== null
452
+ ? [
453
+ ts.factory.createCallExpression(
454
+ IdentifierFactory.access(WRITER())("ldelim"),
455
+ undefined,
456
+ undefined,
457
+ ),
458
+ ]
459
+ : []),
460
+ ].map((expr) => ts.factory.createExpressionStatement(expr)),
243
461
  true,
244
462
  );
463
+ };
245
464
 
246
- const decode_atomic = (
247
- input: ts.Expression,
248
- atomic: Atomic.Literal,
249
- tags: IMetadataTag[],
250
- ): ts.Block => {
251
- if (atomic === "string") return decode_bytes("string")(input);
252
-
253
- const out = (method: string): ts.Block =>
254
- ts.factory.createBlock(
255
- [
256
- ts.factory.createCallExpression(
257
- IdentifierFactory.access(WRITER())(method),
258
- undefined,
259
- [input],
465
+ const decode_array =
466
+ (project: IProject) =>
467
+ (importer: FunctionImporter) =>
468
+ (index: number) =>
469
+ (
470
+ input: ts.Expression,
471
+ array: MetadataArray,
472
+ explore: FeatureProgrammer.IExplore,
473
+ tags: IMetadataTag[],
474
+ ): ts.Block => {
475
+ const wire = get_standalone_wire(array.value, tags);
476
+ const forLoop = (index: number | null) =>
477
+ ts.factory.createForOfStatement(
478
+ undefined,
479
+ ts.factory.createVariableDeclarationList(
480
+ [ts.factory.createVariableDeclaration("elem")],
481
+ ts.NodeFlags.Const,
260
482
  ),
261
- ].map((exp) => ts.factory.createExpressionStatement(exp)),
262
- true,
483
+ input,
484
+ decode(project)(importer)(index)(
485
+ ts.factory.createIdentifier("elem"),
486
+ array.value,
487
+ explore,
488
+ tags,
489
+ ),
490
+ );
491
+ const length = (block: ts.Block) =>
492
+ ts.factory.createBlock(
493
+ [
494
+ ts.factory.createIfStatement(
495
+ ts.factory.createStrictInequality(
496
+ ts.factory.createNumericLiteral(0),
497
+ IdentifierFactory.access(input)("length"),
498
+ ),
499
+ block,
500
+ ),
501
+ ],
502
+ true,
503
+ );
504
+
505
+ if (wire === ProtobufWire.LEN)
506
+ return length(ts.factory.createBlock([forLoop(index)], true));
507
+ return length(
508
+ ts.factory.createBlock(
509
+ [
510
+ ts.factory.createExpressionStatement(
511
+ decode_tag(ProtobufWire.LEN)(index),
512
+ ),
513
+ ts.factory.createExpressionStatement(
514
+ ts.factory.createCallExpression(
515
+ IdentifierFactory.access(WRITER())("fork"),
516
+ undefined,
517
+ undefined,
518
+ ),
519
+ ),
520
+ forLoop(null),
521
+ ts.factory.createExpressionStatement(
522
+ ts.factory.createCallExpression(
523
+ IdentifierFactory.access(WRITER())("ldelim"),
524
+ undefined,
525
+ undefined,
526
+ ),
527
+ ),
528
+ ],
529
+ true,
530
+ ),
263
531
  );
264
- if (atomic === "boolean") return out("bool");
532
+ };
265
533
 
266
- const type: IMetadataTag.INumberType | undefined = tags.find(
267
- (tag) => tag.kind === "type",
268
- ) as IMetadataTag.INumberType | undefined;
269
- if (atomic === "bigint")
270
- return out(type?.value === "uint64" ? "uint64" : "int64");
271
- else if (type === undefined || type?.value === "double")
272
- return out("double");
273
- else if (type.value === "float") return out("float");
274
- return out(
275
- type.value === "uint32" || type.value === "uint"
276
- ? "uint32"
277
- : type.value === "int32" || type.value === "int"
278
- ? "int32"
279
- : type.value === "int64"
280
- ? "int64"
281
- : "uint64",
282
- );
283
- };
534
+ const decode_atomic =
535
+ (index: number | null) =>
536
+ (
537
+ input: ts.Expression,
538
+ atomic: Atomic.Literal,
539
+ tags: IMetadataTag[],
540
+ ): ts.Block => {
541
+ if (atomic === "string")
542
+ return decode_bytes("string")(index!)(input);
543
+
544
+ const out =
545
+ (wire: ProtobufWire) =>
546
+ (method: string): ts.Block =>
547
+ ts.factory.createBlock(
548
+ [
549
+ ...(index !== null
550
+ ? [decode_tag(wire)(index)]
551
+ : []),
552
+ ts.factory.createCallExpression(
553
+ IdentifierFactory.access(WRITER())(method),
554
+ undefined,
555
+ [input],
556
+ ),
557
+ ].map((exp) =>
558
+ ts.factory.createExpressionStatement(exp),
559
+ ),
560
+ true,
561
+ );
562
+ if (atomic === "boolean") return out(ProtobufWire.VARIANT)("bool");
563
+
564
+ const type: IMetadataTag.INumberType | undefined = tags.find(
565
+ (tag) => tag.kind === "type",
566
+ ) as IMetadataTag.INumberType | undefined;
567
+ if (atomic === "bigint")
568
+ return out(ProtobufWire.VARIANT)(
569
+ type?.value === "uint64" ? "uint64" : "int64",
570
+ );
571
+ else if (type === undefined || type?.value === "double")
572
+ return out(ProtobufWire.I64)("double");
573
+ else if (type.value === "float")
574
+ return out(ProtobufWire.I32)("float");
575
+ return out(ProtobufWire.VARIANT)(
576
+ type.value === "uint32" || type.value === "uint"
577
+ ? "uint32"
578
+ : type.value === "int32" || type.value === "int"
579
+ ? "int32"
580
+ : type.value === "int64"
581
+ ? "int64"
582
+ : "uint64",
583
+ );
584
+ };
284
585
 
285
586
  const decode_bytes =
286
587
  (method: "bytes" | "string") =>
588
+ (index: number) =>
287
589
  (input: ts.Expression): ts.Block =>
288
590
  ts.factory.createBlock(
289
591
  [
290
- ts.factory.createCallExpression(
291
- IdentifierFactory.access(WRITER())("uint32"),
292
- undefined,
293
- [IdentifierFactory.access(input)("length")],
294
- ),
592
+ decode_tag(ProtobufWire.LEN)(index),
295
593
  ts.factory.createCallExpression(
296
594
  IdentifierFactory.access(WRITER())(method),
297
595
  undefined,
@@ -310,37 +608,67 @@ export namespace ProtobufEncodeProgrammer {
310
608
  [ts.factory.createNumericLiteral((index << 3) | wire)],
311
609
  );
312
610
 
611
+ const get_standalone_wire = (
612
+ meta: Metadata,
613
+ tags: IMetadataTag[],
614
+ ): ProtobufWire => {
615
+ if (
616
+ meta.arrays.length ||
617
+ meta.objects.length ||
618
+ meta.maps.length ||
619
+ meta.natives.length
620
+ )
621
+ return ProtobufWire.LEN;
622
+
623
+ const v = ProtobufUtil.atomics(meta)[0]!;
624
+ if (v === "string") return ProtobufWire.LEN;
625
+ else if (v === "boolean" || v === "bigint") return ProtobufWire.VARIANT;
626
+
627
+ const type: IMetadataTag.INumberType | undefined = tags.find(
628
+ (t) => t.kind === "type",
629
+ ) as IMetadataTag.INumberType | undefined;
630
+ if (type === undefined) return ProtobufWire.I64;
631
+ else if (type.value === "float") return ProtobufWire.I32;
632
+ return ProtobufWire.VARIANT;
633
+ };
634
+
313
635
  /* -----------------------------------------------------------
314
636
  EXPLORERS
315
637
  ----------------------------------------------------------- */
316
638
  const explore_objects =
317
639
  (project: IProject) =>
318
- (config: FeatureProgrammer.IConfig) =>
640
+ (index: number | null) =>
319
641
  (importer: FunctionImporter) =>
320
642
  (
321
643
  input: ts.Expression,
322
644
  meta: Metadata,
323
645
  explore: FeatureProgrammer.IExplore,
324
- ) =>
646
+ tags: IMetadataTag[],
647
+ ): ts.Block =>
325
648
  meta.objects.length === 1
326
- ? decode_object(project)(config)(importer)(
649
+ ? decode_object(project)(importer)(index)(
327
650
  input,
328
651
  meta.objects[0]!,
329
652
  explore,
653
+ tags,
330
654
  )
331
- : ts.factory.createCallExpression(
332
- ts.factory.createIdentifier(
333
- importer.useLocal(
334
- `${config.prefix}u${meta.union_index!}`,
655
+ : StatementFactory.block(
656
+ ts.factory.createCallExpression(
657
+ ts.factory.createIdentifier(
658
+ importer.useLocal(
659
+ `${PREFIX}u${meta.union_index!}`,
660
+ ),
335
661
  ),
662
+ undefined,
663
+ FeatureProgrammer.argumentsArray({
664
+ trace: false,
665
+ path: false,
666
+ })(explore)(input),
336
667
  ),
337
- undefined,
338
- FeatureProgrammer.argumentsArray(config)(explore)(input),
339
668
  );
340
669
 
341
670
  const explore_arrays =
342
671
  (project: IProject) =>
343
- (config: FeatureProgrammer.IConfig) =>
344
672
  (importer: FunctionImporter) =>
345
673
  (index: number) =>
346
674
  (
@@ -348,37 +676,43 @@ export namespace ProtobufEncodeProgrammer {
348
676
  elements: MetadataArray[],
349
677
  explore: FeatureProgrammer.IExplore,
350
678
  tags: IMetadataTag[],
351
- ): ts.Expression =>
352
- explore_array_like_union_types(config)(importer)(
353
- UnionExplorer.array({
354
- checker: IsProgrammer.decode(project)(importer),
355
- decoder: (input, target, explore, tags) =>
356
- ts.factory.createCallExpression(
357
- ts.factory.createArrowFunction(
358
- undefined,
359
- undefined,
360
- [],
361
- undefined,
362
- undefined,
363
- decode_array(project)(config)(importer)(index)(
364
- input,
365
- target,
366
- explore,
367
- tags,
368
- ),
369
- ),
370
- undefined,
371
- undefined,
372
- ),
373
- empty: ts.factory.createStringLiteral("[]"),
374
- success: ts.factory.createTrue(),
375
- failure: (input, expected) =>
376
- create_throw_error(importer)(expected)(input),
377
- }),
378
- )(input, elements, explore, tags);
679
+ ): ts.Block =>
680
+ elements.length === 1
681
+ ? decode_array(project)(importer)(index)(
682
+ input,
683
+ elements[0]!,
684
+ explore,
685
+ tags,
686
+ )
687
+ : explore_array_like_union_types(importer)(
688
+ UnionExplorer.array({
689
+ checker: IsProgrammer.decode(project)(importer),
690
+ decoder: (input, target, explore, tags) =>
691
+ ts.factory.createCallExpression(
692
+ ts.factory.createArrowFunction(
693
+ undefined,
694
+ undefined,
695
+ [],
696
+ undefined,
697
+ undefined,
698
+ decode_array(project)(importer)(index)(
699
+ input,
700
+ target,
701
+ explore,
702
+ tags,
703
+ ),
704
+ ),
705
+ undefined,
706
+ undefined,
707
+ ),
708
+ empty: ts.factory.createStringLiteral("[]"),
709
+ success: ts.factory.createTrue(),
710
+ failure: (input, expected) =>
711
+ create_throw_error(importer)(expected)(input),
712
+ }),
713
+ )(input, elements, explore, tags);
379
714
 
380
715
  const explore_array_like_union_types =
381
- (config: FeatureProgrammer.IConfig) =>
382
716
  (importer: FunctionImporter) =>
383
717
  <T extends MetadataArray | MetadataTuple>(
384
718
  factory: (
@@ -396,17 +730,19 @@ export namespace ProtobufEncodeProgrammer {
396
730
  elements: T[],
397
731
  explore: FeatureProgrammer.IExplore,
398
732
  tags: IMetadataTag[],
399
- ): ts.Expression => {
733
+ ): ts.Block => {
400
734
  const arrow =
401
735
  (parameters: ts.ParameterDeclaration[]) =>
402
736
  (explore: FeatureProgrammer.IExplore) =>
403
737
  (input: ts.Expression): ts.ArrowFunction =>
404
738
  factory(parameters)(input, elements, explore, tags, []);
405
739
  if (elements.every((e) => e.recursive === false))
406
- ts.factory.createCallExpression(
407
- arrow([])(explore)(input),
408
- undefined,
409
- [],
740
+ return StatementFactory.block(
741
+ ts.factory.createCallExpression(
742
+ arrow([])(explore)(input),
743
+ undefined,
744
+ [],
745
+ ),
410
746
  );
411
747
 
412
748
  explore = {
@@ -414,31 +750,39 @@ export namespace ProtobufEncodeProgrammer {
414
750
  source: "function",
415
751
  from: "array",
416
752
  };
417
- return ts.factory.createCallExpression(
418
- ts.factory.createIdentifier(
419
- importer.emplaceUnion(
420
- config.prefix,
421
- elements.map((e) => e.name).join(" | "),
422
- () =>
423
- arrow(
424
- FeatureProgrammer.parameterDeclarations(config)(
425
- TypeFactory.keyword("any"),
426
- )(ts.factory.createIdentifier("input")),
427
- )({
428
- ...explore,
429
- postfix: "",
430
- })(ts.factory.createIdentifier("input")),
753
+ return StatementFactory.block(
754
+ ts.factory.createCallExpression(
755
+ ts.factory.createIdentifier(
756
+ importer.emplaceUnion(
757
+ PREFIX,
758
+ elements.map((e) => e.name).join(" | "),
759
+ () =>
760
+ arrow(
761
+ FeatureProgrammer.parameterDeclarations(
762
+ CONFIG,
763
+ )(TypeFactory.keyword("any"))(
764
+ ts.factory.createIdentifier("input"),
765
+ ),
766
+ )({
767
+ ...explore,
768
+ postfix: "",
769
+ })(ts.factory.createIdentifier("input")),
770
+ ),
431
771
  ),
772
+ undefined,
773
+ FeatureProgrammer.argumentsArray(CONFIG)(explore)(input),
432
774
  ),
433
- undefined,
434
- FeatureProgrammer.argumentsArray(config)(explore)(input),
435
775
  );
436
776
  };
437
777
 
438
778
  /* -----------------------------------------------------------
439
779
  CONFIGURATIONS
440
780
  ----------------------------------------------------------- */
441
- // const PREFIX = "$pe";
781
+ const PREFIX = "$pe";
782
+ const CONFIG = {
783
+ path: false,
784
+ trace: false,
785
+ };
442
786
 
443
787
  const create_throw_error =
444
788
  (importer: FunctionImporter) =>
@@ -471,7 +815,6 @@ const WRITER = () => ts.factory.createIdentifier("writer");
471
815
 
472
816
  interface IUnion {
473
817
  type: string;
474
- wire: () => ProtobufWire;
475
818
  is: () => ts.Expression;
476
819
  value: (index: number | null) => ts.Block;
477
820
  }