typia 3.6.2 → 3.6.4

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 (73) hide show
  1. package/README.md +2 -1
  2. package/lib/factories/internal/metadata/emplace_metadata_object.js +8 -7
  3. package/lib/factories/internal/metadata/emplace_metadata_object.js.map +1 -1
  4. package/lib/functional/$is_uuid.js +1 -1
  5. package/lib/functional/$is_uuid.js.map +1 -1
  6. package/lib/module.d.ts +2 -2
  7. package/package.json +1 -1
  8. package/src/executable/setup/ArgumentParser.ts +91 -91
  9. package/src/executable/setup/FileRetriever.ts +33 -33
  10. package/src/executable/setup/PackageManager.ts +92 -92
  11. package/src/executable/setup/PluginConfigurator.ts +99 -99
  12. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -140
  13. package/src/functional/$is_uuid.ts +1 -2
  14. package/src/module.ts +1946 -1946
  15. package/src/programmers/AssertCloneProgrammer.ts +70 -70
  16. package/src/programmers/AssertParseProgrammer.ts +65 -65
  17. package/src/programmers/AssertProgrammer.ts +232 -232
  18. package/src/programmers/AssertPruneProgrammer.ts +67 -67
  19. package/src/programmers/AssertStringifyProgrammer.ts +71 -71
  20. package/src/programmers/CheckerProgrammer.ts +893 -893
  21. package/src/programmers/CloneProgrammer.ts +386 -386
  22. package/src/programmers/FeatureProgrammer.ts +505 -505
  23. package/src/programmers/IsCloneProgrammer.ts +80 -80
  24. package/src/programmers/IsParseProgrammer.ts +74 -74
  25. package/src/programmers/IsPruneProgrammer.ts +75 -75
  26. package/src/programmers/IsStringifyProgrammer.ts +81 -81
  27. package/src/programmers/PruneProgrammer.ts +341 -341
  28. package/src/programmers/RandomProgrammer.ts +391 -391
  29. package/src/programmers/StringifyProgrammer.ts +795 -795
  30. package/src/programmers/ValidateCloneProgrammer.ts +90 -90
  31. package/src/programmers/ValidateParseProgrammer.ts +69 -69
  32. package/src/programmers/ValidateProgrammer.ts +266 -266
  33. package/src/programmers/ValidatePruneProgrammer.ts +83 -83
  34. package/src/programmers/ValidateStringifyProgrammer.ts +89 -89
  35. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  36. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  37. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  38. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  39. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  40. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  41. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  42. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  43. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  44. package/src/transformers/features/miscellaneous/CreateRandomGenerator.ts +42 -42
  45. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  46. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  47. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  48. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  49. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  50. package/src/transformers/features/miscellaneous/RandomTransformer.ts +48 -48
  51. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  52. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  53. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  54. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  55. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  56. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  57. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  58. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  59. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  60. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +9 -9
  61. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  62. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  63. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +11 -11
  64. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  65. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  66. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  67. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  68. package/src/transformers/features/validators/CreateAssertTransformer.ts +12 -12
  69. package/src/transformers/features/validators/CreateIsTransformer.ts +10 -10
  70. package/src/transformers/features/validators/CreateValidateTransformer.ts +12 -12
  71. package/src/transformers/features/validators/IsTransformer.ts +10 -10
  72. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  73. package/src/transformers/internal/GenericTransformer.ts +99 -99
@@ -1,505 +1,505 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../factories/IdentifierFactory";
4
- import { MetadataCollection } from "../factories/MetadataCollection";
5
- import { StatementFactory } from "../factories/StatementFactory";
6
- import { TypeFactory } from "../factories/TypeFactory";
7
- import { ValueFactory } from "../factories/ValueFactory";
8
-
9
- import { IMetadataTag } from "../metadata/IMetadataTag";
10
- import { Metadata } from "../metadata/Metadata";
11
- import { MetadataObject } from "../metadata/MetadataObject";
12
-
13
- import { IProject } from "../transformers/IProject";
14
-
15
- import { CheckerProgrammer } from "./CheckerProgrammer";
16
- import { FunctionImporter } from "./helpers/FunctionImporeter";
17
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
18
- import { UnionExplorer } from "./helpers/UnionExplorer";
19
- import { feature_object_entries } from "./internal/feature_object_entries";
20
-
21
- export namespace FeatureProgrammer {
22
- /* -----------------------------------------------------------
23
- PARAMETERS
24
- ----------------------------------------------------------- */
25
- export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
26
- types: IConfig.ITypes;
27
-
28
- /**
29
- * Prefix name of functions for specific object types.
30
- */
31
- functors: string;
32
-
33
- /**
34
- * Prefix name of functions for union object types.
35
- */
36
- unioners: string;
37
-
38
- /**
39
- * Whether to archive access path or not.
40
- */
41
- path: boolean;
42
-
43
- /**
44
- * Whether to trace exception or not.
45
- */
46
- trace: boolean;
47
-
48
- /**
49
- * Initializer of metadata.
50
- */
51
- initializer(
52
- project: IProject,
53
- type: ts.Type,
54
- ): [MetadataCollection, Metadata];
55
-
56
- /**
57
- * Decoder, station of every types.
58
- */
59
- decoder: Decoder<Metadata, Output>;
60
-
61
- /**
62
- * Object configurator.
63
- */
64
- objector: IConfig.IObjector;
65
-
66
- /**
67
- * Generator of functions for object types.
68
- */
69
- generator?: Partial<IConfig.IGenerator>;
70
- }
71
- export namespace IConfig {
72
- export interface ITypes {
73
- input: (type: ts.Type, name?: string) => ts.TypeNode;
74
- output: (type: ts.Type, name?: string) => ts.TypeNode;
75
- }
76
-
77
- export interface IObjector {
78
- /**
79
- * Type checker when union object type comes.
80
- */
81
- checker: Decoder<Metadata, ts.Expression>;
82
-
83
- /**
84
- * Decoder, function call expression generator of specific typed objects.
85
- */
86
- decoder: Decoder<MetadataObject, ts.Expression>;
87
-
88
- /**
89
- * Joiner of expressions from properties.
90
- */
91
- joiner(
92
- input: ts.Expression,
93
- entries: IExpressionEntry[],
94
- parent: MetadataObject,
95
- ): ts.ConciseBody;
96
-
97
- /**
98
- * Union type specificator.
99
- *
100
- * Expression of an algorithm specifying object type and calling
101
- * the `decoder` function of the specified object type.
102
- */
103
- unionizer: Decoder<MetadataObject[], ts.Expression>;
104
-
105
- /**
106
- * Handler of union type specification failure.
107
- *
108
- * @param value Expression of input parameter
109
- * @param expected Expected type name
110
- * @param explore Exploration info
111
- * @returns Statement of failure
112
- */
113
- failure(
114
- value: ts.Expression,
115
- expected: string,
116
- explore?: IExplore,
117
- ): ts.Statement;
118
-
119
- /**
120
- * Transformer of type checking expression by discrimination.
121
- *
122
- * When an object type has been specified by a discrimination without full
123
- * iteration, the `unionizer` will decode the object instance after
124
- * the last type checking.
125
- *
126
- * In such circumtance, you can transform the last type checking function.
127
- *
128
- * @param exp Current expression about type checking
129
- * @returns Transformed expression
130
- * @deprecated
131
- */
132
- is?(exp: ts.Expression): ts.Expression;
133
-
134
- /**
135
- * Transformer of non-undefined type checking by discrimination.
136
- *
137
- * When specifying an union type of objects, `typia` tries to find
138
- * descrimination way just by checking only one property type.
139
- * If succeeded to find the discrimination way, `typia` will check the target
140
- * property type and in the checking, non-undefined type checking would be
141
- * done.
142
- *
143
- * In such process, you can transform the non-undefined type checking.
144
- *
145
- * @param exp
146
- * @returns Transformed expression
147
- * @deprecated
148
- */
149
- required?(exp: ts.Expression): ts.Expression;
150
-
151
- /**
152
- * Conditon wrapper when unable to specify any object type.
153
- *
154
- * When failed to specify an object type through discrimination, full
155
- * iteration type checking would be happend. In such circumstance, you
156
- * can wrap the condition with additional function.
157
- *
158
- * @param condition Current condition
159
- * @returns A function wrapped current condition
160
- */
161
- full?: (
162
- condition: ts.Expression,
163
- ) => (
164
- input: ts.Expression,
165
- expected: string,
166
- explore: IExplore,
167
- ) => ts.Expression;
168
-
169
- /**
170
- * Return type.
171
- */
172
- type?: ts.TypeNode;
173
- }
174
- export interface IGenerator {
175
- /**
176
- *
177
- * @param col
178
- */
179
- functors(col: MetadataCollection): ts.VariableStatement[];
180
-
181
- /**
182
- *
183
- * @param col
184
- */
185
- unioners(col: MetadataCollection): ts.VariableStatement[];
186
- }
187
- }
188
-
189
- export interface IExplore {
190
- tracable: boolean;
191
- source: "top" | "object";
192
- from: "top" | "array" | "object";
193
- postfix: string;
194
- start?: number;
195
- }
196
-
197
- export interface Decoder<
198
- T,
199
- Output extends ts.ConciseBody = ts.ConciseBody,
200
- > {
201
- (
202
- input: ts.Expression,
203
- target: T,
204
- explore: IExplore,
205
- tags: IMetadataTag[],
206
- ): Output;
207
- }
208
-
209
- /* -----------------------------------------------------------
210
- GENERATORS
211
- ----------------------------------------------------------- */
212
- /**
213
- * Generates a decoder function for a specific type.
214
- *
215
- * @param project Project configuration
216
- * @param config Detailed configuration for programming
217
- * @param importer Function importer
218
- * @param addition Generator of additinal statements in the top of function
219
- * @returns Currying function generating type decoder function
220
- */
221
- export const generate =
222
- (
223
- project: IProject,
224
- config: IConfig,
225
- importer: FunctionImporter,
226
- addition: (
227
- collection: MetadataCollection,
228
- ) => ts.Statement[] | undefined,
229
- ) =>
230
- (type: ts.Type, name?: string) => {
231
- const [collection, meta] = config.initializer(project, type);
232
-
233
- // ITERATE OVER ALL METADATA
234
- const output: ts.ConciseBody = config.decoder(
235
- ValueFactory.INPUT(),
236
- meta,
237
- {
238
- tracable: config.path || config.trace,
239
- source: "top",
240
- from: "top",
241
- postfix: '""',
242
- },
243
- [],
244
- );
245
-
246
- // RETURNS THE OPTIMAL ARROW FUNCTION
247
- const functors: ts.VariableStatement[] = (
248
- config.generator?.functors ??
249
- generate_functors(config)(importer)
250
- )(collection);
251
- const unioners: ts.VariableStatement[] = (
252
- config.generator?.unioners ??
253
- generate_unioners(config)(importer)
254
- )(collection);
255
- const added: ts.Statement[] | undefined = addition(collection);
256
-
257
- return ts.factory.createArrowFunction(
258
- undefined,
259
- undefined,
260
- PARAMETERS(config)(config.types.input(type, name))(
261
- ValueFactory.INPUT(),
262
- ),
263
- config.types.output(type, name),
264
- undefined,
265
- ts.factory.createBlock(
266
- [
267
- ...(added || []),
268
- ...functors.filter((_, i) =>
269
- importer.hasLocal(`${config.functors}${i}`),
270
- ),
271
- ...unioners.filter((_, i) =>
272
- importer.hasLocal(`${config.unioners}${i}`),
273
- ),
274
- ...(ts.isBlock(output)
275
- ? output.statements
276
- : [ts.factory.createReturnStatement(output)]),
277
- ],
278
- true,
279
- ),
280
- );
281
- };
282
-
283
- export const generate_functors =
284
- (config: IConfig) =>
285
- (importer: FunctionImporter) =>
286
- (collection: MetadataCollection) =>
287
- collection
288
- .objects()
289
- .map((obj, i) =>
290
- StatementFactory.constant(
291
- `${config.functors}${i}`,
292
- generate_object(config)(importer)(obj),
293
- ),
294
- );
295
-
296
- export const generate_unioners =
297
- (config: IConfig) =>
298
- (importer: FunctionImporter) =>
299
- (collection: MetadataCollection) =>
300
- collection
301
- .unions()
302
- .map((union, i) =>
303
- StatementFactory.constant(
304
- importer.useLocal(`${config.unioners}${i}`),
305
- generate_union(config)(union),
306
- ),
307
- );
308
-
309
- const generate_object =
310
- (config: IConfig) =>
311
- (importer: FunctionImporter) =>
312
- (obj: MetadataObject) =>
313
- ts.factory.createArrowFunction(
314
- undefined,
315
- undefined,
316
- PARAMETERS(config)(TypeFactory.keyword("any"))(
317
- ValueFactory.INPUT(),
318
- ),
319
- config.objector.type ?? TypeFactory.keyword("any"),
320
- undefined,
321
- config.objector.joiner(
322
- ts.factory.createIdentifier("input"),
323
- feature_object_entries(config)(importer)(obj)(
324
- ts.factory.createIdentifier("input"),
325
- ),
326
- obj,
327
- ),
328
- );
329
-
330
- function generate_union(config: IConfig) {
331
- const explorer = UnionExplorer.object(config);
332
- const input = ValueFactory.INPUT();
333
-
334
- return (meta: MetadataObject[]) =>
335
- ts.factory.createArrowFunction(
336
- undefined,
337
- undefined,
338
- PARAMETERS(config)(TypeFactory.keyword("any"))(
339
- ValueFactory.INPUT(),
340
- ),
341
- TypeFactory.keyword("any"),
342
- undefined,
343
- explorer(
344
- input,
345
- meta,
346
- {
347
- tracable: config.path || config.trace,
348
- source: "object",
349
- from: "object",
350
- postfix: "",
351
- },
352
- [],
353
- ),
354
- );
355
- }
356
-
357
- /* -----------------------------------------------------------
358
- DECODERS
359
- ----------------------------------------------------------- */
360
- export function decode_array(
361
- config: Pick<IConfig, "trace" | "path" | "decoder">,
362
- importer: FunctionImporter,
363
- combiner: (
364
- input: ts.Expression,
365
- arrow: ts.ArrowFunction,
366
- tags: IMetadataTag[],
367
- ) => ts.Expression,
368
- ) {
369
- const rand: string = importer.increment().toString();
370
- const tail =
371
- config.path || config.trace
372
- ? [
373
- IdentifierFactory.parameter(
374
- "_index" + rand,
375
- TypeFactory.keyword("number"),
376
- ),
377
- ]
378
- : [];
379
-
380
- return (
381
- input: ts.Expression,
382
- meta: Metadata,
383
- explore: IExplore,
384
- tags: IMetadataTag[],
385
- ) => {
386
- const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
387
- undefined,
388
- undefined,
389
- [
390
- IdentifierFactory.parameter(
391
- "elem",
392
- TypeFactory.keyword("any"),
393
- ),
394
- ...tail,
395
- ],
396
- undefined,
397
- undefined,
398
- config.decoder(
399
- ValueFactory.INPUT("elem"),
400
- meta,
401
- {
402
- tracable: explore.tracable,
403
- source: explore.source,
404
- from: "array",
405
- postfix: INDEX_SYMBOL(explore.start ?? null)(
406
- explore.postfix,
407
- )(rand),
408
- },
409
- tags,
410
- ),
411
- );
412
- return combiner(input, arrow, tags);
413
- };
414
- }
415
-
416
- export const decode_object =
417
- (config: Pick<IConfig, "trace" | "path" | "functors">) =>
418
- (importer: FunctionImporter) =>
419
- (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
420
- ts.factory.createCallExpression(
421
- ts.factory.createIdentifier(
422
- importer.useLocal(`${config.functors}${obj.index}`),
423
- ),
424
- undefined,
425
- get_object_arguments(config)(explore)(input),
426
- );
427
-
428
- export const get_object_arguments =
429
- (config: Pick<IConfig, "path" | "trace">) =>
430
- (explore: FeatureProgrammer.IExplore) => {
431
- const tail: ts.Expression[] =
432
- config.path === false && config.trace === false
433
- ? []
434
- : config.path === true && config.trace === true
435
- ? [
436
- ts.factory.createIdentifier(
437
- explore.postfix
438
- ? `_path + ${explore.postfix}`
439
- : "_path",
440
- ),
441
- explore.source === "object"
442
- ? ts.factory.createIdentifier(
443
- `${explore.tracable} && _exceptionable`,
444
- )
445
- : explore.tracable
446
- ? ts.factory.createTrue()
447
- : ts.factory.createFalse(),
448
- ]
449
- : config.path === true
450
- ? [
451
- ts.factory.createIdentifier(
452
- explore.postfix
453
- ? `_path + ${explore.postfix}`
454
- : "_path",
455
- ),
456
- ]
457
- : [
458
- explore.source === "object"
459
- ? ts.factory.createIdentifier(
460
- `${explore.tracable} && _exceptionable`,
461
- )
462
- : explore.tracable
463
- ? ts.factory.createTrue()
464
- : ts.factory.createFalse(),
465
- ];
466
- return (input: ts.Expression) => [input, ...tail];
467
- };
468
- }
469
-
470
- const INDEX_SYMBOL =
471
- (start: number | null) => (prev: string) => (rand: string) => {
472
- const tail: string =
473
- start !== null
474
- ? `"[" + (${start} + _index${rand}) + "]"`
475
- : `"[" + _index${rand} + "]"`;
476
- if (prev === "") return tail;
477
- else if (prev[prev.length - 1] === `"`)
478
- return prev.substring(0, prev.length - 1) + tail.substring(1);
479
- return prev + ` + ${tail}`;
480
- };
481
-
482
- const PARAMETERS =
483
- (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
484
- (type: ts.TypeNode) => {
485
- const tail: ts.ParameterDeclaration[] = [];
486
- if (props.path)
487
- tail.push(
488
- IdentifierFactory.parameter(
489
- "_path",
490
- TypeFactory.keyword("string"),
491
- ),
492
- );
493
- if (props.trace)
494
- tail.push(
495
- IdentifierFactory.parameter(
496
- "_exceptionable",
497
- TypeFactory.keyword("boolean"),
498
- ts.factory.createTrue(),
499
- ),
500
- );
501
- return (input: ts.Identifier) => [
502
- IdentifierFactory.parameter(input, type),
503
- ...tail,
504
- ];
505
- };
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
4
+ import { MetadataCollection } from "../factories/MetadataCollection";
5
+ import { StatementFactory } from "../factories/StatementFactory";
6
+ import { TypeFactory } from "../factories/TypeFactory";
7
+ import { ValueFactory } from "../factories/ValueFactory";
8
+
9
+ import { IMetadataTag } from "../metadata/IMetadataTag";
10
+ import { Metadata } from "../metadata/Metadata";
11
+ import { MetadataObject } from "../metadata/MetadataObject";
12
+
13
+ import { IProject } from "../transformers/IProject";
14
+
15
+ import { CheckerProgrammer } from "./CheckerProgrammer";
16
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
17
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
18
+ import { UnionExplorer } from "./helpers/UnionExplorer";
19
+ import { feature_object_entries } from "./internal/feature_object_entries";
20
+
21
+ export namespace FeatureProgrammer {
22
+ /* -----------------------------------------------------------
23
+ PARAMETERS
24
+ ----------------------------------------------------------- */
25
+ export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
26
+ types: IConfig.ITypes;
27
+
28
+ /**
29
+ * Prefix name of functions for specific object types.
30
+ */
31
+ functors: string;
32
+
33
+ /**
34
+ * Prefix name of functions for union object types.
35
+ */
36
+ unioners: string;
37
+
38
+ /**
39
+ * Whether to archive access path or not.
40
+ */
41
+ path: boolean;
42
+
43
+ /**
44
+ * Whether to trace exception or not.
45
+ */
46
+ trace: boolean;
47
+
48
+ /**
49
+ * Initializer of metadata.
50
+ */
51
+ initializer(
52
+ project: IProject,
53
+ type: ts.Type,
54
+ ): [MetadataCollection, Metadata];
55
+
56
+ /**
57
+ * Decoder, station of every types.
58
+ */
59
+ decoder: Decoder<Metadata, Output>;
60
+
61
+ /**
62
+ * Object configurator.
63
+ */
64
+ objector: IConfig.IObjector;
65
+
66
+ /**
67
+ * Generator of functions for object types.
68
+ */
69
+ generator?: Partial<IConfig.IGenerator>;
70
+ }
71
+ export namespace IConfig {
72
+ export interface ITypes {
73
+ input: (type: ts.Type, name?: string) => ts.TypeNode;
74
+ output: (type: ts.Type, name?: string) => ts.TypeNode;
75
+ }
76
+
77
+ export interface IObjector {
78
+ /**
79
+ * Type checker when union object type comes.
80
+ */
81
+ checker: Decoder<Metadata, ts.Expression>;
82
+
83
+ /**
84
+ * Decoder, function call expression generator of specific typed objects.
85
+ */
86
+ decoder: Decoder<MetadataObject, ts.Expression>;
87
+
88
+ /**
89
+ * Joiner of expressions from properties.
90
+ */
91
+ joiner(
92
+ input: ts.Expression,
93
+ entries: IExpressionEntry[],
94
+ parent: MetadataObject,
95
+ ): ts.ConciseBody;
96
+
97
+ /**
98
+ * Union type specificator.
99
+ *
100
+ * Expression of an algorithm specifying object type and calling
101
+ * the `decoder` function of the specified object type.
102
+ */
103
+ unionizer: Decoder<MetadataObject[], ts.Expression>;
104
+
105
+ /**
106
+ * Handler of union type specification failure.
107
+ *
108
+ * @param value Expression of input parameter
109
+ * @param expected Expected type name
110
+ * @param explore Exploration info
111
+ * @returns Statement of failure
112
+ */
113
+ failure(
114
+ value: ts.Expression,
115
+ expected: string,
116
+ explore?: IExplore,
117
+ ): ts.Statement;
118
+
119
+ /**
120
+ * Transformer of type checking expression by discrimination.
121
+ *
122
+ * When an object type has been specified by a discrimination without full
123
+ * iteration, the `unionizer` will decode the object instance after
124
+ * the last type checking.
125
+ *
126
+ * In such circumtance, you can transform the last type checking function.
127
+ *
128
+ * @param exp Current expression about type checking
129
+ * @returns Transformed expression
130
+ * @deprecated
131
+ */
132
+ is?(exp: ts.Expression): ts.Expression;
133
+
134
+ /**
135
+ * Transformer of non-undefined type checking by discrimination.
136
+ *
137
+ * When specifying an union type of objects, `typia` tries to find
138
+ * descrimination way just by checking only one property type.
139
+ * If succeeded to find the discrimination way, `typia` will check the target
140
+ * property type and in the checking, non-undefined type checking would be
141
+ * done.
142
+ *
143
+ * In such process, you can transform the non-undefined type checking.
144
+ *
145
+ * @param exp
146
+ * @returns Transformed expression
147
+ * @deprecated
148
+ */
149
+ required?(exp: ts.Expression): ts.Expression;
150
+
151
+ /**
152
+ * Conditon wrapper when unable to specify any object type.
153
+ *
154
+ * When failed to specify an object type through discrimination, full
155
+ * iteration type checking would be happend. In such circumstance, you
156
+ * can wrap the condition with additional function.
157
+ *
158
+ * @param condition Current condition
159
+ * @returns A function wrapped current condition
160
+ */
161
+ full?: (
162
+ condition: ts.Expression,
163
+ ) => (
164
+ input: ts.Expression,
165
+ expected: string,
166
+ explore: IExplore,
167
+ ) => ts.Expression;
168
+
169
+ /**
170
+ * Return type.
171
+ */
172
+ type?: ts.TypeNode;
173
+ }
174
+ export interface IGenerator {
175
+ /**
176
+ *
177
+ * @param col
178
+ */
179
+ functors(col: MetadataCollection): ts.VariableStatement[];
180
+
181
+ /**
182
+ *
183
+ * @param col
184
+ */
185
+ unioners(col: MetadataCollection): ts.VariableStatement[];
186
+ }
187
+ }
188
+
189
+ export interface IExplore {
190
+ tracable: boolean;
191
+ source: "top" | "object";
192
+ from: "top" | "array" | "object";
193
+ postfix: string;
194
+ start?: number;
195
+ }
196
+
197
+ export interface Decoder<
198
+ T,
199
+ Output extends ts.ConciseBody = ts.ConciseBody,
200
+ > {
201
+ (
202
+ input: ts.Expression,
203
+ target: T,
204
+ explore: IExplore,
205
+ tags: IMetadataTag[],
206
+ ): Output;
207
+ }
208
+
209
+ /* -----------------------------------------------------------
210
+ GENERATORS
211
+ ----------------------------------------------------------- */
212
+ /**
213
+ * Generates a decoder function for a specific type.
214
+ *
215
+ * @param project Project configuration
216
+ * @param config Detailed configuration for programming
217
+ * @param importer Function importer
218
+ * @param addition Generator of additinal statements in the top of function
219
+ * @returns Currying function generating type decoder function
220
+ */
221
+ export const generate =
222
+ (
223
+ project: IProject,
224
+ config: IConfig,
225
+ importer: FunctionImporter,
226
+ addition: (
227
+ collection: MetadataCollection,
228
+ ) => ts.Statement[] | undefined,
229
+ ) =>
230
+ (type: ts.Type, name?: string) => {
231
+ const [collection, meta] = config.initializer(project, type);
232
+
233
+ // ITERATE OVER ALL METADATA
234
+ const output: ts.ConciseBody = config.decoder(
235
+ ValueFactory.INPUT(),
236
+ meta,
237
+ {
238
+ tracable: config.path || config.trace,
239
+ source: "top",
240
+ from: "top",
241
+ postfix: '""',
242
+ },
243
+ [],
244
+ );
245
+
246
+ // RETURNS THE OPTIMAL ARROW FUNCTION
247
+ const functors: ts.VariableStatement[] = (
248
+ config.generator?.functors ??
249
+ generate_functors(config)(importer)
250
+ )(collection);
251
+ const unioners: ts.VariableStatement[] = (
252
+ config.generator?.unioners ??
253
+ generate_unioners(config)(importer)
254
+ )(collection);
255
+ const added: ts.Statement[] | undefined = addition(collection);
256
+
257
+ return ts.factory.createArrowFunction(
258
+ undefined,
259
+ undefined,
260
+ PARAMETERS(config)(config.types.input(type, name))(
261
+ ValueFactory.INPUT(),
262
+ ),
263
+ config.types.output(type, name),
264
+ undefined,
265
+ ts.factory.createBlock(
266
+ [
267
+ ...(added || []),
268
+ ...functors.filter((_, i) =>
269
+ importer.hasLocal(`${config.functors}${i}`),
270
+ ),
271
+ ...unioners.filter((_, i) =>
272
+ importer.hasLocal(`${config.unioners}${i}`),
273
+ ),
274
+ ...(ts.isBlock(output)
275
+ ? output.statements
276
+ : [ts.factory.createReturnStatement(output)]),
277
+ ],
278
+ true,
279
+ ),
280
+ );
281
+ };
282
+
283
+ export const generate_functors =
284
+ (config: IConfig) =>
285
+ (importer: FunctionImporter) =>
286
+ (collection: MetadataCollection) =>
287
+ collection
288
+ .objects()
289
+ .map((obj, i) =>
290
+ StatementFactory.constant(
291
+ `${config.functors}${i}`,
292
+ generate_object(config)(importer)(obj),
293
+ ),
294
+ );
295
+
296
+ export const generate_unioners =
297
+ (config: IConfig) =>
298
+ (importer: FunctionImporter) =>
299
+ (collection: MetadataCollection) =>
300
+ collection
301
+ .unions()
302
+ .map((union, i) =>
303
+ StatementFactory.constant(
304
+ importer.useLocal(`${config.unioners}${i}`),
305
+ generate_union(config)(union),
306
+ ),
307
+ );
308
+
309
+ const generate_object =
310
+ (config: IConfig) =>
311
+ (importer: FunctionImporter) =>
312
+ (obj: MetadataObject) =>
313
+ ts.factory.createArrowFunction(
314
+ undefined,
315
+ undefined,
316
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
317
+ ValueFactory.INPUT(),
318
+ ),
319
+ config.objector.type ?? TypeFactory.keyword("any"),
320
+ undefined,
321
+ config.objector.joiner(
322
+ ts.factory.createIdentifier("input"),
323
+ feature_object_entries(config)(importer)(obj)(
324
+ ts.factory.createIdentifier("input"),
325
+ ),
326
+ obj,
327
+ ),
328
+ );
329
+
330
+ function generate_union(config: IConfig) {
331
+ const explorer = UnionExplorer.object(config);
332
+ const input = ValueFactory.INPUT();
333
+
334
+ return (meta: MetadataObject[]) =>
335
+ ts.factory.createArrowFunction(
336
+ undefined,
337
+ undefined,
338
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
339
+ ValueFactory.INPUT(),
340
+ ),
341
+ TypeFactory.keyword("any"),
342
+ undefined,
343
+ explorer(
344
+ input,
345
+ meta,
346
+ {
347
+ tracable: config.path || config.trace,
348
+ source: "object",
349
+ from: "object",
350
+ postfix: "",
351
+ },
352
+ [],
353
+ ),
354
+ );
355
+ }
356
+
357
+ /* -----------------------------------------------------------
358
+ DECODERS
359
+ ----------------------------------------------------------- */
360
+ export function decode_array(
361
+ config: Pick<IConfig, "trace" | "path" | "decoder">,
362
+ importer: FunctionImporter,
363
+ combiner: (
364
+ input: ts.Expression,
365
+ arrow: ts.ArrowFunction,
366
+ tags: IMetadataTag[],
367
+ ) => ts.Expression,
368
+ ) {
369
+ const rand: string = importer.increment().toString();
370
+ const tail =
371
+ config.path || config.trace
372
+ ? [
373
+ IdentifierFactory.parameter(
374
+ "_index" + rand,
375
+ TypeFactory.keyword("number"),
376
+ ),
377
+ ]
378
+ : [];
379
+
380
+ return (
381
+ input: ts.Expression,
382
+ meta: Metadata,
383
+ explore: IExplore,
384
+ tags: IMetadataTag[],
385
+ ) => {
386
+ const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
387
+ undefined,
388
+ undefined,
389
+ [
390
+ IdentifierFactory.parameter(
391
+ "elem",
392
+ TypeFactory.keyword("any"),
393
+ ),
394
+ ...tail,
395
+ ],
396
+ undefined,
397
+ undefined,
398
+ config.decoder(
399
+ ValueFactory.INPUT("elem"),
400
+ meta,
401
+ {
402
+ tracable: explore.tracable,
403
+ source: explore.source,
404
+ from: "array",
405
+ postfix: INDEX_SYMBOL(explore.start ?? null)(
406
+ explore.postfix,
407
+ )(rand),
408
+ },
409
+ tags,
410
+ ),
411
+ );
412
+ return combiner(input, arrow, tags);
413
+ };
414
+ }
415
+
416
+ export const decode_object =
417
+ (config: Pick<IConfig, "trace" | "path" | "functors">) =>
418
+ (importer: FunctionImporter) =>
419
+ (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
420
+ ts.factory.createCallExpression(
421
+ ts.factory.createIdentifier(
422
+ importer.useLocal(`${config.functors}${obj.index}`),
423
+ ),
424
+ undefined,
425
+ get_object_arguments(config)(explore)(input),
426
+ );
427
+
428
+ export const get_object_arguments =
429
+ (config: Pick<IConfig, "path" | "trace">) =>
430
+ (explore: FeatureProgrammer.IExplore) => {
431
+ const tail: ts.Expression[] =
432
+ config.path === false && config.trace === false
433
+ ? []
434
+ : config.path === true && config.trace === true
435
+ ? [
436
+ ts.factory.createIdentifier(
437
+ explore.postfix
438
+ ? `_path + ${explore.postfix}`
439
+ : "_path",
440
+ ),
441
+ explore.source === "object"
442
+ ? ts.factory.createIdentifier(
443
+ `${explore.tracable} && _exceptionable`,
444
+ )
445
+ : explore.tracable
446
+ ? ts.factory.createTrue()
447
+ : ts.factory.createFalse(),
448
+ ]
449
+ : config.path === true
450
+ ? [
451
+ ts.factory.createIdentifier(
452
+ explore.postfix
453
+ ? `_path + ${explore.postfix}`
454
+ : "_path",
455
+ ),
456
+ ]
457
+ : [
458
+ explore.source === "object"
459
+ ? ts.factory.createIdentifier(
460
+ `${explore.tracable} && _exceptionable`,
461
+ )
462
+ : explore.tracable
463
+ ? ts.factory.createTrue()
464
+ : ts.factory.createFalse(),
465
+ ];
466
+ return (input: ts.Expression) => [input, ...tail];
467
+ };
468
+ }
469
+
470
+ const INDEX_SYMBOL =
471
+ (start: number | null) => (prev: string) => (rand: string) => {
472
+ const tail: string =
473
+ start !== null
474
+ ? `"[" + (${start} + _index${rand}) + "]"`
475
+ : `"[" + _index${rand} + "]"`;
476
+ if (prev === "") return tail;
477
+ else if (prev[prev.length - 1] === `"`)
478
+ return prev.substring(0, prev.length - 1) + tail.substring(1);
479
+ return prev + ` + ${tail}`;
480
+ };
481
+
482
+ const PARAMETERS =
483
+ (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
484
+ (type: ts.TypeNode) => {
485
+ const tail: ts.ParameterDeclaration[] = [];
486
+ if (props.path)
487
+ tail.push(
488
+ IdentifierFactory.parameter(
489
+ "_path",
490
+ TypeFactory.keyword("string"),
491
+ ),
492
+ );
493
+ if (props.trace)
494
+ tail.push(
495
+ IdentifierFactory.parameter(
496
+ "_exceptionable",
497
+ TypeFactory.keyword("boolean"),
498
+ ts.factory.createTrue(),
499
+ ),
500
+ );
501
+ return (input: ts.Identifier) => [
502
+ IdentifierFactory.parameter(input, type),
503
+ ...tail,
504
+ ];
505
+ };