typia 7.0.0-dev.20241018 → 7.0.0-dev.20241019

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 (35) hide show
  1. package/lib/factories/ProtobufFactory.js +561 -128
  2. package/lib/factories/ProtobufFactory.js.map +1 -1
  3. package/lib/programmers/helpers/ProtobufUtil.d.ts +2 -0
  4. package/lib/programmers/helpers/ProtobufUtil.js +2 -2
  5. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
  6. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +169 -223
  7. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  8. package/lib/programmers/protobuf/ProtobufMessageProgrammer.d.ts +1 -1
  9. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +48 -140
  10. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  11. package/lib/schemas/metadata/MetadataProperty.d.ts +2 -0
  12. package/lib/schemas/metadata/MetadataProperty.js.map +1 -1
  13. package/lib/schemas/protobuf/IProtobufProperty.d.ts +5 -0
  14. package/lib/schemas/protobuf/IProtobufProperty.js +3 -0
  15. package/lib/schemas/protobuf/IProtobufProperty.js.map +1 -0
  16. package/lib/schemas/protobuf/IProtobufPropertyType.d.ts +28 -0
  17. package/lib/schemas/protobuf/IProtobufPropertyType.js +3 -0
  18. package/lib/schemas/protobuf/IProtobufPropertyType.js.map +1 -0
  19. package/lib/schemas/protobuf/IProtobufSchema.d.ts +38 -0
  20. package/lib/schemas/protobuf/IProtobufSchema.js +3 -0
  21. package/lib/schemas/protobuf/IProtobufSchema.js.map +1 -0
  22. package/lib/utils/{NameEncoder.d.ts → ProtobufNameEncoder.d.ts} +1 -1
  23. package/lib/utils/{NameEncoder.js → ProtobufNameEncoder.js} +7 -7
  24. package/lib/utils/ProtobufNameEncoder.js.map +1 -0
  25. package/package.json +1 -1
  26. package/src/factories/ProtobufFactory.ts +313 -14
  27. package/src/programmers/helpers/ProtobufUtil.ts +1 -1
  28. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +254 -315
  29. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +68 -115
  30. package/src/schemas/metadata/MetadataProperty.ts +2 -4
  31. package/src/schemas/protobuf/IProtobufProperty.ts +6 -0
  32. package/src/schemas/protobuf/IProtobufPropertyType.ts +37 -0
  33. package/src/schemas/protobuf/IProtobufSchema.ts +50 -0
  34. package/src/utils/{NameEncoder.ts → ProtobufNameEncoder.ts} +1 -1
  35. package/lib/utils/NameEncoder.js.map +0 -1
@@ -9,17 +9,15 @@ import { StatementFactory } from "../../factories/StatementFactory";
9
9
  import { TypeFactory } from "../../factories/TypeFactory";
10
10
 
11
11
  import { Metadata } from "../../schemas/metadata/Metadata";
12
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
13
- import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
- import { MetadataMap } from "../../schemas/metadata/MetadataMap";
15
12
  import { MetadataObjectType } from "../../schemas/metadata/MetadataObjectType";
16
13
  import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
14
+ import { IProtobufProperty } from "../../schemas/protobuf/IProtobufProperty";
15
+ import { IProtobufPropertyType } from "../../schemas/protobuf/IProtobufPropertyType";
16
+ import { IProtobufSchema } from "../../schemas/protobuf/IProtobufSchema";
17
17
 
18
18
  import { IProgrammerProps } from "../../transformers/IProgrammerProps";
19
19
  import { ITypiaContext } from "../../transformers/ITypiaContext";
20
20
 
21
- import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
22
-
23
21
  import { FeatureProgrammer } from "../FeatureProgrammer";
24
22
  import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
25
23
  import { ProtobufUtil } from "../helpers/ProtobufUtil";
@@ -149,18 +147,10 @@ export namespace ProtobufDecodeProgrammer {
149
147
  ExpressionFactory.number(0),
150
148
  ),
151
149
  undefined,
152
- ts.factory.createCallExpression(
153
- IdentifierFactory.access(READER(), "size"),
154
- undefined,
155
- undefined,
156
- ),
150
+ callReader("size"),
157
151
  undefined,
158
152
  ts.factory.createAdd(
159
- ts.factory.createCallExpression(
160
- IdentifierFactory.access(READER(), "index"),
161
- undefined,
162
- undefined,
163
- ),
153
+ callReader("index"),
164
154
  ts.factory.createIdentifier("length"),
165
155
  ),
166
156
  ),
@@ -168,18 +158,13 @@ export namespace ProtobufDecodeProgrammer {
168
158
  ),
169
159
  ...write_object_function_body({
170
160
  context: props.context,
171
- functor: props.functor,
172
161
  condition: ts.factory.createLessThan(
173
- ts.factory.createCallExpression(
174
- IdentifierFactory.access(READER(), "index"),
175
- undefined,
176
- undefined,
177
- ),
162
+ callReader("index"),
178
163
  ts.factory.createIdentifier("length"),
179
164
  ),
180
165
  tag: "tag",
181
166
  output: "output",
182
- properties: props.object.properties,
167
+ object: props.object,
183
168
  }),
184
169
  ts.factory.createReturnStatement(
185
170
  ts.factory.createIdentifier("output"),
@@ -191,35 +176,32 @@ export namespace ProtobufDecodeProgrammer {
191
176
 
192
177
  const write_object_function_body = (props: {
193
178
  context: ITypiaContext;
194
- functor: FunctionProgrammer;
195
179
  condition: ts.Expression;
196
180
  tag: string;
197
181
  output: string;
198
- properties: MetadataProperty[];
182
+ object: MetadataObjectType;
199
183
  }): ts.Statement[] => {
200
- let index: number = 1;
201
- const clauses: ts.CaseClause[] = props.properties
202
- .map((p) => {
203
- const clause = decode_property({
184
+ if (props.object.properties.some((p) => p.of_protobuf_ === undefined))
185
+ ProtobufFactory.emplaceObject(props.object);
186
+ const clauses: ts.CaseClause[] = props.object.properties
187
+ .map((p) =>
188
+ decode_property({
204
189
  context: props.context,
205
- functor: props.functor,
206
- index,
207
190
  accessor: IdentifierFactory.access(
208
191
  ts.factory.createIdentifier(props.output),
209
192
  p.key.getSoleLiteral()!,
210
193
  ),
194
+ protobuf: p.of_protobuf_!,
211
195
  metadata: p.value,
212
- });
213
- index += ProtobufUtil.size(p.value);
214
- return clause;
215
- })
196
+ }),
197
+ )
216
198
  .flat();
217
199
  return [
218
200
  StatementFactory.constant({
219
201
  name: props.output,
220
202
  value: ts.factory.createAsExpression(
221
203
  ts.factory.createObjectLiteralExpression(
222
- props.properties
204
+ props.object.properties
223
205
  .filter(
224
206
  (p) =>
225
207
  !(
@@ -243,11 +225,7 @@ export namespace ProtobufDecodeProgrammer {
243
225
  ts.factory.createBlock([
244
226
  StatementFactory.constant({
245
227
  name: props.tag,
246
- value: ts.factory.createCallExpression(
247
- IdentifierFactory.access(READER(), "uint32"),
248
- undefined,
249
- undefined,
250
- ),
228
+ value: callReader("uint32"),
251
229
  }),
252
230
  ts.factory.createSwitchStatement(
253
231
  ts.factory.createUnsignedRightShift(
@@ -258,16 +236,12 @@ export namespace ProtobufDecodeProgrammer {
258
236
  ...clauses,
259
237
  ts.factory.createDefaultClause([
260
238
  ts.factory.createExpressionStatement(
261
- ts.factory.createCallExpression(
262
- IdentifierFactory.access(READER(), "skipType"),
263
- undefined,
264
- [
265
- ts.factory.createBitwiseAnd(
266
- ts.factory.createIdentifier(props.tag),
267
- ExpressionFactory.number(7),
268
- ),
269
- ],
270
- ),
239
+ callReader("skipType", [
240
+ ts.factory.createBitwiseAnd(
241
+ ts.factory.createIdentifier(props.tag),
242
+ ExpressionFactory.number(7),
243
+ ),
244
+ ]),
271
245
  ),
272
246
  ts.factory.createBreakStatement(),
273
247
  ]),
@@ -320,32 +294,43 @@ export namespace ProtobufDecodeProgrammer {
320
294
  );
321
295
 
322
296
  /* -----------------------------------------------------------
323
- DECODERS
324
- ----------------------------------------------------------- */
297
+ DECODERS
298
+ ----------------------------------------------------------- */
325
299
  const decode_property = (props: {
326
300
  context: ITypiaContext;
327
- functor: FunctionProgrammer;
328
- index: number;
329
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
330
301
  metadata: Metadata;
331
- }): ts.CaseClause[] => {
332
- const clauses: ts.CaseClause[] = [];
333
- const emplace = (name: string, value: ts.Expression | ts.Statement[]) =>
334
- clauses.push(
335
- ts.factory.createCaseClause(
336
- ExpressionFactory.number(props.index++),
337
- Array.isArray(value)
338
- ? [
339
- ts.factory.createExpressionStatement(
340
- ts.factory.createIdentifier(`// type: ${name}`),
341
- ),
342
- ...value,
343
- ts.factory.createBreakStatement(),
344
- ]
302
+ protobuf: IProtobufProperty;
303
+ accessor: ts.Expression;
304
+ }): ts.CaseClause[] =>
305
+ props.protobuf.union.map((schema) =>
306
+ decode_property_type({
307
+ context: props.context,
308
+ accessor: props.accessor,
309
+ schema,
310
+ required:
311
+ props.metadata.isRequired() && props.metadata.nullable === false,
312
+ }),
313
+ );
314
+
315
+ const decode_property_type = (props: {
316
+ context: ITypiaContext;
317
+ accessor: ts.Expression;
318
+ schema: IProtobufPropertyType;
319
+ required: boolean;
320
+ }): ts.CaseClause => {
321
+ const out = (
322
+ title: string,
323
+ value: ts.Expression | ts.Statement[],
324
+ ): ts.CaseClause =>
325
+ ts.factory.createCaseClause(
326
+ ExpressionFactory.number(props.schema.index),
327
+ [
328
+ ts.factory.createExpressionStatement(
329
+ ts.factory.createIdentifier(`// ${title}`),
330
+ ),
331
+ ...(Array.isArray(value)
332
+ ? value
345
333
  : [
346
- ts.factory.createExpressionStatement(
347
- ts.factory.createIdentifier(`// ${name}`),
348
- ),
349
334
  ts.factory.createExpressionStatement(
350
335
  ts.factory.createBinaryExpression(
351
336
  props.accessor,
@@ -353,98 +338,111 @@ export namespace ProtobufDecodeProgrammer {
353
338
  value,
354
339
  ),
355
340
  ),
356
- ts.factory.createBreakStatement(),
357
- ],
358
- ),
359
- );
360
-
361
- const required: boolean =
362
- props.metadata.isRequired() && !props.metadata.nullable;
363
- // @todo
364
- for (const [atomic] of ProtobufUtil.getAtomics(props.metadata))
365
- emplace(
366
- atomic,
367
- decode_atomic({
368
- metadata: props.metadata,
369
- type: atomic as ProtobufAtomic,
370
- }),
341
+ ]),
342
+ ts.factory.createBreakStatement(),
343
+ ],
371
344
  );
372
- if (props.metadata.natives.length) emplace("bytes", decode_bytes("bytes"));
373
- for (const array of props.metadata.arrays)
374
- emplace(
375
- `Array<${array.type.value.getName()}>`,
345
+ // ATOMICS
346
+ if (props.schema.type === "bytes") return out("bytes", callReader("bytes"));
347
+ else if (props.schema.type === "bool")
348
+ return out("bool", callReader("bool"));
349
+ else if (props.schema.type === "bigint")
350
+ return out(props.schema.name, callReader(props.schema.name));
351
+ else if (props.schema.type === "number")
352
+ return out(props.schema.name, decode_number(props.schema));
353
+ else if (props.schema.type === "string")
354
+ return out("string", callReader("string"));
355
+ // INSTANCES
356
+ else if (props.schema.type === "array")
357
+ return out(
358
+ `Array<${props.schema.array.value.getName()}>`,
376
359
  decode_array({
377
360
  accessor: props.accessor,
378
- array,
379
- required,
361
+ schema: props.schema,
362
+ required: props.required,
380
363
  }),
381
364
  );
382
- for (const entry of props.metadata.maps)
383
- emplace(
384
- `Map<string, ${entry.value.getName()}>`,
385
- decode_map({
386
- context: props.context,
387
- functor: props.functor,
388
- accessor: props.accessor,
389
- entry,
390
- required,
365
+ else if (props.schema.type === "object")
366
+ return out(
367
+ props.schema.object.name,
368
+ decode_regular_object({
369
+ top: false,
370
+ object: props.schema.object,
391
371
  }),
392
372
  );
393
- for (const object of props.metadata.objects)
394
- emplace(
395
- object.type.name,
396
- ProtobufUtil.isStaticObject(object.type)
397
- ? decode_regular_object({
398
- top: false,
399
- object: object.type,
400
- })
401
- : decode_dynamic_object({
402
- context: props.context,
403
- functor: props.functor,
404
- accessor: props.accessor,
405
- object: object.type,
406
- required,
407
- }),
408
- );
409
- return clauses;
410
- };
411
-
412
- const decode_atomic = (props: {
413
- metadata: Metadata;
414
- type: ProtobufAtomic;
415
- }): ts.Expression => {
416
- if (props.type === "string") return decode_bytes("string");
417
-
418
- const call: ts.CallExpression = ts.factory.createCallExpression(
419
- IdentifierFactory.access(READER(), props.type),
420
- undefined,
421
- undefined,
373
+ else if (props.schema.type === "map")
374
+ if (props.schema.map instanceof MetadataObjectType) {
375
+ const key: Metadata = props.schema.map.properties[0]!.key;
376
+ const value: Metadata = props.schema.map.properties[0]!.value;
377
+ return out(
378
+ `Record<${key.getName()}, ${value.getName()}>`,
379
+ decode_map({
380
+ context: props.context,
381
+ accessor: props.accessor,
382
+ schema: props.schema,
383
+ required: props.required,
384
+ key,
385
+ value,
386
+ initializer: ts.factory.createObjectLiteralExpression([]),
387
+ setter: () =>
388
+ ts.factory.createBinaryExpression(
389
+ ts.factory.createElementAccessExpression(
390
+ props.accessor,
391
+ ts.factory.createIdentifier("entry.key"),
392
+ ),
393
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
394
+ ts.factory.createIdentifier("entry.value"),
395
+ ),
396
+ }),
397
+ );
398
+ } else {
399
+ const key: Metadata = props.schema.map.key;
400
+ const value: Metadata = props.schema.map.value;
401
+ return out(
402
+ `Map<${key.getName()}, ${value.getName()}>`,
403
+ decode_map({
404
+ context: props.context,
405
+ accessor: props.accessor,
406
+ schema: props.schema,
407
+ required: props.required,
408
+ key,
409
+ value,
410
+ initializer: ts.factory.createNewExpression(
411
+ ts.factory.createIdentifier("Map"),
412
+ [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
413
+ [],
414
+ ),
415
+ setter: () =>
416
+ ts.factory.createCallExpression(
417
+ IdentifierFactory.access(props.accessor, "set"),
418
+ undefined,
419
+ [
420
+ ts.factory.createIdentifier("entry.key"),
421
+ ts.factory.createIdentifier("entry.value"),
422
+ ],
423
+ ),
424
+ }),
425
+ );
426
+ }
427
+ throw new Error(
428
+ "Error on ProtobufDecodeProgrammer.write(): unknown property type",
422
429
  );
423
- if (props.type !== "int64" && props.type !== "uint64") return call;
430
+ };
424
431
 
425
- // @todo
426
- const isNumber: boolean = ProtobufUtil.getNumbers(props.metadata).has(
427
- props.type,
428
- );
429
- return isNumber
432
+ const decode_number = (schema: IProtobufSchema.INumber): ts.Expression => {
433
+ const value = callReader(schema.name);
434
+ return schema.name === "int64" || schema.name === "uint64"
430
435
  ? ts.factory.createCallExpression(
431
436
  ts.factory.createIdentifier("Number"),
432
437
  undefined,
433
- [call],
438
+ [value],
434
439
  )
435
- : call;
440
+ : value;
436
441
  };
437
442
 
438
- const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
439
- ts.factory.createCallExpression(
440
- IdentifierFactory.access(READER(), method),
441
- undefined,
442
- undefined,
443
- );
444
-
445
443
  const decode_array = (props: {
446
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
447
- array: MetadataArray;
444
+ accessor: ts.Expression;
445
+ schema: IProtobufSchema.IArray;
448
446
  required: boolean;
449
447
  }): ts.Statement[] => {
450
448
  const statements: Array<ts.Expression | ts.Statement> = [];
@@ -459,28 +457,8 @@ export namespace ProtobufDecodeProgrammer {
459
457
  ),
460
458
  ),
461
459
  );
462
-
463
- // @todo
464
- const atomics = Array.from(
465
- ProtobufUtil.getAtomics(props.array.type.value).keys(),
466
- ) as ProtobufAtomic[];
467
- const decoder = atomics.length
468
- ? () =>
469
- decode_atomic({
470
- metadata: props.array.type.value,
471
- type: atomics[0]!,
472
- })
473
- : props.array.type.value.natives.length
474
- ? () => decode_bytes("bytes")
475
- : props.array.type.value.objects.length
476
- ? () =>
477
- decode_regular_object({
478
- top: false,
479
- object: props.array.type.value.objects[0]!.type,
480
- })
481
- : null;
482
- if (decoder === null) throw new Error("Never reach here.");
483
- else if (atomics.length && atomics[0] !== "string") {
460
+ const decoder: ts.Expression = decode_array_value(props.schema.value);
461
+ if (["bool", "bigint", "number"].includes(props.schema.value.type)) {
484
462
  statements.push(
485
463
  ts.factory.createIfStatement(
486
464
  ts.factory.createStrictEquality(
@@ -495,32 +473,20 @@ export namespace ProtobufDecodeProgrammer {
495
473
  StatementFactory.constant({
496
474
  name: "piece",
497
475
  value: ts.factory.createAdd(
498
- ts.factory.createCallExpression(
499
- IdentifierFactory.access(READER(), "uint32"),
500
- undefined,
501
- undefined,
502
- ),
503
- ts.factory.createCallExpression(
504
- IdentifierFactory.access(READER(), "index"),
505
- undefined,
506
- undefined,
507
- ),
476
+ callReader("uint32"),
477
+ callReader("index"),
508
478
  ),
509
479
  }),
510
480
  ts.factory.createWhileStatement(
511
481
  ts.factory.createLessThan(
512
- ts.factory.createCallExpression(
513
- IdentifierFactory.access(READER(), "index"),
514
- undefined,
515
- undefined,
516
- ),
482
+ callReader("index"),
517
483
  ts.factory.createIdentifier("piece"),
518
484
  ),
519
485
  ts.factory.createExpressionStatement(
520
486
  ts.factory.createCallExpression(
521
487
  IdentifierFactory.access(props.accessor, "push"),
522
488
  undefined,
523
- [decoder()],
489
+ [decoder],
524
490
  ),
525
491
  ),
526
492
  ),
@@ -531,7 +497,7 @@ export namespace ProtobufDecodeProgrammer {
531
497
  ts.factory.createCallExpression(
532
498
  IdentifierFactory.access(props.accessor, "push"),
533
499
  undefined,
534
- [decoder()],
500
+ [decoder],
535
501
  ),
536
502
  ),
537
503
  ),
@@ -541,7 +507,7 @@ export namespace ProtobufDecodeProgrammer {
541
507
  ts.factory.createCallExpression(
542
508
  IdentifierFactory.access(props.accessor, "push"),
543
509
  undefined,
544
- [decoder()],
510
+ [decoder],
545
511
  ),
546
512
  );
547
513
  return statements.map((stmt) =>
@@ -558,158 +524,131 @@ export namespace ProtobufDecodeProgrammer {
558
524
  undefined,
559
525
  [
560
526
  ts.factory.createIdentifier("reader"),
561
- ...(props.top
562
- ? []
563
- : [
564
- ts.factory.createCallExpression(
565
- IdentifierFactory.access(READER(), "uint32"),
566
- undefined,
567
- undefined,
568
- ),
569
- ]),
527
+ ...(props.top ? [] : [callReader("uint32")]),
570
528
  ],
571
529
  );
572
530
 
573
- const decode_dynamic_object = (props: {
574
- context: ITypiaContext;
575
- functor: FunctionProgrammer;
576
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
577
- object: MetadataObjectType;
578
- required: boolean;
579
- }): ts.Statement[] => {
580
- const top: MetadataProperty = props.object.properties[0]!;
581
- return decode_entry({
582
- context: props.context,
583
- functor: props.functor,
584
- initializer: () =>
585
- ts.factory.createBinaryExpression(
586
- props.accessor,
587
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
588
- ts.factory.createObjectLiteralExpression(),
589
- ),
590
- setter: () =>
591
- ts.factory.createBinaryExpression(
592
- ts.factory.createElementAccessExpression(
593
- props.accessor,
594
- ts.factory.createIdentifier("entry.key"),
595
- ),
596
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
597
- ts.factory.createIdentifier("entry.value"),
598
- ),
599
- entry: MetadataProperty.create({
600
- ...top,
601
- key: (() => {
602
- const key: Metadata = Metadata.initialize();
603
- key.atomics.push(
604
- MetadataAtomic.create({
605
- type: "string",
606
- tags: [],
607
- }),
608
- );
609
- return key;
610
- })(),
611
- }),
612
- required: props.required,
613
- });
614
- };
615
-
616
531
  const decode_map = (props: {
617
532
  context: ITypiaContext;
618
- functor: FunctionProgrammer;
619
- accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression;
620
- entry: MetadataMap | MetadataProperty;
533
+ accessor: ts.Expression;
534
+ key: Metadata;
535
+ value: Metadata;
536
+ schema: IProtobufSchema.IMap;
537
+ initializer: ts.Expression;
621
538
  required: boolean;
622
- }): ts.Statement[] =>
623
- decode_entry({
624
- context: props.context,
625
- functor: props.functor,
626
- initializer: () =>
627
- ts.factory.createBinaryExpression(
628
- props.accessor,
629
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
630
- ts.factory.createNewExpression(
631
- ts.factory.createIdentifier("Map"),
632
- [TypeFactory.keyword("any"), TypeFactory.keyword("any")],
633
- [],
634
- ),
635
- ),
636
- setter: () =>
637
- ts.factory.createCallExpression(
638
- IdentifierFactory.access(props.accessor, "set"),
639
- undefined,
640
- [
641
- ts.factory.createIdentifier("entry.key"),
642
- ts.factory.createIdentifier("entry.value"),
643
- ],
644
- ),
645
- entry: props.entry,
646
- required: props.required,
647
- });
648
-
649
- const decode_entry = (props: {
650
- context: ITypiaContext;
651
- functor: FunctionProgrammer;
652
- initializer: () => ts.Expression;
653
539
  setter: () => ts.Expression;
654
- entry: MetadataMap | MetadataProperty;
655
- required: boolean;
656
540
  }): ts.Statement[] => {
657
- const statements: ts.Statement[] = [
658
- ...(props.required
659
- ? []
660
- : [ts.factory.createExpressionStatement(props.initializer())]),
541
+ const statements: Array<ts.Expression | ts.Statement> = [
542
+ ...(props.required === true
543
+ ? [
544
+ ts.factory.createBinaryExpression(
545
+ props.accessor,
546
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
547
+ props.initializer,
548
+ ),
549
+ ]
550
+ : []),
661
551
  StatementFactory.constant({
662
552
  name: "piece",
663
- value: ts.factory.createAdd(
664
- ts.factory.createCallExpression(
665
- IdentifierFactory.access(READER(), "uint32"),
666
- undefined,
667
- undefined,
668
- ),
669
- ts.factory.createCallExpression(
670
- IdentifierFactory.access(READER(), "index"),
671
- undefined,
672
- undefined,
673
- ),
674
- ),
553
+ value: ts.factory.createAdd(callReader("uint32"), callReader("index")),
675
554
  }),
676
555
  ...write_object_function_body({
677
556
  context: props.context,
678
- functor: props.functor,
679
557
  condition: ts.factory.createLessThan(
680
- ts.factory.createCallExpression(
681
- IdentifierFactory.access(READER(), "index"),
682
- undefined,
683
- undefined,
684
- ),
558
+ callReader("index"),
685
559
  ts.factory.createIdentifier("piece"),
686
560
  ),
687
561
  tag: "kind",
688
562
  output: "entry",
689
- properties: [
690
- MetadataProperty.create({
691
- key: MetadataFactory.soleLiteral("key"),
692
- value: props.entry.key,
693
- description: null,
694
- jsDocTags: [],
695
- }),
696
- MetadataProperty.create({
697
- key: MetadataFactory.soleLiteral("value"),
698
- value: props.entry.value,
699
- description: null,
700
- jsDocTags: [],
701
- }),
702
- ],
563
+ object: createObjectType([
564
+ {
565
+ key: "key",
566
+ value: props.key,
567
+ },
568
+ {
569
+ key: "value",
570
+ value: props.value,
571
+ },
572
+ ]),
703
573
  }),
704
- ts.factory.createExpressionStatement(props.setter()),
574
+ ...(props.required === false
575
+ ? [
576
+ ts.factory.createBinaryExpression(
577
+ props.accessor,
578
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
579
+ props.initializer,
580
+ ),
581
+ ]
582
+ : []),
583
+ props.setter(),
705
584
  ];
706
585
  return [
707
586
  ts.factory.createExpressionStatement(
708
- ExpressionFactory.selfCall(ts.factory.createBlock(statements, true)),
587
+ ExpressionFactory.selfCall(
588
+ ts.factory.createBlock(
589
+ statements.map((stmt) =>
590
+ ts.isExpression(stmt)
591
+ ? ts.factory.createExpressionStatement(stmt)
592
+ : stmt,
593
+ ),
594
+ true,
595
+ ),
596
+ ),
709
597
  ),
710
598
  ];
711
599
  };
600
+
601
+ const decode_array_value = (
602
+ schema: IProtobufSchema.IArray["value"],
603
+ ): ts.Expression => {
604
+ if (schema.type === "bytes") return callReader("bytes");
605
+ else if (schema.type === "bool") return callReader("bool");
606
+ else if (schema.type === "bigint") return callReader(schema.name);
607
+ else if (schema.type === "number") return decode_number(schema);
608
+ else if (schema.type === "string") return callReader("string");
609
+ else if (schema.type === "object")
610
+ return decode_regular_object({
611
+ top: false,
612
+ object: schema.object,
613
+ });
614
+ throw new Error("unreachable condition");
615
+ };
712
616
  }
713
617
 
714
618
  const PREFIX = "$pd";
715
- const READER = () => ts.factory.createIdentifier("reader");
619
+ const callReader = (
620
+ method: string,
621
+ args?: ts.Expression[],
622
+ ): ts.CallExpression =>
623
+ ts.factory.createCallExpression(
624
+ IdentifierFactory.access(ts.factory.createIdentifier("reader"), method),
625
+ undefined,
626
+ args,
627
+ );
628
+ const createObjectType = (
629
+ definitions: Array<{
630
+ key: string;
631
+ value: Metadata;
632
+ }>,
633
+ ): MetadataObjectType => {
634
+ const properties: MetadataProperty[] = definitions.map((def) =>
635
+ MetadataProperty.create({
636
+ key: MetadataFactory.soleLiteral(def.key),
637
+ value: def.value,
638
+ description: null,
639
+ jsDocTags: [],
640
+ }),
641
+ );
642
+ const obj: MetadataObjectType = MetadataObjectType.create({
643
+ name: "object.o" + Math.random().toString().slice(2),
644
+ properties,
645
+ description: undefined,
646
+ jsDocTags: [],
647
+ index: -1,
648
+ validated: true,
649
+ recursive: false,
650
+ nullables: [false],
651
+ });
652
+ ProtobufFactory.emplaceObject(obj);
653
+ return obj;
654
+ };