typia 4.2.1 → 4.2.2
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.
- package/lib/programmers/helpers/RandomRanger.js +1 -1
- package/lib/programmers/helpers/RandomRanger.js.map +1 -1
- package/package.json +1 -1
- package/src/executable/TypiaSetupWizard.ts +153 -153
- package/src/executable/setup/CommandExecutor.ts +8 -8
- package/src/programmers/RandomProgrammer.ts +579 -579
- package/src/programmers/helpers/RandomRanger.ts +1 -1
- package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
- package/src/utils/PatternUtil.ts +33 -33
|
@@ -1,579 +1,579 @@
|
|
|
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 "../metadata/ICommentTag";
|
|
12
|
-
import { IMetadataTag } from "../metadata/IMetadataTag";
|
|
13
|
-
import { Metadata } from "../metadata/Metadata";
|
|
14
|
-
import { MetadataArray } from "../metadata/MetadataArray";
|
|
15
|
-
import { MetadataObject } from "../metadata/MetadataObject";
|
|
16
|
-
import { MetadataTuple } from "../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
|
-
return (type: ts.Type, name?: string) => {
|
|
32
|
-
// INITIALIZE METADATA
|
|
33
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
34
|
-
const meta: Metadata = MetadataFactory.analyze(project.checker)(
|
|
35
|
-
{
|
|
36
|
-
resolve: true,
|
|
37
|
-
constant: true,
|
|
38
|
-
absorb: true,
|
|
39
|
-
},
|
|
40
|
-
)(collection)(type);
|
|
41
|
-
|
|
42
|
-
// GENERATE FUNCTION
|
|
43
|
-
const functions = {
|
|
44
|
-
objects: write_object_functions(importer)(collection),
|
|
45
|
-
arrays: write_array_functions(importer)(collection),
|
|
46
|
-
tuples: write_tuple_functions(importer)(collection),
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const output: ts.Expression = decode(importer)({
|
|
50
|
-
function: false,
|
|
51
|
-
recursive: false,
|
|
52
|
-
})(meta, [], []);
|
|
53
|
-
|
|
54
|
-
return ts.factory.createArrowFunction(
|
|
55
|
-
undefined,
|
|
56
|
-
undefined,
|
|
57
|
-
[
|
|
58
|
-
IdentifierFactory.parameter(
|
|
59
|
-
"generator",
|
|
60
|
-
ts.factory.createTypeReferenceNode(
|
|
61
|
-
"Partial<typia.IRandomGenerator>",
|
|
62
|
-
),
|
|
63
|
-
init ??
|
|
64
|
-
ts.factory.createToken(
|
|
65
|
-
ts.SyntaxKind.QuestionToken,
|
|
66
|
-
),
|
|
67
|
-
),
|
|
68
|
-
],
|
|
69
|
-
ts.factory.createTypeReferenceNode(
|
|
70
|
-
`typia.Primitive<${
|
|
71
|
-
name ??
|
|
72
|
-
TypeFactory.getFullName(project.checker)(type)
|
|
73
|
-
}>`,
|
|
74
|
-
),
|
|
75
|
-
undefined,
|
|
76
|
-
ts.factory.createBlock(
|
|
77
|
-
[
|
|
78
|
-
...importer.declare(modulo),
|
|
79
|
-
...functions.objects,
|
|
80
|
-
...functions.arrays,
|
|
81
|
-
...functions.tuples,
|
|
82
|
-
ts.factory.createReturnStatement(output),
|
|
83
|
-
],
|
|
84
|
-
true,
|
|
85
|
-
),
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const write_object_functions =
|
|
91
|
-
(importer: FunctionImporter) =>
|
|
92
|
-
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
93
|
-
collection.objects().map((obj, i) =>
|
|
94
|
-
StatementFactory.constant(
|
|
95
|
-
PREFIX.object(i),
|
|
96
|
-
ts.factory.createArrowFunction(
|
|
97
|
-
undefined,
|
|
98
|
-
undefined,
|
|
99
|
-
[
|
|
100
|
-
IdentifierFactory.parameter(
|
|
101
|
-
"_recursive",
|
|
102
|
-
TypeFactory.keyword("boolean"),
|
|
103
|
-
ts.factory.createIdentifier(
|
|
104
|
-
String(obj.recursive),
|
|
105
|
-
),
|
|
106
|
-
),
|
|
107
|
-
IdentifierFactory.parameter(
|
|
108
|
-
"_depth",
|
|
109
|
-
TypeFactory.keyword("number"),
|
|
110
|
-
ts.factory.createNumericLiteral(0),
|
|
111
|
-
),
|
|
112
|
-
],
|
|
113
|
-
TypeFactory.keyword("any"),
|
|
114
|
-
undefined,
|
|
115
|
-
RandomJoiner.object(COALESCE(importer))(
|
|
116
|
-
decode(importer)({
|
|
117
|
-
recursive: obj.recursive,
|
|
118
|
-
function: true,
|
|
119
|
-
}),
|
|
120
|
-
)(obj),
|
|
121
|
-
),
|
|
122
|
-
),
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
const write_array_functions =
|
|
126
|
-
(importer: FunctionImporter) =>
|
|
127
|
-
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
128
|
-
collection
|
|
129
|
-
.arrays()
|
|
130
|
-
.filter((a) => a.recursive)
|
|
131
|
-
.map((array, i) =>
|
|
132
|
-
StatementFactory.constant(
|
|
133
|
-
PREFIX.array(i),
|
|
134
|
-
ts.factory.createArrowFunction(
|
|
135
|
-
undefined,
|
|
136
|
-
undefined,
|
|
137
|
-
[
|
|
138
|
-
IdentifierFactory.parameter(
|
|
139
|
-
"length",
|
|
140
|
-
TypeFactory.keyword("number"),
|
|
141
|
-
),
|
|
142
|
-
IdentifierFactory.parameter(
|
|
143
|
-
"_recursive",
|
|
144
|
-
TypeFactory.keyword("boolean"),
|
|
145
|
-
ts.factory.createTrue(),
|
|
146
|
-
),
|
|
147
|
-
IdentifierFactory.parameter(
|
|
148
|
-
"_depth",
|
|
149
|
-
TypeFactory.keyword("number"),
|
|
150
|
-
ts.factory.createNumericLiteral(0),
|
|
151
|
-
),
|
|
152
|
-
],
|
|
153
|
-
TypeFactory.keyword("any"),
|
|
154
|
-
undefined,
|
|
155
|
-
RandomJoiner.array(COALESCE(importer))(
|
|
156
|
-
decode(importer)({
|
|
157
|
-
recursive: true,
|
|
158
|
-
function: true,
|
|
159
|
-
}),
|
|
160
|
-
)({
|
|
161
|
-
recursive: true,
|
|
162
|
-
function: true,
|
|
163
|
-
})(ts.factory.createIdentifier("length"))(
|
|
164
|
-
array.value,
|
|
165
|
-
[],
|
|
166
|
-
[],
|
|
167
|
-
),
|
|
168
|
-
),
|
|
169
|
-
),
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
const write_tuple_functions =
|
|
173
|
-
(importer: FunctionImporter) =>
|
|
174
|
-
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
175
|
-
collection
|
|
176
|
-
.tuples()
|
|
177
|
-
.filter((a) => a.recursive)
|
|
178
|
-
.map((tuple, i) =>
|
|
179
|
-
StatementFactory.constant(
|
|
180
|
-
PREFIX.tuple(i),
|
|
181
|
-
ts.factory.createArrowFunction(
|
|
182
|
-
undefined,
|
|
183
|
-
undefined,
|
|
184
|
-
[
|
|
185
|
-
IdentifierFactory.parameter(
|
|
186
|
-
"_recursive",
|
|
187
|
-
TypeFactory.keyword("boolean"),
|
|
188
|
-
ts.factory.createTrue(),
|
|
189
|
-
),
|
|
190
|
-
IdentifierFactory.parameter(
|
|
191
|
-
"_depth",
|
|
192
|
-
TypeFactory.keyword("number"),
|
|
193
|
-
ts.factory.createNumericLiteral(0),
|
|
194
|
-
),
|
|
195
|
-
],
|
|
196
|
-
TypeFactory.keyword("any"),
|
|
197
|
-
undefined,
|
|
198
|
-
RandomJoiner.tuple(
|
|
199
|
-
decode(importer)({
|
|
200
|
-
function: true,
|
|
201
|
-
recursive: true,
|
|
202
|
-
}),
|
|
203
|
-
)(tuple.elements, [], []),
|
|
204
|
-
),
|
|
205
|
-
),
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
/* -----------------------------------------------------------
|
|
209
|
-
DECODERS
|
|
210
|
-
----------------------------------------------------------- */
|
|
211
|
-
const decode =
|
|
212
|
-
(importer: FunctionImporter) =>
|
|
213
|
-
(explore: IExplore) =>
|
|
214
|
-
(
|
|
215
|
-
meta: Metadata,
|
|
216
|
-
tags: IMetadataTag[],
|
|
217
|
-
comments: ICommentTag[],
|
|
218
|
-
): ts.Expression => {
|
|
219
|
-
const expressions: ts.Expression[] = [];
|
|
220
|
-
if (meta.any)
|
|
221
|
-
expressions.push(
|
|
222
|
-
ts.factory.createStringLiteral("any type used..."),
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
// NULL COALESCING
|
|
226
|
-
if (meta.isRequired() === false)
|
|
227
|
-
expressions.push(ts.factory.createIdentifier("undefined"));
|
|
228
|
-
if (meta.nullable === true)
|
|
229
|
-
expressions.push(ts.factory.createNull());
|
|
230
|
-
|
|
231
|
-
// CONSTANT TYPES
|
|
232
|
-
for (const constant of meta.constants)
|
|
233
|
-
for (const value of constant.values)
|
|
234
|
-
expressions.push(decode_atomic(value));
|
|
235
|
-
|
|
236
|
-
// ATOMIC VARIABLES
|
|
237
|
-
for (const template of meta.templates)
|
|
238
|
-
expressions.push(decode_template(importer)(explore)(template));
|
|
239
|
-
for (const atomic of meta.atomics)
|
|
240
|
-
if (atomic === "boolean")
|
|
241
|
-
expressions.push(decode_boolean(importer));
|
|
242
|
-
else if (atomic === "number")
|
|
243
|
-
expressions.push(decode_number(importer)(tags)(comments));
|
|
244
|
-
else if (atomic === "string")
|
|
245
|
-
expressions.push(decode_string(importer)(tags)(comments));
|
|
246
|
-
else if (atomic === "bigint")
|
|
247
|
-
expressions.push(decode_bigint(importer)(tags)(comments));
|
|
248
|
-
|
|
249
|
-
// INSTANCE TYPES
|
|
250
|
-
if (meta.resolved)
|
|
251
|
-
expressions.push(
|
|
252
|
-
decode(importer)(explore)(
|
|
253
|
-
meta.resolved.returns,
|
|
254
|
-
tags,
|
|
255
|
-
comments,
|
|
256
|
-
),
|
|
257
|
-
);
|
|
258
|
-
for (const array of meta.arrays)
|
|
259
|
-
expressions.push(
|
|
260
|
-
decode_array(importer)(explore)(array, tags, comments),
|
|
261
|
-
);
|
|
262
|
-
for (const tuple of meta.tuples)
|
|
263
|
-
expressions.push(
|
|
264
|
-
decode_tuple(importer)(explore)(tuple, tags, comments),
|
|
265
|
-
);
|
|
266
|
-
for (const o of meta.objects)
|
|
267
|
-
expressions.push(decode_object(importer)(explore)(o));
|
|
268
|
-
for (const native of meta.natives)
|
|
269
|
-
if (native === "Boolean")
|
|
270
|
-
expressions.push(decode_boolean(importer));
|
|
271
|
-
else if (native === "Number")
|
|
272
|
-
expressions.push(decode_number(importer)(tags)(comments));
|
|
273
|
-
else if (native === "String")
|
|
274
|
-
expressions.push(decode_string(importer)(tags)(comments));
|
|
275
|
-
else expressions.push(ts.factory.createIdentifier("{}"));
|
|
276
|
-
if (meta.sets.length || meta.maps.length)
|
|
277
|
-
expressions.push(ts.factory.createIdentifier("{}"));
|
|
278
|
-
|
|
279
|
-
// PRIMITIVE TYPES
|
|
280
|
-
if (expressions.length === 1) return expressions[0]!;
|
|
281
|
-
return ts.factory.createCallExpression(
|
|
282
|
-
ts.factory.createCallExpression(
|
|
283
|
-
importer.use("pick"),
|
|
284
|
-
undefined,
|
|
285
|
-
[
|
|
286
|
-
ts.factory.createArrayLiteralExpression(
|
|
287
|
-
expressions.map((expr) =>
|
|
288
|
-
ts.factory.createArrowFunction(
|
|
289
|
-
undefined,
|
|
290
|
-
undefined,
|
|
291
|
-
[],
|
|
292
|
-
undefined,
|
|
293
|
-
undefined,
|
|
294
|
-
expr,
|
|
295
|
-
),
|
|
296
|
-
),
|
|
297
|
-
true,
|
|
298
|
-
),
|
|
299
|
-
],
|
|
300
|
-
),
|
|
301
|
-
undefined,
|
|
302
|
-
undefined,
|
|
303
|
-
);
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
const decode_boolean = (importer: FunctionImporter) =>
|
|
307
|
-
ts.factory.createCallExpression(
|
|
308
|
-
COALESCE(importer)("boolean"),
|
|
309
|
-
undefined,
|
|
310
|
-
undefined,
|
|
311
|
-
);
|
|
312
|
-
|
|
313
|
-
const decode_atomic = (value: Atomic) =>
|
|
314
|
-
typeof value === "boolean"
|
|
315
|
-
? ts.factory.createIdentifier(value.toString())
|
|
316
|
-
: typeof value === "number"
|
|
317
|
-
? ts.factory.createNumericLiteral(value)
|
|
318
|
-
: typeof value === "string"
|
|
319
|
-
? ts.factory.createStringLiteral(value)
|
|
320
|
-
: ts.factory.createBigIntLiteral(value.toString());
|
|
321
|
-
|
|
322
|
-
const decode_template =
|
|
323
|
-
(importer: FunctionImporter) =>
|
|
324
|
-
(explore: IExplore) =>
|
|
325
|
-
(template: Metadata[]) =>
|
|
326
|
-
TemplateFactory.generate(
|
|
327
|
-
template.map((meta) => decode(importer)(explore)(meta, [], [])),
|
|
328
|
-
);
|
|
329
|
-
|
|
330
|
-
const decode_number =
|
|
331
|
-
(importer: FunctionImporter) =>
|
|
332
|
-
(tags: IMetadataTag[]) =>
|
|
333
|
-
(comments: ICommentTag[]): ts.Expression => {
|
|
334
|
-
const type = tags.find(
|
|
335
|
-
(t) => t.kind === "type" && t.value === "uint",
|
|
336
|
-
)
|
|
337
|
-
? "int"
|
|
338
|
-
: tags.find((t) => t.kind === "type" && t.value === "int")
|
|
339
|
-
? "uint"
|
|
340
|
-
: "double";
|
|
341
|
-
return random_custom(COALESCE(importer))("number")(comments)(
|
|
342
|
-
RandomRanger.number({
|
|
343
|
-
type,
|
|
344
|
-
transform: (value) =>
|
|
345
|
-
ts.factory.createNumericLiteral(value),
|
|
346
|
-
setter: (args) =>
|
|
347
|
-
ts.factory.createCallExpression(
|
|
348
|
-
type === "double" &&
|
|
349
|
-
tags.every(
|
|
350
|
-
(t) =>
|
|
351
|
-
t.kind !== "multipleOf" &&
|
|
352
|
-
t.kind !== "step",
|
|
353
|
-
)
|
|
354
|
-
? COALESCE(importer)("number")
|
|
355
|
-
: COALESCE(importer)("integer"),
|
|
356
|
-
undefined,
|
|
357
|
-
args.map((val) =>
|
|
358
|
-
ts.factory.createNumericLiteral(val),
|
|
359
|
-
),
|
|
360
|
-
),
|
|
361
|
-
})({
|
|
362
|
-
minimum: 0,
|
|
363
|
-
maximum: 100,
|
|
364
|
-
gap: 10,
|
|
365
|
-
})(tags),
|
|
366
|
-
);
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
const decode_bigint =
|
|
370
|
-
(importer: FunctionImporter) =>
|
|
371
|
-
(tags: IMetadataTag[]) =>
|
|
372
|
-
(comments: ICommentTag[]): ts.Expression =>
|
|
373
|
-
random_custom(COALESCE(importer))("bigint")(comments)(
|
|
374
|
-
RandomRanger.number({
|
|
375
|
-
type: tags.find(
|
|
376
|
-
(t) => t.kind === "type" && t.value === "uint",
|
|
377
|
-
)
|
|
378
|
-
? "uint"
|
|
379
|
-
: "int",
|
|
380
|
-
transform: (value) =>
|
|
381
|
-
ts.factory.createCallExpression(
|
|
382
|
-
ts.factory.createIdentifier("BigInt"),
|
|
383
|
-
undefined,
|
|
384
|
-
[ts.factory.createStringLiteral(value.toString())],
|
|
385
|
-
),
|
|
386
|
-
setter: (args) =>
|
|
387
|
-
ts.factory.createCallExpression(
|
|
388
|
-
COALESCE(importer)("bigint"),
|
|
389
|
-
undefined,
|
|
390
|
-
args.map((value) =>
|
|
391
|
-
ts.factory.createCallExpression(
|
|
392
|
-
ts.factory.createIdentifier("BigInt"),
|
|
393
|
-
undefined,
|
|
394
|
-
[
|
|
395
|
-
ts.factory.createStringLiteral(
|
|
396
|
-
value.toString(),
|
|
397
|
-
),
|
|
398
|
-
],
|
|
399
|
-
),
|
|
400
|
-
),
|
|
401
|
-
),
|
|
402
|
-
})({
|
|
403
|
-
minimum: 0,
|
|
404
|
-
maximum: 100,
|
|
405
|
-
gap: 10,
|
|
406
|
-
})(tags),
|
|
407
|
-
);
|
|
408
|
-
|
|
409
|
-
const decode_string =
|
|
410
|
-
(importer: FunctionImporter) =>
|
|
411
|
-
(tags: IMetadataTag[]) =>
|
|
412
|
-
(comments: ICommentTag[]): ts.Expression =>
|
|
413
|
-
random_custom(COALESCE(importer))("string")(comments)(
|
|
414
|
-
(() => {
|
|
415
|
-
for (const t of tags)
|
|
416
|
-
if (t.kind === "format")
|
|
417
|
-
return ts.factory.createCallExpression(
|
|
418
|
-
COALESCE(importer)(t.value),
|
|
419
|
-
undefined,
|
|
420
|
-
undefined,
|
|
421
|
-
);
|
|
422
|
-
else if (t.kind === "pattern")
|
|
423
|
-
return ts.factory.createCallExpression(
|
|
424
|
-
COALESCE(importer)("pattern"),
|
|
425
|
-
undefined,
|
|
426
|
-
[ts.factory.createIdentifier(`/${t.value}/`)],
|
|
427
|
-
);
|
|
428
|
-
|
|
429
|
-
const tail = RandomRanger.length(COALESCE(importer))({
|
|
430
|
-
minimum: 5,
|
|
431
|
-
maximum: 25,
|
|
432
|
-
gap: 5,
|
|
433
|
-
})({
|
|
434
|
-
fixed: "length",
|
|
435
|
-
minimum: "minLength",
|
|
436
|
-
maximum: "maxLength",
|
|
437
|
-
})(tags);
|
|
438
|
-
return ts.factory.createCallExpression(
|
|
439
|
-
COALESCE(importer)("string"),
|
|
440
|
-
undefined,
|
|
441
|
-
tail ? [tail] : undefined,
|
|
442
|
-
);
|
|
443
|
-
})(),
|
|
444
|
-
);
|
|
445
|
-
|
|
446
|
-
const decode_array =
|
|
447
|
-
(importer: FunctionImporter) =>
|
|
448
|
-
(explore: IExplore) =>
|
|
449
|
-
(
|
|
450
|
-
array: MetadataArray,
|
|
451
|
-
tags: IMetadataTag[],
|
|
452
|
-
comments: ICommentTag[],
|
|
453
|
-
) => {
|
|
454
|
-
const length: ts.Expression | undefined = RandomRanger.length(
|
|
455
|
-
COALESCE(importer),
|
|
456
|
-
)({
|
|
457
|
-
minimum: 0,
|
|
458
|
-
maximum: 3,
|
|
459
|
-
gap: 3,
|
|
460
|
-
})({
|
|
461
|
-
fixed: "items",
|
|
462
|
-
minimum: "minItems",
|
|
463
|
-
maximum: "maxItems",
|
|
464
|
-
})(tags);
|
|
465
|
-
if (array.recursive)
|
|
466
|
-
return ts.factory.createCallExpression(
|
|
467
|
-
ts.factory.createIdentifier(
|
|
468
|
-
importer.useLocal(PREFIX.array(array.index!)),
|
|
469
|
-
),
|
|
470
|
-
undefined,
|
|
471
|
-
[
|
|
472
|
-
length ?? COALESCE(importer)("length"),
|
|
473
|
-
ts.factory.createTrue(),
|
|
474
|
-
explore.recursive
|
|
475
|
-
? ts.factory.createAdd(
|
|
476
|
-
ts.factory.createNumericLiteral(1),
|
|
477
|
-
ts.factory.createIdentifier("_depth"),
|
|
478
|
-
)
|
|
479
|
-
: ts.factory.createNumericLiteral(0),
|
|
480
|
-
],
|
|
481
|
-
);
|
|
482
|
-
const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
|
|
483
|
-
decode(importer)(explore),
|
|
484
|
-
)(explore)(length)(array.value, tags, comments);
|
|
485
|
-
return explore.recursive
|
|
486
|
-
? ts.factory.createConditionalExpression(
|
|
487
|
-
ts.factory.createLogicalAnd(
|
|
488
|
-
ts.factory.createIdentifier("_recursive"),
|
|
489
|
-
ts.factory.createLessThan(
|
|
490
|
-
ts.factory.createNumericLiteral(5),
|
|
491
|
-
ts.factory.createIdentifier("_depth"),
|
|
492
|
-
),
|
|
493
|
-
),
|
|
494
|
-
undefined,
|
|
495
|
-
ts.factory.createIdentifier("[]"),
|
|
496
|
-
undefined,
|
|
497
|
-
expr,
|
|
498
|
-
)
|
|
499
|
-
: expr;
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
const decode_tuple =
|
|
503
|
-
(importer: FunctionImporter) =>
|
|
504
|
-
(explore: IExplore) =>
|
|
505
|
-
(
|
|
506
|
-
tuple: MetadataTuple,
|
|
507
|
-
tags: IMetadataTag[],
|
|
508
|
-
comments: ICommentTag[],
|
|
509
|
-
): ts.Expression =>
|
|
510
|
-
tuple.recursive
|
|
511
|
-
? ts.factory.createCallExpression(
|
|
512
|
-
ts.factory.createIdentifier(
|
|
513
|
-
importer.useLocal(PREFIX.tuple(tuple.index!)),
|
|
514
|
-
),
|
|
515
|
-
undefined,
|
|
516
|
-
[
|
|
517
|
-
ts.factory.createTrue(),
|
|
518
|
-
explore.recursive
|
|
519
|
-
? ts.factory.createAdd(
|
|
520
|
-
ts.factory.createNumericLiteral(1),
|
|
521
|
-
ts.factory.createIdentifier("_depth"),
|
|
522
|
-
)
|
|
523
|
-
: ts.factory.createNumericLiteral(0),
|
|
524
|
-
],
|
|
525
|
-
)
|
|
526
|
-
: RandomJoiner.tuple(decode(importer)(explore))(
|
|
527
|
-
tuple.elements,
|
|
528
|
-
tags,
|
|
529
|
-
comments,
|
|
530
|
-
);
|
|
531
|
-
|
|
532
|
-
const decode_object =
|
|
533
|
-
(importer: FunctionImporter) =>
|
|
534
|
-
(explore: IExplore) =>
|
|
535
|
-
(object: MetadataObject) =>
|
|
536
|
-
ts.factory.createCallExpression(
|
|
537
|
-
ts.factory.createIdentifier(
|
|
538
|
-
importer.useLocal(PREFIX.object(object.index)),
|
|
539
|
-
),
|
|
540
|
-
undefined,
|
|
541
|
-
explore.function
|
|
542
|
-
? [
|
|
543
|
-
explore.recursive
|
|
544
|
-
? ts.factory.createTrue()
|
|
545
|
-
: ts.factory.createIdentifier("_recursive"),
|
|
546
|
-
ts.factory.createConditionalExpression(
|
|
547
|
-
ts.factory.createIdentifier("_recursive"),
|
|
548
|
-
undefined,
|
|
549
|
-
ts.factory.createAdd(
|
|
550
|
-
ts.factory.createNumericLiteral(1),
|
|
551
|
-
ts.factory.createIdentifier("_depth"),
|
|
552
|
-
),
|
|
553
|
-
undefined,
|
|
554
|
-
ts.factory.createIdentifier("_depth"),
|
|
555
|
-
),
|
|
556
|
-
]
|
|
557
|
-
: undefined,
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
type Atomic = boolean | number | string | bigint;
|
|
562
|
-
interface IExplore {
|
|
563
|
-
function: boolean;
|
|
564
|
-
recursive: boolean;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
const PREFIX = {
|
|
568
|
-
object: (i: number) => `$ro${i}`,
|
|
569
|
-
array: (i: number) => `$ra${i}`,
|
|
570
|
-
tuple: (i: number) => `$rt${i}`,
|
|
571
|
-
};
|
|
572
|
-
const COALESCE = (importer: FunctionImporter) => (name: string) =>
|
|
573
|
-
ExpressionFactory.coalesce(
|
|
574
|
-
ts.factory.createPropertyAccessChain(
|
|
575
|
-
ts.factory.createIdentifier("generator"),
|
|
576
|
-
ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
|
|
577
|
-
ts.factory.createIdentifier(name),
|
|
578
|
-
),
|
|
579
|
-
)(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 "../metadata/ICommentTag";
|
|
12
|
+
import { IMetadataTag } from "../metadata/IMetadataTag";
|
|
13
|
+
import { Metadata } from "../metadata/Metadata";
|
|
14
|
+
import { MetadataArray } from "../metadata/MetadataArray";
|
|
15
|
+
import { MetadataObject } from "../metadata/MetadataObject";
|
|
16
|
+
import { MetadataTuple } from "../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
|
+
return (type: ts.Type, name?: string) => {
|
|
32
|
+
// INITIALIZE METADATA
|
|
33
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
34
|
+
const meta: Metadata = MetadataFactory.analyze(project.checker)(
|
|
35
|
+
{
|
|
36
|
+
resolve: true,
|
|
37
|
+
constant: true,
|
|
38
|
+
absorb: true,
|
|
39
|
+
},
|
|
40
|
+
)(collection)(type);
|
|
41
|
+
|
|
42
|
+
// GENERATE FUNCTION
|
|
43
|
+
const functions = {
|
|
44
|
+
objects: write_object_functions(importer)(collection),
|
|
45
|
+
arrays: write_array_functions(importer)(collection),
|
|
46
|
+
tuples: write_tuple_functions(importer)(collection),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const output: ts.Expression = decode(importer)({
|
|
50
|
+
function: false,
|
|
51
|
+
recursive: false,
|
|
52
|
+
})(meta, [], []);
|
|
53
|
+
|
|
54
|
+
return ts.factory.createArrowFunction(
|
|
55
|
+
undefined,
|
|
56
|
+
undefined,
|
|
57
|
+
[
|
|
58
|
+
IdentifierFactory.parameter(
|
|
59
|
+
"generator",
|
|
60
|
+
ts.factory.createTypeReferenceNode(
|
|
61
|
+
"Partial<typia.IRandomGenerator>",
|
|
62
|
+
),
|
|
63
|
+
init ??
|
|
64
|
+
ts.factory.createToken(
|
|
65
|
+
ts.SyntaxKind.QuestionToken,
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
],
|
|
69
|
+
ts.factory.createTypeReferenceNode(
|
|
70
|
+
`typia.Primitive<${
|
|
71
|
+
name ??
|
|
72
|
+
TypeFactory.getFullName(project.checker)(type)
|
|
73
|
+
}>`,
|
|
74
|
+
),
|
|
75
|
+
undefined,
|
|
76
|
+
ts.factory.createBlock(
|
|
77
|
+
[
|
|
78
|
+
...importer.declare(modulo),
|
|
79
|
+
...functions.objects,
|
|
80
|
+
...functions.arrays,
|
|
81
|
+
...functions.tuples,
|
|
82
|
+
ts.factory.createReturnStatement(output),
|
|
83
|
+
],
|
|
84
|
+
true,
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const write_object_functions =
|
|
91
|
+
(importer: FunctionImporter) =>
|
|
92
|
+
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
93
|
+
collection.objects().map((obj, i) =>
|
|
94
|
+
StatementFactory.constant(
|
|
95
|
+
PREFIX.object(i),
|
|
96
|
+
ts.factory.createArrowFunction(
|
|
97
|
+
undefined,
|
|
98
|
+
undefined,
|
|
99
|
+
[
|
|
100
|
+
IdentifierFactory.parameter(
|
|
101
|
+
"_recursive",
|
|
102
|
+
TypeFactory.keyword("boolean"),
|
|
103
|
+
ts.factory.createIdentifier(
|
|
104
|
+
String(obj.recursive),
|
|
105
|
+
),
|
|
106
|
+
),
|
|
107
|
+
IdentifierFactory.parameter(
|
|
108
|
+
"_depth",
|
|
109
|
+
TypeFactory.keyword("number"),
|
|
110
|
+
ts.factory.createNumericLiteral(0),
|
|
111
|
+
),
|
|
112
|
+
],
|
|
113
|
+
TypeFactory.keyword("any"),
|
|
114
|
+
undefined,
|
|
115
|
+
RandomJoiner.object(COALESCE(importer))(
|
|
116
|
+
decode(importer)({
|
|
117
|
+
recursive: obj.recursive,
|
|
118
|
+
function: true,
|
|
119
|
+
}),
|
|
120
|
+
)(obj),
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const write_array_functions =
|
|
126
|
+
(importer: FunctionImporter) =>
|
|
127
|
+
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
128
|
+
collection
|
|
129
|
+
.arrays()
|
|
130
|
+
.filter((a) => a.recursive)
|
|
131
|
+
.map((array, i) =>
|
|
132
|
+
StatementFactory.constant(
|
|
133
|
+
PREFIX.array(i),
|
|
134
|
+
ts.factory.createArrowFunction(
|
|
135
|
+
undefined,
|
|
136
|
+
undefined,
|
|
137
|
+
[
|
|
138
|
+
IdentifierFactory.parameter(
|
|
139
|
+
"length",
|
|
140
|
+
TypeFactory.keyword("number"),
|
|
141
|
+
),
|
|
142
|
+
IdentifierFactory.parameter(
|
|
143
|
+
"_recursive",
|
|
144
|
+
TypeFactory.keyword("boolean"),
|
|
145
|
+
ts.factory.createTrue(),
|
|
146
|
+
),
|
|
147
|
+
IdentifierFactory.parameter(
|
|
148
|
+
"_depth",
|
|
149
|
+
TypeFactory.keyword("number"),
|
|
150
|
+
ts.factory.createNumericLiteral(0),
|
|
151
|
+
),
|
|
152
|
+
],
|
|
153
|
+
TypeFactory.keyword("any"),
|
|
154
|
+
undefined,
|
|
155
|
+
RandomJoiner.array(COALESCE(importer))(
|
|
156
|
+
decode(importer)({
|
|
157
|
+
recursive: true,
|
|
158
|
+
function: true,
|
|
159
|
+
}),
|
|
160
|
+
)({
|
|
161
|
+
recursive: true,
|
|
162
|
+
function: true,
|
|
163
|
+
})(ts.factory.createIdentifier("length"))(
|
|
164
|
+
array.value,
|
|
165
|
+
[],
|
|
166
|
+
[],
|
|
167
|
+
),
|
|
168
|
+
),
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const write_tuple_functions =
|
|
173
|
+
(importer: FunctionImporter) =>
|
|
174
|
+
(collection: MetadataCollection): ts.VariableStatement[] =>
|
|
175
|
+
collection
|
|
176
|
+
.tuples()
|
|
177
|
+
.filter((a) => a.recursive)
|
|
178
|
+
.map((tuple, i) =>
|
|
179
|
+
StatementFactory.constant(
|
|
180
|
+
PREFIX.tuple(i),
|
|
181
|
+
ts.factory.createArrowFunction(
|
|
182
|
+
undefined,
|
|
183
|
+
undefined,
|
|
184
|
+
[
|
|
185
|
+
IdentifierFactory.parameter(
|
|
186
|
+
"_recursive",
|
|
187
|
+
TypeFactory.keyword("boolean"),
|
|
188
|
+
ts.factory.createTrue(),
|
|
189
|
+
),
|
|
190
|
+
IdentifierFactory.parameter(
|
|
191
|
+
"_depth",
|
|
192
|
+
TypeFactory.keyword("number"),
|
|
193
|
+
ts.factory.createNumericLiteral(0),
|
|
194
|
+
),
|
|
195
|
+
],
|
|
196
|
+
TypeFactory.keyword("any"),
|
|
197
|
+
undefined,
|
|
198
|
+
RandomJoiner.tuple(
|
|
199
|
+
decode(importer)({
|
|
200
|
+
function: true,
|
|
201
|
+
recursive: true,
|
|
202
|
+
}),
|
|
203
|
+
)(tuple.elements, [], []),
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
/* -----------------------------------------------------------
|
|
209
|
+
DECODERS
|
|
210
|
+
----------------------------------------------------------- */
|
|
211
|
+
const decode =
|
|
212
|
+
(importer: FunctionImporter) =>
|
|
213
|
+
(explore: IExplore) =>
|
|
214
|
+
(
|
|
215
|
+
meta: Metadata,
|
|
216
|
+
tags: IMetadataTag[],
|
|
217
|
+
comments: ICommentTag[],
|
|
218
|
+
): ts.Expression => {
|
|
219
|
+
const expressions: ts.Expression[] = [];
|
|
220
|
+
if (meta.any)
|
|
221
|
+
expressions.push(
|
|
222
|
+
ts.factory.createStringLiteral("any type used..."),
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// NULL COALESCING
|
|
226
|
+
if (meta.isRequired() === false)
|
|
227
|
+
expressions.push(ts.factory.createIdentifier("undefined"));
|
|
228
|
+
if (meta.nullable === true)
|
|
229
|
+
expressions.push(ts.factory.createNull());
|
|
230
|
+
|
|
231
|
+
// CONSTANT TYPES
|
|
232
|
+
for (const constant of meta.constants)
|
|
233
|
+
for (const value of constant.values)
|
|
234
|
+
expressions.push(decode_atomic(value));
|
|
235
|
+
|
|
236
|
+
// ATOMIC VARIABLES
|
|
237
|
+
for (const template of meta.templates)
|
|
238
|
+
expressions.push(decode_template(importer)(explore)(template));
|
|
239
|
+
for (const atomic of meta.atomics)
|
|
240
|
+
if (atomic === "boolean")
|
|
241
|
+
expressions.push(decode_boolean(importer));
|
|
242
|
+
else if (atomic === "number")
|
|
243
|
+
expressions.push(decode_number(importer)(tags)(comments));
|
|
244
|
+
else if (atomic === "string")
|
|
245
|
+
expressions.push(decode_string(importer)(tags)(comments));
|
|
246
|
+
else if (atomic === "bigint")
|
|
247
|
+
expressions.push(decode_bigint(importer)(tags)(comments));
|
|
248
|
+
|
|
249
|
+
// INSTANCE TYPES
|
|
250
|
+
if (meta.resolved)
|
|
251
|
+
expressions.push(
|
|
252
|
+
decode(importer)(explore)(
|
|
253
|
+
meta.resolved.returns,
|
|
254
|
+
tags,
|
|
255
|
+
comments,
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
for (const array of meta.arrays)
|
|
259
|
+
expressions.push(
|
|
260
|
+
decode_array(importer)(explore)(array, tags, comments),
|
|
261
|
+
);
|
|
262
|
+
for (const tuple of meta.tuples)
|
|
263
|
+
expressions.push(
|
|
264
|
+
decode_tuple(importer)(explore)(tuple, tags, comments),
|
|
265
|
+
);
|
|
266
|
+
for (const o of meta.objects)
|
|
267
|
+
expressions.push(decode_object(importer)(explore)(o));
|
|
268
|
+
for (const native of meta.natives)
|
|
269
|
+
if (native === "Boolean")
|
|
270
|
+
expressions.push(decode_boolean(importer));
|
|
271
|
+
else if (native === "Number")
|
|
272
|
+
expressions.push(decode_number(importer)(tags)(comments));
|
|
273
|
+
else if (native === "String")
|
|
274
|
+
expressions.push(decode_string(importer)(tags)(comments));
|
|
275
|
+
else expressions.push(ts.factory.createIdentifier("{}"));
|
|
276
|
+
if (meta.sets.length || meta.maps.length)
|
|
277
|
+
expressions.push(ts.factory.createIdentifier("{}"));
|
|
278
|
+
|
|
279
|
+
// PRIMITIVE TYPES
|
|
280
|
+
if (expressions.length === 1) return expressions[0]!;
|
|
281
|
+
return ts.factory.createCallExpression(
|
|
282
|
+
ts.factory.createCallExpression(
|
|
283
|
+
importer.use("pick"),
|
|
284
|
+
undefined,
|
|
285
|
+
[
|
|
286
|
+
ts.factory.createArrayLiteralExpression(
|
|
287
|
+
expressions.map((expr) =>
|
|
288
|
+
ts.factory.createArrowFunction(
|
|
289
|
+
undefined,
|
|
290
|
+
undefined,
|
|
291
|
+
[],
|
|
292
|
+
undefined,
|
|
293
|
+
undefined,
|
|
294
|
+
expr,
|
|
295
|
+
),
|
|
296
|
+
),
|
|
297
|
+
true,
|
|
298
|
+
),
|
|
299
|
+
],
|
|
300
|
+
),
|
|
301
|
+
undefined,
|
|
302
|
+
undefined,
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const decode_boolean = (importer: FunctionImporter) =>
|
|
307
|
+
ts.factory.createCallExpression(
|
|
308
|
+
COALESCE(importer)("boolean"),
|
|
309
|
+
undefined,
|
|
310
|
+
undefined,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const decode_atomic = (value: Atomic) =>
|
|
314
|
+
typeof value === "boolean"
|
|
315
|
+
? ts.factory.createIdentifier(value.toString())
|
|
316
|
+
: typeof value === "number"
|
|
317
|
+
? ts.factory.createNumericLiteral(value)
|
|
318
|
+
: typeof value === "string"
|
|
319
|
+
? ts.factory.createStringLiteral(value)
|
|
320
|
+
: ts.factory.createBigIntLiteral(value.toString());
|
|
321
|
+
|
|
322
|
+
const decode_template =
|
|
323
|
+
(importer: FunctionImporter) =>
|
|
324
|
+
(explore: IExplore) =>
|
|
325
|
+
(template: Metadata[]) =>
|
|
326
|
+
TemplateFactory.generate(
|
|
327
|
+
template.map((meta) => decode(importer)(explore)(meta, [], [])),
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
const decode_number =
|
|
331
|
+
(importer: FunctionImporter) =>
|
|
332
|
+
(tags: IMetadataTag[]) =>
|
|
333
|
+
(comments: ICommentTag[]): ts.Expression => {
|
|
334
|
+
const type = tags.find(
|
|
335
|
+
(t) => t.kind === "type" && t.value === "uint",
|
|
336
|
+
)
|
|
337
|
+
? "int"
|
|
338
|
+
: tags.find((t) => t.kind === "type" && t.value === "int")
|
|
339
|
+
? "uint"
|
|
340
|
+
: "double";
|
|
341
|
+
return random_custom(COALESCE(importer))("number")(comments)(
|
|
342
|
+
RandomRanger.number({
|
|
343
|
+
type,
|
|
344
|
+
transform: (value) =>
|
|
345
|
+
ts.factory.createNumericLiteral(value),
|
|
346
|
+
setter: (args) =>
|
|
347
|
+
ts.factory.createCallExpression(
|
|
348
|
+
type === "double" &&
|
|
349
|
+
tags.every(
|
|
350
|
+
(t) =>
|
|
351
|
+
t.kind !== "multipleOf" &&
|
|
352
|
+
t.kind !== "step",
|
|
353
|
+
)
|
|
354
|
+
? COALESCE(importer)("number")
|
|
355
|
+
: COALESCE(importer)("integer"),
|
|
356
|
+
undefined,
|
|
357
|
+
args.map((val) =>
|
|
358
|
+
ts.factory.createNumericLiteral(val),
|
|
359
|
+
),
|
|
360
|
+
),
|
|
361
|
+
})({
|
|
362
|
+
minimum: 0,
|
|
363
|
+
maximum: 100,
|
|
364
|
+
gap: 10,
|
|
365
|
+
})(tags),
|
|
366
|
+
);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const decode_bigint =
|
|
370
|
+
(importer: FunctionImporter) =>
|
|
371
|
+
(tags: IMetadataTag[]) =>
|
|
372
|
+
(comments: ICommentTag[]): ts.Expression =>
|
|
373
|
+
random_custom(COALESCE(importer))("bigint")(comments)(
|
|
374
|
+
RandomRanger.number({
|
|
375
|
+
type: tags.find(
|
|
376
|
+
(t) => t.kind === "type" && t.value === "uint",
|
|
377
|
+
)
|
|
378
|
+
? "uint"
|
|
379
|
+
: "int",
|
|
380
|
+
transform: (value) =>
|
|
381
|
+
ts.factory.createCallExpression(
|
|
382
|
+
ts.factory.createIdentifier("BigInt"),
|
|
383
|
+
undefined,
|
|
384
|
+
[ts.factory.createStringLiteral(value.toString())],
|
|
385
|
+
),
|
|
386
|
+
setter: (args) =>
|
|
387
|
+
ts.factory.createCallExpression(
|
|
388
|
+
COALESCE(importer)("bigint"),
|
|
389
|
+
undefined,
|
|
390
|
+
args.map((value) =>
|
|
391
|
+
ts.factory.createCallExpression(
|
|
392
|
+
ts.factory.createIdentifier("BigInt"),
|
|
393
|
+
undefined,
|
|
394
|
+
[
|
|
395
|
+
ts.factory.createStringLiteral(
|
|
396
|
+
value.toString(),
|
|
397
|
+
),
|
|
398
|
+
],
|
|
399
|
+
),
|
|
400
|
+
),
|
|
401
|
+
),
|
|
402
|
+
})({
|
|
403
|
+
minimum: 0,
|
|
404
|
+
maximum: 100,
|
|
405
|
+
gap: 10,
|
|
406
|
+
})(tags),
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const decode_string =
|
|
410
|
+
(importer: FunctionImporter) =>
|
|
411
|
+
(tags: IMetadataTag[]) =>
|
|
412
|
+
(comments: ICommentTag[]): ts.Expression =>
|
|
413
|
+
random_custom(COALESCE(importer))("string")(comments)(
|
|
414
|
+
(() => {
|
|
415
|
+
for (const t of tags)
|
|
416
|
+
if (t.kind === "format")
|
|
417
|
+
return ts.factory.createCallExpression(
|
|
418
|
+
COALESCE(importer)(t.value),
|
|
419
|
+
undefined,
|
|
420
|
+
undefined,
|
|
421
|
+
);
|
|
422
|
+
else if (t.kind === "pattern")
|
|
423
|
+
return ts.factory.createCallExpression(
|
|
424
|
+
COALESCE(importer)("pattern"),
|
|
425
|
+
undefined,
|
|
426
|
+
[ts.factory.createIdentifier(`/${t.value}/`)],
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const tail = RandomRanger.length(COALESCE(importer))({
|
|
430
|
+
minimum: 5,
|
|
431
|
+
maximum: 25,
|
|
432
|
+
gap: 5,
|
|
433
|
+
})({
|
|
434
|
+
fixed: "length",
|
|
435
|
+
minimum: "minLength",
|
|
436
|
+
maximum: "maxLength",
|
|
437
|
+
})(tags);
|
|
438
|
+
return ts.factory.createCallExpression(
|
|
439
|
+
COALESCE(importer)("string"),
|
|
440
|
+
undefined,
|
|
441
|
+
tail ? [tail] : undefined,
|
|
442
|
+
);
|
|
443
|
+
})(),
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
const decode_array =
|
|
447
|
+
(importer: FunctionImporter) =>
|
|
448
|
+
(explore: IExplore) =>
|
|
449
|
+
(
|
|
450
|
+
array: MetadataArray,
|
|
451
|
+
tags: IMetadataTag[],
|
|
452
|
+
comments: ICommentTag[],
|
|
453
|
+
) => {
|
|
454
|
+
const length: ts.Expression | undefined = RandomRanger.length(
|
|
455
|
+
COALESCE(importer),
|
|
456
|
+
)({
|
|
457
|
+
minimum: 0,
|
|
458
|
+
maximum: 3,
|
|
459
|
+
gap: 3,
|
|
460
|
+
})({
|
|
461
|
+
fixed: "items",
|
|
462
|
+
minimum: "minItems",
|
|
463
|
+
maximum: "maxItems",
|
|
464
|
+
})(tags);
|
|
465
|
+
if (array.recursive)
|
|
466
|
+
return ts.factory.createCallExpression(
|
|
467
|
+
ts.factory.createIdentifier(
|
|
468
|
+
importer.useLocal(PREFIX.array(array.index!)),
|
|
469
|
+
),
|
|
470
|
+
undefined,
|
|
471
|
+
[
|
|
472
|
+
length ?? COALESCE(importer)("length"),
|
|
473
|
+
ts.factory.createTrue(),
|
|
474
|
+
explore.recursive
|
|
475
|
+
? ts.factory.createAdd(
|
|
476
|
+
ts.factory.createNumericLiteral(1),
|
|
477
|
+
ts.factory.createIdentifier("_depth"),
|
|
478
|
+
)
|
|
479
|
+
: ts.factory.createNumericLiteral(0),
|
|
480
|
+
],
|
|
481
|
+
);
|
|
482
|
+
const expr: ts.Expression = RandomJoiner.array(COALESCE(importer))(
|
|
483
|
+
decode(importer)(explore),
|
|
484
|
+
)(explore)(length)(array.value, tags, comments);
|
|
485
|
+
return explore.recursive
|
|
486
|
+
? ts.factory.createConditionalExpression(
|
|
487
|
+
ts.factory.createLogicalAnd(
|
|
488
|
+
ts.factory.createIdentifier("_recursive"),
|
|
489
|
+
ts.factory.createLessThan(
|
|
490
|
+
ts.factory.createNumericLiteral(5),
|
|
491
|
+
ts.factory.createIdentifier("_depth"),
|
|
492
|
+
),
|
|
493
|
+
),
|
|
494
|
+
undefined,
|
|
495
|
+
ts.factory.createIdentifier("[]"),
|
|
496
|
+
undefined,
|
|
497
|
+
expr,
|
|
498
|
+
)
|
|
499
|
+
: expr;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
const decode_tuple =
|
|
503
|
+
(importer: FunctionImporter) =>
|
|
504
|
+
(explore: IExplore) =>
|
|
505
|
+
(
|
|
506
|
+
tuple: MetadataTuple,
|
|
507
|
+
tags: IMetadataTag[],
|
|
508
|
+
comments: ICommentTag[],
|
|
509
|
+
): ts.Expression =>
|
|
510
|
+
tuple.recursive
|
|
511
|
+
? ts.factory.createCallExpression(
|
|
512
|
+
ts.factory.createIdentifier(
|
|
513
|
+
importer.useLocal(PREFIX.tuple(tuple.index!)),
|
|
514
|
+
),
|
|
515
|
+
undefined,
|
|
516
|
+
[
|
|
517
|
+
ts.factory.createTrue(),
|
|
518
|
+
explore.recursive
|
|
519
|
+
? ts.factory.createAdd(
|
|
520
|
+
ts.factory.createNumericLiteral(1),
|
|
521
|
+
ts.factory.createIdentifier("_depth"),
|
|
522
|
+
)
|
|
523
|
+
: ts.factory.createNumericLiteral(0),
|
|
524
|
+
],
|
|
525
|
+
)
|
|
526
|
+
: RandomJoiner.tuple(decode(importer)(explore))(
|
|
527
|
+
tuple.elements,
|
|
528
|
+
tags,
|
|
529
|
+
comments,
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
const decode_object =
|
|
533
|
+
(importer: FunctionImporter) =>
|
|
534
|
+
(explore: IExplore) =>
|
|
535
|
+
(object: MetadataObject) =>
|
|
536
|
+
ts.factory.createCallExpression(
|
|
537
|
+
ts.factory.createIdentifier(
|
|
538
|
+
importer.useLocal(PREFIX.object(object.index)),
|
|
539
|
+
),
|
|
540
|
+
undefined,
|
|
541
|
+
explore.function
|
|
542
|
+
? [
|
|
543
|
+
explore.recursive
|
|
544
|
+
? ts.factory.createTrue()
|
|
545
|
+
: ts.factory.createIdentifier("_recursive"),
|
|
546
|
+
ts.factory.createConditionalExpression(
|
|
547
|
+
ts.factory.createIdentifier("_recursive"),
|
|
548
|
+
undefined,
|
|
549
|
+
ts.factory.createAdd(
|
|
550
|
+
ts.factory.createNumericLiteral(1),
|
|
551
|
+
ts.factory.createIdentifier("_depth"),
|
|
552
|
+
),
|
|
553
|
+
undefined,
|
|
554
|
+
ts.factory.createIdentifier("_depth"),
|
|
555
|
+
),
|
|
556
|
+
]
|
|
557
|
+
: undefined,
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
type Atomic = boolean | number | string | bigint;
|
|
562
|
+
interface IExplore {
|
|
563
|
+
function: boolean;
|
|
564
|
+
recursive: boolean;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const PREFIX = {
|
|
568
|
+
object: (i: number) => `$ro${i}`,
|
|
569
|
+
array: (i: number) => `$ra${i}`,
|
|
570
|
+
tuple: (i: number) => `$rt${i}`,
|
|
571
|
+
};
|
|
572
|
+
const COALESCE = (importer: FunctionImporter) => (name: string) =>
|
|
573
|
+
ExpressionFactory.coalesce(
|
|
574
|
+
ts.factory.createPropertyAccessChain(
|
|
575
|
+
ts.factory.createIdentifier("generator"),
|
|
576
|
+
ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
|
|
577
|
+
ts.factory.createIdentifier(name),
|
|
578
|
+
),
|
|
579
|
+
)(IdentifierFactory.access(importer.use("generator"))(name));
|