typia 6.7.0 → 6.7.1

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 (85) hide show
  1. package/lib/programmers/AssertProgrammer.d.ts +0 -1
  2. package/lib/programmers/AssertProgrammer.js.map +1 -1
  3. package/lib/programmers/CheckerProgrammer.d.ts +1 -1
  4. package/lib/programmers/FeatureProgrammer.d.ts +1 -1
  5. package/lib/programmers/IsProgrammer.d.ts +1 -1
  6. package/lib/programmers/RandomProgrammer.d.ts +10 -1
  7. package/lib/programmers/RandomProgrammer.js +62 -37
  8. package/lib/programmers/RandomProgrammer.js.map +1 -1
  9. package/lib/programmers/ValidateProgrammer.d.ts +1 -1
  10. package/lib/programmers/json/JsonIsParseProgrammer.d.ts +1 -1
  11. package/lib/programmers/json/JsonIsStringifyProgrammer.d.ts +1 -1
  12. package/lib/programmers/json/JsonStringifyProgrammer.d.ts +1 -1
  13. package/lib/programmers/json/JsonStringifyProgrammer.js +1 -3
  14. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  15. package/lib/programmers/json/JsonValidateParseProgrammer.d.ts +1 -1
  16. package/lib/programmers/json/JsonValidateStringifyProgrammer.d.ts +1 -1
  17. package/lib/programmers/misc/MiscCloneProgrammer.d.ts +1 -1
  18. package/lib/programmers/misc/MiscCloneProgrammer.js +33 -14
  19. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  20. package/lib/programmers/misc/MiscIsCloneProgrammer.d.ts +1 -1
  21. package/lib/programmers/misc/MiscIsPruneProgrammer.d.ts +1 -1
  22. package/lib/programmers/misc/MiscPruneProgrammer.d.ts +1 -1
  23. package/lib/programmers/misc/MiscValidateCloneProgrammer.d.ts +1 -1
  24. package/lib/programmers/misc/MiscValidatePruneProgrammer.d.ts +1 -1
  25. package/lib/programmers/notations/NotationGeneralProgrammer.d.ts +1 -1
  26. package/lib/programmers/notations/NotationGeneralProgrammer.js +30 -11
  27. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  28. package/lib/programmers/notations/NotationValidateGeneralProgrammer.d.ts +1 -1
  29. package/package.json +1 -1
  30. package/src/programmers/AssertProgrammer.ts +397 -398
  31. package/src/programmers/CheckerProgrammer.ts +1138 -1138
  32. package/src/programmers/FeatureProgrammer.ts +549 -549
  33. package/src/programmers/IsProgrammer.ts +1 -1
  34. package/src/programmers/RandomProgrammer.ts +112 -77
  35. package/src/programmers/ValidateProgrammer.ts +382 -382
  36. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +141 -141
  37. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +108 -108
  38. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +98 -98
  39. package/src/programmers/functional/FunctionalIsFunctionProgrammer.ts +72 -72
  40. package/src/programmers/functional/FunctionalIsParametersProgrammer.ts +101 -101
  41. package/src/programmers/functional/FunctionalIsReturnProgrammer.ts +106 -106
  42. package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +123 -123
  43. package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +267 -267
  44. package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +126 -126
  45. package/src/programmers/helpers/FunctionImporter.ts +97 -97
  46. package/src/programmers/http/HttpAssertFormDataProgrammer.ts +91 -91
  47. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +91 -91
  48. package/src/programmers/http/HttpAssertQueryProgrammer.ts +93 -93
  49. package/src/programmers/http/HttpFormDataProgrammer.ts +278 -278
  50. package/src/programmers/http/HttpHeadersProgrammer.ts +347 -347
  51. package/src/programmers/http/HttpIsFormDataProgrammer.ts +102 -102
  52. package/src/programmers/http/HttpIsHeadersProgrammer.ts +102 -102
  53. package/src/programmers/http/HttpIsQueryProgrammer.ts +104 -104
  54. package/src/programmers/http/HttpQueryProgrammer.ts +298 -298
  55. package/src/programmers/http/HttpValidateFormDataProgrammer.ts +85 -85
  56. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +85 -85
  57. package/src/programmers/http/HttpValidateQueryProgrammer.ts +87 -87
  58. package/src/programmers/json/JsonAssertParseProgrammer.ts +96 -96
  59. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +104 -104
  60. package/src/programmers/json/JsonIsParseProgrammer.ts +110 -110
  61. package/src/programmers/json/JsonIsStringifyProgrammer.ts +102 -102
  62. package/src/programmers/json/JsonStringifyProgrammer.ts +909 -910
  63. package/src/programmers/json/JsonValidateParseProgrammer.ts +87 -87
  64. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +111 -111
  65. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +87 -87
  66. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +87 -87
  67. package/src/programmers/misc/MiscCloneProgrammer.ts +781 -759
  68. package/src/programmers/misc/MiscIsCloneProgrammer.ts +93 -93
  69. package/src/programmers/misc/MiscIsPruneProgrammer.ts +94 -94
  70. package/src/programmers/misc/MiscPruneProgrammer.ts +560 -560
  71. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +104 -104
  72. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +98 -98
  73. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +91 -91
  74. package/src/programmers/notations/NotationGeneralProgrammer.ts +709 -685
  75. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +97 -97
  76. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +107 -107
  77. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +91 -91
  78. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +95 -95
  79. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +646 -646
  80. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +852 -852
  81. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +104 -104
  82. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +93 -93
  83. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +85 -85
  84. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +109 -109
  85. package/src/transformers/internal/GenericTransformer.ts +104 -104
@@ -1,549 +1,549 @@
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 { Metadata } from "../schemas/metadata/Metadata";
10
- import { MetadataArray } from "../schemas/metadata/MetadataArray";
11
- import { MetadataObject } from "../schemas/metadata/MetadataObject";
12
-
13
- import { IProject } from "../transformers/IProject";
14
-
15
- import { CheckerProgrammer } from "./CheckerProgrammer";
16
- import { FunctionImporter } from "./helpers/FunctionImporter";
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 internal functions for specific types.
30
- */
31
- prefix: string;
32
-
33
- /**
34
- * Whether to archive access path or not.
35
- */
36
- path: boolean;
37
-
38
- /**
39
- * Whether to trace exception or not.
40
- */
41
- trace: boolean;
42
-
43
- addition?: undefined | ((collection: MetadataCollection) => ts.Statement[]);
44
-
45
- /**
46
- * Initializer of metadata.
47
- */
48
- initializer: (
49
- project: IProject,
50
- ) => (
51
- importer: FunctionImporter,
52
- ) => (type: ts.Type) => [MetadataCollection, Metadata];
53
-
54
- /**
55
- * Decoder, station of every types.
56
- */
57
- decoder: () => Decoder<Metadata, Output>;
58
-
59
- /**
60
- * Object configurator.
61
- */
62
- objector: IConfig.IObjector<Output>;
63
-
64
- /**
65
- * Generator of functions for object types.
66
- */
67
- generator: IConfig.IGenerator;
68
- }
69
- export namespace IConfig {
70
- export interface ITypes {
71
- input: (type: ts.Type, name?: undefined | string) => ts.TypeNode;
72
- output: (type: ts.Type, name?: undefined | string) => ts.TypeNode;
73
- }
74
-
75
- export interface IObjector<Output extends ts.ConciseBody = ts.ConciseBody> {
76
- /**
77
- * Type checker when union object type comes.
78
- */
79
- checker: () => Decoder<Metadata, ts.Expression>;
80
-
81
- /**
82
- * Decoder, function call expression generator of specific typed objects.
83
- */
84
- decoder: () => Decoder<MetadataObject, ts.Expression>;
85
-
86
- /**
87
- * Joiner of expressions from properties.
88
- */
89
- joiner(
90
- input: ts.Expression,
91
- entries: IExpressionEntry<Output>[],
92
- parent: MetadataObject,
93
- ): ts.ConciseBody;
94
-
95
- /**
96
- * Union type specificator.
97
- *
98
- * Expression of an algorithm specifying object type and calling
99
- * the `decoder` function of the specified object type.
100
- */
101
- unionizer: Decoder<MetadataObject[], ts.Expression>;
102
-
103
- /**
104
- * Handler of union type specification failure.
105
- *
106
- * @param value Expression of input parameter
107
- * @param expected Expected type name
108
- * @param explore Exploration info
109
- * @returns Statement of failure
110
- */
111
- failure(
112
- value: ts.Expression,
113
- expected: string,
114
- explore?: undefined | IExplore,
115
- ): ts.Statement;
116
-
117
- /**
118
- * Transformer of type checking expression by discrimination.
119
- *
120
- * When an object type has been specified by a discrimination without full
121
- * iteration, the `unionizer` will decode the object instance after
122
- * the last type checking.
123
- *
124
- * In such circumtance, you can transform the last type checking function.
125
- *
126
- * @param exp Current expression about type checking
127
- * @returns Transformed expression
128
- * @deprecated
129
- */
130
- is?: undefined | ((exp: ts.Expression) => ts.Expression);
131
-
132
- /**
133
- * Transformer of non-undefined type checking by discrimination.
134
- *
135
- * When specifying an union type of objects, `typia` tries to find
136
- * descrimination way just by checking only one property type.
137
- * If succeeded to find the discrimination way, `typia` will check the target
138
- * property type and in the checking, non-undefined type checking would be
139
- * done.
140
- *
141
- * In such process, you can transform the non-undefined type checking.
142
- *
143
- * @param exp
144
- * @returns Transformed expression
145
- * @deprecated
146
- */
147
- required?: undefined | ((exp: ts.Expression) => ts.Expression);
148
-
149
- /**
150
- * Conditon wrapper when unable to specify any object type.
151
- *
152
- * When failed to specify an object type through discrimination, full
153
- * iteration type checking would be happend. In such circumstance, you
154
- * can wrap the condition with additional function.
155
- *
156
- * @param condition Current condition
157
- * @returns A function wrapped current condition
158
- */
159
- full?:
160
- | undefined
161
- | ((
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?: undefined | ts.TypeNode;
173
- }
174
- export interface IGenerator {
175
- objects?:
176
- | undefined
177
- | (() => (col: MetadataCollection) => ts.VariableStatement[]);
178
- unions?:
179
- | undefined
180
- | (() => (col: MetadataCollection) => ts.VariableStatement[]);
181
- arrays(): (col: MetadataCollection) => ts.VariableStatement[];
182
- tuples(): (col: MetadataCollection) => ts.VariableStatement[];
183
- }
184
- }
185
-
186
- export interface IExplore {
187
- tracable: boolean;
188
- source: "top" | "function";
189
- from: "top" | "array" | "object";
190
- postfix: string;
191
- start?: undefined | number;
192
- }
193
-
194
- export interface Decoder<T, Output extends ts.ConciseBody = ts.ConciseBody> {
195
- (input: ts.Expression, target: T, explore: IExplore): Output;
196
- }
197
-
198
- /* -----------------------------------------------------------
199
- GENERATORS
200
- ----------------------------------------------------------- */
201
- export interface IComposed {
202
- body: ts.ConciseBody;
203
- parameters: ts.ParameterDeclaration[];
204
- functions: Record<string, ts.VariableStatement>;
205
- statements: ts.Statement[];
206
- response: ts.TypeNode;
207
- }
208
- export interface IDecomposed {
209
- functions: Record<string, ts.VariableStatement>;
210
- statements: ts.Statement[];
211
- arrow: ts.ArrowFunction;
212
- }
213
-
214
- export const compose = (props: {
215
- project: IProject;
216
- config: IConfig;
217
- importer: FunctionImporter;
218
- type: ts.Type;
219
- name?: string;
220
- }): IComposed => {
221
- const [collection, meta] = props.config.initializer(props.project)(
222
- props.importer,
223
- )(props.type);
224
- return {
225
- body: props.config.decoder()(ValueFactory.INPUT(), meta, {
226
- tracable: props.config.path || props.config.trace,
227
- source: "top",
228
- from: "top",
229
- postfix: '""',
230
- }),
231
- statements: props.config.addition
232
- ? props.config.addition(collection)
233
- : [],
234
- functions: {
235
- ...Object.fromEntries(
236
- (
237
- props.config.generator.objects?.() ??
238
- write_object_functions(props.config)(props.importer)
239
- )(collection).map((v, i) => [`${props.config.prefix}o${i}`, v]),
240
- ),
241
- ...Object.fromEntries(
242
- (
243
- props.config.generator.unions?.() ??
244
- write_union_functions(props.config)
245
- )(collection).map((v, i) => [`${props.config.prefix}u${i}`, v]),
246
- ),
247
- ...Object.fromEntries(
248
- props.config.generator
249
- .arrays()(collection)
250
- .map((v, i) => [`${props.config.prefix}a${i}`, v]),
251
- ),
252
- ...Object.fromEntries(
253
- props.config.generator
254
- .tuples()(collection)
255
- .map((v, i) => [`${props.config.prefix}t${i}`, v]),
256
- ),
257
- },
258
- parameters: parameterDeclarations(props.config)(
259
- props.config.types.input(props.type, props.name),
260
- )(ValueFactory.INPUT()),
261
- response: props.config.types.output(props.type, props.name),
262
- };
263
- };
264
-
265
- export const writeDecomposed = (props: {
266
- modulo: ts.LeftHandSideExpression;
267
- importer: FunctionImporter;
268
- result: IDecomposed;
269
- }): ts.CallExpression =>
270
- ts.factory.createCallExpression(
271
- ts.factory.createArrowFunction(
272
- undefined,
273
- undefined,
274
- [],
275
- undefined,
276
- undefined,
277
- ts.factory.createBlock([
278
- ...props.importer.declare(props.modulo),
279
- ...Object.entries(props.result.functions)
280
- .filter(([k]) => props.importer.hasLocal(k))
281
- .map(([_k, v]) => v),
282
- ...props.result.statements,
283
- ts.factory.createReturnStatement(props.result.arrow),
284
- ]),
285
- ),
286
- undefined,
287
- undefined,
288
- );
289
-
290
- export const write =
291
- (project: IProject) =>
292
- (config: IConfig) =>
293
- (importer: FunctionImporter) =>
294
- (type: ts.Type, name?: string): ts.ArrowFunction => {
295
- const [collection, meta] = config.initializer(project)(importer)(type);
296
-
297
- // ITERATE OVER ALL METADATA
298
- const output: ts.ConciseBody = config.decoder()(
299
- ValueFactory.INPUT(),
300
- meta,
301
- {
302
- tracable: config.path || config.trace,
303
- source: "top",
304
- from: "top",
305
- postfix: '""',
306
- },
307
- );
308
-
309
- // RETURNS THE OPTIMAL ARROW FUNCTION
310
- const functions = {
311
- objects: (
312
- config.generator.objects?.() ??
313
- write_object_functions(config)(importer)
314
- )(collection),
315
- unions: (config.generator.unions?.() ?? write_union_functions(config))(
316
- collection,
317
- ),
318
- arrays: config.generator.arrays()(collection),
319
- tuples: config.generator.tuples()(collection),
320
- };
321
- const added: ts.Statement[] = (config.addition ?? (() => []))(collection);
322
-
323
- return ts.factory.createArrowFunction(
324
- undefined,
325
- undefined,
326
- parameterDeclarations(config)(config.types.input(type, name))(
327
- ValueFactory.INPUT(),
328
- ),
329
- config.types.output(type, name),
330
- undefined,
331
- ts.factory.createBlock(
332
- [
333
- ...added,
334
- ...functions.objects.filter((_, i) =>
335
- importer.hasLocal(`${config.prefix}o${i}`),
336
- ),
337
- ...functions.unions.filter((_, i) =>
338
- importer.hasLocal(`${config.prefix}u${i}`),
339
- ),
340
- ...functions.arrays.filter((_, i) =>
341
- importer.hasLocal(`${config.prefix}a${i}`),
342
- ),
343
- ...functions.tuples.filter((_, i) =>
344
- importer.hasLocal(`${config.prefix}t${i}`),
345
- ),
346
- ...(ts.isBlock(output)
347
- ? output.statements
348
- : [ts.factory.createReturnStatement(output)]),
349
- ],
350
- true,
351
- ),
352
- );
353
- };
354
-
355
- export const write_object_functions =
356
- (config: IConfig) =>
357
- (importer: FunctionImporter) =>
358
- (collection: MetadataCollection) =>
359
- collection
360
- .objects()
361
- .map((obj) =>
362
- StatementFactory.constant(
363
- `${config.prefix}o${obj.index}`,
364
- ts.factory.createArrowFunction(
365
- undefined,
366
- undefined,
367
- parameterDeclarations(config)(TypeFactory.keyword("any"))(
368
- ValueFactory.INPUT(),
369
- ),
370
- config.objector.type ?? TypeFactory.keyword("any"),
371
- undefined,
372
- config.objector.joiner(
373
- ts.factory.createIdentifier("input"),
374
- feature_object_entries(config)(importer)(obj)(
375
- ts.factory.createIdentifier("input"),
376
- ),
377
- obj,
378
- ),
379
- ),
380
- ),
381
- );
382
-
383
- export const write_union_functions =
384
- (config: IConfig) => (collection: MetadataCollection) =>
385
- collection
386
- .unions()
387
- .map((union, i) =>
388
- StatementFactory.constant(
389
- `${config.prefix}u${i}`,
390
- write_union(config)(union),
391
- ),
392
- );
393
-
394
- const write_union = (config: IConfig) => {
395
- const explorer = UnionExplorer.object(config);
396
- const input = ValueFactory.INPUT();
397
-
398
- return (meta: MetadataObject[]) =>
399
- ts.factory.createArrowFunction(
400
- undefined,
401
- undefined,
402
- parameterDeclarations(config)(TypeFactory.keyword("any"))(
403
- ValueFactory.INPUT(),
404
- ),
405
- TypeFactory.keyword("any"),
406
- undefined,
407
- explorer(input, meta, {
408
- tracable: config.path || config.trace,
409
- source: "function",
410
- from: "object",
411
- postfix: "",
412
- }),
413
- );
414
- };
415
-
416
- /* -----------------------------------------------------------
417
- DECODERS
418
- ----------------------------------------------------------- */
419
- export const decode_array =
420
- (config: Pick<IConfig, "trace" | "path" | "decoder" | "prefix">) =>
421
- (importer: FunctionImporter) =>
422
- (
423
- combiner: (
424
- input: ts.Expression,
425
- arrow: ts.ArrowFunction,
426
- ) => ts.Expression,
427
- ) => {
428
- const rand: string = importer.increment().toString();
429
- const tail =
430
- config.path || config.trace
431
- ? [
432
- IdentifierFactory.parameter(
433
- "_index" + rand,
434
- TypeFactory.keyword("number"),
435
- ),
436
- ]
437
- : [];
438
-
439
- return (
440
- input: ts.Expression,
441
- array: MetadataArray,
442
- explore: IExplore,
443
- ) => {
444
- const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
445
- undefined,
446
- undefined,
447
- [
448
- IdentifierFactory.parameter("elem", TypeFactory.keyword("any")),
449
- ...tail,
450
- ],
451
- undefined,
452
- undefined,
453
- config.decoder()(ValueFactory.INPUT("elem"), array.type.value, {
454
- tracable: explore.tracable,
455
- source: explore.source,
456
- from: "array",
457
- postfix: index(explore.start ?? null)(explore.postfix)(rand),
458
- }),
459
- );
460
- return combiner(input, arrow);
461
- };
462
- };
463
-
464
- export const decode_object =
465
- (config: Pick<IConfig, "trace" | "path" | "prefix">) =>
466
- (importer: FunctionImporter) =>
467
- (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
468
- ts.factory.createCallExpression(
469
- ts.factory.createIdentifier(
470
- importer.useLocal(`${config.prefix}o${obj.index}`),
471
- ),
472
- undefined,
473
- argumentsArray(config)(explore)(input),
474
- );
475
-
476
- /* -----------------------------------------------------------
477
- UTILITIES FOR INTERNAL FUNCTIONS
478
- ----------------------------------------------------------- */
479
- export const index =
480
- (start: number | null) => (prev: string) => (rand: string) => {
481
- const tail: string =
482
- start !== null
483
- ? `"[" + (${start} + _index${rand}) + "]"`
484
- : `"[" + _index${rand} + "]"`;
485
- if (prev === "") return tail;
486
- else if (prev[prev.length - 1] === `"`)
487
- return prev.substring(0, prev.length - 1) + tail.substring(1);
488
- return prev + ` + ${tail}`;
489
- };
490
-
491
- export const argumentsArray =
492
- (config: Pick<IConfig, "path" | "trace">) =>
493
- (explore: FeatureProgrammer.IExplore) => {
494
- const tail: ts.Expression[] =
495
- config.path === false && config.trace === false
496
- ? []
497
- : config.path === true && config.trace === true
498
- ? [
499
- ts.factory.createIdentifier(
500
- explore.postfix ? `_path + ${explore.postfix}` : "_path",
501
- ),
502
- explore.source === "function"
503
- ? ts.factory.createIdentifier(
504
- `${explore.tracable} && _exceptionable`,
505
- )
506
- : explore.tracable
507
- ? ts.factory.createTrue()
508
- : ts.factory.createFalse(),
509
- ]
510
- : config.path === true
511
- ? [
512
- ts.factory.createIdentifier(
513
- explore.postfix ? `_path + ${explore.postfix}` : "_path",
514
- ),
515
- ]
516
- : [
517
- explore.source === "function"
518
- ? ts.factory.createIdentifier(
519
- `${explore.tracable} && _exceptionable`,
520
- )
521
- : explore.tracable
522
- ? ts.factory.createTrue()
523
- : ts.factory.createFalse(),
524
- ];
525
- return (input: ts.Expression) => [input, ...tail];
526
- };
527
-
528
- export const parameterDeclarations =
529
- (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
530
- (type: ts.TypeNode) => {
531
- const tail: ts.ParameterDeclaration[] = [];
532
- if (props.path)
533
- tail.push(
534
- IdentifierFactory.parameter("_path", TypeFactory.keyword("string")),
535
- );
536
- if (props.trace)
537
- tail.push(
538
- IdentifierFactory.parameter(
539
- "_exceptionable",
540
- TypeFactory.keyword("boolean"),
541
- ts.factory.createTrue(),
542
- ),
543
- );
544
- return (input: ts.Identifier): ts.ParameterDeclaration[] => [
545
- IdentifierFactory.parameter(input, type),
546
- ...tail,
547
- ];
548
- };
549
- }
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 { Metadata } from "../schemas/metadata/Metadata";
10
+ import { MetadataArray } from "../schemas/metadata/MetadataArray";
11
+ import { MetadataObject } from "../schemas/metadata/MetadataObject";
12
+
13
+ import { IProject } from "../transformers/IProject";
14
+
15
+ import { CheckerProgrammer } from "./CheckerProgrammer";
16
+ import { FunctionImporter } from "./helpers/FunctionImporter";
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 internal functions for specific types.
30
+ */
31
+ prefix: string;
32
+
33
+ /**
34
+ * Whether to archive access path or not.
35
+ */
36
+ path: boolean;
37
+
38
+ /**
39
+ * Whether to trace exception or not.
40
+ */
41
+ trace: boolean;
42
+
43
+ addition?: undefined | ((collection: MetadataCollection) => ts.Statement[]);
44
+
45
+ /**
46
+ * Initializer of metadata.
47
+ */
48
+ initializer: (
49
+ project: IProject,
50
+ ) => (
51
+ importer: FunctionImporter,
52
+ ) => (type: ts.Type) => [MetadataCollection, Metadata];
53
+
54
+ /**
55
+ * Decoder, station of every types.
56
+ */
57
+ decoder: () => Decoder<Metadata, Output>;
58
+
59
+ /**
60
+ * Object configurator.
61
+ */
62
+ objector: IConfig.IObjector<Output>;
63
+
64
+ /**
65
+ * Generator of functions for object types.
66
+ */
67
+ generator: IConfig.IGenerator;
68
+ }
69
+ export namespace IConfig {
70
+ export interface ITypes {
71
+ input: (type: ts.Type, name?: undefined | string) => ts.TypeNode;
72
+ output: (type: ts.Type, name?: undefined | string) => ts.TypeNode;
73
+ }
74
+
75
+ export interface IObjector<Output extends ts.ConciseBody = ts.ConciseBody> {
76
+ /**
77
+ * Type checker when union object type comes.
78
+ */
79
+ checker: () => Decoder<Metadata, ts.Expression>;
80
+
81
+ /**
82
+ * Decoder, function call expression generator of specific typed objects.
83
+ */
84
+ decoder: () => Decoder<MetadataObject, ts.Expression>;
85
+
86
+ /**
87
+ * Joiner of expressions from properties.
88
+ */
89
+ joiner(
90
+ input: ts.Expression,
91
+ entries: IExpressionEntry<Output>[],
92
+ parent: MetadataObject,
93
+ ): ts.ConciseBody;
94
+
95
+ /**
96
+ * Union type specificator.
97
+ *
98
+ * Expression of an algorithm specifying object type and calling
99
+ * the `decoder` function of the specified object type.
100
+ */
101
+ unionizer: Decoder<MetadataObject[], ts.Expression>;
102
+
103
+ /**
104
+ * Handler of union type specification failure.
105
+ *
106
+ * @param value Expression of input parameter
107
+ * @param expected Expected type name
108
+ * @param explore Exploration info
109
+ * @returns Statement of failure
110
+ */
111
+ failure(
112
+ value: ts.Expression,
113
+ expected: string,
114
+ explore?: undefined | IExplore,
115
+ ): ts.Statement;
116
+
117
+ /**
118
+ * Transformer of type checking expression by discrimination.
119
+ *
120
+ * When an object type has been specified by a discrimination without full
121
+ * iteration, the `unionizer` will decode the object instance after
122
+ * the last type checking.
123
+ *
124
+ * In such circumtance, you can transform the last type checking function.
125
+ *
126
+ * @param exp Current expression about type checking
127
+ * @returns Transformed expression
128
+ * @deprecated
129
+ */
130
+ is?: undefined | ((exp: ts.Expression) => ts.Expression);
131
+
132
+ /**
133
+ * Transformer of non-undefined type checking by discrimination.
134
+ *
135
+ * When specifying an union type of objects, `typia` tries to find
136
+ * descrimination way just by checking only one property type.
137
+ * If succeeded to find the discrimination way, `typia` will check the target
138
+ * property type and in the checking, non-undefined type checking would be
139
+ * done.
140
+ *
141
+ * In such process, you can transform the non-undefined type checking.
142
+ *
143
+ * @param exp
144
+ * @returns Transformed expression
145
+ * @deprecated
146
+ */
147
+ required?: undefined | ((exp: ts.Expression) => ts.Expression);
148
+
149
+ /**
150
+ * Conditon wrapper when unable to specify any object type.
151
+ *
152
+ * When failed to specify an object type through discrimination, full
153
+ * iteration type checking would be happend. In such circumstance, you
154
+ * can wrap the condition with additional function.
155
+ *
156
+ * @param condition Current condition
157
+ * @returns A function wrapped current condition
158
+ */
159
+ full?:
160
+ | undefined
161
+ | ((
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?: undefined | ts.TypeNode;
173
+ }
174
+ export interface IGenerator {
175
+ objects?:
176
+ | undefined
177
+ | (() => (col: MetadataCollection) => ts.VariableStatement[]);
178
+ unions?:
179
+ | undefined
180
+ | (() => (col: MetadataCollection) => ts.VariableStatement[]);
181
+ arrays(): (col: MetadataCollection) => ts.VariableStatement[];
182
+ tuples(): (col: MetadataCollection) => ts.VariableStatement[];
183
+ }
184
+ }
185
+
186
+ export interface IExplore {
187
+ tracable: boolean;
188
+ source: "top" | "function";
189
+ from: "top" | "array" | "object";
190
+ postfix: string;
191
+ start?: undefined | number;
192
+ }
193
+
194
+ export interface Decoder<T, Output extends ts.ConciseBody = ts.ConciseBody> {
195
+ (input: ts.Expression, target: T, explore: IExplore): Output;
196
+ }
197
+
198
+ /* -----------------------------------------------------------
199
+ GENERATORS
200
+ ----------------------------------------------------------- */
201
+ export interface IComposed {
202
+ body: ts.ConciseBody;
203
+ parameters: ts.ParameterDeclaration[];
204
+ functions: Record<string, ts.VariableStatement>;
205
+ statements: ts.Statement[];
206
+ response: ts.TypeNode;
207
+ }
208
+ export interface IDecomposed {
209
+ functions: Record<string, ts.VariableStatement>;
210
+ statements: ts.Statement[];
211
+ arrow: ts.ArrowFunction;
212
+ }
213
+
214
+ export const compose = (props: {
215
+ project: IProject;
216
+ config: IConfig;
217
+ importer: FunctionImporter;
218
+ type: ts.Type;
219
+ name: string | undefined;
220
+ }): IComposed => {
221
+ const [collection, meta] = props.config.initializer(props.project)(
222
+ props.importer,
223
+ )(props.type);
224
+ return {
225
+ body: props.config.decoder()(ValueFactory.INPUT(), meta, {
226
+ tracable: props.config.path || props.config.trace,
227
+ source: "top",
228
+ from: "top",
229
+ postfix: '""',
230
+ }),
231
+ statements: props.config.addition
232
+ ? props.config.addition(collection)
233
+ : [],
234
+ functions: {
235
+ ...Object.fromEntries(
236
+ (
237
+ props.config.generator.objects?.() ??
238
+ write_object_functions(props.config)(props.importer)
239
+ )(collection).map((v, i) => [`${props.config.prefix}o${i}`, v]),
240
+ ),
241
+ ...Object.fromEntries(
242
+ (
243
+ props.config.generator.unions?.() ??
244
+ write_union_functions(props.config)
245
+ )(collection).map((v, i) => [`${props.config.prefix}u${i}`, v]),
246
+ ),
247
+ ...Object.fromEntries(
248
+ props.config.generator
249
+ .arrays()(collection)
250
+ .map((v, i) => [`${props.config.prefix}a${i}`, v]),
251
+ ),
252
+ ...Object.fromEntries(
253
+ props.config.generator
254
+ .tuples()(collection)
255
+ .map((v, i) => [`${props.config.prefix}t${i}`, v]),
256
+ ),
257
+ },
258
+ parameters: parameterDeclarations(props.config)(
259
+ props.config.types.input(props.type, props.name),
260
+ )(ValueFactory.INPUT()),
261
+ response: props.config.types.output(props.type, props.name),
262
+ };
263
+ };
264
+
265
+ export const writeDecomposed = (props: {
266
+ modulo: ts.LeftHandSideExpression;
267
+ importer: FunctionImporter;
268
+ result: IDecomposed;
269
+ }): ts.CallExpression =>
270
+ ts.factory.createCallExpression(
271
+ ts.factory.createArrowFunction(
272
+ undefined,
273
+ undefined,
274
+ [],
275
+ undefined,
276
+ undefined,
277
+ ts.factory.createBlock([
278
+ ...props.importer.declare(props.modulo),
279
+ ...Object.entries(props.result.functions)
280
+ .filter(([k]) => props.importer.hasLocal(k))
281
+ .map(([_k, v]) => v),
282
+ ...props.result.statements,
283
+ ts.factory.createReturnStatement(props.result.arrow),
284
+ ]),
285
+ ),
286
+ undefined,
287
+ undefined,
288
+ );
289
+
290
+ export const write =
291
+ (project: IProject) =>
292
+ (config: IConfig) =>
293
+ (importer: FunctionImporter) =>
294
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
295
+ const [collection, meta] = config.initializer(project)(importer)(type);
296
+
297
+ // ITERATE OVER ALL METADATA
298
+ const output: ts.ConciseBody = config.decoder()(
299
+ ValueFactory.INPUT(),
300
+ meta,
301
+ {
302
+ tracable: config.path || config.trace,
303
+ source: "top",
304
+ from: "top",
305
+ postfix: '""',
306
+ },
307
+ );
308
+
309
+ // RETURNS THE OPTIMAL ARROW FUNCTION
310
+ const functions = {
311
+ objects: (
312
+ config.generator.objects?.() ??
313
+ write_object_functions(config)(importer)
314
+ )(collection),
315
+ unions: (config.generator.unions?.() ?? write_union_functions(config))(
316
+ collection,
317
+ ),
318
+ arrays: config.generator.arrays()(collection),
319
+ tuples: config.generator.tuples()(collection),
320
+ };
321
+ const added: ts.Statement[] = (config.addition ?? (() => []))(collection);
322
+
323
+ return ts.factory.createArrowFunction(
324
+ undefined,
325
+ undefined,
326
+ parameterDeclarations(config)(config.types.input(type, name))(
327
+ ValueFactory.INPUT(),
328
+ ),
329
+ config.types.output(type, name),
330
+ undefined,
331
+ ts.factory.createBlock(
332
+ [
333
+ ...added,
334
+ ...functions.objects.filter((_, i) =>
335
+ importer.hasLocal(`${config.prefix}o${i}`),
336
+ ),
337
+ ...functions.unions.filter((_, i) =>
338
+ importer.hasLocal(`${config.prefix}u${i}`),
339
+ ),
340
+ ...functions.arrays.filter((_, i) =>
341
+ importer.hasLocal(`${config.prefix}a${i}`),
342
+ ),
343
+ ...functions.tuples.filter((_, i) =>
344
+ importer.hasLocal(`${config.prefix}t${i}`),
345
+ ),
346
+ ...(ts.isBlock(output)
347
+ ? output.statements
348
+ : [ts.factory.createReturnStatement(output)]),
349
+ ],
350
+ true,
351
+ ),
352
+ );
353
+ };
354
+
355
+ export const write_object_functions =
356
+ (config: IConfig) =>
357
+ (importer: FunctionImporter) =>
358
+ (collection: MetadataCollection) =>
359
+ collection
360
+ .objects()
361
+ .map((obj) =>
362
+ StatementFactory.constant(
363
+ `${config.prefix}o${obj.index}`,
364
+ ts.factory.createArrowFunction(
365
+ undefined,
366
+ undefined,
367
+ parameterDeclarations(config)(TypeFactory.keyword("any"))(
368
+ ValueFactory.INPUT(),
369
+ ),
370
+ config.objector.type ?? TypeFactory.keyword("any"),
371
+ undefined,
372
+ config.objector.joiner(
373
+ ts.factory.createIdentifier("input"),
374
+ feature_object_entries(config)(importer)(obj)(
375
+ ts.factory.createIdentifier("input"),
376
+ ),
377
+ obj,
378
+ ),
379
+ ),
380
+ ),
381
+ );
382
+
383
+ export const write_union_functions =
384
+ (config: IConfig) => (collection: MetadataCollection) =>
385
+ collection
386
+ .unions()
387
+ .map((union, i) =>
388
+ StatementFactory.constant(
389
+ `${config.prefix}u${i}`,
390
+ write_union(config)(union),
391
+ ),
392
+ );
393
+
394
+ const write_union = (config: IConfig) => {
395
+ const explorer = UnionExplorer.object(config);
396
+ const input = ValueFactory.INPUT();
397
+
398
+ return (meta: MetadataObject[]) =>
399
+ ts.factory.createArrowFunction(
400
+ undefined,
401
+ undefined,
402
+ parameterDeclarations(config)(TypeFactory.keyword("any"))(
403
+ ValueFactory.INPUT(),
404
+ ),
405
+ TypeFactory.keyword("any"),
406
+ undefined,
407
+ explorer(input, meta, {
408
+ tracable: config.path || config.trace,
409
+ source: "function",
410
+ from: "object",
411
+ postfix: "",
412
+ }),
413
+ );
414
+ };
415
+
416
+ /* -----------------------------------------------------------
417
+ DECODERS
418
+ ----------------------------------------------------------- */
419
+ export const decode_array =
420
+ (config: Pick<IConfig, "trace" | "path" | "decoder" | "prefix">) =>
421
+ (importer: FunctionImporter) =>
422
+ (
423
+ combiner: (
424
+ input: ts.Expression,
425
+ arrow: ts.ArrowFunction,
426
+ ) => ts.Expression,
427
+ ) => {
428
+ const rand: string = importer.increment().toString();
429
+ const tail =
430
+ config.path || config.trace
431
+ ? [
432
+ IdentifierFactory.parameter(
433
+ "_index" + rand,
434
+ TypeFactory.keyword("number"),
435
+ ),
436
+ ]
437
+ : [];
438
+
439
+ return (
440
+ input: ts.Expression,
441
+ array: MetadataArray,
442
+ explore: IExplore,
443
+ ) => {
444
+ const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
445
+ undefined,
446
+ undefined,
447
+ [
448
+ IdentifierFactory.parameter("elem", TypeFactory.keyword("any")),
449
+ ...tail,
450
+ ],
451
+ undefined,
452
+ undefined,
453
+ config.decoder()(ValueFactory.INPUT("elem"), array.type.value, {
454
+ tracable: explore.tracable,
455
+ source: explore.source,
456
+ from: "array",
457
+ postfix: index(explore.start ?? null)(explore.postfix)(rand),
458
+ }),
459
+ );
460
+ return combiner(input, arrow);
461
+ };
462
+ };
463
+
464
+ export const decode_object =
465
+ (config: Pick<IConfig, "trace" | "path" | "prefix">) =>
466
+ (importer: FunctionImporter) =>
467
+ (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
468
+ ts.factory.createCallExpression(
469
+ ts.factory.createIdentifier(
470
+ importer.useLocal(`${config.prefix}o${obj.index}`),
471
+ ),
472
+ undefined,
473
+ argumentsArray(config)(explore)(input),
474
+ );
475
+
476
+ /* -----------------------------------------------------------
477
+ UTILITIES FOR INTERNAL FUNCTIONS
478
+ ----------------------------------------------------------- */
479
+ export const index =
480
+ (start: number | null) => (prev: string) => (rand: string) => {
481
+ const tail: string =
482
+ start !== null
483
+ ? `"[" + (${start} + _index${rand}) + "]"`
484
+ : `"[" + _index${rand} + "]"`;
485
+ if (prev === "") return tail;
486
+ else if (prev[prev.length - 1] === `"`)
487
+ return prev.substring(0, prev.length - 1) + tail.substring(1);
488
+ return prev + ` + ${tail}`;
489
+ };
490
+
491
+ export const argumentsArray =
492
+ (config: Pick<IConfig, "path" | "trace">) =>
493
+ (explore: FeatureProgrammer.IExplore) => {
494
+ const tail: ts.Expression[] =
495
+ config.path === false && config.trace === false
496
+ ? []
497
+ : config.path === true && config.trace === true
498
+ ? [
499
+ ts.factory.createIdentifier(
500
+ explore.postfix ? `_path + ${explore.postfix}` : "_path",
501
+ ),
502
+ explore.source === "function"
503
+ ? ts.factory.createIdentifier(
504
+ `${explore.tracable} && _exceptionable`,
505
+ )
506
+ : explore.tracable
507
+ ? ts.factory.createTrue()
508
+ : ts.factory.createFalse(),
509
+ ]
510
+ : config.path === true
511
+ ? [
512
+ ts.factory.createIdentifier(
513
+ explore.postfix ? `_path + ${explore.postfix}` : "_path",
514
+ ),
515
+ ]
516
+ : [
517
+ explore.source === "function"
518
+ ? ts.factory.createIdentifier(
519
+ `${explore.tracable} && _exceptionable`,
520
+ )
521
+ : explore.tracable
522
+ ? ts.factory.createTrue()
523
+ : ts.factory.createFalse(),
524
+ ];
525
+ return (input: ts.Expression) => [input, ...tail];
526
+ };
527
+
528
+ export const parameterDeclarations =
529
+ (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
530
+ (type: ts.TypeNode) => {
531
+ const tail: ts.ParameterDeclaration[] = [];
532
+ if (props.path)
533
+ tail.push(
534
+ IdentifierFactory.parameter("_path", TypeFactory.keyword("string")),
535
+ );
536
+ if (props.trace)
537
+ tail.push(
538
+ IdentifierFactory.parameter(
539
+ "_exceptionable",
540
+ TypeFactory.keyword("boolean"),
541
+ ts.factory.createTrue(),
542
+ ),
543
+ );
544
+ return (input: ts.Identifier): ts.ParameterDeclaration[] => [
545
+ IdentifierFactory.parameter(input, type),
546
+ ...tail,
547
+ ];
548
+ };
549
+ }