typia 5.2.6 → 5.2.7-dev.20231109

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 (125) hide show
  1. package/lib/factories/ExpressionFactory.js +14 -16
  2. package/lib/factories/ExpressionFactory.js.map +1 -1
  3. package/lib/factories/JsonMetadataFactory.d.ts +1 -1
  4. package/lib/factories/JsonMetadataFactory.js +2 -2
  5. package/lib/factories/JsonMetadataFactory.js.map +1 -1
  6. package/lib/factories/LiteralFactory.js +2 -0
  7. package/lib/factories/LiteralFactory.js.map +1 -1
  8. package/lib/factories/MetadataFactory.d.ts +1 -1
  9. package/lib/factories/MetadataFactory.js +233 -168
  10. package/lib/factories/MetadataFactory.js.map +1 -1
  11. package/lib/factories/NumericRangeFactory.d.ts +2 -2
  12. package/lib/factories/NumericRangeFactory.js +41 -21
  13. package/lib/factories/NumericRangeFactory.js.map +1 -1
  14. package/lib/factories/ProtobufFactory.d.ts +1 -1
  15. package/lib/factories/ProtobufFactory.js +2 -2
  16. package/lib/factories/ProtobufFactory.js.map +1 -1
  17. package/lib/programmers/CheckerProgrammer.js +11 -16
  18. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  19. package/lib/programmers/RandomProgrammer.js +1 -1
  20. package/lib/programmers/RandomProgrammer.js.map +1 -1
  21. package/lib/programmers/http/HttpHeadersProgrammer.js +1 -1
  22. package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
  23. package/lib/programmers/http/HttpParameterProgrammer.js +1 -1
  24. package/lib/programmers/http/HttpParameterProgrammer.js.map +1 -1
  25. package/lib/programmers/http/HttpQueryProgrammer.js +1 -1
  26. package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -1
  27. package/lib/programmers/internal/application_array.js +8 -0
  28. package/lib/programmers/internal/application_array.js.map +1 -1
  29. package/lib/programmers/internal/application_number.js +8 -1
  30. package/lib/programmers/internal/application_number.js.map +1 -1
  31. package/lib/programmers/internal/application_string.js +8 -1
  32. package/lib/programmers/internal/application_string.js.map +1 -1
  33. package/lib/programmers/internal/check_array_length.js +7 -4
  34. package/lib/programmers/internal/check_array_length.js.map +1 -1
  35. package/lib/programmers/internal/check_bigint.js +7 -4
  36. package/lib/programmers/internal/check_bigint.js.map +1 -1
  37. package/lib/programmers/internal/check_number.js +7 -4
  38. package/lib/programmers/internal/check_number.js.map +1 -1
  39. package/lib/programmers/internal/check_string.js.map +1 -1
  40. package/lib/programmers/json/JsonAssertParseProgrammer.js +1 -1
  41. package/lib/programmers/json/JsonAssertParseProgrammer.js.map +1 -1
  42. package/lib/programmers/json/JsonIsParseProgrammer.js +1 -1
  43. package/lib/programmers/json/JsonIsParseProgrammer.js.map +1 -1
  44. package/lib/programmers/json/JsonStringifyProgrammer.js +3 -8
  45. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  46. package/lib/programmers/json/JsonValidateParseProgrammer.js +1 -1
  47. package/lib/programmers/json/JsonValidateParseProgrammer.js.map +1 -1
  48. package/lib/programmers/misc/MiscCloneProgrammer.js +19 -24
  49. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  50. package/lib/programmers/misc/MiscLiteralsProgrammer.js +1 -1
  51. package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
  52. package/lib/programmers/misc/MiscPruneProgrammer.js +11 -16
  53. package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
  54. package/lib/programmers/notations/NotationGeneralProgrammer.js +11 -16
  55. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  56. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +1 -1
  57. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  58. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +38 -42
  59. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  60. package/lib/programmers/protobuf/ProtobufMessageProgrammer.d.ts +1 -1
  61. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +22 -25
  62. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  63. package/lib/schemas/json/IJsonSchema.d.ts +1 -0
  64. package/lib/schemas/metadata/Metadata.js +1 -6
  65. package/lib/schemas/metadata/Metadata.js.map +1 -1
  66. package/lib/schemas/metadata/MetadataAtomic.d.ts +2 -0
  67. package/lib/schemas/metadata/MetadataAtomic.js +15 -0
  68. package/lib/schemas/metadata/MetadataAtomic.js.map +1 -1
  69. package/lib/transformers/features/json/JsonApplicationTransformer.d.ts +1 -1
  70. package/lib/transformers/features/json/JsonApplicationTransformer.js +4 -5
  71. package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
  72. package/lib/transformers/features/misc/MetadataTransformer.d.ts +1 -1
  73. package/lib/transformers/features/misc/MetadataTransformer.js +3 -4
  74. package/lib/transformers/features/misc/MetadataTransformer.js.map +1 -1
  75. package/package.json +2 -2
  76. package/src/Primitive.ts +135 -135
  77. package/src/executable/TypiaSetupWizard.ts +142 -142
  78. package/src/executable/setup/CommandExecutor.ts +8 -8
  79. package/src/factories/ExpressionFactory.ts +12 -13
  80. package/src/factories/JsonMetadataFactory.ts +53 -50
  81. package/src/factories/LiteralFactory.ts +2 -0
  82. package/src/factories/MetadataCollection.ts +282 -282
  83. package/src/factories/MetadataFactory.ts +48 -18
  84. package/src/factories/NumericRangeFactory.ts +56 -17
  85. package/src/factories/ProtobufFactory.ts +5 -2
  86. package/src/factories/internal/metadata/emplace_metadata_object.ts +178 -178
  87. package/src/functional/$stoll.ts +8 -8
  88. package/src/functional/Namespace.ts +168 -168
  89. package/src/programmers/AssertProgrammer.ts +322 -322
  90. package/src/programmers/CheckerProgrammer.ts +16 -17
  91. package/src/programmers/IsProgrammer.ts +258 -258
  92. package/src/programmers/RandomProgrammer.ts +4 -1
  93. package/src/programmers/ValidateProgrammer.ts +350 -350
  94. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  95. package/src/programmers/http/HttpHeadersProgrammer.ts +4 -1
  96. package/src/programmers/http/HttpParameterProgrammer.ts +4 -1
  97. package/src/programmers/http/HttpQueryProgrammer.ts +4 -1
  98. package/src/programmers/internal/application_array.ts +8 -0
  99. package/src/programmers/internal/application_number.ts +8 -1
  100. package/src/programmers/internal/application_string.ts +8 -1
  101. package/src/programmers/internal/check_array_length.ts +5 -2
  102. package/src/programmers/internal/check_bigint.ts +5 -2
  103. package/src/programmers/internal/check_dynamic_key.ts +178 -178
  104. package/src/programmers/internal/check_dynamic_properties.ts +202 -202
  105. package/src/programmers/internal/check_number.ts +5 -2
  106. package/src/programmers/internal/check_object.ts +62 -62
  107. package/src/programmers/internal/check_string.ts +0 -1
  108. package/src/programmers/json/JsonAssertParseProgrammer.ts +1 -0
  109. package/src/programmers/json/JsonIsParseProgrammer.ts +1 -0
  110. package/src/programmers/json/JsonStringifyProgrammer.ts +959 -960
  111. package/src/programmers/json/JsonValidateParseProgrammer.ts +1 -0
  112. package/src/programmers/misc/MiscCloneProgrammer.ts +787 -786
  113. package/src/programmers/misc/MiscLiteralsProgrammer.ts +4 -1
  114. package/src/programmers/misc/MiscPruneProgrammer.ts +549 -548
  115. package/src/programmers/notations/NotationGeneralProgrammer.ts +717 -716
  116. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +1 -0
  117. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +872 -882
  118. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +21 -21
  119. package/src/schemas/json/IJsonSchema.ts +1 -0
  120. package/src/schemas/metadata/IMetadataTypeTag.ts +7 -0
  121. package/src/schemas/metadata/Metadata.ts +1 -6
  122. package/src/schemas/metadata/MetadataAtomic.ts +17 -0
  123. package/src/transform.ts +35 -35
  124. package/src/transformers/features/json/JsonApplicationTransformer.ts +7 -4
  125. package/src/transformers/features/misc/MetadataTransformer.ts +6 -3
@@ -1,716 +1,717 @@
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 { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { StringUtil } from "../../utils/StringUtil";
19
-
20
- import { FeatureProgrammer } from "../FeatureProgrammer";
21
- import { IsProgrammer } from "../IsProgrammer";
22
- import { FunctionImporter } from "../helpers/FunctionImporeter";
23
- import { NotationJoiner } from "../helpers/NotationJoiner";
24
- import { UnionExplorer } from "../helpers/UnionExplorer";
25
- import { decode_union_object } from "../internal/decode_union_object";
26
- import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
27
-
28
- export namespace NotationGeneralProgrammer {
29
- export const returnType =
30
- (rename: (str: string) => string) => (type: string) =>
31
- `typia.${StringUtil.capitalize(rename.name)}Case<${type}>`;
32
-
33
- export const write =
34
- (rename: (str: string) => string) =>
35
- (project: IProject) =>
36
- (modulo: ts.LeftHandSideExpression) => {
37
- const importer: FunctionImporter = new FunctionImporter(
38
- modulo.getText(),
39
- );
40
- return FeatureProgrammer.write(project)({
41
- ...configure(rename)(project)(importer),
42
- addition: (collection) => [
43
- ...IsProgrammer.write_function_statements(project)(
44
- importer,
45
- )(collection),
46
- ...importer.declare(modulo),
47
- ],
48
- })(importer);
49
- };
50
-
51
- const write_array_functions =
52
- (config: FeatureProgrammer.IConfig) =>
53
- (importer: FunctionImporter) =>
54
- (collection: MetadataCollection): ts.VariableStatement[] =>
55
- collection
56
- .arrays()
57
- .filter((a) => a.recursive)
58
- .map((type, i) =>
59
- StatementFactory.constant(
60
- `${config.prefix}a${i}`,
61
- ts.factory.createArrowFunction(
62
- undefined,
63
- undefined,
64
- FeatureProgrammer.parameterDeclarations(config)(
65
- TypeFactory.keyword("any"),
66
- )(ts.factory.createIdentifier("input")),
67
- TypeFactory.keyword("any"),
68
- undefined,
69
- decode_array_inline(config)(importer)(
70
- ts.factory.createIdentifier("input"),
71
- MetadataArray.create({
72
- type,
73
- tags: [],
74
- }),
75
- {
76
- tracable: config.trace,
77
- source: "function",
78
- from: "array",
79
- postfix: "",
80
- },
81
- ),
82
- ),
83
- ),
84
- );
85
-
86
- const write_tuple_functions =
87
- (project: IProject) =>
88
- (config: FeatureProgrammer.IConfig) =>
89
- (importer: FunctionImporter) =>
90
- (collection: MetadataCollection): ts.VariableStatement[] =>
91
- collection
92
- .tuples()
93
- .filter((t) => t.recursive)
94
- .map((tuple, i) =>
95
- StatementFactory.constant(
96
- `${config.prefix}t${i}`,
97
- ts.factory.createArrowFunction(
98
- undefined,
99
- undefined,
100
- FeatureProgrammer.parameterDeclarations(config)(
101
- TypeFactory.keyword("any"),
102
- )(ts.factory.createIdentifier("input")),
103
- TypeFactory.keyword("any"),
104
- undefined,
105
- decode_tuple_inline(project)(config)(importer)(
106
- ts.factory.createIdentifier("input"),
107
- tuple,
108
- {
109
- tracable: config.trace,
110
- source: "function",
111
- from: "array",
112
- postfix: "",
113
- },
114
- ),
115
- ),
116
- ),
117
- );
118
-
119
- /* -----------------------------------------------------------
120
- DECODERS
121
- ----------------------------------------------------------- */
122
- const decode =
123
- (project: IProject) =>
124
- (config: FeatureProgrammer.IConfig) =>
125
- (importer: FunctionImporter) =>
126
- (
127
- input: ts.Expression,
128
- meta: Metadata,
129
- explore: FeatureProgrammer.IExplore,
130
- ): ts.Expression => {
131
- // ANY TYPE
132
- if (
133
- meta.any ||
134
- meta.arrays.some((a) => a.type.value.any) ||
135
- meta.tuples.some(
136
- (t) =>
137
- !!t.type.elements.length &&
138
- t.type.elements.every((e) => e.any),
139
- )
140
- )
141
- return ts.factory.createCallExpression(
142
- importer.use("any"),
143
- undefined,
144
- [input],
145
- );
146
-
147
- interface IUnion {
148
- type: string;
149
- is: () => ts.Expression;
150
- value: () => ts.Expression;
151
- }
152
- const unions: IUnion[] = [];
153
-
154
- //----
155
- // LIST UP UNION TYPES
156
- //----
157
- // FUNCTIONAL
158
- if (meta.functional)
159
- unions.push({
160
- type: "functional",
161
- is: () =>
162
- ts.factory.createStrictEquality(
163
- ts.factory.createStringLiteral("function"),
164
- ts.factory.createTypeOfExpression(input),
165
- ),
166
- value: () => ts.factory.createIdentifier("undefined"),
167
- });
168
-
169
- // TUPLES
170
- for (const tuple of meta.tuples)
171
- unions.push({
172
- type: "tuple",
173
- is: () =>
174
- IsProgrammer.decode(project)(importer)(
175
- input,
176
- (() => {
177
- const partial = Metadata.initialize();
178
- partial.tuples.push(tuple);
179
- return partial;
180
- })(),
181
- explore,
182
- ),
183
- value: () =>
184
- decode_tuple(project)(config)(importer)(
185
- input,
186
- tuple,
187
- explore,
188
- ),
189
- });
190
-
191
- // ARRAYS
192
- if (meta.arrays.length)
193
- unions.push({
194
- type: "array",
195
- is: () => ExpressionFactory.isArray(input),
196
- value: () =>
197
- explore_arrays(project)(config)(importer)(
198
- input,
199
- meta.arrays,
200
- {
201
- ...explore,
202
- from: "array",
203
- },
204
- ),
205
- });
206
-
207
- // NATIVE TYPES
208
- if (meta.sets.length)
209
- unions.push({
210
- type: "set",
211
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
212
- value: () =>
213
- explore_sets(project)(config)(importer)(
214
- input,
215
- meta.sets,
216
- { ...explore, from: "array" },
217
- ),
218
- });
219
- if (meta.maps.length)
220
- unions.push({
221
- type: "map",
222
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
223
- value: () =>
224
- explore_maps(project)(config)(importer)(
225
- input,
226
- meta.maps,
227
- {
228
- ...explore,
229
- from: "array",
230
- },
231
- ),
232
- });
233
- for (const native of meta.natives) {
234
- if (native === "WeakSet" || native === "WeakMap") continue;
235
- unions.push({
236
- type: "native",
237
- is: () => ExpressionFactory.isInstanceOf(native)(input),
238
- value: () =>
239
- native === "Boolean" ||
240
- native === "Number" ||
241
- native === "String"
242
- ? ts.factory.createCallExpression(
243
- IdentifierFactory.access(input)("valueOf"),
244
- undefined,
245
- undefined,
246
- )
247
- : decode_native(native)(input),
248
- });
249
- }
250
-
251
- // OBJECTS
252
- if (meta.objects.length)
253
- unions.push({
254
- type: "object",
255
- is: () =>
256
- ExpressionFactory.isObject({
257
- checkNull: true,
258
- checkArray: false,
259
- })(input),
260
- value: () =>
261
- explore_objects(config)(importer)(input, meta, {
262
- ...explore,
263
- from: "object",
264
- }),
265
- });
266
-
267
- // COMPOSITION
268
- let last: ts.Expression = input;
269
- for (const u of unions.reverse())
270
- last = ts.factory.createConditionalExpression(
271
- u.is(),
272
- undefined,
273
- u.value(),
274
- undefined,
275
- last,
276
- );
277
- return ts.factory.createAsExpression(
278
- last,
279
- TypeFactory.keyword("any"),
280
- );
281
- };
282
-
283
- const decode_object = (importer: FunctionImporter) =>
284
- FeatureProgrammer.decode_object({
285
- trace: false,
286
- path: false,
287
- prefix: PREFIX,
288
- })(importer);
289
-
290
- const decode_array =
291
- (config: FeatureProgrammer.IConfig) =>
292
- (importer: FunctionImporter) =>
293
- (
294
- input: ts.Expression,
295
- array: MetadataArray,
296
- explore: FeatureProgrammer.IExplore,
297
- ) =>
298
- array.type.recursive
299
- ? ts.factory.createCallExpression(
300
- ts.factory.createIdentifier(
301
- importer.useLocal(
302
- `${config.prefix}a${array.type.index}`,
303
- ),
304
- ),
305
- undefined,
306
- FeatureProgrammer.argumentsArray(config)({
307
- ...explore,
308
- source: "function",
309
- from: "array",
310
- })(input),
311
- )
312
- : decode_array_inline(config)(importer)(input, array, explore);
313
-
314
- const decode_array_inline =
315
- (config: FeatureProgrammer.IConfig) =>
316
- (importer: FunctionImporter) =>
317
- (
318
- input: ts.Expression,
319
- array: MetadataArray,
320
- explore: FeatureProgrammer.IExplore,
321
- ) =>
322
- FeatureProgrammer.decode_array(config)(importer)(
323
- NotationJoiner.array,
324
- )(input, array, explore);
325
-
326
- const decode_tuple =
327
- (project: IProject) =>
328
- (config: FeatureProgrammer.IConfig) =>
329
- (importer: FunctionImporter) =>
330
- (
331
- input: ts.Expression,
332
- tuple: MetadataTuple,
333
- explore: FeatureProgrammer.IExplore,
334
- ): ts.Expression =>
335
- tuple.type.recursive
336
- ? ts.factory.createCallExpression(
337
- ts.factory.createIdentifier(
338
- importer.useLocal(
339
- `${config.prefix}t${tuple.type.index}`,
340
- ),
341
- ),
342
- undefined,
343
- FeatureProgrammer.argumentsArray(config)({
344
- ...explore,
345
- source: "function",
346
- })(input),
347
- )
348
- : decode_tuple_inline(project)(config)(importer)(
349
- input,
350
- tuple.type,
351
- explore,
352
- );
353
-
354
- const decode_tuple_inline =
355
- (project: IProject) =>
356
- (config: FeatureProgrammer.IConfig) =>
357
- (importer: FunctionImporter) =>
358
- (
359
- input: ts.Expression,
360
- tuple: MetadataTupleType,
361
- explore: FeatureProgrammer.IExplore,
362
- ): ts.Expression => {
363
- const children: ts.Expression[] = tuple.elements
364
- .filter((m) => m.rest === null)
365
- .map((elem, index) =>
366
- decode(project)(config)(importer)(
367
- ts.factory.createElementAccessExpression(input, index),
368
- elem,
369
- {
370
- ...explore,
371
- from: "array",
372
- postfix: explore.postfix.length
373
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
374
- : `"[${index}]"`,
375
- },
376
- ),
377
- );
378
- const rest = (() => {
379
- if (tuple.elements.length === 0) return null;
380
-
381
- const last: Metadata = tuple.elements.at(-1)!;
382
- const rest: Metadata | null = last.rest;
383
- if (rest === null) return null;
384
-
385
- return decode(project)(config)(importer)(
386
- ts.factory.createCallExpression(
387
- IdentifierFactory.access(input)("slice"),
388
- undefined,
389
- [ExpressionFactory.number(tuple.elements.length - 1)],
390
- ),
391
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
392
- {
393
- ...explore,
394
- start: tuple.elements.length - 1,
395
- },
396
- );
397
- })();
398
- return NotationJoiner.tuple(children, rest);
399
- };
400
-
401
- /* -----------------------------------------------------------
402
- NATIVE CLASSES
403
- ----------------------------------------------------------- */
404
- const decode_native = (type: string) => (input: ts.Expression) =>
405
- type === "Date"
406
- ? ts.factory.createNewExpression(
407
- ts.factory.createIdentifier(type),
408
- undefined,
409
- [input],
410
- )
411
- : input;
412
-
413
- /* -----------------------------------------------------------
414
- EXPLORERS FOR UNION TYPES
415
- ----------------------------------------------------------- */
416
- const explore_sets =
417
- (project: IProject) =>
418
- (config: FeatureProgrammer.IConfig) =>
419
- (importer: FunctionImporter) =>
420
- (
421
- input: ts.Expression,
422
- sets: Metadata[],
423
- explore: FeatureProgrammer.IExplore,
424
- ): ts.Expression =>
425
- ts.factory.createCallExpression(
426
- UnionExplorer.set({
427
- checker: IsProgrammer.decode(project)(importer),
428
- decoder: (input, array, explore) =>
429
- ts.factory.createNewExpression(
430
- ts.factory.createIdentifier("Set"),
431
- [TypeFactory.keyword("any")],
432
- [
433
- decode_array(config)(importer)(
434
- input,
435
- array,
436
- explore,
437
- ),
438
- ],
439
- ),
440
- empty: ts.factory.createNewExpression(
441
- ts.factory.createIdentifier("Set"),
442
- [TypeFactory.keyword("any")],
443
- [],
444
- ),
445
- success: ts.factory.createTrue(),
446
- failure: (input, expected) =>
447
- create_throw_error(importer)(expected)(input),
448
- })([])(input, sets, explore),
449
- undefined,
450
- undefined,
451
- );
452
-
453
- const explore_maps =
454
- (project: IProject) =>
455
- (config: FeatureProgrammer.IConfig) =>
456
- (importer: FunctionImporter) =>
457
- (
458
- input: ts.Expression,
459
- maps: Metadata.Entry[],
460
- explore: FeatureProgrammer.IExplore,
461
- ): ts.Expression =>
462
- ts.factory.createCallExpression(
463
- UnionExplorer.map({
464
- checker: (top, entry, explore) => {
465
- const func = IsProgrammer.decode(project)(importer);
466
- return ts.factory.createLogicalAnd(
467
- func(
468
- ts.factory.createElementAccessExpression(
469
- top,
470
- 0,
471
- ),
472
- entry[0],
473
- {
474
- ...explore,
475
- postfix: `${explore.postfix}[0]`,
476
- },
477
- ),
478
- func(
479
- ts.factory.createElementAccessExpression(
480
- top,
481
- 1,
482
- ),
483
- entry[1],
484
- {
485
- ...explore,
486
- postfix: `${explore.postfix}[1]`,
487
- },
488
- ),
489
- );
490
- },
491
- decoder: (input, array, explore) =>
492
- ts.factory.createNewExpression(
493
- ts.factory.createIdentifier("Map"),
494
- [
495
- TypeFactory.keyword("any"),
496
- TypeFactory.keyword("any"),
497
- ],
498
- [
499
- decode_array(config)(importer)(
500
- input,
501
- array,
502
- explore,
503
- ),
504
- ],
505
- ),
506
- empty: ts.factory.createNewExpression(
507
- ts.factory.createIdentifier("Map"),
508
- [
509
- TypeFactory.keyword("any"),
510
- TypeFactory.keyword("any"),
511
- ],
512
- [],
513
- ),
514
- success: ts.factory.createTrue(),
515
- failure: (input, expected) =>
516
- create_throw_error(importer)(expected)(input),
517
- })([])(input, maps, explore),
518
- undefined,
519
- undefined,
520
- );
521
-
522
- const explore_objects =
523
- (config: FeatureProgrammer.IConfig) =>
524
- (importer: FunctionImporter) =>
525
- (
526
- input: ts.Expression,
527
- meta: Metadata,
528
- explore: FeatureProgrammer.IExplore,
529
- ) => {
530
- if (meta.objects.length === 1)
531
- return decode_object(importer)(
532
- input,
533
- meta.objects[0]!,
534
- explore,
535
- );
536
-
537
- return ts.factory.createCallExpression(
538
- ts.factory.createIdentifier(
539
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
540
- ),
541
- undefined,
542
- FeatureProgrammer.argumentsArray(config)(explore)(input),
543
- );
544
- };
545
-
546
- const explore_arrays =
547
- (project: IProject) =>
548
- (config: FeatureProgrammer.IConfig) =>
549
- (importer: FunctionImporter) =>
550
- (
551
- input: ts.Expression,
552
- elements: MetadataArray[],
553
- explore: FeatureProgrammer.IExplore,
554
- ): ts.Expression =>
555
- explore_array_like_union_types(config)(importer)(
556
- UnionExplorer.array({
557
- checker: IsProgrammer.decode(project)(importer),
558
- decoder: decode_array(config)(importer),
559
- empty: ts.factory.createIdentifier("[]"),
560
- success: ts.factory.createTrue(),
561
- failure: (input, expected) =>
562
- create_throw_error(importer)(expected)(input),
563
- }),
564
- )(input, elements, explore);
565
-
566
- const explore_array_like_union_types =
567
- (config: FeatureProgrammer.IConfig) =>
568
- (importer: FunctionImporter) =>
569
- <T extends MetadataArray | MetadataTuple>(
570
- factory: (
571
- parameters: ts.ParameterDeclaration[],
572
- ) => (
573
- input: ts.Expression,
574
- elements: T[],
575
- explore: FeatureProgrammer.IExplore,
576
- ) => ts.ArrowFunction,
577
- ) =>
578
- (
579
- input: ts.Expression,
580
- elements: T[],
581
- explore: FeatureProgrammer.IExplore,
582
- ): ts.Expression => {
583
- const arrow =
584
- (parameters: ts.ParameterDeclaration[]) =>
585
- (explore: FeatureProgrammer.IExplore) =>
586
- (input: ts.Expression): ts.ArrowFunction =>
587
- factory(parameters)(input, elements, explore);
588
- if (elements.every((e) => e.type.recursive === false))
589
- ts.factory.createCallExpression(
590
- arrow([])(explore)(input),
591
- undefined,
592
- [],
593
- );
594
-
595
- explore = {
596
- ...explore,
597
- source: "function",
598
- from: "array",
599
- };
600
- return ts.factory.createCallExpression(
601
- ts.factory.createIdentifier(
602
- importer.emplaceUnion(
603
- config.prefix,
604
- elements.map((e) => e.type.name).join(" | "),
605
- () =>
606
- arrow(
607
- FeatureProgrammer.parameterDeclarations(config)(
608
- TypeFactory.keyword("any"),
609
- )(ts.factory.createIdentifier("input")),
610
- )({
611
- ...explore,
612
- postfix: "",
613
- })(ts.factory.createIdentifier("input")),
614
- ),
615
- ),
616
- undefined,
617
- FeatureProgrammer.argumentsArray(config)(explore)(input),
618
- );
619
- };
620
-
621
- /* -----------------------------------------------------------
622
- CONFIGURATIONS
623
- ----------------------------------------------------------- */
624
- const PREFIX = "$c";
625
-
626
- const configure =
627
- (rename: (str: string) => string) =>
628
- (project: IProject) =>
629
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
630
- const config: FeatureProgrammer.IConfig = {
631
- types: {
632
- input: (type, name) =>
633
- ts.factory.createTypeReferenceNode(
634
- name ??
635
- TypeFactory.getFullName(project.checker)(type),
636
- ),
637
- output: (type, name) =>
638
- ts.factory.createTypeReferenceNode(
639
- returnType(rename)(
640
- name ??
641
- TypeFactory.getFullName(project.checker)(
642
- type,
643
- ),
644
- ),
645
- ),
646
- },
647
- prefix: PREFIX,
648
- trace: false,
649
- path: false,
650
- initializer,
651
- decoder: () => decode(project)(config)(importer),
652
- objector: {
653
- checker: () => IsProgrammer.decode(project)(importer),
654
- decoder: () => decode_object(importer),
655
- joiner: NotationJoiner.object(rename),
656
- unionizer: decode_union_object(
657
- IsProgrammer.decode_object(project)(importer),
658
- )(decode_object(importer))((exp) => exp)(
659
- (input, expected) =>
660
- create_throw_error(importer)(expected)(input),
661
- ),
662
- failure: (input, expected) =>
663
- create_throw_error(importer)(expected)(input),
664
- },
665
- generator: {
666
- arrays: () => write_array_functions(config)(importer),
667
- tuples: () =>
668
- write_tuple_functions(project)(config)(importer),
669
- },
670
- };
671
- return config;
672
- };
673
-
674
- const initializer: FeatureProgrammer.IConfig["initializer"] =
675
- ({ checker }) =>
676
- (importer) =>
677
- (type) => {
678
- const collection = new MetadataCollection();
679
- const result = MetadataFactory.analyze(checker)({
680
- escape: false,
681
- constant: true,
682
- absorb: true,
683
- })(collection)(type);
684
- if (result.success === false)
685
- throw TransformerError.from(`typia.misc.${importer.method}`)(
686
- result.errors,
687
- );
688
- return [collection, result.data];
689
- };
690
-
691
- const create_throw_error =
692
- (importer: FunctionImporter) =>
693
- (expected: string) =>
694
- (value: ts.Expression) =>
695
- ts.factory.createExpressionStatement(
696
- ts.factory.createCallExpression(
697
- importer.use("throws"),
698
- [],
699
- [
700
- ts.factory.createObjectLiteralExpression(
701
- [
702
- ts.factory.createPropertyAssignment(
703
- "expected",
704
- ts.factory.createStringLiteral(expected),
705
- ),
706
- ts.factory.createPropertyAssignment(
707
- "value",
708
- value,
709
- ),
710
- ],
711
- true,
712
- ),
713
- ],
714
- ),
715
- );
716
- }
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 { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { StringUtil } from "../../utils/StringUtil";
19
+
20
+ import { FeatureProgrammer } from "../FeatureProgrammer";
21
+ import { IsProgrammer } from "../IsProgrammer";
22
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
23
+ import { NotationJoiner } from "../helpers/NotationJoiner";
24
+ import { UnionExplorer } from "../helpers/UnionExplorer";
25
+ import { decode_union_object } from "../internal/decode_union_object";
26
+ import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
27
+
28
+ export namespace NotationGeneralProgrammer {
29
+ export const returnType =
30
+ (rename: (str: string) => string) => (type: string) =>
31
+ `typia.${StringUtil.capitalize(rename.name)}Case<${type}>`;
32
+
33
+ export const write =
34
+ (rename: (str: string) => string) =>
35
+ (project: IProject) =>
36
+ (modulo: ts.LeftHandSideExpression) => {
37
+ const importer: FunctionImporter = new FunctionImporter(
38
+ modulo.getText(),
39
+ );
40
+ return FeatureProgrammer.write(project)({
41
+ ...configure(rename)(project)(importer),
42
+ addition: (collection) => [
43
+ ...IsProgrammer.write_function_statements(project)(
44
+ importer,
45
+ )(collection),
46
+ ...importer.declare(modulo),
47
+ ],
48
+ })(importer);
49
+ };
50
+
51
+ const write_array_functions =
52
+ (config: FeatureProgrammer.IConfig) =>
53
+ (importer: FunctionImporter) =>
54
+ (collection: MetadataCollection): ts.VariableStatement[] =>
55
+ collection
56
+ .arrays()
57
+ .filter((a) => a.recursive)
58
+ .map((type, i) =>
59
+ StatementFactory.constant(
60
+ `${config.prefix}a${i}`,
61
+ ts.factory.createArrowFunction(
62
+ undefined,
63
+ undefined,
64
+ FeatureProgrammer.parameterDeclarations(config)(
65
+ TypeFactory.keyword("any"),
66
+ )(ts.factory.createIdentifier("input")),
67
+ TypeFactory.keyword("any"),
68
+ undefined,
69
+ decode_array_inline(config)(importer)(
70
+ ts.factory.createIdentifier("input"),
71
+ MetadataArray.create({
72
+ type,
73
+ tags: [],
74
+ }),
75
+ {
76
+ tracable: config.trace,
77
+ source: "function",
78
+ from: "array",
79
+ postfix: "",
80
+ },
81
+ ),
82
+ ),
83
+ ),
84
+ );
85
+
86
+ const write_tuple_functions =
87
+ (project: IProject) =>
88
+ (config: FeatureProgrammer.IConfig) =>
89
+ (importer: FunctionImporter) =>
90
+ (collection: MetadataCollection): ts.VariableStatement[] =>
91
+ collection
92
+ .tuples()
93
+ .filter((t) => t.recursive)
94
+ .map((tuple, i) =>
95
+ StatementFactory.constant(
96
+ `${config.prefix}t${i}`,
97
+ ts.factory.createArrowFunction(
98
+ undefined,
99
+ undefined,
100
+ FeatureProgrammer.parameterDeclarations(config)(
101
+ TypeFactory.keyword("any"),
102
+ )(ts.factory.createIdentifier("input")),
103
+ TypeFactory.keyword("any"),
104
+ undefined,
105
+ decode_tuple_inline(project)(config)(importer)(
106
+ ts.factory.createIdentifier("input"),
107
+ tuple,
108
+ {
109
+ tracable: config.trace,
110
+ source: "function",
111
+ from: "array",
112
+ postfix: "",
113
+ },
114
+ ),
115
+ ),
116
+ ),
117
+ );
118
+
119
+ /* -----------------------------------------------------------
120
+ DECODERS
121
+ ----------------------------------------------------------- */
122
+ const decode =
123
+ (project: IProject) =>
124
+ (config: FeatureProgrammer.IConfig) =>
125
+ (importer: FunctionImporter) =>
126
+ (
127
+ input: ts.Expression,
128
+ meta: Metadata,
129
+ explore: FeatureProgrammer.IExplore,
130
+ ): ts.Expression => {
131
+ // ANY TYPE
132
+ if (
133
+ meta.any ||
134
+ meta.arrays.some((a) => a.type.value.any) ||
135
+ meta.tuples.some(
136
+ (t) =>
137
+ !!t.type.elements.length &&
138
+ t.type.elements.every((e) => e.any),
139
+ )
140
+ )
141
+ return ts.factory.createCallExpression(
142
+ importer.use("any"),
143
+ undefined,
144
+ [input],
145
+ );
146
+
147
+ interface IUnion {
148
+ type: string;
149
+ is: () => ts.Expression;
150
+ value: () => ts.Expression;
151
+ }
152
+ const unions: IUnion[] = [];
153
+
154
+ //----
155
+ // LIST UP UNION TYPES
156
+ //----
157
+ // FUNCTIONAL
158
+ if (meta.functional)
159
+ unions.push({
160
+ type: "functional",
161
+ is: () =>
162
+ ts.factory.createStrictEquality(
163
+ ts.factory.createStringLiteral("function"),
164
+ ts.factory.createTypeOfExpression(input),
165
+ ),
166
+ value: () => ts.factory.createIdentifier("undefined"),
167
+ });
168
+
169
+ // TUPLES
170
+ for (const tuple of meta.tuples)
171
+ unions.push({
172
+ type: "tuple",
173
+ is: () =>
174
+ IsProgrammer.decode(project)(importer)(
175
+ input,
176
+ (() => {
177
+ const partial = Metadata.initialize();
178
+ partial.tuples.push(tuple);
179
+ return partial;
180
+ })(),
181
+ explore,
182
+ ),
183
+ value: () =>
184
+ decode_tuple(project)(config)(importer)(
185
+ input,
186
+ tuple,
187
+ explore,
188
+ ),
189
+ });
190
+
191
+ // ARRAYS
192
+ if (meta.arrays.length)
193
+ unions.push({
194
+ type: "array",
195
+ is: () => ExpressionFactory.isArray(input),
196
+ value: () =>
197
+ explore_arrays(project)(config)(importer)(
198
+ input,
199
+ meta.arrays,
200
+ {
201
+ ...explore,
202
+ from: "array",
203
+ },
204
+ ),
205
+ });
206
+
207
+ // NATIVE TYPES
208
+ if (meta.sets.length)
209
+ unions.push({
210
+ type: "set",
211
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
212
+ value: () =>
213
+ explore_sets(project)(config)(importer)(
214
+ input,
215
+ meta.sets,
216
+ { ...explore, from: "array" },
217
+ ),
218
+ });
219
+ if (meta.maps.length)
220
+ unions.push({
221
+ type: "map",
222
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
223
+ value: () =>
224
+ explore_maps(project)(config)(importer)(
225
+ input,
226
+ meta.maps,
227
+ {
228
+ ...explore,
229
+ from: "array",
230
+ },
231
+ ),
232
+ });
233
+ for (const native of meta.natives) {
234
+ if (native === "WeakSet" || native === "WeakMap") continue;
235
+ unions.push({
236
+ type: "native",
237
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
238
+ value: () =>
239
+ native === "Boolean" ||
240
+ native === "Number" ||
241
+ native === "String"
242
+ ? ts.factory.createCallExpression(
243
+ IdentifierFactory.access(input)("valueOf"),
244
+ undefined,
245
+ undefined,
246
+ )
247
+ : decode_native(native)(input),
248
+ });
249
+ }
250
+
251
+ // OBJECTS
252
+ if (meta.objects.length)
253
+ unions.push({
254
+ type: "object",
255
+ is: () =>
256
+ ExpressionFactory.isObject({
257
+ checkNull: true,
258
+ checkArray: false,
259
+ })(input),
260
+ value: () =>
261
+ explore_objects(config)(importer)(input, meta, {
262
+ ...explore,
263
+ from: "object",
264
+ }),
265
+ });
266
+
267
+ // COMPOSITION
268
+ let last: ts.Expression = input;
269
+ for (const u of unions.reverse())
270
+ last = ts.factory.createConditionalExpression(
271
+ u.is(),
272
+ undefined,
273
+ u.value(),
274
+ undefined,
275
+ last,
276
+ );
277
+ return ts.factory.createAsExpression(
278
+ last,
279
+ TypeFactory.keyword("any"),
280
+ );
281
+ };
282
+
283
+ const decode_object = (importer: FunctionImporter) =>
284
+ FeatureProgrammer.decode_object({
285
+ trace: false,
286
+ path: false,
287
+ prefix: PREFIX,
288
+ })(importer);
289
+
290
+ const decode_array =
291
+ (config: FeatureProgrammer.IConfig) =>
292
+ (importer: FunctionImporter) =>
293
+ (
294
+ input: ts.Expression,
295
+ array: MetadataArray,
296
+ explore: FeatureProgrammer.IExplore,
297
+ ) =>
298
+ array.type.recursive
299
+ ? ts.factory.createCallExpression(
300
+ ts.factory.createIdentifier(
301
+ importer.useLocal(
302
+ `${config.prefix}a${array.type.index}`,
303
+ ),
304
+ ),
305
+ undefined,
306
+ FeatureProgrammer.argumentsArray(config)({
307
+ ...explore,
308
+ source: "function",
309
+ from: "array",
310
+ })(input),
311
+ )
312
+ : decode_array_inline(config)(importer)(input, array, explore);
313
+
314
+ const decode_array_inline =
315
+ (config: FeatureProgrammer.IConfig) =>
316
+ (importer: FunctionImporter) =>
317
+ (
318
+ input: ts.Expression,
319
+ array: MetadataArray,
320
+ explore: FeatureProgrammer.IExplore,
321
+ ) =>
322
+ FeatureProgrammer.decode_array(config)(importer)(
323
+ NotationJoiner.array,
324
+ )(input, array, explore);
325
+
326
+ const decode_tuple =
327
+ (project: IProject) =>
328
+ (config: FeatureProgrammer.IConfig) =>
329
+ (importer: FunctionImporter) =>
330
+ (
331
+ input: ts.Expression,
332
+ tuple: MetadataTuple,
333
+ explore: FeatureProgrammer.IExplore,
334
+ ): ts.Expression =>
335
+ tuple.type.recursive
336
+ ? ts.factory.createCallExpression(
337
+ ts.factory.createIdentifier(
338
+ importer.useLocal(
339
+ `${config.prefix}t${tuple.type.index}`,
340
+ ),
341
+ ),
342
+ undefined,
343
+ FeatureProgrammer.argumentsArray(config)({
344
+ ...explore,
345
+ source: "function",
346
+ })(input),
347
+ )
348
+ : decode_tuple_inline(project)(config)(importer)(
349
+ input,
350
+ tuple.type,
351
+ explore,
352
+ );
353
+
354
+ const decode_tuple_inline =
355
+ (project: IProject) =>
356
+ (config: FeatureProgrammer.IConfig) =>
357
+ (importer: FunctionImporter) =>
358
+ (
359
+ input: ts.Expression,
360
+ tuple: MetadataTupleType,
361
+ explore: FeatureProgrammer.IExplore,
362
+ ): ts.Expression => {
363
+ const children: ts.Expression[] = tuple.elements
364
+ .filter((m) => m.rest === null)
365
+ .map((elem, index) =>
366
+ decode(project)(config)(importer)(
367
+ ts.factory.createElementAccessExpression(input, index),
368
+ elem,
369
+ {
370
+ ...explore,
371
+ from: "array",
372
+ postfix: explore.postfix.length
373
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
374
+ : `"[${index}]"`,
375
+ },
376
+ ),
377
+ );
378
+ const rest = (() => {
379
+ if (tuple.elements.length === 0) return null;
380
+
381
+ const last: Metadata = tuple.elements.at(-1)!;
382
+ const rest: Metadata | null = last.rest;
383
+ if (rest === null) return null;
384
+
385
+ return decode(project)(config)(importer)(
386
+ ts.factory.createCallExpression(
387
+ IdentifierFactory.access(input)("slice"),
388
+ undefined,
389
+ [ExpressionFactory.number(tuple.elements.length - 1)],
390
+ ),
391
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
392
+ {
393
+ ...explore,
394
+ start: tuple.elements.length - 1,
395
+ },
396
+ );
397
+ })();
398
+ return NotationJoiner.tuple(children, rest);
399
+ };
400
+
401
+ /* -----------------------------------------------------------
402
+ NATIVE CLASSES
403
+ ----------------------------------------------------------- */
404
+ const decode_native = (type: string) => (input: ts.Expression) =>
405
+ type === "Date"
406
+ ? ts.factory.createNewExpression(
407
+ ts.factory.createIdentifier(type),
408
+ undefined,
409
+ [input],
410
+ )
411
+ : input;
412
+
413
+ /* -----------------------------------------------------------
414
+ EXPLORERS FOR UNION TYPES
415
+ ----------------------------------------------------------- */
416
+ const explore_sets =
417
+ (project: IProject) =>
418
+ (config: FeatureProgrammer.IConfig) =>
419
+ (importer: FunctionImporter) =>
420
+ (
421
+ input: ts.Expression,
422
+ sets: Metadata[],
423
+ explore: FeatureProgrammer.IExplore,
424
+ ): ts.Expression =>
425
+ ts.factory.createCallExpression(
426
+ UnionExplorer.set({
427
+ checker: IsProgrammer.decode(project)(importer),
428
+ decoder: (input, array, explore) =>
429
+ ts.factory.createNewExpression(
430
+ ts.factory.createIdentifier("Set"),
431
+ [TypeFactory.keyword("any")],
432
+ [
433
+ decode_array(config)(importer)(
434
+ input,
435
+ array,
436
+ explore,
437
+ ),
438
+ ],
439
+ ),
440
+ empty: ts.factory.createNewExpression(
441
+ ts.factory.createIdentifier("Set"),
442
+ [TypeFactory.keyword("any")],
443
+ [],
444
+ ),
445
+ success: ts.factory.createTrue(),
446
+ failure: (input, expected) =>
447
+ create_throw_error(importer)(expected)(input),
448
+ })([])(input, sets, explore),
449
+ undefined,
450
+ undefined,
451
+ );
452
+
453
+ const explore_maps =
454
+ (project: IProject) =>
455
+ (config: FeatureProgrammer.IConfig) =>
456
+ (importer: FunctionImporter) =>
457
+ (
458
+ input: ts.Expression,
459
+ maps: Metadata.Entry[],
460
+ explore: FeatureProgrammer.IExplore,
461
+ ): ts.Expression =>
462
+ ts.factory.createCallExpression(
463
+ UnionExplorer.map({
464
+ checker: (top, entry, explore) => {
465
+ const func = IsProgrammer.decode(project)(importer);
466
+ return ts.factory.createLogicalAnd(
467
+ func(
468
+ ts.factory.createElementAccessExpression(
469
+ top,
470
+ 0,
471
+ ),
472
+ entry[0],
473
+ {
474
+ ...explore,
475
+ postfix: `${explore.postfix}[0]`,
476
+ },
477
+ ),
478
+ func(
479
+ ts.factory.createElementAccessExpression(
480
+ top,
481
+ 1,
482
+ ),
483
+ entry[1],
484
+ {
485
+ ...explore,
486
+ postfix: `${explore.postfix}[1]`,
487
+ },
488
+ ),
489
+ );
490
+ },
491
+ decoder: (input, array, explore) =>
492
+ ts.factory.createNewExpression(
493
+ ts.factory.createIdentifier("Map"),
494
+ [
495
+ TypeFactory.keyword("any"),
496
+ TypeFactory.keyword("any"),
497
+ ],
498
+ [
499
+ decode_array(config)(importer)(
500
+ input,
501
+ array,
502
+ explore,
503
+ ),
504
+ ],
505
+ ),
506
+ empty: ts.factory.createNewExpression(
507
+ ts.factory.createIdentifier("Map"),
508
+ [
509
+ TypeFactory.keyword("any"),
510
+ TypeFactory.keyword("any"),
511
+ ],
512
+ [],
513
+ ),
514
+ success: ts.factory.createTrue(),
515
+ failure: (input, expected) =>
516
+ create_throw_error(importer)(expected)(input),
517
+ })([])(input, maps, explore),
518
+ undefined,
519
+ undefined,
520
+ );
521
+
522
+ const explore_objects =
523
+ (config: FeatureProgrammer.IConfig) =>
524
+ (importer: FunctionImporter) =>
525
+ (
526
+ input: ts.Expression,
527
+ meta: Metadata,
528
+ explore: FeatureProgrammer.IExplore,
529
+ ) => {
530
+ if (meta.objects.length === 1)
531
+ return decode_object(importer)(
532
+ input,
533
+ meta.objects[0]!,
534
+ explore,
535
+ );
536
+
537
+ return ts.factory.createCallExpression(
538
+ ts.factory.createIdentifier(
539
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
540
+ ),
541
+ undefined,
542
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
543
+ );
544
+ };
545
+
546
+ const explore_arrays =
547
+ (project: IProject) =>
548
+ (config: FeatureProgrammer.IConfig) =>
549
+ (importer: FunctionImporter) =>
550
+ (
551
+ input: ts.Expression,
552
+ elements: MetadataArray[],
553
+ explore: FeatureProgrammer.IExplore,
554
+ ): ts.Expression =>
555
+ explore_array_like_union_types(config)(importer)(
556
+ UnionExplorer.array({
557
+ checker: IsProgrammer.decode(project)(importer),
558
+ decoder: decode_array(config)(importer),
559
+ empty: ts.factory.createIdentifier("[]"),
560
+ success: ts.factory.createTrue(),
561
+ failure: (input, expected) =>
562
+ create_throw_error(importer)(expected)(input),
563
+ }),
564
+ )(input, elements, explore);
565
+
566
+ const explore_array_like_union_types =
567
+ (config: FeatureProgrammer.IConfig) =>
568
+ (importer: FunctionImporter) =>
569
+ <T extends MetadataArray | MetadataTuple>(
570
+ factory: (
571
+ parameters: ts.ParameterDeclaration[],
572
+ ) => (
573
+ input: ts.Expression,
574
+ elements: T[],
575
+ explore: FeatureProgrammer.IExplore,
576
+ ) => ts.ArrowFunction,
577
+ ) =>
578
+ (
579
+ input: ts.Expression,
580
+ elements: T[],
581
+ explore: FeatureProgrammer.IExplore,
582
+ ): ts.Expression => {
583
+ const arrow =
584
+ (parameters: ts.ParameterDeclaration[]) =>
585
+ (explore: FeatureProgrammer.IExplore) =>
586
+ (input: ts.Expression): ts.ArrowFunction =>
587
+ factory(parameters)(input, elements, explore);
588
+ if (elements.every((e) => e.type.recursive === false))
589
+ ts.factory.createCallExpression(
590
+ arrow([])(explore)(input),
591
+ undefined,
592
+ [],
593
+ );
594
+
595
+ explore = {
596
+ ...explore,
597
+ source: "function",
598
+ from: "array",
599
+ };
600
+ return ts.factory.createCallExpression(
601
+ ts.factory.createIdentifier(
602
+ importer.emplaceUnion(
603
+ config.prefix,
604
+ elements.map((e) => e.type.name).join(" | "),
605
+ () =>
606
+ arrow(
607
+ FeatureProgrammer.parameterDeclarations(config)(
608
+ TypeFactory.keyword("any"),
609
+ )(ts.factory.createIdentifier("input")),
610
+ )({
611
+ ...explore,
612
+ postfix: "",
613
+ })(ts.factory.createIdentifier("input")),
614
+ ),
615
+ ),
616
+ undefined,
617
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
618
+ );
619
+ };
620
+
621
+ /* -----------------------------------------------------------
622
+ CONFIGURATIONS
623
+ ----------------------------------------------------------- */
624
+ const PREFIX = "$c";
625
+
626
+ const configure =
627
+ (rename: (str: string) => string) =>
628
+ (project: IProject) =>
629
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
630
+ const config: FeatureProgrammer.IConfig = {
631
+ types: {
632
+ input: (type, name) =>
633
+ ts.factory.createTypeReferenceNode(
634
+ name ??
635
+ TypeFactory.getFullName(project.checker)(type),
636
+ ),
637
+ output: (type, name) =>
638
+ ts.factory.createTypeReferenceNode(
639
+ returnType(rename)(
640
+ name ??
641
+ TypeFactory.getFullName(project.checker)(
642
+ type,
643
+ ),
644
+ ),
645
+ ),
646
+ },
647
+ prefix: PREFIX,
648
+ trace: false,
649
+ path: false,
650
+ initializer,
651
+ decoder: () => decode(project)(config)(importer),
652
+ objector: {
653
+ checker: () => IsProgrammer.decode(project)(importer),
654
+ decoder: () => decode_object(importer),
655
+ joiner: NotationJoiner.object(rename),
656
+ unionizer: decode_union_object(
657
+ IsProgrammer.decode_object(project)(importer),
658
+ )(decode_object(importer))((exp) => exp)(
659
+ (input, expected) =>
660
+ create_throw_error(importer)(expected)(input),
661
+ ),
662
+ failure: (input, expected) =>
663
+ create_throw_error(importer)(expected)(input),
664
+ },
665
+ generator: {
666
+ arrays: () => write_array_functions(config)(importer),
667
+ tuples: () =>
668
+ write_tuple_functions(project)(config)(importer),
669
+ },
670
+ };
671
+ return config;
672
+ };
673
+
674
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
675
+ (project) => (importer) => (type) => {
676
+ const collection = new MetadataCollection();
677
+ const result = MetadataFactory.analyze(
678
+ project.checker,
679
+ project.context,
680
+ )({
681
+ escape: false,
682
+ constant: true,
683
+ absorb: true,
684
+ })(collection)(type);
685
+ if (result.success === false)
686
+ throw TransformerError.from(`typia.misc.${importer.method}`)(
687
+ result.errors,
688
+ );
689
+ return [collection, result.data];
690
+ };
691
+
692
+ const create_throw_error =
693
+ (importer: FunctionImporter) =>
694
+ (expected: string) =>
695
+ (value: ts.Expression) =>
696
+ ts.factory.createExpressionStatement(
697
+ ts.factory.createCallExpression(
698
+ importer.use("throws"),
699
+ [],
700
+ [
701
+ ts.factory.createObjectLiteralExpression(
702
+ [
703
+ ts.factory.createPropertyAssignment(
704
+ "expected",
705
+ ts.factory.createStringLiteral(expected),
706
+ ),
707
+ ts.factory.createPropertyAssignment(
708
+ "value",
709
+ value,
710
+ ),
711
+ ],
712
+ true,
713
+ ),
714
+ ],
715
+ ),
716
+ );
717
+ }