typia 7.0.0-dev.20240921 → 7.0.0-dev.20240922

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.
@@ -43,12 +43,16 @@ export namespace ProtobufDecodeProgrammer {
43
43
  functions: Object.fromEntries(
44
44
  collection
45
45
  .objects()
46
- .filter((obj) => ProtobufUtil.isStaticObject(obj))
47
- .map((obj) => [
48
- `${PREFIX}o${obj.index}`,
46
+ .filter((object) => ProtobufUtil.isStaticObject(object))
47
+ .map((object) => [
48
+ `${PREFIX}o${object.index}`,
49
49
  StatementFactory.constant(
50
- props.importer.useLocal(`${PREFIX}o${obj.index}`),
51
- write_object_function(props.context)(props.importer)(obj),
50
+ props.importer.useLocal(`${PREFIX}o${object.index}`),
51
+ write_object_function({
52
+ context: props.context,
53
+ importer: props.importer,
54
+ object,
55
+ }),
52
56
  ),
53
57
  ]),
54
58
  ),
@@ -87,7 +91,10 @@ export namespace ProtobufDecodeProgrammer {
87
91
  ),
88
92
  ),
89
93
  ts.factory.createReturnStatement(
90
- decode_regular_object(true)(meta.objects[0]!),
94
+ decode_regular_object({
95
+ top: true,
96
+ object: meta.objects[0]!,
97
+ }),
91
98
  ),
92
99
  ],
93
100
  true,
@@ -111,153 +118,163 @@ export namespace ProtobufDecodeProgrammer {
111
118
  });
112
119
  };
113
120
 
114
- const write_object_function =
115
- (project: ITypiaContext) =>
116
- (importer: FunctionImporter) =>
117
- (obj: MetadataObject): ts.ArrowFunction =>
118
- ts.factory.createArrowFunction(
119
- undefined,
120
- undefined,
121
+ const write_object_function = (props: {
122
+ context: ITypiaContext;
123
+ importer: FunctionImporter;
124
+ object: MetadataObject;
125
+ }): ts.ArrowFunction =>
126
+ ts.factory.createArrowFunction(
127
+ undefined,
128
+ undefined,
129
+ [
130
+ IdentifierFactory.parameter("reader"),
131
+ IdentifierFactory.parameter(
132
+ "length",
133
+ TypeFactory.keyword("number"),
134
+ ExpressionFactory.number(-1),
135
+ ),
136
+ ],
137
+ TypeFactory.keyword("any"),
138
+ undefined,
139
+ ts.factory.createBlock(
121
140
  [
122
- IdentifierFactory.parameter("reader"),
123
- IdentifierFactory.parameter(
124
- "length",
125
- TypeFactory.keyword("number"),
126
- ExpressionFactory.number(-1),
127
- ),
128
- ],
129
- TypeFactory.keyword("any"),
130
- undefined,
131
- ts.factory.createBlock(
132
- [
133
- ts.factory.createExpressionStatement(
134
- ts.factory.createBinaryExpression(
135
- ts.factory.createIdentifier("length"),
136
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
137
- ts.factory.createConditionalExpression(
138
- ts.factory.createLessThan(
139
- ts.factory.createIdentifier("length"),
140
- ExpressionFactory.number(0),
141
- ),
141
+ ts.factory.createExpressionStatement(
142
+ ts.factory.createBinaryExpression(
143
+ ts.factory.createIdentifier("length"),
144
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
145
+ ts.factory.createConditionalExpression(
146
+ ts.factory.createLessThan(
147
+ ts.factory.createIdentifier("length"),
148
+ ExpressionFactory.number(0),
149
+ ),
150
+ undefined,
151
+ ts.factory.createCallExpression(
152
+ IdentifierFactory.access(READER())("size"),
142
153
  undefined,
154
+ undefined,
155
+ ),
156
+ undefined,
157
+ ts.factory.createAdd(
143
158
  ts.factory.createCallExpression(
144
- IdentifierFactory.access(READER())("size"),
159
+ IdentifierFactory.access(READER())("index"),
145
160
  undefined,
146
161
  undefined,
147
162
  ),
148
- undefined,
149
- ts.factory.createAdd(
150
- ts.factory.createCallExpression(
151
- IdentifierFactory.access(READER())("index"),
152
- undefined,
153
- undefined,
154
- ),
155
- ts.factory.createIdentifier("length"),
156
- ),
163
+ ts.factory.createIdentifier("length"),
157
164
  ),
158
165
  ),
159
166
  ),
160
- ...write_object_function_body(project)(importer)({
161
- condition: ts.factory.createLessThan(
162
- ts.factory.createCallExpression(
163
- IdentifierFactory.access(READER())("index"),
164
- undefined,
165
- undefined,
166
- ),
167
- ts.factory.createIdentifier("length"),
167
+ ),
168
+ ...write_object_function_body({
169
+ context: props.context,
170
+ importer: props.importer,
171
+ condition: ts.factory.createLessThan(
172
+ ts.factory.createCallExpression(
173
+ IdentifierFactory.access(READER())("index"),
174
+ undefined,
175
+ undefined,
168
176
  ),
169
- tag: "tag",
170
- output: "output",
171
- })(obj.properties),
172
- ts.factory.createReturnStatement(
173
- ts.factory.createIdentifier("output"),
177
+ ts.factory.createIdentifier("length"),
174
178
  ),
175
- ],
176
- true,
177
- ),
178
- );
179
+ tag: "tag",
180
+ output: "output",
181
+ properties: props.object.properties,
182
+ }),
183
+ ts.factory.createReturnStatement(
184
+ ts.factory.createIdentifier("output"),
185
+ ),
186
+ ],
187
+ true,
188
+ ),
189
+ );
179
190
 
180
- const write_object_function_body =
181
- (project: ITypiaContext) =>
182
- (importer: FunctionImporter) =>
183
- (props: { condition: ts.Expression; tag: string; output: string }) =>
184
- (properties: MetadataProperty[]): ts.Statement[] => {
185
- let i: number = 1;
186
- const clauses: ts.CaseClause[] = properties
187
- .map((p) => {
188
- const clause = decode_property(project)(importer)(i)(
189
- IdentifierFactory.access(ts.factory.createIdentifier(props.output))(
190
- p.key.getSoleLiteral()!,
191
- ),
192
- p.value,
193
- );
194
- i += ProtobufUtil.size(p.value);
195
- return clause;
196
- })
197
- .flat();
198
- return [
199
- StatementFactory.constant(
200
- props.output,
201
- ts.factory.createAsExpression(
202
- ts.factory.createObjectLiteralExpression(
203
- properties
204
- .filter(
205
- (p) =>
206
- !(
207
- project.compilerOptions.exactOptionalPropertyTypes ===
208
- true && p.value.optional === true
209
- ),
210
- )
211
- .map((p) =>
212
- ts.factory.createPropertyAssignment(
213
- IdentifierFactory.identifier(p.key.getSoleLiteral()!),
214
- write_property_default_value(p.value),
191
+ const write_object_function_body = (props: {
192
+ context: ITypiaContext;
193
+ importer: FunctionImporter;
194
+ condition: ts.Expression;
195
+ tag: string;
196
+ output: string;
197
+ properties: MetadataProperty[];
198
+ }): ts.Statement[] => {
199
+ let index: number = 1;
200
+ const clauses: ts.CaseClause[] = props.properties
201
+ .map((p) => {
202
+ const clause = decode_property({
203
+ context: props.context,
204
+ importer: props.importer,
205
+ index,
206
+ accessor: IdentifierFactory.access(
207
+ ts.factory.createIdentifier(props.output),
208
+ )(p.key.getSoleLiteral()!),
209
+ metadata: p.value,
210
+ });
211
+ index += ProtobufUtil.size(p.value);
212
+ return clause;
213
+ })
214
+ .flat();
215
+ return [
216
+ StatementFactory.constant(
217
+ props.output,
218
+ ts.factory.createAsExpression(
219
+ ts.factory.createObjectLiteralExpression(
220
+ props.properties
221
+ .filter(
222
+ (p) =>
223
+ !(
224
+ props.context.compilerOptions.exactOptionalPropertyTypes ===
225
+ true && p.value.optional === true
215
226
  ),
227
+ )
228
+ .map((p) =>
229
+ ts.factory.createPropertyAssignment(
230
+ IdentifierFactory.identifier(p.key.getSoleLiteral()!),
231
+ write_property_default_value(p.value),
216
232
  ),
217
- true,
218
- ),
219
- TypeFactory.keyword("any"),
233
+ ),
234
+ true,
220
235
  ),
236
+ TypeFactory.keyword("any"),
221
237
  ),
222
- ts.factory.createWhileStatement(
223
- props.condition,
224
- ts.factory.createBlock([
225
- StatementFactory.constant(
226
- props.tag,
227
- ts.factory.createCallExpression(
228
- IdentifierFactory.access(READER())("uint32"),
229
- undefined,
230
- undefined,
231
- ),
238
+ ),
239
+ ts.factory.createWhileStatement(
240
+ props.condition,
241
+ ts.factory.createBlock([
242
+ StatementFactory.constant(
243
+ props.tag,
244
+ ts.factory.createCallExpression(
245
+ IdentifierFactory.access(READER())("uint32"),
246
+ undefined,
247
+ undefined,
232
248
  ),
233
- ts.factory.createSwitchStatement(
234
- ts.factory.createUnsignedRightShift(
235
- ts.factory.createIdentifier(props.tag),
236
- ExpressionFactory.number(3),
237
- ),
238
- ts.factory.createCaseBlock([
239
- ...clauses,
240
- ts.factory.createDefaultClause([
241
- ts.factory.createExpressionStatement(
242
- ts.factory.createCallExpression(
243
- IdentifierFactory.access(READER())("skipType"),
244
- undefined,
245
- [
246
- ts.factory.createBitwiseAnd(
247
- ts.factory.createIdentifier(props.tag),
248
- ExpressionFactory.number(7),
249
- ),
250
- ],
251
- ),
249
+ ),
250
+ ts.factory.createSwitchStatement(
251
+ ts.factory.createUnsignedRightShift(
252
+ ts.factory.createIdentifier(props.tag),
253
+ ExpressionFactory.number(3),
254
+ ),
255
+ ts.factory.createCaseBlock([
256
+ ...clauses,
257
+ ts.factory.createDefaultClause([
258
+ ts.factory.createExpressionStatement(
259
+ ts.factory.createCallExpression(
260
+ IdentifierFactory.access(READER())("skipType"),
261
+ undefined,
262
+ [
263
+ ts.factory.createBitwiseAnd(
264
+ ts.factory.createIdentifier(props.tag),
265
+ ExpressionFactory.number(7),
266
+ ),
267
+ ],
252
268
  ),
253
- ts.factory.createBreakStatement(),
254
- ]),
269
+ ),
270
+ ts.factory.createBreakStatement(),
255
271
  ]),
256
- ),
257
- ]),
258
- ),
259
- ];
260
- };
272
+ ]),
273
+ ),
274
+ ]),
275
+ ),
276
+ ];
277
+ };
261
278
 
262
279
  const write_property_default_value = (value: Metadata) =>
263
280
  ts.factory.createAsExpression(
@@ -303,87 +320,118 @@ export namespace ProtobufDecodeProgrammer {
303
320
  /* -----------------------------------------------------------
304
321
  DECODERS
305
322
  ----------------------------------------------------------- */
306
- const decode_property =
307
- (project: ITypiaContext) =>
308
- (importer: FunctionImporter) =>
309
- (index: number) =>
310
- (
311
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
312
- meta: Metadata,
313
- ): ts.CaseClause[] => {
314
- const clauses: ts.CaseClause[] = [];
315
- const emplace = (name: string) => (v: ts.Expression | ts.Statement[]) =>
316
- clauses.push(
317
- ts.factory.createCaseClause(
318
- ExpressionFactory.number(index++),
319
- Array.isArray(v)
320
- ? [
321
- ts.factory.createExpressionStatement(
322
- ts.factory.createIdentifier(`// type: ${name}`),
323
- ),
324
- ...v,
325
- ts.factory.createBreakStatement(),
326
- ]
327
- : [
328
- ts.factory.createExpressionStatement(
329
- ts.factory.createIdentifier(`// ${name}`),
330
- ),
331
- ts.factory.createExpressionStatement(
332
- ts.factory.createBinaryExpression(
333
- accessor,
334
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
335
- v,
336
- ),
323
+ const decode_property = (props: {
324
+ context: ITypiaContext;
325
+ importer: FunctionImporter;
326
+ index: number;
327
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
328
+ metadata: Metadata;
329
+ }): ts.CaseClause[] => {
330
+ const clauses: ts.CaseClause[] = [];
331
+ const emplace = (name: string, value: ts.Expression | ts.Statement[]) =>
332
+ clauses.push(
333
+ ts.factory.createCaseClause(
334
+ ExpressionFactory.number(props.index++),
335
+ Array.isArray(value)
336
+ ? [
337
+ ts.factory.createExpressionStatement(
338
+ ts.factory.createIdentifier(`// type: ${name}`),
339
+ ),
340
+ ...value,
341
+ ts.factory.createBreakStatement(),
342
+ ]
343
+ : [
344
+ ts.factory.createExpressionStatement(
345
+ ts.factory.createIdentifier(`// ${name}`),
346
+ ),
347
+ ts.factory.createExpressionStatement(
348
+ ts.factory.createBinaryExpression(
349
+ props.accessor,
350
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
351
+ value,
337
352
  ),
338
- ts.factory.createBreakStatement(),
339
- ],
340
- ),
341
- );
353
+ ),
354
+ ts.factory.createBreakStatement(),
355
+ ],
356
+ ),
357
+ );
342
358
 
343
- const required: boolean = meta.isRequired() && !meta.nullable;
344
- for (const atomic of ProtobufUtil.getAtomics(meta))
345
- emplace(atomic)(decode_atomic(meta)(atomic));
346
- if (meta.natives.length) emplace("bytes")(decode_bytes("bytes"));
347
- for (const array of meta.arrays)
348
- emplace(`Array<${array.type.value.getName()}>`)(
349
- decode_array(accessor, array, required),
350
- );
351
- for (const map of meta.maps)
352
- emplace(`Map<string, ${map.value.getName()}>`)(
353
- decode_map(project)(importer)(accessor, map, required),
354
- );
355
- for (const obj of meta.objects)
356
- emplace(obj.name)(
357
- ProtobufUtil.isStaticObject(obj)
358
- ? decode_regular_object(false)(obj)
359
- : decode_dynamic_object(project)(importer)(accessor, obj, required),
360
- );
361
- return clauses;
362
- };
359
+ const required: boolean =
360
+ props.metadata.isRequired() && !props.metadata.nullable;
361
+ for (const atomic of ProtobufUtil.getAtomics(props.metadata))
362
+ emplace(
363
+ atomic,
364
+ decode_atomic({
365
+ metadata: props.metadata,
366
+ type: atomic,
367
+ }),
368
+ );
369
+ if (props.metadata.natives.length) emplace("bytes", decode_bytes("bytes"));
370
+ for (const array of props.metadata.arrays)
371
+ emplace(
372
+ `Array<${array.type.value.getName()}>`,
373
+ decode_array({
374
+ accessor: props.accessor,
375
+ array,
376
+ required,
377
+ }),
378
+ );
379
+ for (const entry of props.metadata.maps)
380
+ emplace(
381
+ `Map<string, ${entry.value.getName()}>`,
382
+ decode_map({
383
+ context: props.context,
384
+ importer: props.importer,
385
+ accessor: props.accessor,
386
+ entry,
387
+ required,
388
+ }),
389
+ );
390
+ for (const object of props.metadata.objects)
391
+ emplace(
392
+ object.name,
393
+ ProtobufUtil.isStaticObject(object)
394
+ ? decode_regular_object({
395
+ top: false,
396
+ object,
397
+ })
398
+ : decode_dynamic_object({
399
+ context: props.context,
400
+ importer: props.importer,
401
+ accessor: props.accessor,
402
+ object,
403
+ required,
404
+ }),
405
+ );
406
+ return clauses;
407
+ };
363
408
 
364
- const decode_atomic =
365
- (meta: Metadata) =>
366
- (atomic: ProtobufAtomic): ts.Expression => {
367
- if (atomic === "string") return decode_bytes("string");
409
+ const decode_atomic = (props: {
410
+ metadata: Metadata;
411
+ type: ProtobufAtomic;
412
+ }): ts.Expression => {
413
+ if (props.type === "string") return decode_bytes("string");
368
414
 
369
- const call: ts.CallExpression = ts.factory.createCallExpression(
370
- IdentifierFactory.access(ts.factory.createIdentifier("reader"))(atomic),
371
- undefined,
372
- undefined,
373
- );
374
- if (atomic !== "int64" && atomic !== "uint64") return call;
415
+ const call: ts.CallExpression = ts.factory.createCallExpression(
416
+ IdentifierFactory.access(ts.factory.createIdentifier("reader"))(
417
+ props.type,
418
+ ),
419
+ undefined,
420
+ undefined,
421
+ );
422
+ if (props.type !== "int64" && props.type !== "uint64") return call;
375
423
 
376
- const isNumber: boolean = ProtobufUtil.getNumbers(meta).some(
377
- (n) => n === atomic,
378
- );
379
- return isNumber
380
- ? ts.factory.createCallExpression(
381
- ts.factory.createIdentifier("Number"),
382
- undefined,
383
- [call],
384
- )
385
- : call;
386
- };
424
+ const isNumber: boolean = ProtobufUtil.getNumbers(props.metadata).some(
425
+ (n) => n === props.type,
426
+ );
427
+ return isNumber
428
+ ? ts.factory.createCallExpression(
429
+ ts.factory.createIdentifier("Number"),
430
+ undefined,
431
+ [call],
432
+ )
433
+ : call;
434
+ };
387
435
 
388
436
  const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
389
437
  ts.factory.createCallExpression(
@@ -392,16 +440,16 @@ export namespace ProtobufDecodeProgrammer {
392
440
  undefined,
393
441
  );
394
442
 
395
- const decode_array = (
396
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
397
- array: MetadataArray,
398
- required: boolean,
399
- ): ts.Statement[] => {
443
+ const decode_array = (props: {
444
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
445
+ array: MetadataArray;
446
+ required: boolean;
447
+ }): ts.Statement[] => {
400
448
  const statements: Array<ts.Expression | ts.Statement> = [];
401
- if (required === false)
449
+ if (props.required === false)
402
450
  statements.push(
403
451
  ts.factory.createBinaryExpression(
404
- accessor,
452
+ props.accessor,
405
453
  ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
406
454
  ts.factory.createAsExpression(
407
455
  ts.factory.createArrayLiteralExpression(),
@@ -409,13 +457,21 @@ export namespace ProtobufDecodeProgrammer {
409
457
  ),
410
458
  ),
411
459
  );
412
- const atomics = ProtobufUtil.getAtomics(array.type.value);
460
+ const atomics = ProtobufUtil.getAtomics(props.array.type.value);
413
461
  const decoder = atomics.length
414
- ? () => decode_atomic(array.type.value)(atomics[0]!)
415
- : array.type.value.natives.length
462
+ ? () =>
463
+ decode_atomic({
464
+ metadata: props.array.type.value,
465
+ type: atomics[0]!,
466
+ })
467
+ : props.array.type.value.natives.length
416
468
  ? () => decode_bytes("bytes")
417
- : array.type.value.objects.length
418
- ? () => decode_regular_object(false)(array.type.value.objects[0]!)
469
+ : props.array.type.value.objects.length
470
+ ? () =>
471
+ decode_regular_object({
472
+ top: false,
473
+ object: props.array.type.value.objects[0]!,
474
+ })
419
475
  : null;
420
476
  if (decoder === null) throw new Error("Never reach here.");
421
477
  else if (atomics.length && atomics[0] !== "string") {
@@ -456,7 +512,7 @@ export namespace ProtobufDecodeProgrammer {
456
512
  ),
457
513
  ts.factory.createExpressionStatement(
458
514
  ts.factory.createCallExpression(
459
- IdentifierFactory.access(accessor)("push"),
515
+ IdentifierFactory.access(props.accessor)("push"),
460
516
  undefined,
461
517
  [decoder()],
462
518
  ),
@@ -467,7 +523,7 @@ export namespace ProtobufDecodeProgrammer {
467
523
  ),
468
524
  ts.factory.createExpressionStatement(
469
525
  ts.factory.createCallExpression(
470
- IdentifierFactory.access(accessor)("push"),
526
+ IdentifierFactory.access(props.accessor)("push"),
471
527
  undefined,
472
528
  [decoder()],
473
529
  ),
@@ -477,7 +533,7 @@ export namespace ProtobufDecodeProgrammer {
477
533
  } else
478
534
  statements.push(
479
535
  ts.factory.createCallExpression(
480
- IdentifierFactory.access(accessor)("push"),
536
+ IdentifierFactory.access(props.accessor)("push"),
481
537
  undefined,
482
538
  [decoder()],
483
539
  ),
@@ -487,159 +543,166 @@ export namespace ProtobufDecodeProgrammer {
487
543
  );
488
544
  };
489
545
 
490
- const decode_regular_object =
491
- (top: boolean) =>
492
- (obj: MetadataObject): ts.Expression =>
493
- ts.factory.createCallExpression(
494
- ts.factory.createIdentifier(`${PREFIX}o${obj.index}`),
495
- undefined,
496
- [
497
- ts.factory.createIdentifier("reader"),
498
- ...(top
499
- ? []
500
- : [
501
- ts.factory.createCallExpression(
502
- IdentifierFactory.access(READER())("uint32"),
503
- undefined,
504
- undefined,
505
- ),
506
- ]),
507
- ],
508
- );
546
+ const decode_regular_object = (props: {
547
+ top: boolean;
548
+ object: MetadataObject;
549
+ }): ts.Expression =>
550
+ ts.factory.createCallExpression(
551
+ ts.factory.createIdentifier(`${PREFIX}o${props.object.index}`),
552
+ undefined,
553
+ [
554
+ ts.factory.createIdentifier("reader"),
555
+ ...(props.top
556
+ ? []
557
+ : [
558
+ ts.factory.createCallExpression(
559
+ IdentifierFactory.access(READER())("uint32"),
560
+ undefined,
561
+ undefined,
562
+ ),
563
+ ]),
564
+ ],
565
+ );
509
566
 
510
- const decode_dynamic_object =
511
- (project: ITypiaContext) =>
512
- (importer: FunctionImporter) =>
513
- (
514
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
515
- obj: MetadataObject,
516
- required: boolean,
517
- ): ts.Statement[] => {
518
- const top = obj.properties[0]!;
519
- return decode_entry(project)(importer)({
520
- initializer: () =>
521
- ts.factory.createBinaryExpression(
522
- accessor,
523
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
524
- ts.factory.createObjectLiteralExpression(),
525
- ),
526
- setter: () =>
527
- ts.factory.createBinaryExpression(
528
- ts.factory.createElementAccessExpression(
529
- accessor,
530
- ts.factory.createIdentifier("entry.key"),
531
- ),
532
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
533
- ts.factory.createIdentifier("entry.value"),
567
+ const decode_dynamic_object = (props: {
568
+ context: ITypiaContext;
569
+ importer: FunctionImporter;
570
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
571
+ object: MetadataObject;
572
+ required: boolean;
573
+ }): ts.Statement[] => {
574
+ const top: MetadataProperty = props.object.properties[0]!;
575
+ return decode_entry({
576
+ context: props.context,
577
+ importer: props.importer,
578
+ initializer: () =>
579
+ ts.factory.createBinaryExpression(
580
+ props.accessor,
581
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
582
+ ts.factory.createObjectLiteralExpression(),
583
+ ),
584
+ setter: () =>
585
+ ts.factory.createBinaryExpression(
586
+ ts.factory.createElementAccessExpression(
587
+ props.accessor,
588
+ ts.factory.createIdentifier("entry.key"),
534
589
  ),
535
- })(
536
- MetadataProperty.create({
537
- ...top,
538
- key: (() => {
539
- const key: Metadata = Metadata.initialize();
540
- key.atomics.push(
541
- MetadataAtomic.create({
542
- type: "string",
543
- tags: [],
544
- }),
545
- );
546
- return key;
547
- })(),
548
- }),
549
- required,
550
- );
551
- };
590
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
591
+ ts.factory.createIdentifier("entry.value"),
592
+ ),
593
+ entry: MetadataProperty.create({
594
+ ...top,
595
+ key: (() => {
596
+ const key: Metadata = Metadata.initialize();
597
+ key.atomics.push(
598
+ MetadataAtomic.create({
599
+ type: "string",
600
+ tags: [],
601
+ }),
602
+ );
603
+ return key;
604
+ })(),
605
+ }),
606
+ required: props.required,
607
+ });
608
+ };
552
609
 
553
- const decode_map =
554
- (project: ITypiaContext) =>
555
- (importer: FunctionImporter) =>
556
- (
557
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
558
- map: Metadata.Entry,
559
- required: boolean,
560
- ): ts.Statement[] =>
561
- decode_entry(project)(importer)({
562
- initializer: () =>
563
- ts.factory.createBinaryExpression(
564
- accessor,
565
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
566
- ts.factory.createNewExpression(
567
- ts.factory.createIdentifier("Map"),
568
- [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
569
- [],
570
- ),
610
+ const decode_map = (props: {
611
+ context: ITypiaContext;
612
+ importer: FunctionImporter;
613
+ accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
614
+ entry: Metadata.Entry;
615
+ required: boolean;
616
+ }): ts.Statement[] =>
617
+ decode_entry({
618
+ context: props.context,
619
+ importer: props.importer,
620
+ initializer: () =>
621
+ ts.factory.createBinaryExpression(
622
+ props.accessor,
623
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
624
+ ts.factory.createNewExpression(
625
+ ts.factory.createIdentifier("Map"),
626
+ [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
627
+ [],
571
628
  ),
572
- setter: () =>
629
+ ),
630
+ setter: () =>
631
+ ts.factory.createCallExpression(
632
+ IdentifierFactory.access(props.accessor)("set"),
633
+ undefined,
634
+ [
635
+ ts.factory.createIdentifier("entry.key"),
636
+ ts.factory.createIdentifier("entry.value"),
637
+ ],
638
+ ),
639
+ entry: props.entry,
640
+ required: props.required,
641
+ });
642
+
643
+ const decode_entry = (props: {
644
+ context: ITypiaContext;
645
+ importer: FunctionImporter;
646
+ initializer: () => ts.Expression;
647
+ setter: () => ts.Expression;
648
+ entry: Metadata.Entry;
649
+ required: boolean;
650
+ }): ts.Statement[] => {
651
+ const statements: ts.Statement[] = [
652
+ ...(props.required
653
+ ? []
654
+ : [ts.factory.createExpressionStatement(props.initializer())]),
655
+ StatementFactory.constant(
656
+ "piece",
657
+ ts.factory.createAdd(
573
658
  ts.factory.createCallExpression(
574
- IdentifierFactory.access(accessor)("set"),
659
+ IdentifierFactory.access(READER())("uint32"),
660
+ undefined,
575
661
  undefined,
576
- [
577
- ts.factory.createIdentifier("entry.key"),
578
- ts.factory.createIdentifier("entry.value"),
579
- ],
580
662
  ),
581
- })(map, required);
582
-
583
- const decode_entry =
584
- (project: ITypiaContext) =>
585
- (importer: FunctionImporter) =>
586
- (props: {
587
- initializer: () => ts.Expression;
588
- setter: () => ts.Expression;
589
- }) =>
590
- (map: Metadata.Entry, required: boolean): ts.Statement[] => {
591
- const statements: ts.Statement[] = [
592
- ...(required
593
- ? []
594
- : [ts.factory.createExpressionStatement(props.initializer())]),
595
- StatementFactory.constant(
596
- "piece",
597
- ts.factory.createAdd(
598
- ts.factory.createCallExpression(
599
- IdentifierFactory.access(READER())("uint32"),
600
- undefined,
601
- undefined,
602
- ),
603
- ts.factory.createCallExpression(
604
- IdentifierFactory.access(READER())("index"),
605
- undefined,
606
- undefined,
607
- ),
663
+ ts.factory.createCallExpression(
664
+ IdentifierFactory.access(READER())("index"),
665
+ undefined,
666
+ undefined,
608
667
  ),
609
668
  ),
610
- ...write_object_function_body(project)(importer)({
611
- condition: ts.factory.createLessThan(
612
- ts.factory.createCallExpression(
613
- IdentifierFactory.access(READER())("index"),
614
- undefined,
615
- undefined,
616
- ),
617
- ts.factory.createIdentifier("piece"),
669
+ ),
670
+ ...write_object_function_body({
671
+ context: props.context,
672
+ importer: props.importer,
673
+ condition: ts.factory.createLessThan(
674
+ ts.factory.createCallExpression(
675
+ IdentifierFactory.access(READER())("index"),
676
+ undefined,
677
+ undefined,
618
678
  ),
619
- tag: "kind",
620
- output: "entry",
621
- })([
679
+ ts.factory.createIdentifier("piece"),
680
+ ),
681
+ tag: "kind",
682
+ output: "entry",
683
+ properties: [
622
684
  MetadataProperty.create({
623
685
  key: MetadataFactory.soleLiteral("key"),
624
- value: map.key,
686
+ value: props.entry.key,
625
687
  description: null,
626
688
  jsDocTags: [],
627
689
  }),
628
690
  MetadataProperty.create({
629
691
  key: MetadataFactory.soleLiteral("value"),
630
- value: map.value,
692
+ value: props.entry.value,
631
693
  description: null,
632
694
  jsDocTags: [],
633
695
  }),
634
- ]),
635
- ts.factory.createExpressionStatement(props.setter()),
636
- ];
637
- return [
638
- ts.factory.createExpressionStatement(
639
- ExpressionFactory.selfCall(ts.factory.createBlock(statements, true)),
640
- ),
641
- ];
642
- };
696
+ ],
697
+ }),
698
+ ts.factory.createExpressionStatement(props.setter()),
699
+ ];
700
+ return [
701
+ ts.factory.createExpressionStatement(
702
+ ExpressionFactory.selfCall(ts.factory.createBlock(statements, true)),
703
+ ),
704
+ ];
705
+ };
643
706
  }
644
707
 
645
708
  const PREFIX = "$pd";