typia 5.3.0-dev.20231120 → 5.3.0-dev.20231122

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.
@@ -1,607 +1,607 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../../factories/MetadataCollection";
6
- import { MetadataFactory } from "../../factories/MetadataFactory";
7
- import { ProtobufFactory } from "../../factories/ProtobufFactory";
8
- import { StatementFactory } from "../../factories/StatementFactory";
9
- import { TypeFactory } from "../../factories/TypeFactory";
10
-
11
- import { Metadata } from "../../schemas/metadata/Metadata";
12
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
13
- import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
16
-
17
- import { IProject } from "../../transformers/IProject";
18
-
19
- import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
20
-
21
- import { FunctionImporter } from "../helpers/FunctionImporeter";
22
- import { ProtobufUtil } from "../helpers/ProtobufUtil";
23
-
24
- export namespace ProtobufDecodeProgrammer {
25
- export const write =
26
- (project: IProject) =>
27
- (modulo: ts.LeftHandSideExpression) =>
28
- (type: ts.Type, name?: string): ts.ArrowFunction => {
29
- const importer: FunctionImporter = new FunctionImporter(modulo.getText());
30
- const collection: MetadataCollection = new MetadataCollection();
31
- const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
32
- project.checker,
33
- project.context,
34
- )(collection)(type);
35
-
36
- const functors = collection
37
- .objects()
38
- .filter((obj) => ProtobufUtil.isStaticObject(obj))
39
- .map((obj) =>
40
- StatementFactory.constant(
41
- `${PREFIX}o${obj.index}`,
42
- write_object_function(project)(importer)(obj),
43
- ),
44
- );
45
- const reader = StatementFactory.constant(
46
- "reader",
47
- ts.factory.createNewExpression(importer.use("Reader"), undefined, [
48
- ts.factory.createIdentifier("input"),
49
- ]),
50
- );
51
-
52
- return ts.factory.createArrowFunction(
53
- undefined,
54
- undefined,
55
- [
56
- IdentifierFactory.parameter(
57
- "input",
58
- ts.factory.createTypeReferenceNode("Uint8Array"),
59
- ),
60
- ],
61
- ts.factory.createTypeReferenceNode(
62
- `typia.Resolved<${
63
- name ?? TypeFactory.getFullName(project.checker)(type)
64
- }>`,
65
- ),
66
- undefined,
67
- ts.factory.createBlock(
68
- [
69
- ...importer.declare(modulo),
70
- ...functors,
71
- reader,
72
- ts.factory.createReturnStatement(
73
- decode_regular_object(true)(meta.objects[0]!),
74
- ),
75
- ],
76
- true,
77
- ),
78
- );
79
- };
80
-
81
- const write_object_function =
82
- (project: IProject) =>
83
- (importer: FunctionImporter) =>
84
- (obj: MetadataObject): ts.ArrowFunction =>
85
- ts.factory.createArrowFunction(
86
- undefined,
87
- undefined,
88
- [
89
- IdentifierFactory.parameter("reader"),
90
- IdentifierFactory.parameter(
91
- "length",
92
- TypeFactory.keyword("number"),
93
- ExpressionFactory.number(-1),
94
- ),
95
- ],
96
- TypeFactory.keyword("any"),
97
- undefined,
98
- ts.factory.createBlock(
99
- [
100
- ts.factory.createExpressionStatement(
101
- ts.factory.createBinaryExpression(
102
- ts.factory.createIdentifier("length"),
103
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
104
- ts.factory.createConditionalExpression(
105
- ts.factory.createLessThan(
106
- ts.factory.createIdentifier("length"),
107
- ExpressionFactory.number(0),
108
- ),
109
- undefined,
110
- ts.factory.createCallExpression(
111
- IdentifierFactory.access(READER())("size"),
112
- undefined,
113
- undefined,
114
- ),
115
- undefined,
116
- ts.factory.createAdd(
117
- ts.factory.createCallExpression(
118
- IdentifierFactory.access(READER())("index"),
119
- undefined,
120
- undefined,
121
- ),
122
- ts.factory.createIdentifier("length"),
123
- ),
124
- ),
125
- ),
126
- ),
127
- ...write_object_function_body(project)(importer)({
128
- condition: ts.factory.createLessThan(
129
- ts.factory.createCallExpression(
130
- IdentifierFactory.access(READER())("index"),
131
- undefined,
132
- undefined,
133
- ),
134
- ts.factory.createIdentifier("length"),
135
- ),
136
- tag: "tag",
137
- output: "output",
138
- })(obj.properties),
139
- ts.factory.createReturnStatement(
140
- ts.factory.createIdentifier("output"),
141
- ),
142
- ],
143
- true,
144
- ),
145
- );
146
-
147
- const write_object_function_body =
148
- (project: IProject) =>
149
- (importer: FunctionImporter) =>
150
- (props: { condition: ts.Expression; tag: string; output: string }) =>
151
- (properties: MetadataProperty[]): ts.Statement[] => {
152
- let i: number = 1;
153
- const clauses: ts.CaseClause[] = properties
154
- .map((p) => {
155
- const clause = decode_property(project)(importer)(i)(
156
- IdentifierFactory.access(ts.factory.createIdentifier(props.output))(
157
- p.key.getSoleLiteral()!,
158
- ),
159
- p.value,
160
- );
161
- i += ProtobufUtil.size(p.value);
162
- return clause;
163
- })
164
- .flat();
165
- return [
166
- StatementFactory.constant(
167
- props.output,
168
- ts.factory.createAsExpression(
169
- ts.factory.createObjectLiteralExpression(
170
- properties
171
- .filter(
172
- (p) =>
173
- !(
174
- project.compilerOptions.exactOptionalPropertyTypes ===
175
- true && p.value.optional === true
176
- ),
177
- )
178
- .map((p) =>
179
- ts.factory.createPropertyAssignment(
180
- IdentifierFactory.identifier(p.key.getSoleLiteral()!),
181
- write_property_default_value(p.value),
182
- ),
183
- ),
184
- true,
185
- ),
186
- TypeFactory.keyword("any"),
187
- ),
188
- ),
189
- ts.factory.createWhileStatement(
190
- props.condition,
191
- ts.factory.createBlock([
192
- StatementFactory.constant(
193
- props.tag,
194
- ts.factory.createCallExpression(
195
- IdentifierFactory.access(READER())("uint32"),
196
- undefined,
197
- undefined,
198
- ),
199
- ),
200
- ts.factory.createSwitchStatement(
201
- ts.factory.createUnsignedRightShift(
202
- ts.factory.createIdentifier(props.tag),
203
- ExpressionFactory.number(3),
204
- ),
205
- ts.factory.createCaseBlock([
206
- ...clauses,
207
- ts.factory.createDefaultClause([
208
- ts.factory.createExpressionStatement(
209
- ts.factory.createCallExpression(
210
- IdentifierFactory.access(READER())("skipType"),
211
- undefined,
212
- [
213
- ts.factory.createBitwiseAnd(
214
- ts.factory.createIdentifier(props.tag),
215
- ExpressionFactory.number(7),
216
- ),
217
- ],
218
- ),
219
- ),
220
- ts.factory.createBreakStatement(),
221
- ]),
222
- ]),
223
- ),
224
- ]),
225
- ),
226
- ];
227
- };
228
-
229
- const write_property_default_value = (value: Metadata) =>
230
- ts.factory.createAsExpression(
231
- value.nullable
232
- ? ts.factory.createNull()
233
- : value.isRequired() === false
234
- ? ts.factory.createIdentifier("undefined")
235
- : value.arrays.length
236
- ? ts.factory.createArrayLiteralExpression()
237
- : value.maps.length
238
- ? ts.factory.createNewExpression(
239
- ts.factory.createIdentifier("Map"),
240
- undefined,
241
- [],
242
- )
243
- : value.natives.length
244
- ? ts.factory.createNewExpression(
245
- ts.factory.createIdentifier("Uint8Array"),
246
- undefined,
247
- [],
248
- )
249
- : value.atomics.some((a) => a.type === "string") ||
250
- value.constants.some(
251
- (c) => c.type === "string" && c.values.some((v) => v === ""),
252
- ) ||
253
- value.templates.some(
254
- (tpl) => tpl.length === 1 && tpl[0]!.getName() === "string",
255
- )
256
- ? ts.factory.createStringLiteral("")
257
- : value.objects.length &&
258
- value.objects.some((obj) => !ProtobufUtil.isStaticObject(obj))
259
- ? ts.factory.createObjectLiteralExpression()
260
- : ts.factory.createIdentifier("undefined"),
261
- TypeFactory.keyword("any"),
262
- );
263
-
264
- /* -----------------------------------------------------------
265
- DECODERS
266
- ----------------------------------------------------------- */
267
- const decode_property =
268
- (project: IProject) =>
269
- (importer: FunctionImporter) =>
270
- (index: number) =>
271
- (
272
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
273
- meta: Metadata,
274
- ): ts.CaseClause[] => {
275
- const clauses: ts.CaseClause[] = [];
276
- const emplace = (name: string) => (v: ts.Expression | ts.Statement[]) =>
277
- clauses.push(
278
- ts.factory.createCaseClause(
279
- ExpressionFactory.number(index++),
280
- Array.isArray(v)
281
- ? [
282
- ts.factory.createExpressionStatement(
283
- ts.factory.createIdentifier(`// type: ${name}`),
284
- ),
285
- ...v,
286
- ts.factory.createBreakStatement(),
287
- ]
288
- : [
289
- ts.factory.createExpressionStatement(
290
- ts.factory.createIdentifier(`// ${name}`),
291
- ),
292
- ts.factory.createExpressionStatement(
293
- ts.factory.createBinaryExpression(
294
- accessor,
295
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
296
- v,
297
- ),
298
- ),
299
- ts.factory.createBreakStatement(),
300
- ],
301
- ),
302
- );
303
-
304
- const required: boolean = meta.isRequired() && !meta.nullable;
305
- for (const atomic of ProtobufUtil.getAtomics(meta))
306
- emplace(atomic)(decode_atomic(meta)(atomic));
307
- if (meta.natives.length) emplace("bytes")(decode_bytes("bytes"));
308
- for (const array of meta.arrays)
309
- emplace(`Array<${array.type.value.getName()}>`)(
310
- decode_array(accessor, array, required),
311
- );
312
- for (const map of meta.maps)
313
- emplace(`Map<string, ${map.value.getName()}>`)(
314
- decode_map(project)(importer)(accessor, map, required),
315
- );
316
- for (const obj of meta.objects)
317
- emplace(obj.name)(
318
- ProtobufUtil.isStaticObject(obj)
319
- ? decode_regular_object(false)(obj)
320
- : decode_dynamic_object(project)(importer)(accessor, obj, required),
321
- );
322
- return clauses;
323
- };
324
-
325
- const decode_atomic =
326
- (meta: Metadata) =>
327
- (atomic: ProtobufAtomic): ts.Expression => {
328
- if (atomic === "string") return decode_bytes("string");
329
-
330
- const call: ts.CallExpression = ts.factory.createCallExpression(
331
- IdentifierFactory.access(ts.factory.createIdentifier("reader"))(atomic),
332
- undefined,
333
- undefined,
334
- );
335
- if (atomic !== "int64" && atomic !== "uint64") return call;
336
-
337
- const isNumber: boolean = ProtobufUtil.getNumbers(meta).some(
338
- (n) => n === atomic,
339
- );
340
- return isNumber
341
- ? ts.factory.createCallExpression(
342
- ts.factory.createIdentifier("Number"),
343
- undefined,
344
- [call],
345
- )
346
- : call;
347
- };
348
-
349
- const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
350
- ts.factory.createCallExpression(
351
- IdentifierFactory.access(ts.factory.createIdentifier("reader"))(method),
352
- undefined,
353
- undefined,
354
- );
355
-
356
- const decode_array = (
357
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
358
- array: MetadataArray,
359
- required: boolean,
360
- ): ts.Statement[] => {
361
- const statements: Array<ts.Expression | ts.Statement> = [];
362
- if (required === false)
363
- statements.push(
364
- ts.factory.createBinaryExpression(
365
- accessor,
366
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
367
- ts.factory.createAsExpression(
368
- ts.factory.createArrayLiteralExpression(),
369
- ts.factory.createTypeReferenceNode("any[]"),
370
- ),
371
- ),
372
- );
373
- const atomics = ProtobufUtil.getAtomics(array.type.value);
374
- const decoder = atomics.length
375
- ? () => decode_atomic(array.type.value)(atomics[0]!)
376
- : array.type.value.natives.length
377
- ? () => decode_bytes("bytes")
378
- : array.type.value.objects.length
379
- ? () => decode_regular_object(false)(array.type.value.objects[0]!)
380
- : null;
381
- if (decoder === null) throw new Error("Never reach here.");
382
- else if (atomics.length && atomics[0] !== "string") {
383
- statements.push(
384
- ts.factory.createIfStatement(
385
- ts.factory.createStrictEquality(
386
- ExpressionFactory.number(2),
387
- ts.factory.createBitwiseAnd(
388
- ts.factory.createIdentifier("tag"),
389
- ExpressionFactory.number(7),
390
- ),
391
- ),
392
- ts.factory.createBlock(
393
- [
394
- StatementFactory.constant(
395
- "piece",
396
- ts.factory.createAdd(
397
- ts.factory.createCallExpression(
398
- IdentifierFactory.access(READER())("uint32"),
399
- undefined,
400
- undefined,
401
- ),
402
- ts.factory.createCallExpression(
403
- IdentifierFactory.access(READER())("index"),
404
- undefined,
405
- undefined,
406
- ),
407
- ),
408
- ),
409
- ts.factory.createWhileStatement(
410
- ts.factory.createLessThan(
411
- ts.factory.createCallExpression(
412
- IdentifierFactory.access(READER())("index"),
413
- undefined,
414
- undefined,
415
- ),
416
- ts.factory.createIdentifier("piece"),
417
- ),
418
- ts.factory.createExpressionStatement(
419
- ts.factory.createCallExpression(
420
- IdentifierFactory.access(accessor)("push"),
421
- undefined,
422
- [decoder()],
423
- ),
424
- ),
425
- ),
426
- ],
427
- true,
428
- ),
429
- ts.factory.createExpressionStatement(
430
- ts.factory.createCallExpression(
431
- IdentifierFactory.access(accessor)("push"),
432
- undefined,
433
- [decoder()],
434
- ),
435
- ),
436
- ),
437
- );
438
- } else
439
- statements.push(
440
- ts.factory.createCallExpression(
441
- IdentifierFactory.access(accessor)("push"),
442
- undefined,
443
- [decoder()],
444
- ),
445
- );
446
- return statements.map((stmt) =>
447
- ts.isExpression(stmt) ? ts.factory.createExpressionStatement(stmt) : stmt,
448
- );
449
- };
450
-
451
- const decode_regular_object =
452
- (top: boolean) =>
453
- (obj: MetadataObject): ts.Expression =>
454
- ts.factory.createCallExpression(
455
- ts.factory.createIdentifier(`${PREFIX}o${obj.index}`),
456
- undefined,
457
- [
458
- ts.factory.createIdentifier("reader"),
459
- ...(top
460
- ? []
461
- : [
462
- ts.factory.createCallExpression(
463
- IdentifierFactory.access(READER())("uint32"),
464
- undefined,
465
- undefined,
466
- ),
467
- ]),
468
- ],
469
- );
470
-
471
- const decode_dynamic_object =
472
- (project: IProject) =>
473
- (importer: FunctionImporter) =>
474
- (
475
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
476
- obj: MetadataObject,
477
- required: boolean,
478
- ): ts.Statement[] => {
479
- const top = obj.properties[0]!;
480
- return decode_entry(project)(importer)({
481
- initializer: () =>
482
- ts.factory.createBinaryExpression(
483
- accessor,
484
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
485
- ts.factory.createObjectLiteralExpression(),
486
- ),
487
- setter: () =>
488
- ts.factory.createBinaryExpression(
489
- ts.factory.createElementAccessExpression(
490
- accessor,
491
- ts.factory.createIdentifier("entry.key"),
492
- ),
493
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
494
- ts.factory.createIdentifier("entry.value"),
495
- ),
496
- })(
497
- MetadataProperty.create({
498
- ...top,
499
- key: (() => {
500
- const key: Metadata = Metadata.initialize();
501
- key.atomics.push(
502
- MetadataAtomic.create({
503
- type: "string",
504
- tags: [],
505
- }),
506
- );
507
- return key;
508
- })(),
509
- }),
510
- required,
511
- );
512
- };
513
-
514
- const decode_map =
515
- (project: IProject) =>
516
- (importer: FunctionImporter) =>
517
- (
518
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
519
- map: Metadata.Entry,
520
- required: boolean,
521
- ): ts.Statement[] =>
522
- decode_entry(project)(importer)({
523
- initializer: () =>
524
- ts.factory.createBinaryExpression(
525
- accessor,
526
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
527
- ts.factory.createNewExpression(
528
- ts.factory.createIdentifier("Map"),
529
- [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
530
- [],
531
- ),
532
- ),
533
- setter: () =>
534
- ts.factory.createCallExpression(
535
- IdentifierFactory.access(accessor)("set"),
536
- undefined,
537
- [
538
- ts.factory.createIdentifier("entry.key"),
539
- ts.factory.createIdentifier("entry.value"),
540
- ],
541
- ),
542
- })(map, required);
543
-
544
- const decode_entry =
545
- (project: IProject) =>
546
- (importer: FunctionImporter) =>
547
- (props: {
548
- initializer: () => ts.Expression;
549
- setter: () => ts.Expression;
550
- }) =>
551
- (map: Metadata.Entry, required: boolean): ts.Statement[] => {
552
- const statements: ts.Statement[] = [
553
- ...(required
554
- ? []
555
- : [ts.factory.createExpressionStatement(props.initializer())]),
556
- StatementFactory.constant(
557
- "piece",
558
- ts.factory.createAdd(
559
- ts.factory.createCallExpression(
560
- IdentifierFactory.access(READER())("uint32"),
561
- undefined,
562
- undefined,
563
- ),
564
- ts.factory.createCallExpression(
565
- IdentifierFactory.access(READER())("index"),
566
- undefined,
567
- undefined,
568
- ),
569
- ),
570
- ),
571
- ...write_object_function_body(project)(importer)({
572
- condition: ts.factory.createLessThan(
573
- ts.factory.createCallExpression(
574
- IdentifierFactory.access(READER())("index"),
575
- undefined,
576
- undefined,
577
- ),
578
- ts.factory.createIdentifier("piece"),
579
- ),
580
- tag: "kind",
581
- output: "entry",
582
- })([
583
- MetadataProperty.create({
584
- key: MetadataFactory.soleLiteral("key"),
585
- value: map.key,
586
- description: null,
587
- jsDocTags: [],
588
- }),
589
- MetadataProperty.create({
590
- key: MetadataFactory.soleLiteral("value"),
591
- value: map.value,
592
- description: null,
593
- jsDocTags: [],
594
- }),
595
- ]),
596
- ts.factory.createExpressionStatement(props.setter()),
597
- ];
598
- return [
599
- ts.factory.createExpressionStatement(
600
- ExpressionFactory.selfCall(ts.factory.createBlock(statements, true)),
601
- ),
602
- ];
603
- };
604
- }
605
-
606
- const PREFIX = "$pd";
607
- const READER = () => ts.factory.createIdentifier("reader");
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../../factories/MetadataFactory";
7
+ import { ProtobufFactory } from "../../factories/ProtobufFactory";
8
+ import { StatementFactory } from "../../factories/StatementFactory";
9
+ import { TypeFactory } from "../../factories/TypeFactory";
10
+
11
+ import { Metadata } from "../../schemas/metadata/Metadata";
12
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
13
+ import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
+ import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
16
+
17
+ import { IProject } from "../../transformers/IProject";
18
+
19
+ import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
20
+
21
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
22
+ import { ProtobufUtil } from "../helpers/ProtobufUtil";
23
+
24
+ export namespace ProtobufDecodeProgrammer {
25
+ export const write =
26
+ (project: IProject) =>
27
+ (modulo: ts.LeftHandSideExpression) =>
28
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
29
+ const importer: FunctionImporter = new FunctionImporter(modulo.getText());
30
+ const collection: MetadataCollection = new MetadataCollection();
31
+ const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
32
+ project.checker,
33
+ project.context,
34
+ )(collection)(type);
35
+
36
+ const functors = collection
37
+ .objects()
38
+ .filter((obj) => ProtobufUtil.isStaticObject(obj))
39
+ .map((obj) =>
40
+ StatementFactory.constant(
41
+ `${PREFIX}o${obj.index}`,
42
+ write_object_function(project)(importer)(obj),
43
+ ),
44
+ );
45
+ const reader = StatementFactory.constant(
46
+ "reader",
47
+ ts.factory.createNewExpression(importer.use("Reader"), undefined, [
48
+ ts.factory.createIdentifier("input"),
49
+ ]),
50
+ );
51
+
52
+ return ts.factory.createArrowFunction(
53
+ undefined,
54
+ undefined,
55
+ [
56
+ IdentifierFactory.parameter(
57
+ "input",
58
+ ts.factory.createTypeReferenceNode("Uint8Array"),
59
+ ),
60
+ ],
61
+ ts.factory.createTypeReferenceNode(
62
+ `typia.Resolved<${
63
+ name ?? TypeFactory.getFullName(project.checker)(type)
64
+ }>`,
65
+ ),
66
+ undefined,
67
+ ts.factory.createBlock(
68
+ [
69
+ ...importer.declare(modulo),
70
+ ...functors,
71
+ reader,
72
+ ts.factory.createReturnStatement(
73
+ decode_regular_object(true)(meta.objects[0]!),
74
+ ),
75
+ ],
76
+ true,
77
+ ),
78
+ );
79
+ };
80
+
81
+ const write_object_function =
82
+ (project: IProject) =>
83
+ (importer: FunctionImporter) =>
84
+ (obj: MetadataObject): ts.ArrowFunction =>
85
+ ts.factory.createArrowFunction(
86
+ undefined,
87
+ undefined,
88
+ [
89
+ IdentifierFactory.parameter("reader"),
90
+ IdentifierFactory.parameter(
91
+ "length",
92
+ TypeFactory.keyword("number"),
93
+ ExpressionFactory.number(-1),
94
+ ),
95
+ ],
96
+ TypeFactory.keyword("any"),
97
+ undefined,
98
+ ts.factory.createBlock(
99
+ [
100
+ ts.factory.createExpressionStatement(
101
+ ts.factory.createBinaryExpression(
102
+ ts.factory.createIdentifier("length"),
103
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
104
+ ts.factory.createConditionalExpression(
105
+ ts.factory.createLessThan(
106
+ ts.factory.createIdentifier("length"),
107
+ ExpressionFactory.number(0),
108
+ ),
109
+ undefined,
110
+ ts.factory.createCallExpression(
111
+ IdentifierFactory.access(READER())("size"),
112
+ undefined,
113
+ undefined,
114
+ ),
115
+ undefined,
116
+ ts.factory.createAdd(
117
+ ts.factory.createCallExpression(
118
+ IdentifierFactory.access(READER())("index"),
119
+ undefined,
120
+ undefined,
121
+ ),
122
+ ts.factory.createIdentifier("length"),
123
+ ),
124
+ ),
125
+ ),
126
+ ),
127
+ ...write_object_function_body(project)(importer)({
128
+ condition: ts.factory.createLessThan(
129
+ ts.factory.createCallExpression(
130
+ IdentifierFactory.access(READER())("index"),
131
+ undefined,
132
+ undefined,
133
+ ),
134
+ ts.factory.createIdentifier("length"),
135
+ ),
136
+ tag: "tag",
137
+ output: "output",
138
+ })(obj.properties),
139
+ ts.factory.createReturnStatement(
140
+ ts.factory.createIdentifier("output"),
141
+ ),
142
+ ],
143
+ true,
144
+ ),
145
+ );
146
+
147
+ const write_object_function_body =
148
+ (project: IProject) =>
149
+ (importer: FunctionImporter) =>
150
+ (props: { condition: ts.Expression; tag: string; output: string }) =>
151
+ (properties: MetadataProperty[]): ts.Statement[] => {
152
+ let i: number = 1;
153
+ const clauses: ts.CaseClause[] = properties
154
+ .map((p) => {
155
+ const clause = decode_property(project)(importer)(i)(
156
+ IdentifierFactory.access(ts.factory.createIdentifier(props.output))(
157
+ p.key.getSoleLiteral()!,
158
+ ),
159
+ p.value,
160
+ );
161
+ i += ProtobufUtil.size(p.value);
162
+ return clause;
163
+ })
164
+ .flat();
165
+ return [
166
+ StatementFactory.constant(
167
+ props.output,
168
+ ts.factory.createAsExpression(
169
+ ts.factory.createObjectLiteralExpression(
170
+ properties
171
+ .filter(
172
+ (p) =>
173
+ !(
174
+ project.compilerOptions.exactOptionalPropertyTypes ===
175
+ true && p.value.optional === true
176
+ ),
177
+ )
178
+ .map((p) =>
179
+ ts.factory.createPropertyAssignment(
180
+ IdentifierFactory.identifier(p.key.getSoleLiteral()!),
181
+ write_property_default_value(p.value),
182
+ ),
183
+ ),
184
+ true,
185
+ ),
186
+ TypeFactory.keyword("any"),
187
+ ),
188
+ ),
189
+ ts.factory.createWhileStatement(
190
+ props.condition,
191
+ ts.factory.createBlock([
192
+ StatementFactory.constant(
193
+ props.tag,
194
+ ts.factory.createCallExpression(
195
+ IdentifierFactory.access(READER())("uint32"),
196
+ undefined,
197
+ undefined,
198
+ ),
199
+ ),
200
+ ts.factory.createSwitchStatement(
201
+ ts.factory.createUnsignedRightShift(
202
+ ts.factory.createIdentifier(props.tag),
203
+ ExpressionFactory.number(3),
204
+ ),
205
+ ts.factory.createCaseBlock([
206
+ ...clauses,
207
+ ts.factory.createDefaultClause([
208
+ ts.factory.createExpressionStatement(
209
+ ts.factory.createCallExpression(
210
+ IdentifierFactory.access(READER())("skipType"),
211
+ undefined,
212
+ [
213
+ ts.factory.createBitwiseAnd(
214
+ ts.factory.createIdentifier(props.tag),
215
+ ExpressionFactory.number(7),
216
+ ),
217
+ ],
218
+ ),
219
+ ),
220
+ ts.factory.createBreakStatement(),
221
+ ]),
222
+ ]),
223
+ ),
224
+ ]),
225
+ ),
226
+ ];
227
+ };
228
+
229
+ const write_property_default_value = (value: Metadata) =>
230
+ ts.factory.createAsExpression(
231
+ value.nullable
232
+ ? ts.factory.createNull()
233
+ : value.isRequired() === false
234
+ ? ts.factory.createIdentifier("undefined")
235
+ : value.arrays.length
236
+ ? ts.factory.createArrayLiteralExpression()
237
+ : value.maps.length
238
+ ? ts.factory.createNewExpression(
239
+ ts.factory.createIdentifier("Map"),
240
+ undefined,
241
+ [],
242
+ )
243
+ : value.natives.length
244
+ ? ts.factory.createNewExpression(
245
+ ts.factory.createIdentifier("Uint8Array"),
246
+ undefined,
247
+ [],
248
+ )
249
+ : value.atomics.some((a) => a.type === "string") ||
250
+ value.constants.some(
251
+ (c) => c.type === "string" && c.values.some((v) => v === ""),
252
+ ) ||
253
+ value.templates.some(
254
+ (tpl) => tpl.length === 1 && tpl[0]!.getName() === "string",
255
+ )
256
+ ? ts.factory.createStringLiteral("")
257
+ : value.objects.length &&
258
+ value.objects.some((obj) => !ProtobufUtil.isStaticObject(obj))
259
+ ? ts.factory.createObjectLiteralExpression()
260
+ : ts.factory.createIdentifier("undefined"),
261
+ TypeFactory.keyword("any"),
262
+ );
263
+
264
+ /* -----------------------------------------------------------
265
+ DECODERS
266
+ ----------------------------------------------------------- */
267
+ const decode_property =
268
+ (project: IProject) =>
269
+ (importer: FunctionImporter) =>
270
+ (index: number) =>
271
+ (
272
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
273
+ meta: Metadata,
274
+ ): ts.CaseClause[] => {
275
+ const clauses: ts.CaseClause[] = [];
276
+ const emplace = (name: string) => (v: ts.Expression | ts.Statement[]) =>
277
+ clauses.push(
278
+ ts.factory.createCaseClause(
279
+ ExpressionFactory.number(index++),
280
+ Array.isArray(v)
281
+ ? [
282
+ ts.factory.createExpressionStatement(
283
+ ts.factory.createIdentifier(`// type: ${name}`),
284
+ ),
285
+ ...v,
286
+ ts.factory.createBreakStatement(),
287
+ ]
288
+ : [
289
+ ts.factory.createExpressionStatement(
290
+ ts.factory.createIdentifier(`// ${name}`),
291
+ ),
292
+ ts.factory.createExpressionStatement(
293
+ ts.factory.createBinaryExpression(
294
+ accessor,
295
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
296
+ v,
297
+ ),
298
+ ),
299
+ ts.factory.createBreakStatement(),
300
+ ],
301
+ ),
302
+ );
303
+
304
+ const required: boolean = meta.isRequired() && !meta.nullable;
305
+ for (const atomic of ProtobufUtil.getAtomics(meta))
306
+ emplace(atomic)(decode_atomic(meta)(atomic));
307
+ if (meta.natives.length) emplace("bytes")(decode_bytes("bytes"));
308
+ for (const array of meta.arrays)
309
+ emplace(`Array<${array.type.value.getName()}>`)(
310
+ decode_array(accessor, array, required),
311
+ );
312
+ for (const map of meta.maps)
313
+ emplace(`Map<string, ${map.value.getName()}>`)(
314
+ decode_map(project)(importer)(accessor, map, required),
315
+ );
316
+ for (const obj of meta.objects)
317
+ emplace(obj.name)(
318
+ ProtobufUtil.isStaticObject(obj)
319
+ ? decode_regular_object(false)(obj)
320
+ : decode_dynamic_object(project)(importer)(accessor, obj, required),
321
+ );
322
+ return clauses;
323
+ };
324
+
325
+ const decode_atomic =
326
+ (meta: Metadata) =>
327
+ (atomic: ProtobufAtomic): ts.Expression => {
328
+ if (atomic === "string") return decode_bytes("string");
329
+
330
+ const call: ts.CallExpression = ts.factory.createCallExpression(
331
+ IdentifierFactory.access(ts.factory.createIdentifier("reader"))(atomic),
332
+ undefined,
333
+ undefined,
334
+ );
335
+ if (atomic !== "int64" && atomic !== "uint64") return call;
336
+
337
+ const isNumber: boolean = ProtobufUtil.getNumbers(meta).some(
338
+ (n) => n === atomic,
339
+ );
340
+ return isNumber
341
+ ? ts.factory.createCallExpression(
342
+ ts.factory.createIdentifier("Number"),
343
+ undefined,
344
+ [call],
345
+ )
346
+ : call;
347
+ };
348
+
349
+ const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
350
+ ts.factory.createCallExpression(
351
+ IdentifierFactory.access(ts.factory.createIdentifier("reader"))(method),
352
+ undefined,
353
+ undefined,
354
+ );
355
+
356
+ const decode_array = (
357
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
358
+ array: MetadataArray,
359
+ required: boolean,
360
+ ): ts.Statement[] => {
361
+ const statements: Array<ts.Expression | ts.Statement> = [];
362
+ if (required === false)
363
+ statements.push(
364
+ ts.factory.createBinaryExpression(
365
+ accessor,
366
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
367
+ ts.factory.createAsExpression(
368
+ ts.factory.createArrayLiteralExpression(),
369
+ ts.factory.createTypeReferenceNode("any[]"),
370
+ ),
371
+ ),
372
+ );
373
+ const atomics = ProtobufUtil.getAtomics(array.type.value);
374
+ const decoder = atomics.length
375
+ ? () => decode_atomic(array.type.value)(atomics[0]!)
376
+ : array.type.value.natives.length
377
+ ? () => decode_bytes("bytes")
378
+ : array.type.value.objects.length
379
+ ? () => decode_regular_object(false)(array.type.value.objects[0]!)
380
+ : null;
381
+ if (decoder === null) throw new Error("Never reach here.");
382
+ else if (atomics.length && atomics[0] !== "string") {
383
+ statements.push(
384
+ ts.factory.createIfStatement(
385
+ ts.factory.createStrictEquality(
386
+ ExpressionFactory.number(2),
387
+ ts.factory.createBitwiseAnd(
388
+ ts.factory.createIdentifier("tag"),
389
+ ExpressionFactory.number(7),
390
+ ),
391
+ ),
392
+ ts.factory.createBlock(
393
+ [
394
+ StatementFactory.constant(
395
+ "piece",
396
+ ts.factory.createAdd(
397
+ ts.factory.createCallExpression(
398
+ IdentifierFactory.access(READER())("uint32"),
399
+ undefined,
400
+ undefined,
401
+ ),
402
+ ts.factory.createCallExpression(
403
+ IdentifierFactory.access(READER())("index"),
404
+ undefined,
405
+ undefined,
406
+ ),
407
+ ),
408
+ ),
409
+ ts.factory.createWhileStatement(
410
+ ts.factory.createLessThan(
411
+ ts.factory.createCallExpression(
412
+ IdentifierFactory.access(READER())("index"),
413
+ undefined,
414
+ undefined,
415
+ ),
416
+ ts.factory.createIdentifier("piece"),
417
+ ),
418
+ ts.factory.createExpressionStatement(
419
+ ts.factory.createCallExpression(
420
+ IdentifierFactory.access(accessor)("push"),
421
+ undefined,
422
+ [decoder()],
423
+ ),
424
+ ),
425
+ ),
426
+ ],
427
+ true,
428
+ ),
429
+ ts.factory.createExpressionStatement(
430
+ ts.factory.createCallExpression(
431
+ IdentifierFactory.access(accessor)("push"),
432
+ undefined,
433
+ [decoder()],
434
+ ),
435
+ ),
436
+ ),
437
+ );
438
+ } else
439
+ statements.push(
440
+ ts.factory.createCallExpression(
441
+ IdentifierFactory.access(accessor)("push"),
442
+ undefined,
443
+ [decoder()],
444
+ ),
445
+ );
446
+ return statements.map((stmt) =>
447
+ ts.isExpression(stmt) ? ts.factory.createExpressionStatement(stmt) : stmt,
448
+ );
449
+ };
450
+
451
+ const decode_regular_object =
452
+ (top: boolean) =>
453
+ (obj: MetadataObject): ts.Expression =>
454
+ ts.factory.createCallExpression(
455
+ ts.factory.createIdentifier(`${PREFIX}o${obj.index}`),
456
+ undefined,
457
+ [
458
+ ts.factory.createIdentifier("reader"),
459
+ ...(top
460
+ ? []
461
+ : [
462
+ ts.factory.createCallExpression(
463
+ IdentifierFactory.access(READER())("uint32"),
464
+ undefined,
465
+ undefined,
466
+ ),
467
+ ]),
468
+ ],
469
+ );
470
+
471
+ const decode_dynamic_object =
472
+ (project: IProject) =>
473
+ (importer: FunctionImporter) =>
474
+ (
475
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
476
+ obj: MetadataObject,
477
+ required: boolean,
478
+ ): ts.Statement[] => {
479
+ const top = obj.properties[0]!;
480
+ return decode_entry(project)(importer)({
481
+ initializer: () =>
482
+ ts.factory.createBinaryExpression(
483
+ accessor,
484
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
485
+ ts.factory.createObjectLiteralExpression(),
486
+ ),
487
+ setter: () =>
488
+ ts.factory.createBinaryExpression(
489
+ ts.factory.createElementAccessExpression(
490
+ accessor,
491
+ ts.factory.createIdentifier("entry.key"),
492
+ ),
493
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
494
+ ts.factory.createIdentifier("entry.value"),
495
+ ),
496
+ })(
497
+ MetadataProperty.create({
498
+ ...top,
499
+ key: (() => {
500
+ const key: Metadata = Metadata.initialize();
501
+ key.atomics.push(
502
+ MetadataAtomic.create({
503
+ type: "string",
504
+ tags: [],
505
+ }),
506
+ );
507
+ return key;
508
+ })(),
509
+ }),
510
+ required,
511
+ );
512
+ };
513
+
514
+ const decode_map =
515
+ (project: IProject) =>
516
+ (importer: FunctionImporter) =>
517
+ (
518
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
519
+ map: Metadata.Entry,
520
+ required: boolean,
521
+ ): ts.Statement[] =>
522
+ decode_entry(project)(importer)({
523
+ initializer: () =>
524
+ ts.factory.createBinaryExpression(
525
+ accessor,
526
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
527
+ ts.factory.createNewExpression(
528
+ ts.factory.createIdentifier("Map"),
529
+ [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
530
+ [],
531
+ ),
532
+ ),
533
+ setter: () =>
534
+ ts.factory.createCallExpression(
535
+ IdentifierFactory.access(accessor)("set"),
536
+ undefined,
537
+ [
538
+ ts.factory.createIdentifier("entry.key"),
539
+ ts.factory.createIdentifier("entry.value"),
540
+ ],
541
+ ),
542
+ })(map, required);
543
+
544
+ const decode_entry =
545
+ (project: IProject) =>
546
+ (importer: FunctionImporter) =>
547
+ (props: {
548
+ initializer: () => ts.Expression;
549
+ setter: () => ts.Expression;
550
+ }) =>
551
+ (map: Metadata.Entry, required: boolean): ts.Statement[] => {
552
+ const statements: ts.Statement[] = [
553
+ ...(required
554
+ ? []
555
+ : [ts.factory.createExpressionStatement(props.initializer())]),
556
+ StatementFactory.constant(
557
+ "piece",
558
+ ts.factory.createAdd(
559
+ ts.factory.createCallExpression(
560
+ IdentifierFactory.access(READER())("uint32"),
561
+ undefined,
562
+ undefined,
563
+ ),
564
+ ts.factory.createCallExpression(
565
+ IdentifierFactory.access(READER())("index"),
566
+ undefined,
567
+ undefined,
568
+ ),
569
+ ),
570
+ ),
571
+ ...write_object_function_body(project)(importer)({
572
+ condition: ts.factory.createLessThan(
573
+ ts.factory.createCallExpression(
574
+ IdentifierFactory.access(READER())("index"),
575
+ undefined,
576
+ undefined,
577
+ ),
578
+ ts.factory.createIdentifier("piece"),
579
+ ),
580
+ tag: "kind",
581
+ output: "entry",
582
+ })([
583
+ MetadataProperty.create({
584
+ key: MetadataFactory.soleLiteral("key"),
585
+ value: map.key,
586
+ description: null,
587
+ jsDocTags: [],
588
+ }),
589
+ MetadataProperty.create({
590
+ key: MetadataFactory.soleLiteral("value"),
591
+ value: map.value,
592
+ description: null,
593
+ jsDocTags: [],
594
+ }),
595
+ ]),
596
+ ts.factory.createExpressionStatement(props.setter()),
597
+ ];
598
+ return [
599
+ ts.factory.createExpressionStatement(
600
+ ExpressionFactory.selfCall(ts.factory.createBlock(statements, true)),
601
+ ),
602
+ ];
603
+ };
604
+ }
605
+
606
+ const PREFIX = "$pd";
607
+ const READER = () => ts.factory.createIdentifier("reader");