typia 5.0.0-dev.20230823-2 → 5.0.0-dev.2023084

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 (148) hide show
  1. package/lib/factories/MetadataTagFactory.js +4 -2
  2. package/lib/factories/MetadataTagFactory.js.map +1 -1
  3. package/lib/factories/ProtobufFactory.js +2 -1
  4. package/lib/factories/ProtobufFactory.js.map +1 -1
  5. package/lib/factories/internal/metadata/emend_metadata_atomics.js +6 -6
  6. package/lib/factories/internal/metadata/emend_metadata_atomics.js.map +1 -1
  7. package/lib/factories/internal/metadata/iterate_metadata_atomic.js +1 -1
  8. package/lib/factories/internal/metadata/iterate_metadata_atomic.js.map +1 -1
  9. package/lib/programmers/CheckerProgrammer.js +6 -6
  10. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  11. package/lib/programmers/RandomProgrammer.js +5 -5
  12. package/lib/programmers/RandomProgrammer.js.map +1 -1
  13. package/lib/programmers/helpers/AtomicPredicator.js +2 -2
  14. package/lib/programmers/helpers/AtomicPredicator.js.map +1 -1
  15. package/lib/programmers/helpers/ProtobufUtil.js +2 -2
  16. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
  17. package/lib/programmers/internal/application_default_string.js +2 -2
  18. package/lib/programmers/internal/application_default_string.js.map +1 -1
  19. package/lib/programmers/internal/application_schema.js +5 -5
  20. package/lib/programmers/internal/application_schema.js.map +1 -1
  21. package/lib/programmers/internal/metadata_to_pattern.js +4 -4
  22. package/lib/programmers/internal/metadata_to_pattern.js.map +1 -1
  23. package/lib/programmers/internal/stringify_dynamic_properties.js +2 -1
  24. package/lib/programmers/internal/stringify_dynamic_properties.js.map +1 -1
  25. package/lib/programmers/json/JsonStringifyProgrammer.js +16 -10
  26. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  27. package/lib/programmers/misc/MiscLiteralsProgrammer.js +2 -2
  28. package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
  29. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +1 -1
  30. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  31. package/lib/schemas/metadata/IMetadata.d.ts +2 -2
  32. package/lib/schemas/metadata/IMetadataAtomic.d.ts +12 -0
  33. package/lib/schemas/metadata/IMetadataAtomic.js +3 -0
  34. package/lib/schemas/metadata/IMetadataAtomic.js.map +1 -0
  35. package/lib/schemas/metadata/Metadata.d.ts +2 -2
  36. package/lib/schemas/metadata/Metadata.js +49 -44
  37. package/lib/schemas/metadata/Metadata.js.map +1 -1
  38. package/lib/tags/ExclusiveMaximum.d.ts +8 -0
  39. package/lib/tags/ExclusiveMaximum.js +3 -0
  40. package/lib/tags/ExclusiveMaximum.js.map +1 -0
  41. package/lib/tags/ExclusiveMinimum.d.ts +8 -0
  42. package/lib/tags/ExclusiveMinimum.js +3 -0
  43. package/lib/tags/ExclusiveMinimum.js.map +1 -0
  44. package/lib/tags/Format.d.ts +8 -0
  45. package/lib/tags/Format.js +3 -0
  46. package/lib/tags/Format.js.map +1 -0
  47. package/lib/tags/MaxItems.d.ts +8 -0
  48. package/lib/tags/MaxItems.js +3 -0
  49. package/lib/tags/MaxItems.js.map +1 -0
  50. package/lib/tags/MaxLength.d.ts +8 -0
  51. package/lib/tags/MaxLength.js +3 -0
  52. package/lib/tags/MaxLength.js.map +1 -0
  53. package/lib/tags/Maximum.d.ts +8 -0
  54. package/lib/tags/Maximum.js +3 -0
  55. package/lib/tags/Maximum.js.map +1 -0
  56. package/lib/tags/MinItems.d.ts +8 -0
  57. package/lib/tags/MinItems.js +3 -0
  58. package/lib/tags/MinItems.js.map +1 -0
  59. package/lib/tags/MinLength.d.ts +8 -0
  60. package/lib/tags/MinLength.js +3 -0
  61. package/lib/tags/MinLength.js.map +1 -0
  62. package/lib/tags/Minimum.d.ts +8 -0
  63. package/lib/tags/Minimum.js +3 -0
  64. package/lib/tags/Minimum.js.map +1 -0
  65. package/lib/tags/MultipleOf.d.ts +8 -0
  66. package/lib/tags/MultipleOf.js +3 -0
  67. package/lib/tags/MultipleOf.js.map +1 -0
  68. package/lib/tags/Pattern.d.ts +8 -0
  69. package/lib/tags/Pattern.js +3 -0
  70. package/lib/tags/Pattern.js.map +1 -0
  71. package/lib/tags/TagBase.d.ts +10 -0
  72. package/lib/tags/TagBase.js +3 -0
  73. package/lib/tags/TagBase.js.map +1 -0
  74. package/lib/tags/Type.d.ts +13 -0
  75. package/lib/tags/Type.js +3 -0
  76. package/lib/tags/Type.js.map +1 -0
  77. package/lib/tags/index.d.ts +12 -0
  78. package/lib/tags/index.js +29 -0
  79. package/lib/tags/index.js.map +1 -0
  80. package/lib/transformers/features/json/JsonApplicationTransformer.js +1 -1
  81. package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
  82. package/package.json +1 -1
  83. package/src/Primitive.ts +135 -135
  84. package/src/Resolved.ts +116 -116
  85. package/src/factories/MetadataFactory.ts +46 -46
  86. package/src/factories/MetadataTagFactory.ts +366 -364
  87. package/src/factories/ProtobufFactory.ts +268 -266
  88. package/src/factories/internal/metadata/emend_metadata_atomics.ts +35 -33
  89. package/src/factories/internal/metadata/explore_metadata.ts +38 -38
  90. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +63 -59
  91. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  92. package/src/factories/internal/metadata/iterate_metadata_native.ts +210 -210
  93. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  94. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  95. package/src/functional/$ProtobufWriter.ts +151 -151
  96. package/src/json.ts +648 -648
  97. package/src/misc.ts +651 -651
  98. package/src/module.ts +709 -709
  99. package/src/programmers/AssertProgrammer.ts +281 -281
  100. package/src/programmers/CheckerProgrammer.ts +1174 -1173
  101. package/src/programmers/IsProgrammer.ts +241 -241
  102. package/src/programmers/RandomProgrammer.ts +874 -874
  103. package/src/programmers/ValidateProgrammer.ts +307 -307
  104. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  105. package/src/programmers/helpers/FunctionImporeter.ts +91 -91
  106. package/src/programmers/helpers/ProtobufUtil.ts +29 -29
  107. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  108. package/src/programmers/helpers/disable_function_importer_declare.ts +32 -32
  109. package/src/programmers/internal/application_default_string.ts +37 -33
  110. package/src/programmers/internal/application_resolved.ts +55 -55
  111. package/src/programmers/internal/application_schema.ts +157 -157
  112. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  113. package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
  114. package/src/programmers/json/JsonStringifyProgrammer.ts +995 -989
  115. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  116. package/src/programmers/misc/MiscCloneProgrammer.ts +775 -775
  117. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  118. package/src/programmers/misc/MiscLiteralsProgrammer.ts +64 -64
  119. package/src/programmers/misc/MiscPruneProgrammer.ts +544 -544
  120. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  121. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +75 -75
  122. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +673 -673
  123. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +814 -814
  124. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +85 -85
  125. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +75 -75
  126. package/src/protobuf.ts +881 -881
  127. package/src/schemas/metadata/IMetadata.ts +27 -28
  128. package/src/schemas/metadata/IMetadataAtomic.ts +13 -0
  129. package/src/schemas/metadata/IMetadataEscaped.ts +6 -6
  130. package/src/schemas/metadata/Metadata.ts +643 -634
  131. package/src/schemas/metadata/MetadataEscaped.ts +51 -51
  132. package/src/tags/ExclusiveMaximum.ts +8 -0
  133. package/src/tags/ExclusiveMinimum.ts +8 -0
  134. package/src/tags/Format.ts +29 -0
  135. package/src/tags/MaxItems.ts +8 -0
  136. package/src/tags/MaxLength.ts +8 -0
  137. package/src/tags/Maximum.ts +8 -0
  138. package/src/tags/MinItems.ts +8 -0
  139. package/src/tags/MinLength.ts +8 -0
  140. package/src/tags/Minimum.ts +8 -0
  141. package/src/tags/MultipleOf.ts +10 -0
  142. package/src/tags/Pattern.ts +8 -0
  143. package/src/tags/TagBase.ts +17 -0
  144. package/src/tags/Type.ts +30 -0
  145. package/src/tags/index.ts +12 -0
  146. package/src/transformers/features/json/JsonApplicationTransformer.ts +111 -111
  147. package/src/transformers/features/misc/MetadataTransformer.ts +53 -53
  148. package/src/utils/RandomGenerator.ts +83 -83
@@ -1,874 +1,874 @@
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 { StatementFactory } from "../factories/StatementFactory";
8
- import { TemplateFactory } from "../factories/TemplateFactory";
9
- import { TypeFactory } from "../factories/TypeFactory";
10
-
11
- import { ICommentTag } from "../schemas/metadata/ICommentTag";
12
- import { IMetadataTag } from "../schemas/metadata/IMetadataTag";
13
- import { Metadata } from "../schemas/metadata/Metadata";
14
- import { MetadataArray } from "../schemas/metadata/MetadataArray";
15
- import { MetadataObject } from "../schemas/metadata/MetadataObject";
16
- import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
17
-
18
- import { IProject } from "../transformers/IProject";
19
-
20
- import { FunctionImporter } from "./helpers/FunctionImporeter";
21
- import { RandomJoiner } from "./helpers/RandomJoiner";
22
- import { RandomRanger } from "./helpers/RandomRanger";
23
- import { random_custom } from "./internal/random_custom";
24
-
25
- export namespace RandomProgrammer {
26
- export const write =
27
- (project: IProject) =>
28
- (modulo: ts.LeftHandSideExpression) =>
29
- (init?: ts.Expression) => {
30
- const importer: FunctionImporter = new FunctionImporter(
31
- modulo.getText(),
32
- );
33
- return (type: ts.Type, name?: string) => {
34
- // INITIALIZE METADATA
35
- const collection: MetadataCollection = new MetadataCollection();
36
- const meta: Metadata = MetadataFactory.analyze(project.checker)(
37
- {
38
- escape: false,
39
- constant: true,
40
- absorb: true,
41
- validate: (meta) => {
42
- if (meta.natives.some((n) => n === "WeakSet"))
43
- throw new Error(
44
- `Error on ${modulo.getText()}(): WeakSet is not supported.`,
45
- );
46
- else if (meta.natives.some((n) => n === "WeakMap"))
47
- throw new Error(
48
- `Error on ${modulo.getText()}(): WeakMap is not supported.`,
49
- );
50
- },
51
- },
52
- )(collection)(type);
53
-
54
- // GENERATE FUNCTION
55
- const functions = {
56
- objects: write_object_functions(importer)(collection),
57
- arrays: write_array_functions(importer)(collection),
58
- tuples: write_tuple_functions(importer)(collection),
59
- };
60
-
61
- const output: ts.Expression = decode(importer)({
62
- function: false,
63
- recursive: false,
64
- })(meta, [], []);
65
-
66
- return ts.factory.createArrowFunction(
67
- undefined,
68
- undefined,
69
- [
70
- IdentifierFactory.parameter(
71
- "generator",
72
- ts.factory.createTypeReferenceNode(
73
- "Partial<typia.IRandomGenerator>",
74
- ),
75
- init ??
76
- ts.factory.createToken(
77
- ts.SyntaxKind.QuestionToken,
78
- ),
79
- ),
80
- ],
81
- ts.factory.createTypeReferenceNode(
82
- `typia.Resolved<${
83
- name ??
84
- TypeFactory.getFullName(project.checker)(type)
85
- }>`,
86
- ),
87
- undefined,
88
- ts.factory.createBlock(
89
- [
90
- ...importer.declare(modulo),
91
- ...functions.objects,
92
- ...functions.arrays,
93
- ...functions.tuples,
94
- ts.factory.createReturnStatement(output),
95
- ],
96
- true,
97
- ),
98
- );
99
- };
100
- };
101
-
102
- const write_object_functions =
103
- (importer: FunctionImporter) =>
104
- (collection: MetadataCollection): ts.VariableStatement[] =>
105
- collection.objects().map((obj, i) =>
106
- StatementFactory.constant(
107
- PREFIX.object(i),
108
- ts.factory.createArrowFunction(
109
- undefined,
110
- undefined,
111
- [
112
- IdentifierFactory.parameter(
113
- "_recursive",
114
- TypeFactory.keyword("boolean"),
115
- ts.factory.createIdentifier(
116
- String(obj.recursive),
117
- ),
118
- ),
119
- IdentifierFactory.parameter(
120
- "_depth",
121
- TypeFactory.keyword("number"),
122
- ts.factory.createNumericLiteral(0),
123
- ),
124
- ],
125
- TypeFactory.keyword("any"),
126
- undefined,
127
- RandomJoiner.object(COALESCE(importer))(
128
- decode(importer)({
129
- recursive: obj.recursive,
130
- function: true,
131
- }),
132
- )(obj),
133
- ),
134
- ),
135
- );
136
-
137
- const write_array_functions =
138
- (importer: FunctionImporter) =>
139
- (collection: MetadataCollection): ts.VariableStatement[] =>
140
- collection
141
- .arrays()
142
- .filter((a) => a.recursive)
143
- .map((array, i) =>
144
- StatementFactory.constant(
145
- PREFIX.array(i),
146
- ts.factory.createArrowFunction(
147
- undefined,
148
- undefined,
149
- [
150
- IdentifierFactory.parameter(
151
- "length",
152
- TypeFactory.keyword("number"),
153
- ),
154
- IdentifierFactory.parameter(
155
- "_recursive",
156
- TypeFactory.keyword("boolean"),
157
- ts.factory.createTrue(),
158
- ),
159
- IdentifierFactory.parameter(
160
- "_depth",
161
- TypeFactory.keyword("number"),
162
- ts.factory.createNumericLiteral(0),
163
- ),
164
- ],
165
- TypeFactory.keyword("any"),
166
- undefined,
167
- RandomJoiner.array(COALESCE(importer))(
168
- decode(importer)({
169
- recursive: true,
170
- function: true,
171
- }),
172
- )({
173
- recursive: true,
174
- function: true,
175
- })(ts.factory.createIdentifier("length"))(
176
- array.value,
177
- [],
178
- [],
179
- ),
180
- ),
181
- ),
182
- );
183
-
184
- const write_tuple_functions =
185
- (importer: FunctionImporter) =>
186
- (collection: MetadataCollection): ts.VariableStatement[] =>
187
- collection
188
- .tuples()
189
- .filter((a) => a.recursive)
190
- .map((tuple, i) =>
191
- StatementFactory.constant(
192
- PREFIX.tuple(i),
193
- ts.factory.createArrowFunction(
194
- undefined,
195
- undefined,
196
- [
197
- IdentifierFactory.parameter(
198
- "_recursive",
199
- TypeFactory.keyword("boolean"),
200
- ts.factory.createTrue(),
201
- ),
202
- IdentifierFactory.parameter(
203
- "_depth",
204
- TypeFactory.keyword("number"),
205
- ts.factory.createNumericLiteral(0),
206
- ),
207
- ],
208
- TypeFactory.keyword("any"),
209
- undefined,
210
- RandomJoiner.tuple(
211
- decode(importer)({
212
- function: true,
213
- recursive: true,
214
- }),
215
- )(tuple.elements, [], []),
216
- ),
217
- ),
218
- );
219
-
220
- /* -----------------------------------------------------------
221
- DECODERS
222
- ----------------------------------------------------------- */
223
- const decode =
224
- (importer: FunctionImporter) =>
225
- (explore: IExplore) =>
226
- (
227
- meta: Metadata,
228
- tags: IMetadataTag[],
229
- comments: ICommentTag[],
230
- ): ts.Expression => {
231
- const expressions: ts.Expression[] = [];
232
- if (meta.any)
233
- expressions.push(
234
- ts.factory.createStringLiteral("any type used..."),
235
- );
236
-
237
- // NULL COALESCING
238
- if (meta.isRequired() === false || meta.functional === true)
239
- expressions.push(ts.factory.createIdentifier("undefined"));
240
- if (meta.nullable === true)
241
- expressions.push(ts.factory.createNull());
242
-
243
- // CONSTANT TYPES
244
- for (const constant of meta.constants)
245
- for (const value of constant.values)
246
- expressions.push(decode_atomic(value));
247
-
248
- // ATOMIC VARIABLES
249
- for (const template of meta.templates)
250
- expressions.push(decode_template(importer)(explore)(template));
251
- for (const atomic of meta.atomics)
252
- if (atomic === "boolean")
253
- expressions.push(decode_boolean(importer));
254
- else if (atomic === "number")
255
- expressions.push(decode_number(importer)(tags, comments));
256
- else if (atomic === "string")
257
- expressions.push(decode_string(importer)(tags, comments));
258
- else if (atomic === "bigint")
259
- expressions.push(decode_bigint(importer)(tags, comments));
260
-
261
- // INSTANCE TYPES
262
- if (meta.escaped)
263
- expressions.push(
264
- decode(importer)(explore)(
265
- meta.escaped.returns,
266
- tags,
267
- comments,
268
- ),
269
- );
270
- for (const array of meta.arrays)
271
- expressions.push(
272
- decode_array(importer)(explore)(array, tags, comments),
273
- );
274
- for (const tuple of meta.tuples)
275
- expressions.push(
276
- decode_tuple(importer)(explore)(tuple, tags, comments),
277
- );
278
- for (const o of meta.objects)
279
- expressions.push(decode_object(importer)(explore)(o));
280
- for (const native of meta.natives)
281
- expressions.push(
282
- decode_native(importer)(native, tags, comments),
283
- );
284
- for (const set of meta.sets)
285
- expressions.push(
286
- decode_set(importer)(explore)(set, tags, comments),
287
- );
288
- for (const map of meta.maps)
289
- expressions.push(
290
- decode_map(importer)(explore)(map, tags, comments),
291
- );
292
-
293
- // PICK UP A TYPE
294
- if (expressions.length === 1) return expressions[0]!;
295
- return ts.factory.createCallExpression(
296
- ts.factory.createCallExpression(
297
- importer.use("pick"),
298
- undefined,
299
- [
300
- ts.factory.createArrayLiteralExpression(
301
- expressions.map((expr) =>
302
- ts.factory.createArrowFunction(
303
- undefined,
304
- undefined,
305
- [],
306
- undefined,
307
- undefined,
308
- expr,
309
- ),
310
- ),
311
- true,
312
- ),
313
- ],
314
- ),
315
- undefined,
316
- undefined,
317
- );
318
- };
319
-
320
- const decode_boolean = (importer: FunctionImporter) =>
321
- ts.factory.createCallExpression(
322
- COALESCE(importer)("boolean"),
323
- undefined,
324
- undefined,
325
- );
326
-
327
- const decode_atomic = (value: Atomic) =>
328
- typeof value === "boolean"
329
- ? ts.factory.createIdentifier(value.toString())
330
- : typeof value === "number"
331
- ? ts.factory.createNumericLiteral(value)
332
- : typeof value === "string"
333
- ? ts.factory.createStringLiteral(value)
334
- : ExpressionFactory.bigint(Number(value));
335
-
336
- const decode_template =
337
- (importer: FunctionImporter) =>
338
- (explore: IExplore) =>
339
- (template: Metadata[]) =>
340
- TemplateFactory.generate(
341
- template.map((meta) => decode(importer)(explore)(meta, [], [])),
342
- );
343
-
344
- const decode_number =
345
- (importer: FunctionImporter) =>
346
- (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression => {
347
- const type = tags.find(
348
- (t) =>
349
- t.kind === "type" &&
350
- (t.value === "int" ||
351
- t.value === "int32" ||
352
- t.value === "int64"),
353
- )
354
- ? "int"
355
- : tags.find(
356
- (t) =>
357
- t.kind === "type" &&
358
- (t.value === "uint" ||
359
- t.value === "uint32" ||
360
- t.value === "uint64"),
361
- )
362
- ? "uint"
363
- : "double";
364
- return random_custom(COALESCE(importer))("number")(comments)(
365
- RandomRanger.number({
366
- type,
367
- transform: (value) =>
368
- ts.factory.createNumericLiteral(value),
369
- setter: (args) =>
370
- ts.factory.createCallExpression(
371
- type === "double" &&
372
- tags.every(
373
- (t) =>
374
- t.kind !== "multipleOf" &&
375
- t.kind !== "step",
376
- )
377
- ? COALESCE(importer)("number")
378
- : COALESCE(importer)("integer"),
379
- undefined,
380
- args.map((val) =>
381
- ts.factory.createNumericLiteral(val),
382
- ),
383
- ),
384
- })({
385
- minimum: 0,
386
- maximum: 100,
387
- gap: 10,
388
- })(tags),
389
- );
390
- };
391
-
392
- const decode_bigint =
393
- (importer: FunctionImporter) =>
394
- (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression =>
395
- random_custom(COALESCE(importer))("bigint")(comments)(
396
- RandomRanger.number({
397
- type: tags.find(
398
- (t) =>
399
- t.kind === "type" &&
400
- (t.value === "uint" || t.value === "uint64"),
401
- )
402
- ? "uint"
403
- : "int",
404
- transform: (value) => ExpressionFactory.bigint(value),
405
- setter: (args) =>
406
- ts.factory.createCallExpression(
407
- COALESCE(importer)("bigint"),
408
- undefined,
409
- args.map((value) =>
410
- ExpressionFactory.bigint(value),
411
- ),
412
- ),
413
- })({
414
- minimum: 0,
415
- maximum: 100,
416
- gap: 10,
417
- })(tags),
418
- );
419
-
420
- const decode_string =
421
- (importer: FunctionImporter) =>
422
- (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression =>
423
- random_custom(COALESCE(importer))("string")(comments)(
424
- (() => {
425
- for (const t of tags)
426
- if (t.kind === "format")
427
- return ts.factory.createCallExpression(
428
- COALESCE(importer)(t.value),
429
- undefined,
430
- undefined,
431
- );
432
- else if (t.kind === "pattern")
433
- return ts.factory.createCallExpression(
434
- COALESCE(importer)("pattern"),
435
- undefined,
436
- [ts.factory.createIdentifier(`/${t.value}/`)],
437
- );
438
-
439
- const tail = RandomRanger.length(COALESCE(importer))({
440
- minimum: 5,
441
- maximum: 25,
442
- gap: 5,
443
- })({
444
- fixed: "length",
445
- minimum: "minLength",
446
- maximum: "maxLength",
447
- })(tags);
448
- return ts.factory.createCallExpression(
449
- COALESCE(importer)("string"),
450
- undefined,
451
- tail ? [tail] : undefined,
452
- );
453
- })(),
454
- );
455
-
456
- const decode_array =
457
- (importer: FunctionImporter) =>
458
- (explore: IExplore) =>
459
- (
460
- array: MetadataArray,
461
- tags: IMetadataTag[],
462
- comments: ICommentTag[],
463
- ) => {
464
- const length: ts.Expression | undefined = RandomRanger.length(
465
- COALESCE(importer),
466
- )({
467
- minimum: 0,
468
- maximum: 3,
469
- gap: 3,
470
- })({
471
- fixed: "items",
472
- minimum: "minItems",
473
- maximum: "maxItems",
474
- })(tags);
475
- if (array.recursive)
476
- return ts.factory.createCallExpression(
477
- ts.factory.createIdentifier(
478
- importer.useLocal(PREFIX.array(array.index!)),
479
- ),
480
- undefined,
481
- [
482
- length ?? COALESCE(importer)("length"),
483
- ts.factory.createTrue(),
484
- explore.recursive
485
- ? ts.factory.createAdd(
486
- ts.factory.createNumericLiteral(1),
487
- ts.factory.createIdentifier("_depth"),
488
- )
489
- : ts.factory.createNumericLiteral(0),
490
- ],
491
- );
492
- const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
493
- decode(importer)(explore),
494
- )(explore)(length)(array.value, tags, comments);
495
- return explore.recursive
496
- ? ts.factory.createConditionalExpression(
497
- ts.factory.createLogicalAnd(
498
- ts.factory.createIdentifier("_recursive"),
499
- ts.factory.createLessThan(
500
- ts.factory.createNumericLiteral(5),
501
- ts.factory.createIdentifier("_depth"),
502
- ),
503
- ),
504
- undefined,
505
- ts.factory.createIdentifier("[]"),
506
- undefined,
507
- expr,
508
- )
509
- : expr;
510
- };
511
-
512
- const decode_tuple =
513
- (importer: FunctionImporter) =>
514
- (explore: IExplore) =>
515
- (
516
- tuple: MetadataTuple,
517
- tags: IMetadataTag[],
518
- comments: ICommentTag[],
519
- ): ts.Expression =>
520
- tuple.recursive
521
- ? ts.factory.createCallExpression(
522
- ts.factory.createIdentifier(
523
- importer.useLocal(PREFIX.tuple(tuple.index!)),
524
- ),
525
- undefined,
526
- [
527
- ts.factory.createTrue(),
528
- explore.recursive
529
- ? ts.factory.createAdd(
530
- ts.factory.createNumericLiteral(1),
531
- ts.factory.createIdentifier("_depth"),
532
- )
533
- : ts.factory.createNumericLiteral(0),
534
- ],
535
- )
536
- : RandomJoiner.tuple(decode(importer)(explore))(
537
- tuple.elements,
538
- tags,
539
- comments,
540
- );
541
-
542
- const decode_object =
543
- (importer: FunctionImporter) =>
544
- (explore: IExplore) =>
545
- (object: MetadataObject) =>
546
- ts.factory.createCallExpression(
547
- ts.factory.createIdentifier(
548
- importer.useLocal(PREFIX.object(object.index)),
549
- ),
550
- undefined,
551
- explore.function
552
- ? [
553
- explore.recursive
554
- ? ts.factory.createTrue()
555
- : ts.factory.createIdentifier("_recursive"),
556
- ts.factory.createConditionalExpression(
557
- ts.factory.createIdentifier("_recursive"),
558
- undefined,
559
- ts.factory.createAdd(
560
- ts.factory.createNumericLiteral(1),
561
- ts.factory.createIdentifier("_depth"),
562
- ),
563
- undefined,
564
- ts.factory.createIdentifier("_depth"),
565
- ),
566
- ]
567
- : undefined,
568
- );
569
-
570
- /* -----------------------------------------------------------
571
- NATIVE CLASSES
572
- ----------------------------------------------------------- */
573
- const decode_set =
574
- (importer: FunctionImporter) =>
575
- (explore: IExplore) =>
576
- (meta: Metadata, tags: IMetadataTag[], comments: ICommentTag[]) =>
577
- ts.factory.createNewExpression(
578
- ts.factory.createIdentifier("Set"),
579
- undefined,
580
- [
581
- decode_array(importer)(explore)(
582
- MetadataArray.create({
583
- value: meta,
584
- recursive: false,
585
- index: null,
586
- nullables: [],
587
- name: `Set<${meta.getName()}>`,
588
- }),
589
- tags,
590
- comments,
591
- ),
592
- ],
593
- );
594
-
595
- const decode_map =
596
- (importer: FunctionImporter) =>
597
- (explore: IExplore) =>
598
- (m: Metadata.Entry, tags: IMetadataTag[], comments: ICommentTag[]) =>
599
- ts.factory.createNewExpression(
600
- ts.factory.createIdentifier("Map"),
601
- undefined,
602
- [
603
- decode_array(importer)(explore)(
604
- MetadataArray.create({
605
- name: `Map<${m.key.getName()}, ${m.value.getName()}>`,
606
- index: null,
607
- recursive: false,
608
- nullables: [],
609
- value: Metadata.create({
610
- ...Metadata.initialize(),
611
- tuples: [
612
- (() => {
613
- const tuple = MetadataTuple.create({
614
- name: `[${m.key.getName()}, ${m.value.getName()}]`,
615
- index: null,
616
- recursive: false,
617
- nullables: [],
618
- elements: [m.key, m.value],
619
- });
620
- tuple.of_map = true;
621
- return tuple;
622
- })(),
623
- ],
624
- }),
625
- }),
626
- tags,
627
- comments,
628
- ),
629
- ],
630
- );
631
-
632
- const decode_native =
633
- (importer: FunctionImporter) =>
634
- (
635
- type: string,
636
- tags: IMetadataTag[],
637
- comments: ICommentTag[],
638
- ): ts.Expression => {
639
- if (type === "Boolean") return decode_boolean(importer);
640
- else if (type === "Number")
641
- return decode_number(importer)(tags, comments);
642
- else if (type === "String")
643
- return decode_string(importer)(tags, comments);
644
- else if (type === "Date") return decode_native_date(importer);
645
- else if (
646
- type === "Uint8Array" ||
647
- type === "Uint8ClampedArray" ||
648
- type === "Uint16Array" ||
649
- type === "Uint32Array" ||
650
- type === "BigUint64Array" ||
651
- type === "Int8Array" ||
652
- type === "Int16Array" ||
653
- type === "Int32Array" ||
654
- type === "BigInt64Array" ||
655
- type === "Float32Array" ||
656
- type === "Float64Array"
657
- )
658
- return decode_native_byte_array(importer)(type);
659
- else if (type === "ArrayBuffer" || type === "SharedArrayBuffer")
660
- return decode_native_array_buffer(importer)(type);
661
- else if (type === "DataView")
662
- return decode_native_data_view(importer);
663
- else
664
- return ts.factory.createNewExpression(
665
- ts.factory.createIdentifier(type),
666
- undefined,
667
- [],
668
- );
669
- };
670
-
671
- const decode_native_date = (importer: FunctionImporter) =>
672
- ts.factory.createNewExpression(
673
- ts.factory.createIdentifier("Date"),
674
- undefined,
675
- [
676
- ts.factory.createCallExpression(
677
- COALESCE(importer)("datetime"),
678
- undefined,
679
- [],
680
- ),
681
- ],
682
- );
683
-
684
- const decode_native_byte_array =
685
- (importer: FunctionImporter) =>
686
- (
687
- type:
688
- | "Uint8Array"
689
- | "Uint8ClampedArray"
690
- | "Uint16Array"
691
- | "Uint32Array"
692
- | "BigUint64Array"
693
- | "Int8Array"
694
- | "Int16Array"
695
- | "Int32Array"
696
- | "BigInt64Array"
697
- | "Float32Array"
698
- | "Float64Array",
699
- ): ts.Expression => {
700
- new BigInt64Array();
701
- const [minimum, maximum]: [number, number] = (() => {
702
- if (type === "Uint8Array" || type === "Uint8ClampedArray")
703
- return [0, 255];
704
- else if (type === "Uint16Array") return [0, 65535];
705
- else if (type === "Uint32Array") return [0, 4294967295];
706
- else if (type === "BigUint64Array")
707
- return [0, 18446744073709551615];
708
- else if (type === "Int8Array") return [-128, 127];
709
- else if (type === "Int16Array") return [-32768, 32767];
710
- else if (type === "Int32Array")
711
- return [-2147483648, 2147483647];
712
- else if (type === "BigInt64Array")
713
- return [-9223372036854775808, 9223372036854775807];
714
- else if (type === "Float32Array")
715
- return [-1.175494351e38, 3.4028235e38];
716
- return [Number.MIN_VALUE, Number.MAX_VALUE];
717
- })();
718
- const literal =
719
- type === "BigInt64Array" || type === "BigUint64Array"
720
- ? ExpressionFactory.bigint
721
- : ts.factory.createNumericLiteral;
722
- return ts.factory.createNewExpression(
723
- ts.factory.createIdentifier(type),
724
- [],
725
- [
726
- ts.factory.createCallExpression(
727
- COALESCE(importer)("array"),
728
- undefined,
729
- [
730
- ts.factory.createArrowFunction(
731
- undefined,
732
- undefined,
733
- [],
734
- TypeFactory.keyword("any"),
735
- undefined,
736
- ts.factory.createCallExpression(
737
- COALESCE(importer)(
738
- type === "Float32Array" ||
739
- type === "Float64Array"
740
- ? "number"
741
- : type === "BigInt64Array" ||
742
- type === "BigUint64Array"
743
- ? "bigint"
744
- : "integer",
745
- ),
746
- undefined,
747
- [literal(minimum), literal(maximum)],
748
- ),
749
- ),
750
- ],
751
- ),
752
- ],
753
- );
754
- };
755
-
756
- const decode_native_array_buffer =
757
- (importer: FunctionImporter) =>
758
- (type: "ArrayBuffer" | "SharedArrayBuffer"): ts.Expression =>
759
- type === "ArrayBuffer"
760
- ? IdentifierFactory.access(
761
- decode_native_byte_array(importer)("Uint8Array"),
762
- )("buffer")
763
- : ExpressionFactory.selfCall(
764
- ts.factory.createBlock(
765
- [
766
- StatementFactory.constant(
767
- "length",
768
- ts.factory.createCallExpression(
769
- COALESCE(importer)("integer"),
770
- undefined,
771
- [],
772
- ),
773
- ),
774
- StatementFactory.constant(
775
- "buffer",
776
- ts.factory.createNewExpression(
777
- ts.factory.createIdentifier(
778
- "SharedArrayBuffer",
779
- ),
780
- [],
781
- [ts.factory.createIdentifier("length")],
782
- ),
783
- ),
784
- StatementFactory.constant(
785
- "bytes",
786
- ts.factory.createNewExpression(
787
- ts.factory.createIdentifier("Uint8Array"),
788
- [],
789
- [ts.factory.createIdentifier("buffer")],
790
- ),
791
- ),
792
- ts.factory.createExpressionStatement(
793
- ts.factory.createCallExpression(
794
- IdentifierFactory.access(
795
- ts.factory.createIdentifier("bytes"),
796
- )("set"),
797
- undefined,
798
- [
799
- ts.factory.createCallExpression(
800
- COALESCE(importer)("array"),
801
- undefined,
802
- [
803
- ts.factory.createArrowFunction(
804
- undefined,
805
- undefined,
806
- [],
807
- TypeFactory.keyword(
808
- "any",
809
- ),
810
- undefined,
811
- ts.factory.createCallExpression(
812
- COALESCE(importer)(
813
- "integer",
814
- ),
815
- undefined,
816
- [
817
- ts.factory.createNumericLiteral(
818
- 0,
819
- ),
820
- ts.factory.createNumericLiteral(
821
- 255,
822
- ),
823
- ],
824
- ),
825
- ),
826
- ts.factory.createIdentifier(
827
- "length",
828
- ),
829
- ],
830
- ),
831
- ts.factory.createNumericLiteral(0),
832
- ],
833
- ),
834
- ),
835
- ts.factory.createReturnStatement(
836
- ts.factory.createIdentifier("buffer"),
837
- ),
838
- ],
839
- true,
840
- ),
841
- );
842
-
843
- const decode_native_data_view = (importer: FunctionImporter) =>
844
- ts.factory.createNewExpression(
845
- ts.factory.createIdentifier("DataView"),
846
- [],
847
- [
848
- IdentifierFactory.access(
849
- decode_native_byte_array(importer)("Uint8Array"),
850
- )("buffer"),
851
- ],
852
- );
853
- }
854
-
855
- type Atomic = boolean | number | string | bigint;
856
- interface IExplore {
857
- function: boolean;
858
- recursive: boolean;
859
- }
860
-
861
- const PREFIX = {
862
- object: (i: number) => `$ro${i}`,
863
- array: (i: number) => `$ra${i}`,
864
- tuple: (i: number) => `$rt${i}`,
865
- };
866
-
867
- const COALESCE = (importer: FunctionImporter) => (name: string) =>
868
- ExpressionFactory.coalesce(
869
- ts.factory.createPropertyAccessChain(
870
- ts.factory.createIdentifier("generator"),
871
- ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
872
- ts.factory.createIdentifier(name),
873
- ),
874
- )(IdentifierFactory.access(importer.use("generator"))(name));
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 { StatementFactory } from "../factories/StatementFactory";
8
+ import { TemplateFactory } from "../factories/TemplateFactory";
9
+ import { TypeFactory } from "../factories/TypeFactory";
10
+
11
+ import { ICommentTag } from "../schemas/metadata/ICommentTag";
12
+ import { IMetadataTag } from "../schemas/metadata/IMetadataTag";
13
+ import { Metadata } from "../schemas/metadata/Metadata";
14
+ import { MetadataArray } from "../schemas/metadata/MetadataArray";
15
+ import { MetadataObject } from "../schemas/metadata/MetadataObject";
16
+ import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../transformers/IProject";
19
+
20
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
21
+ import { RandomJoiner } from "./helpers/RandomJoiner";
22
+ import { RandomRanger } from "./helpers/RandomRanger";
23
+ import { random_custom } from "./internal/random_custom";
24
+
25
+ export namespace RandomProgrammer {
26
+ export const write =
27
+ (project: IProject) =>
28
+ (modulo: ts.LeftHandSideExpression) =>
29
+ (init?: ts.Expression) => {
30
+ const importer: FunctionImporter = new FunctionImporter(
31
+ modulo.getText(),
32
+ );
33
+ return (type: ts.Type, name?: string) => {
34
+ // INITIALIZE METADATA
35
+ const collection: MetadataCollection = new MetadataCollection();
36
+ const meta: Metadata = MetadataFactory.analyze(project.checker)(
37
+ {
38
+ escape: false,
39
+ constant: true,
40
+ absorb: true,
41
+ validate: (meta) => {
42
+ if (meta.natives.some((n) => n === "WeakSet"))
43
+ throw new Error(
44
+ `Error on ${modulo.getText()}(): WeakSet is not supported.`,
45
+ );
46
+ else if (meta.natives.some((n) => n === "WeakMap"))
47
+ throw new Error(
48
+ `Error on ${modulo.getText()}(): WeakMap is not supported.`,
49
+ );
50
+ },
51
+ },
52
+ )(collection)(type);
53
+
54
+ // GENERATE FUNCTION
55
+ const functions = {
56
+ objects: write_object_functions(importer)(collection),
57
+ arrays: write_array_functions(importer)(collection),
58
+ tuples: write_tuple_functions(importer)(collection),
59
+ };
60
+
61
+ const output: ts.Expression = decode(importer)({
62
+ function: false,
63
+ recursive: false,
64
+ })(meta, [], []);
65
+
66
+ return ts.factory.createArrowFunction(
67
+ undefined,
68
+ undefined,
69
+ [
70
+ IdentifierFactory.parameter(
71
+ "generator",
72
+ ts.factory.createTypeReferenceNode(
73
+ "Partial<typia.IRandomGenerator>",
74
+ ),
75
+ init ??
76
+ ts.factory.createToken(
77
+ ts.SyntaxKind.QuestionToken,
78
+ ),
79
+ ),
80
+ ],
81
+ ts.factory.createTypeReferenceNode(
82
+ `typia.Resolved<${
83
+ name ??
84
+ TypeFactory.getFullName(project.checker)(type)
85
+ }>`,
86
+ ),
87
+ undefined,
88
+ ts.factory.createBlock(
89
+ [
90
+ ...importer.declare(modulo),
91
+ ...functions.objects,
92
+ ...functions.arrays,
93
+ ...functions.tuples,
94
+ ts.factory.createReturnStatement(output),
95
+ ],
96
+ true,
97
+ ),
98
+ );
99
+ };
100
+ };
101
+
102
+ const write_object_functions =
103
+ (importer: FunctionImporter) =>
104
+ (collection: MetadataCollection): ts.VariableStatement[] =>
105
+ collection.objects().map((obj, i) =>
106
+ StatementFactory.constant(
107
+ PREFIX.object(i),
108
+ ts.factory.createArrowFunction(
109
+ undefined,
110
+ undefined,
111
+ [
112
+ IdentifierFactory.parameter(
113
+ "_recursive",
114
+ TypeFactory.keyword("boolean"),
115
+ ts.factory.createIdentifier(
116
+ String(obj.recursive),
117
+ ),
118
+ ),
119
+ IdentifierFactory.parameter(
120
+ "_depth",
121
+ TypeFactory.keyword("number"),
122
+ ts.factory.createNumericLiteral(0),
123
+ ),
124
+ ],
125
+ TypeFactory.keyword("any"),
126
+ undefined,
127
+ RandomJoiner.object(COALESCE(importer))(
128
+ decode(importer)({
129
+ recursive: obj.recursive,
130
+ function: true,
131
+ }),
132
+ )(obj),
133
+ ),
134
+ ),
135
+ );
136
+
137
+ const write_array_functions =
138
+ (importer: FunctionImporter) =>
139
+ (collection: MetadataCollection): ts.VariableStatement[] =>
140
+ collection
141
+ .arrays()
142
+ .filter((a) => a.recursive)
143
+ .map((array, i) =>
144
+ StatementFactory.constant(
145
+ PREFIX.array(i),
146
+ ts.factory.createArrowFunction(
147
+ undefined,
148
+ undefined,
149
+ [
150
+ IdentifierFactory.parameter(
151
+ "length",
152
+ TypeFactory.keyword("number"),
153
+ ),
154
+ IdentifierFactory.parameter(
155
+ "_recursive",
156
+ TypeFactory.keyword("boolean"),
157
+ ts.factory.createTrue(),
158
+ ),
159
+ IdentifierFactory.parameter(
160
+ "_depth",
161
+ TypeFactory.keyword("number"),
162
+ ts.factory.createNumericLiteral(0),
163
+ ),
164
+ ],
165
+ TypeFactory.keyword("any"),
166
+ undefined,
167
+ RandomJoiner.array(COALESCE(importer))(
168
+ decode(importer)({
169
+ recursive: true,
170
+ function: true,
171
+ }),
172
+ )({
173
+ recursive: true,
174
+ function: true,
175
+ })(ts.factory.createIdentifier("length"))(
176
+ array.value,
177
+ [],
178
+ [],
179
+ ),
180
+ ),
181
+ ),
182
+ );
183
+
184
+ const write_tuple_functions =
185
+ (importer: FunctionImporter) =>
186
+ (collection: MetadataCollection): ts.VariableStatement[] =>
187
+ collection
188
+ .tuples()
189
+ .filter((a) => a.recursive)
190
+ .map((tuple, i) =>
191
+ StatementFactory.constant(
192
+ PREFIX.tuple(i),
193
+ ts.factory.createArrowFunction(
194
+ undefined,
195
+ undefined,
196
+ [
197
+ IdentifierFactory.parameter(
198
+ "_recursive",
199
+ TypeFactory.keyword("boolean"),
200
+ ts.factory.createTrue(),
201
+ ),
202
+ IdentifierFactory.parameter(
203
+ "_depth",
204
+ TypeFactory.keyword("number"),
205
+ ts.factory.createNumericLiteral(0),
206
+ ),
207
+ ],
208
+ TypeFactory.keyword("any"),
209
+ undefined,
210
+ RandomJoiner.tuple(
211
+ decode(importer)({
212
+ function: true,
213
+ recursive: true,
214
+ }),
215
+ )(tuple.elements, [], []),
216
+ ),
217
+ ),
218
+ );
219
+
220
+ /* -----------------------------------------------------------
221
+ DECODERS
222
+ ----------------------------------------------------------- */
223
+ const decode =
224
+ (importer: FunctionImporter) =>
225
+ (explore: IExplore) =>
226
+ (
227
+ meta: Metadata,
228
+ tags: IMetadataTag[],
229
+ comments: ICommentTag[],
230
+ ): ts.Expression => {
231
+ const expressions: ts.Expression[] = [];
232
+ if (meta.any)
233
+ expressions.push(
234
+ ts.factory.createStringLiteral("any type used..."),
235
+ );
236
+
237
+ // NULL COALESCING
238
+ if (meta.isRequired() === false || meta.functional === true)
239
+ expressions.push(ts.factory.createIdentifier("undefined"));
240
+ if (meta.nullable === true)
241
+ expressions.push(ts.factory.createNull());
242
+
243
+ // CONSTANT TYPES
244
+ for (const constant of meta.constants)
245
+ for (const value of constant.values)
246
+ expressions.push(decode_atomic(value));
247
+
248
+ // ATOMIC VARIABLES
249
+ for (const template of meta.templates)
250
+ expressions.push(decode_template(importer)(explore)(template));
251
+ for (const a of meta.atomics)
252
+ if (a.type === "boolean")
253
+ expressions.push(decode_boolean(importer));
254
+ else if (a.type === "number")
255
+ expressions.push(decode_number(importer)(tags, comments));
256
+ else if (a.type === "string")
257
+ expressions.push(decode_string(importer)(tags, comments));
258
+ else if (a.type === "bigint")
259
+ expressions.push(decode_bigint(importer)(tags, comments));
260
+
261
+ // INSTANCE TYPES
262
+ if (meta.escaped)
263
+ expressions.push(
264
+ decode(importer)(explore)(
265
+ meta.escaped.returns,
266
+ tags,
267
+ comments,
268
+ ),
269
+ );
270
+ for (const array of meta.arrays)
271
+ expressions.push(
272
+ decode_array(importer)(explore)(array, tags, comments),
273
+ );
274
+ for (const tuple of meta.tuples)
275
+ expressions.push(
276
+ decode_tuple(importer)(explore)(tuple, tags, comments),
277
+ );
278
+ for (const o of meta.objects)
279
+ expressions.push(decode_object(importer)(explore)(o));
280
+ for (const native of meta.natives)
281
+ expressions.push(
282
+ decode_native(importer)(native, tags, comments),
283
+ );
284
+ for (const set of meta.sets)
285
+ expressions.push(
286
+ decode_set(importer)(explore)(set, tags, comments),
287
+ );
288
+ for (const map of meta.maps)
289
+ expressions.push(
290
+ decode_map(importer)(explore)(map, tags, comments),
291
+ );
292
+
293
+ // PICK UP A TYPE
294
+ if (expressions.length === 1) return expressions[0]!;
295
+ return ts.factory.createCallExpression(
296
+ ts.factory.createCallExpression(
297
+ importer.use("pick"),
298
+ undefined,
299
+ [
300
+ ts.factory.createArrayLiteralExpression(
301
+ expressions.map((expr) =>
302
+ ts.factory.createArrowFunction(
303
+ undefined,
304
+ undefined,
305
+ [],
306
+ undefined,
307
+ undefined,
308
+ expr,
309
+ ),
310
+ ),
311
+ true,
312
+ ),
313
+ ],
314
+ ),
315
+ undefined,
316
+ undefined,
317
+ );
318
+ };
319
+
320
+ const decode_boolean = (importer: FunctionImporter) =>
321
+ ts.factory.createCallExpression(
322
+ COALESCE(importer)("boolean"),
323
+ undefined,
324
+ undefined,
325
+ );
326
+
327
+ const decode_atomic = (value: Atomic) =>
328
+ typeof value === "boolean"
329
+ ? ts.factory.createIdentifier(value.toString())
330
+ : typeof value === "number"
331
+ ? ts.factory.createNumericLiteral(value)
332
+ : typeof value === "string"
333
+ ? ts.factory.createStringLiteral(value)
334
+ : ExpressionFactory.bigint(Number(value));
335
+
336
+ const decode_template =
337
+ (importer: FunctionImporter) =>
338
+ (explore: IExplore) =>
339
+ (template: Metadata[]) =>
340
+ TemplateFactory.generate(
341
+ template.map((meta) => decode(importer)(explore)(meta, [], [])),
342
+ );
343
+
344
+ const decode_number =
345
+ (importer: FunctionImporter) =>
346
+ (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression => {
347
+ const type = tags.find(
348
+ (t) =>
349
+ t.kind === "type" &&
350
+ (t.value === "int" ||
351
+ t.value === "int32" ||
352
+ t.value === "int64"),
353
+ )
354
+ ? "int"
355
+ : tags.find(
356
+ (t) =>
357
+ t.kind === "type" &&
358
+ (t.value === "uint" ||
359
+ t.value === "uint32" ||
360
+ t.value === "uint64"),
361
+ )
362
+ ? "uint"
363
+ : "double";
364
+ return random_custom(COALESCE(importer))("number")(comments)(
365
+ RandomRanger.number({
366
+ type,
367
+ transform: (value) =>
368
+ ts.factory.createNumericLiteral(value),
369
+ setter: (args) =>
370
+ ts.factory.createCallExpression(
371
+ type === "double" &&
372
+ tags.every(
373
+ (t) =>
374
+ t.kind !== "multipleOf" &&
375
+ t.kind !== "step",
376
+ )
377
+ ? COALESCE(importer)("number")
378
+ : COALESCE(importer)("integer"),
379
+ undefined,
380
+ args.map((val) =>
381
+ ts.factory.createNumericLiteral(val),
382
+ ),
383
+ ),
384
+ })({
385
+ minimum: 0,
386
+ maximum: 100,
387
+ gap: 10,
388
+ })(tags),
389
+ );
390
+ };
391
+
392
+ const decode_bigint =
393
+ (importer: FunctionImporter) =>
394
+ (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression =>
395
+ random_custom(COALESCE(importer))("bigint")(comments)(
396
+ RandomRanger.number({
397
+ type: tags.find(
398
+ (t) =>
399
+ t.kind === "type" &&
400
+ (t.value === "uint" || t.value === "uint64"),
401
+ )
402
+ ? "uint"
403
+ : "int",
404
+ transform: (value) => ExpressionFactory.bigint(value),
405
+ setter: (args) =>
406
+ ts.factory.createCallExpression(
407
+ COALESCE(importer)("bigint"),
408
+ undefined,
409
+ args.map((value) =>
410
+ ExpressionFactory.bigint(value),
411
+ ),
412
+ ),
413
+ })({
414
+ minimum: 0,
415
+ maximum: 100,
416
+ gap: 10,
417
+ })(tags),
418
+ );
419
+
420
+ const decode_string =
421
+ (importer: FunctionImporter) =>
422
+ (tags: IMetadataTag[], comments: ICommentTag[]): ts.Expression =>
423
+ random_custom(COALESCE(importer))("string")(comments)(
424
+ (() => {
425
+ for (const t of tags)
426
+ if (t.kind === "format")
427
+ return ts.factory.createCallExpression(
428
+ COALESCE(importer)(t.value),
429
+ undefined,
430
+ undefined,
431
+ );
432
+ else if (t.kind === "pattern")
433
+ return ts.factory.createCallExpression(
434
+ COALESCE(importer)("pattern"),
435
+ undefined,
436
+ [ts.factory.createIdentifier(`/${t.value}/`)],
437
+ );
438
+
439
+ const tail = RandomRanger.length(COALESCE(importer))({
440
+ minimum: 5,
441
+ maximum: 25,
442
+ gap: 5,
443
+ })({
444
+ fixed: "length",
445
+ minimum: "minLength",
446
+ maximum: "maxLength",
447
+ })(tags);
448
+ return ts.factory.createCallExpression(
449
+ COALESCE(importer)("string"),
450
+ undefined,
451
+ tail ? [tail] : undefined,
452
+ );
453
+ })(),
454
+ );
455
+
456
+ const decode_array =
457
+ (importer: FunctionImporter) =>
458
+ (explore: IExplore) =>
459
+ (
460
+ array: MetadataArray,
461
+ tags: IMetadataTag[],
462
+ comments: ICommentTag[],
463
+ ) => {
464
+ const length: ts.Expression | undefined = RandomRanger.length(
465
+ COALESCE(importer),
466
+ )({
467
+ minimum: 0,
468
+ maximum: 3,
469
+ gap: 3,
470
+ })({
471
+ fixed: "items",
472
+ minimum: "minItems",
473
+ maximum: "maxItems",
474
+ })(tags);
475
+ if (array.recursive)
476
+ return ts.factory.createCallExpression(
477
+ ts.factory.createIdentifier(
478
+ importer.useLocal(PREFIX.array(array.index!)),
479
+ ),
480
+ undefined,
481
+ [
482
+ length ?? COALESCE(importer)("length"),
483
+ ts.factory.createTrue(),
484
+ explore.recursive
485
+ ? ts.factory.createAdd(
486
+ ts.factory.createNumericLiteral(1),
487
+ ts.factory.createIdentifier("_depth"),
488
+ )
489
+ : ts.factory.createNumericLiteral(0),
490
+ ],
491
+ );
492
+ const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
493
+ decode(importer)(explore),
494
+ )(explore)(length)(array.value, tags, comments);
495
+ return explore.recursive
496
+ ? ts.factory.createConditionalExpression(
497
+ ts.factory.createLogicalAnd(
498
+ ts.factory.createIdentifier("_recursive"),
499
+ ts.factory.createLessThan(
500
+ ts.factory.createNumericLiteral(5),
501
+ ts.factory.createIdentifier("_depth"),
502
+ ),
503
+ ),
504
+ undefined,
505
+ ts.factory.createIdentifier("[]"),
506
+ undefined,
507
+ expr,
508
+ )
509
+ : expr;
510
+ };
511
+
512
+ const decode_tuple =
513
+ (importer: FunctionImporter) =>
514
+ (explore: IExplore) =>
515
+ (
516
+ tuple: MetadataTuple,
517
+ tags: IMetadataTag[],
518
+ comments: ICommentTag[],
519
+ ): ts.Expression =>
520
+ tuple.recursive
521
+ ? ts.factory.createCallExpression(
522
+ ts.factory.createIdentifier(
523
+ importer.useLocal(PREFIX.tuple(tuple.index!)),
524
+ ),
525
+ undefined,
526
+ [
527
+ ts.factory.createTrue(),
528
+ explore.recursive
529
+ ? ts.factory.createAdd(
530
+ ts.factory.createNumericLiteral(1),
531
+ ts.factory.createIdentifier("_depth"),
532
+ )
533
+ : ts.factory.createNumericLiteral(0),
534
+ ],
535
+ )
536
+ : RandomJoiner.tuple(decode(importer)(explore))(
537
+ tuple.elements,
538
+ tags,
539
+ comments,
540
+ );
541
+
542
+ const decode_object =
543
+ (importer: FunctionImporter) =>
544
+ (explore: IExplore) =>
545
+ (object: MetadataObject) =>
546
+ ts.factory.createCallExpression(
547
+ ts.factory.createIdentifier(
548
+ importer.useLocal(PREFIX.object(object.index)),
549
+ ),
550
+ undefined,
551
+ explore.function
552
+ ? [
553
+ explore.recursive
554
+ ? ts.factory.createTrue()
555
+ : ts.factory.createIdentifier("_recursive"),
556
+ ts.factory.createConditionalExpression(
557
+ ts.factory.createIdentifier("_recursive"),
558
+ undefined,
559
+ ts.factory.createAdd(
560
+ ts.factory.createNumericLiteral(1),
561
+ ts.factory.createIdentifier("_depth"),
562
+ ),
563
+ undefined,
564
+ ts.factory.createIdentifier("_depth"),
565
+ ),
566
+ ]
567
+ : undefined,
568
+ );
569
+
570
+ /* -----------------------------------------------------------
571
+ NATIVE CLASSES
572
+ ----------------------------------------------------------- */
573
+ const decode_set =
574
+ (importer: FunctionImporter) =>
575
+ (explore: IExplore) =>
576
+ (meta: Metadata, tags: IMetadataTag[], comments: ICommentTag[]) =>
577
+ ts.factory.createNewExpression(
578
+ ts.factory.createIdentifier("Set"),
579
+ undefined,
580
+ [
581
+ decode_array(importer)(explore)(
582
+ MetadataArray.create({
583
+ value: meta,
584
+ recursive: false,
585
+ index: null,
586
+ nullables: [],
587
+ name: `Set<${meta.getName()}>`,
588
+ }),
589
+ tags,
590
+ comments,
591
+ ),
592
+ ],
593
+ );
594
+
595
+ const decode_map =
596
+ (importer: FunctionImporter) =>
597
+ (explore: IExplore) =>
598
+ (m: Metadata.Entry, tags: IMetadataTag[], comments: ICommentTag[]) =>
599
+ ts.factory.createNewExpression(
600
+ ts.factory.createIdentifier("Map"),
601
+ undefined,
602
+ [
603
+ decode_array(importer)(explore)(
604
+ MetadataArray.create({
605
+ name: `Map<${m.key.getName()}, ${m.value.getName()}>`,
606
+ index: null,
607
+ recursive: false,
608
+ nullables: [],
609
+ value: Metadata.create({
610
+ ...Metadata.initialize(),
611
+ tuples: [
612
+ (() => {
613
+ const tuple = MetadataTuple.create({
614
+ name: `[${m.key.getName()}, ${m.value.getName()}]`,
615
+ index: null,
616
+ recursive: false,
617
+ nullables: [],
618
+ elements: [m.key, m.value],
619
+ });
620
+ tuple.of_map = true;
621
+ return tuple;
622
+ })(),
623
+ ],
624
+ }),
625
+ }),
626
+ tags,
627
+ comments,
628
+ ),
629
+ ],
630
+ );
631
+
632
+ const decode_native =
633
+ (importer: FunctionImporter) =>
634
+ (
635
+ type: string,
636
+ tags: IMetadataTag[],
637
+ comments: ICommentTag[],
638
+ ): ts.Expression => {
639
+ if (type === "Boolean") return decode_boolean(importer);
640
+ else if (type === "Number")
641
+ return decode_number(importer)(tags, comments);
642
+ else if (type === "String")
643
+ return decode_string(importer)(tags, comments);
644
+ else if (type === "Date") return decode_native_date(importer);
645
+ else if (
646
+ type === "Uint8Array" ||
647
+ type === "Uint8ClampedArray" ||
648
+ type === "Uint16Array" ||
649
+ type === "Uint32Array" ||
650
+ type === "BigUint64Array" ||
651
+ type === "Int8Array" ||
652
+ type === "Int16Array" ||
653
+ type === "Int32Array" ||
654
+ type === "BigInt64Array" ||
655
+ type === "Float32Array" ||
656
+ type === "Float64Array"
657
+ )
658
+ return decode_native_byte_array(importer)(type);
659
+ else if (type === "ArrayBuffer" || type === "SharedArrayBuffer")
660
+ return decode_native_array_buffer(importer)(type);
661
+ else if (type === "DataView")
662
+ return decode_native_data_view(importer);
663
+ else
664
+ return ts.factory.createNewExpression(
665
+ ts.factory.createIdentifier(type),
666
+ undefined,
667
+ [],
668
+ );
669
+ };
670
+
671
+ const decode_native_date = (importer: FunctionImporter) =>
672
+ ts.factory.createNewExpression(
673
+ ts.factory.createIdentifier("Date"),
674
+ undefined,
675
+ [
676
+ ts.factory.createCallExpression(
677
+ COALESCE(importer)("datetime"),
678
+ undefined,
679
+ [],
680
+ ),
681
+ ],
682
+ );
683
+
684
+ const decode_native_byte_array =
685
+ (importer: FunctionImporter) =>
686
+ (
687
+ type:
688
+ | "Uint8Array"
689
+ | "Uint8ClampedArray"
690
+ | "Uint16Array"
691
+ | "Uint32Array"
692
+ | "BigUint64Array"
693
+ | "Int8Array"
694
+ | "Int16Array"
695
+ | "Int32Array"
696
+ | "BigInt64Array"
697
+ | "Float32Array"
698
+ | "Float64Array",
699
+ ): ts.Expression => {
700
+ new BigInt64Array();
701
+ const [minimum, maximum]: [number, number] = (() => {
702
+ if (type === "Uint8Array" || type === "Uint8ClampedArray")
703
+ return [0, 255];
704
+ else if (type === "Uint16Array") return [0, 65535];
705
+ else if (type === "Uint32Array") return [0, 4294967295];
706
+ else if (type === "BigUint64Array")
707
+ return [0, 18446744073709551615];
708
+ else if (type === "Int8Array") return [-128, 127];
709
+ else if (type === "Int16Array") return [-32768, 32767];
710
+ else if (type === "Int32Array")
711
+ return [-2147483648, 2147483647];
712
+ else if (type === "BigInt64Array")
713
+ return [-9223372036854775808, 9223372036854775807];
714
+ else if (type === "Float32Array")
715
+ return [-1.175494351e38, 3.4028235e38];
716
+ return [Number.MIN_VALUE, Number.MAX_VALUE];
717
+ })();
718
+ const literal =
719
+ type === "BigInt64Array" || type === "BigUint64Array"
720
+ ? ExpressionFactory.bigint
721
+ : ts.factory.createNumericLiteral;
722
+ return ts.factory.createNewExpression(
723
+ ts.factory.createIdentifier(type),
724
+ [],
725
+ [
726
+ ts.factory.createCallExpression(
727
+ COALESCE(importer)("array"),
728
+ undefined,
729
+ [
730
+ ts.factory.createArrowFunction(
731
+ undefined,
732
+ undefined,
733
+ [],
734
+ TypeFactory.keyword("any"),
735
+ undefined,
736
+ ts.factory.createCallExpression(
737
+ COALESCE(importer)(
738
+ type === "Float32Array" ||
739
+ type === "Float64Array"
740
+ ? "number"
741
+ : type === "BigInt64Array" ||
742
+ type === "BigUint64Array"
743
+ ? "bigint"
744
+ : "integer",
745
+ ),
746
+ undefined,
747
+ [literal(minimum), literal(maximum)],
748
+ ),
749
+ ),
750
+ ],
751
+ ),
752
+ ],
753
+ );
754
+ };
755
+
756
+ const decode_native_array_buffer =
757
+ (importer: FunctionImporter) =>
758
+ (type: "ArrayBuffer" | "SharedArrayBuffer"): ts.Expression =>
759
+ type === "ArrayBuffer"
760
+ ? IdentifierFactory.access(
761
+ decode_native_byte_array(importer)("Uint8Array"),
762
+ )("buffer")
763
+ : ExpressionFactory.selfCall(
764
+ ts.factory.createBlock(
765
+ [
766
+ StatementFactory.constant(
767
+ "length",
768
+ ts.factory.createCallExpression(
769
+ COALESCE(importer)("integer"),
770
+ undefined,
771
+ [],
772
+ ),
773
+ ),
774
+ StatementFactory.constant(
775
+ "buffer",
776
+ ts.factory.createNewExpression(
777
+ ts.factory.createIdentifier(
778
+ "SharedArrayBuffer",
779
+ ),
780
+ [],
781
+ [ts.factory.createIdentifier("length")],
782
+ ),
783
+ ),
784
+ StatementFactory.constant(
785
+ "bytes",
786
+ ts.factory.createNewExpression(
787
+ ts.factory.createIdentifier("Uint8Array"),
788
+ [],
789
+ [ts.factory.createIdentifier("buffer")],
790
+ ),
791
+ ),
792
+ ts.factory.createExpressionStatement(
793
+ ts.factory.createCallExpression(
794
+ IdentifierFactory.access(
795
+ ts.factory.createIdentifier("bytes"),
796
+ )("set"),
797
+ undefined,
798
+ [
799
+ ts.factory.createCallExpression(
800
+ COALESCE(importer)("array"),
801
+ undefined,
802
+ [
803
+ ts.factory.createArrowFunction(
804
+ undefined,
805
+ undefined,
806
+ [],
807
+ TypeFactory.keyword(
808
+ "any",
809
+ ),
810
+ undefined,
811
+ ts.factory.createCallExpression(
812
+ COALESCE(importer)(
813
+ "integer",
814
+ ),
815
+ undefined,
816
+ [
817
+ ts.factory.createNumericLiteral(
818
+ 0,
819
+ ),
820
+ ts.factory.createNumericLiteral(
821
+ 255,
822
+ ),
823
+ ],
824
+ ),
825
+ ),
826
+ ts.factory.createIdentifier(
827
+ "length",
828
+ ),
829
+ ],
830
+ ),
831
+ ts.factory.createNumericLiteral(0),
832
+ ],
833
+ ),
834
+ ),
835
+ ts.factory.createReturnStatement(
836
+ ts.factory.createIdentifier("buffer"),
837
+ ),
838
+ ],
839
+ true,
840
+ ),
841
+ );
842
+
843
+ const decode_native_data_view = (importer: FunctionImporter) =>
844
+ ts.factory.createNewExpression(
845
+ ts.factory.createIdentifier("DataView"),
846
+ [],
847
+ [
848
+ IdentifierFactory.access(
849
+ decode_native_byte_array(importer)("Uint8Array"),
850
+ )("buffer"),
851
+ ],
852
+ );
853
+ }
854
+
855
+ type Atomic = boolean | number | string | bigint;
856
+ interface IExplore {
857
+ function: boolean;
858
+ recursive: boolean;
859
+ }
860
+
861
+ const PREFIX = {
862
+ object: (i: number) => `$ro${i}`,
863
+ array: (i: number) => `$ra${i}`,
864
+ tuple: (i: number) => `$rt${i}`,
865
+ };
866
+
867
+ const COALESCE = (importer: FunctionImporter) => (name: string) =>
868
+ ExpressionFactory.coalesce(
869
+ ts.factory.createPropertyAccessChain(
870
+ ts.factory.createIdentifier("generator"),
871
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
872
+ ts.factory.createIdentifier(name),
873
+ ),
874
+ )(IdentifierFactory.access(importer.use("generator"))(name));