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,960 +1,959 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { JsonMetadataFactory } from "../../factories/JsonMetadataFactory";
6
- import { MetadataCollection } from "../../factories/MetadataCollection";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
- import { ValueFactory } from "../../factories/ValueFactory";
10
-
11
- import { Metadata } from "../../schemas/metadata/Metadata";
12
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
13
- import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
16
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
17
-
18
- import { IProject } from "../../transformers/IProject";
19
-
20
- import { Atomic } from "../../typings/Atomic";
21
-
22
- import { ArrayUtil } from "../../utils/ArrayUtil";
23
-
24
- import { FeatureProgrammer } from "../FeatureProgrammer";
25
- import { IsProgrammer } from "../IsProgrammer";
26
- import { AtomicPredicator } from "../helpers/AtomicPredicator";
27
- import { FunctionImporter } from "../helpers/FunctionImporeter";
28
- import { IExpressionEntry } from "../helpers/IExpressionEntry";
29
- import { OptionPredicator } from "../helpers/OptionPredicator";
30
- import { StringifyJoiner } from "../helpers/StringifyJoinder";
31
- import { StringifyPredicator } from "../helpers/StringifyPredicator";
32
- import { UnionExplorer } from "../helpers/UnionExplorer";
33
- import { check_native } from "../internal/check_native";
34
- import { decode_union_object } from "../internal/decode_union_object";
35
- import { feature_object_entries } from "../internal/feature_object_entries";
36
- import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
37
-
38
- export namespace JsonStringifyProgrammer {
39
- /* -----------------------------------------------------------
40
- WRITER
41
- ----------------------------------------------------------- */
42
-
43
- export const write =
44
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
- const importer: FunctionImporter = new FunctionImporter(
46
- modulo.getText(),
47
- );
48
- const config: FeatureProgrammer.IConfig =
49
- configure(project)(importer);
50
-
51
- return FeatureProgrammer.write(project)({
52
- ...config,
53
- addition: (collection) => [
54
- ...IsProgrammer.write_function_statements(project)(
55
- importer,
56
- )(collection),
57
- ...importer.declare(modulo),
58
- ],
59
- })(importer);
60
- };
61
-
62
- const write_array_functions =
63
- (config: FeatureProgrammer.IConfig) =>
64
- (importer: FunctionImporter) =>
65
- (collection: MetadataCollection): ts.VariableStatement[] =>
66
- collection
67
- .arrays()
68
- .filter((a) => a.recursive)
69
- .map((type, i) =>
70
- StatementFactory.constant(
71
- `${config.prefix}a${i}`,
72
- ts.factory.createArrowFunction(
73
- undefined,
74
- undefined,
75
- FeatureProgrammer.parameterDeclarations(config)(
76
- TypeFactory.keyword("any"),
77
- )(ts.factory.createIdentifier("input")),
78
- TypeFactory.keyword("any"),
79
- undefined,
80
- decode_array_inline(config)(importer)(
81
- ts.factory.createIdentifier("input"),
82
- MetadataArray.create({
83
- type,
84
- tags: [],
85
- }),
86
- {
87
- tracable: config.trace,
88
- source: "function",
89
- from: "array",
90
- postfix: "",
91
- },
92
- ),
93
- ),
94
- ),
95
- );
96
-
97
- const write_tuple_functions =
98
- (project: IProject) =>
99
- (config: FeatureProgrammer.IConfig) =>
100
- (importer: FunctionImporter) =>
101
- (collection: MetadataCollection): ts.VariableStatement[] =>
102
- collection
103
- .tuples()
104
- .filter((t) => t.recursive)
105
- .map((tuple, i) =>
106
- StatementFactory.constant(
107
- `${config.prefix}t${i}`,
108
- ts.factory.createArrowFunction(
109
- undefined,
110
- undefined,
111
- FeatureProgrammer.parameterDeclarations(config)(
112
- TypeFactory.keyword("any"),
113
- )(ts.factory.createIdentifier("input")),
114
- TypeFactory.keyword("any"),
115
- undefined,
116
- decode_tuple_inline(project)(config)(importer)(
117
- ts.factory.createIdentifier("input"),
118
- tuple,
119
- {
120
- tracable: config.trace,
121
- source: "function",
122
- from: "array",
123
- postfix: "",
124
- },
125
- ),
126
- ),
127
- ),
128
- );
129
-
130
- /* -----------------------------------------------------------
131
- DECODERS
132
- ----------------------------------------------------------- */
133
- const decode =
134
- (project: IProject) =>
135
- (config: FeatureProgrammer.IConfig) =>
136
- (importer: FunctionImporter) =>
137
- (
138
- input: ts.Expression,
139
- meta: Metadata,
140
- explore: FeatureProgrammer.IExplore,
141
- ): ts.Expression => {
142
- // ANY TYPE
143
- if (meta.any === true)
144
- return wrap_required(
145
- input,
146
- meta,
147
- explore,
148
- )(
149
- wrap_functional(
150
- input,
151
- meta,
152
- explore,
153
- )(
154
- ts.factory.createCallExpression(
155
- ts.factory.createIdentifier("JSON.stringify"),
156
- undefined,
157
- [input],
158
- ),
159
- ),
160
- );
161
-
162
- // ONLY NULL OR UNDEFINED
163
- const size: number = meta.size();
164
- if (
165
- size === 0 &&
166
- (meta.isRequired() === false || meta.nullable === true)
167
- ) {
168
- if (meta.isRequired() === false && meta.nullable === true)
169
- return explore.from === "array"
170
- ? ts.factory.createStringLiteral("null")
171
- : ts.factory.createConditionalExpression(
172
- ts.factory.createStrictEquality(
173
- ts.factory.createNull(),
174
- input,
175
- ),
176
- undefined,
177
- ts.factory.createStringLiteral("null"),
178
- undefined,
179
- ts.factory.createIdentifier("undefined"),
180
- );
181
- else if (meta.isRequired() === false)
182
- return explore.from === "array"
183
- ? ts.factory.createStringLiteral("null")
184
- : ts.factory.createIdentifier("undefined");
185
- else return ts.factory.createStringLiteral("null");
186
- }
187
-
188
- //----
189
- // LIST UP UNION TYPES
190
- //----
191
- const unions: IUnion[] = [];
192
-
193
- // toJSON() METHOD
194
- if (meta.escaped !== null)
195
- unions.push({
196
- type: "resolved",
197
- is: () => IsProgrammer.decode_to_json(false)(input),
198
- value: () =>
199
- decode_to_json(project)(config)(importer)(
200
- input,
201
- meta.escaped!.returns,
202
- explore,
203
- ),
204
- });
205
- else if (meta.functional === true)
206
- unions.push({
207
- type: "functional",
208
- is: () => IsProgrammer.decode_functional(input),
209
- value: () => decode_functional(explore),
210
- });
211
-
212
- // TEMPLATES
213
- if (
214
- meta.templates.length ||
215
- ArrayUtil.has(meta.constants, (c) => c.type === "string")
216
- )
217
- if (AtomicPredicator.template(meta)) {
218
- const partial = Metadata.initialize();
219
- partial.atomics.push(
220
- MetadataAtomic.create({ type: "string", tags: [] }),
221
- ),
222
- unions.push({
223
- type: "template literal",
224
- is: () =>
225
- IsProgrammer.decode(project)(importer)(
226
- input,
227
- partial,
228
- explore,
229
- ),
230
- value: () =>
231
- decode_atomic(project)(importer)(
232
- input,
233
- "string",
234
- explore,
235
- ),
236
- });
237
- }
238
-
239
- // CONSTANTS
240
- for (const constant of meta.constants)
241
- if (AtomicPredicator.constant(meta)(constant.type) === false)
242
- continue;
243
- else if (constant.type !== "string")
244
- unions.push({
245
- type: "atomic",
246
- is: () =>
247
- IsProgrammer.decode(project)(importer)(
248
- input,
249
- (() => {
250
- const partial = Metadata.initialize();
251
- partial.atomics.push(
252
- MetadataAtomic.create({
253
- type: constant.type,
254
- tags: [],
255
- }),
256
- );
257
- return partial;
258
- })(),
259
- explore,
260
- ),
261
- value: () =>
262
- decode_atomic(project)(importer)(
263
- input,
264
- constant.type,
265
- explore,
266
- ),
267
- });
268
- else if (meta.templates.length === 0)
269
- unions.push({
270
- type: "const string",
271
- is: () =>
272
- IsProgrammer.decode(project)(importer)(
273
- input,
274
- (() => {
275
- const partial = Metadata.initialize();
276
- partial.atomics.push(
277
- MetadataAtomic.create({
278
- type: "string",
279
- tags: [],
280
- }),
281
- );
282
- return partial;
283
- })(),
284
- explore,
285
- ),
286
- value: () =>
287
- decode_constant_string(project)(importer)(
288
- input,
289
- [...constant.values] as string[],
290
- explore,
291
- ),
292
- });
293
-
294
- /// ATOMICS
295
- for (const a of meta.atomics)
296
- if (AtomicPredicator.atomic(meta)(a.type))
297
- unions.push({
298
- type: "atomic",
299
- is: () =>
300
- IsProgrammer.decode(project)(importer)(
301
- input,
302
- (() => {
303
- const partial = Metadata.initialize();
304
- partial.atomics.push(a);
305
- return partial;
306
- })(),
307
- explore,
308
- ),
309
- value: () =>
310
- decode_atomic(project)(importer)(
311
- input,
312
- a.type,
313
- explore,
314
- ),
315
- });
316
-
317
- // TUPLES
318
- for (const tuple of meta.tuples)
319
- unions.push({
320
- type: "tuple",
321
- is: () =>
322
- IsProgrammer.decode(project)(importer)(
323
- input,
324
- (() => {
325
- const partial = Metadata.initialize();
326
- partial.tuples.push(tuple);
327
- return partial;
328
- })(),
329
- explore,
330
- ),
331
- value: () =>
332
- decode_tuple(project)(config)(importer)(
333
- input,
334
- tuple,
335
- explore,
336
- ),
337
- });
338
-
339
- // ARRAYS
340
- if (meta.arrays.length) {
341
- const value: () => ts.Expression =
342
- meta.arrays.length === 1
343
- ? () =>
344
- decode_array(config)(importer)(
345
- input,
346
- meta.arrays[0]!,
347
- {
348
- ...explore,
349
- from: "array",
350
- },
351
- )
352
- : meta.arrays.some((elem) => elem.type.value.any)
353
- ? () =>
354
- ts.factory.createCallExpression(
355
- ts.factory.createIdentifier("JSON.stringify"),
356
- undefined,
357
- [input],
358
- )
359
- : () =>
360
- explore_arrays(project)(config)(importer)(
361
- input,
362
- meta.arrays,
363
- {
364
- ...explore,
365
- from: "array",
366
- },
367
- );
368
-
369
- unions.push({
370
- type: "array",
371
- is: () => ExpressionFactory.isArray(input),
372
- value,
373
- });
374
- }
375
-
376
- // BUILT-IN CLASSES
377
- if (meta.natives.length)
378
- for (const native of meta.natives)
379
- unions.push({
380
- type: "object",
381
- is: () => check_native(native)(input),
382
- value: () =>
383
- AtomicPredicator.native(native)
384
- ? decode_atomic(project)(importer)(
385
- input,
386
- native.toLowerCase() as Atomic.Literal,
387
- explore,
388
- )
389
- : ts.factory.createStringLiteral("{}"),
390
- });
391
-
392
- // SETS
393
- if (meta.sets.length)
394
- unions.push({
395
- type: "object",
396
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
397
- value: () => ts.factory.createStringLiteral("{}"),
398
- });
399
-
400
- // MAPS
401
- if (meta.maps.length)
402
- unions.push({
403
- type: "object",
404
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
405
- value: () => ts.factory.createStringLiteral("{}"),
406
- });
407
-
408
- // OBJECTS
409
- if (meta.objects.length)
410
- unions.push({
411
- type: "object",
412
- is: () =>
413
- ExpressionFactory.isObject({
414
- checkNull: true,
415
- checkArray: meta.objects.some((obj) =>
416
- obj.properties.every(
417
- (prop) =>
418
- !prop.key.isSoleLiteral() ||
419
- !prop.value.isRequired(),
420
- ),
421
- ),
422
- })(input),
423
- value: () =>
424
- meta.isParentResolved() === false &&
425
- meta.objects.length === 1 &&
426
- meta.objects[0]!._Is_simple(
427
- explore.from === "top" ? 0 : 1,
428
- )
429
- ? (() => {
430
- const obj: MetadataObject = meta.objects[0]!;
431
- const entries: IExpressionEntry<ts.Expression>[] =
432
- feature_object_entries({
433
- decoder: () =>
434
- decode(project)(config)(importer),
435
- trace: false,
436
- path: false,
437
- })(importer)(obj)(
438
- ts.factory.createAsExpression(
439
- input,
440
- TypeFactory.keyword("any"),
441
- ),
442
- );
443
- return StringifyJoiner.object(importer)(
444
- ts.factory.createAsExpression(
445
- input,
446
- TypeFactory.keyword("any"),
447
- ),
448
- entries,
449
- );
450
- })()
451
- : explore_objects(config)(importer)(input, meta, {
452
- ...explore,
453
- from: "object",
454
- }),
455
- });
456
-
457
- //----
458
- // RETURNS
459
- //----
460
- // CHECK NULL AND UNDEFINED
461
- const wrapper = (output: ts.Expression) =>
462
- wrap_required(
463
- input,
464
- meta,
465
- explore,
466
- )(wrap_nullable(input, meta)(output));
467
-
468
- // DIRECT RETURN
469
- if (unions.length === 0)
470
- return ts.factory.createCallExpression(
471
- ts.factory.createIdentifier("JSON.stringify"),
472
- undefined,
473
- [input],
474
- );
475
- else if (unions.length === 1) return wrapper(unions[0]!.value());
476
-
477
- // RETURN WITH TYPE CHECKING
478
- return wrapper(
479
- ts.factory.createCallExpression(
480
- ts.factory.createArrowFunction(
481
- undefined,
482
- undefined,
483
- [],
484
- undefined,
485
- undefined,
486
- iterate(importer, input, unions, meta.getName()),
487
- ),
488
- undefined,
489
- undefined,
490
- ),
491
- );
492
- };
493
-
494
- const decode_object = (importer: FunctionImporter) =>
495
- FeatureProgrammer.decode_object({
496
- trace: false,
497
- path: false,
498
- prefix: PREFIX,
499
- })(importer);
500
-
501
- const decode_array =
502
- (config: FeatureProgrammer.IConfig) =>
503
- (importer: FunctionImporter) =>
504
- (
505
- input: ts.Expression,
506
- array: MetadataArray,
507
- explore: FeatureProgrammer.IExplore,
508
- ) =>
509
- array.type.recursive
510
- ? ts.factory.createCallExpression(
511
- ts.factory.createIdentifier(
512
- importer.useLocal(
513
- `${config.prefix}a${array.type.index}`,
514
- ),
515
- ),
516
- undefined,
517
- FeatureProgrammer.argumentsArray(config)({
518
- ...explore,
519
- source: "function",
520
- from: "array",
521
- })(input),
522
- )
523
- : decode_array_inline(config)(importer)(input, array, explore);
524
-
525
- const decode_array_inline =
526
- (config: FeatureProgrammer.IConfig) =>
527
- (importer: FunctionImporter) =>
528
- (
529
- input: ts.Expression,
530
- array: MetadataArray,
531
- explore: FeatureProgrammer.IExplore,
532
- ) =>
533
- FeatureProgrammer.decode_array(config)(importer)(
534
- StringifyJoiner.array,
535
- )(input, array, explore);
536
-
537
- const decode_tuple =
538
- (project: IProject) =>
539
- (config: FeatureProgrammer.IConfig) =>
540
- (importer: FunctionImporter) =>
541
- (
542
- input: ts.Expression,
543
- tuple: MetadataTuple,
544
- explore: FeatureProgrammer.IExplore,
545
- ): ts.Expression =>
546
- tuple.type.recursive
547
- ? ts.factory.createCallExpression(
548
- ts.factory.createIdentifier(
549
- importer.useLocal(
550
- `${config.prefix}t${tuple.type.index}`,
551
- ),
552
- ),
553
- undefined,
554
- FeatureProgrammer.argumentsArray(config)({
555
- ...explore,
556
- source: "function",
557
- })(input),
558
- )
559
- : decode_tuple_inline(project)(config)(importer)(
560
- input,
561
- tuple.type,
562
- explore,
563
- );
564
-
565
- const decode_tuple_inline =
566
- (project: IProject) =>
567
- (config: FeatureProgrammer.IConfig) =>
568
- (importer: FunctionImporter) =>
569
- (
570
- input: ts.Expression,
571
- tuple: MetadataTupleType,
572
- explore: FeatureProgrammer.IExplore,
573
- ): ts.Expression => {
574
- const children: ts.Expression[] = tuple.elements
575
- .filter((elem) => elem.rest === null)
576
- .map((elem, index) =>
577
- decode(project)(config)(importer)(
578
- ts.factory.createElementAccessExpression(input, index),
579
- elem,
580
- {
581
- ...explore,
582
- from: "array",
583
- postfix: explore.postfix.length
584
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
585
- : `"[${index}]"`,
586
- },
587
- ),
588
- );
589
- const rest = (() => {
590
- if (tuple.elements.length === 0) return null;
591
- const last = tuple.elements.at(-1)!;
592
- if (last.rest === null) return null;
593
-
594
- const code = decode(project)(config)(importer)(
595
- ts.factory.createCallExpression(
596
- IdentifierFactory.access(input)("slice"),
597
- undefined,
598
- [ExpressionFactory.number(tuple.elements.length - 1)],
599
- ),
600
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
601
- {
602
- ...explore,
603
- start: tuple.elements.length - 1,
604
- },
605
- );
606
- return ts.factory.createCallExpression(
607
- importer.use("rest"),
608
- undefined,
609
- [code],
610
- );
611
- })();
612
- return StringifyJoiner.tuple(children, rest);
613
- };
614
-
615
- const decode_atomic =
616
- (project: IProject) =>
617
- (importer: FunctionImporter) =>
618
- (
619
- input: ts.Expression,
620
- type: string,
621
- explore: FeatureProgrammer.IExplore,
622
- ) => {
623
- if (type === "string")
624
- return ts.factory.createCallExpression(
625
- importer.use("string"),
626
- undefined,
627
- [input],
628
- );
629
- else if (
630
- type === "number" &&
631
- OptionPredicator.numeric(project.options)
632
- )
633
- input = ts.factory.createCallExpression(
634
- importer.use("number"),
635
- undefined,
636
- [input],
637
- );
638
-
639
- return explore.from !== "top"
640
- ? input
641
- : ts.factory.createCallExpression(
642
- IdentifierFactory.access(input)("toString"),
643
- undefined,
644
- undefined,
645
- );
646
- };
647
-
648
- const decode_constant_string =
649
- (project: IProject) =>
650
- (importer: FunctionImporter) =>
651
- (
652
- input: ts.Expression,
653
- values: string[],
654
- explore: FeatureProgrammer.IExplore,
655
- ): ts.Expression => {
656
- if (values.every((v) => !StringifyPredicator.require_escape(v)))
657
- return [
658
- ts.factory.createStringLiteral('"'),
659
- input,
660
- ts.factory.createStringLiteral('"'),
661
- ].reduce((x, y) => ts.factory.createAdd(x, y));
662
- else
663
- return decode_atomic(project)(importer)(
664
- input,
665
- "string",
666
- explore,
667
- );
668
- };
669
-
670
- const decode_to_json =
671
- (project: IProject) =>
672
- (config: FeatureProgrammer.IConfig) =>
673
- (importer: FunctionImporter) =>
674
- (
675
- input: ts.Expression,
676
- resolved: Metadata,
677
- explore: FeatureProgrammer.IExplore,
678
- ): ts.Expression => {
679
- return decode(project)(config)(importer)(
680
- ts.factory.createCallExpression(
681
- IdentifierFactory.access(input)("toJSON"),
682
- undefined,
683
- [],
684
- ),
685
- resolved,
686
- explore,
687
- );
688
- };
689
-
690
- const decode_functional = (explore: FeatureProgrammer.IExplore) =>
691
- explore.from === "array"
692
- ? ts.factory.createStringLiteral("null")
693
- : ts.factory.createIdentifier("undefined");
694
-
695
- /* -----------------------------------------------------------
696
- EXPLORERS
697
- ----------------------------------------------------------- */
698
- const explore_objects =
699
- (config: FeatureProgrammer.IConfig) =>
700
- (importer: FunctionImporter) =>
701
- (
702
- input: ts.Expression,
703
- meta: Metadata,
704
- explore: FeatureProgrammer.IExplore,
705
- ) =>
706
- meta.objects.length === 1
707
- ? decode_object(importer)(input, meta.objects[0]!, explore)
708
- : ts.factory.createCallExpression(
709
- ts.factory.createIdentifier(
710
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
711
- ),
712
- undefined,
713
- FeatureProgrammer.argumentsArray(config)(explore)(input),
714
- );
715
-
716
- const explore_arrays =
717
- (project: IProject) =>
718
- (config: FeatureProgrammer.IConfig) =>
719
- (importer: FunctionImporter) =>
720
- (
721
- input: ts.Expression,
722
- elements: MetadataArray[],
723
- explore: FeatureProgrammer.IExplore,
724
- ): ts.Expression =>
725
- explore_array_like_union_types(config)(importer)(
726
- UnionExplorer.array({
727
- checker: IsProgrammer.decode(project)(importer),
728
- decoder: decode_array(config)(importer),
729
- empty: ts.factory.createStringLiteral("[]"),
730
- success: ts.factory.createTrue(),
731
- failure: (input, expected) =>
732
- create_throw_error(importer)(expected)(input),
733
- }),
734
- )(input, elements, explore);
735
-
736
- const explore_array_like_union_types =
737
- (config: FeatureProgrammer.IConfig) =>
738
- (importer: FunctionImporter) =>
739
- <T extends MetadataArray | MetadataTuple>(
740
- factory: (
741
- parameters: ts.ParameterDeclaration[],
742
- ) => (
743
- input: ts.Expression,
744
- elements: T[],
745
- explore: FeatureProgrammer.IExplore,
746
- ) => ts.ArrowFunction,
747
- ) =>
748
- (
749
- input: ts.Expression,
750
- elements: T[],
751
- explore: FeatureProgrammer.IExplore,
752
- ): ts.Expression => {
753
- const arrow =
754
- (parameters: ts.ParameterDeclaration[]) =>
755
- (explore: FeatureProgrammer.IExplore) =>
756
- (input: ts.Expression): ts.ArrowFunction =>
757
- factory(parameters)(input, elements, explore);
758
- if (elements.every((e) => e.type.recursive === false))
759
- ts.factory.createCallExpression(
760
- arrow([])(explore)(input),
761
- undefined,
762
- [],
763
- );
764
-
765
- explore = {
766
- ...explore,
767
- source: "function",
768
- from: "array",
769
- };
770
- return ts.factory.createCallExpression(
771
- ts.factory.createIdentifier(
772
- importer.emplaceUnion(
773
- config.prefix,
774
- elements.map((e) => e.type.name).join(" | "),
775
- () =>
776
- arrow(
777
- FeatureProgrammer.parameterDeclarations(config)(
778
- TypeFactory.keyword("any"),
779
- )(ts.factory.createIdentifier("input")),
780
- )({
781
- ...explore,
782
- postfix: "",
783
- })(ts.factory.createIdentifier("input")),
784
- ),
785
- ),
786
- undefined,
787
- FeatureProgrammer.argumentsArray(config)(explore)(input),
788
- );
789
- };
790
-
791
- /* -----------------------------------------------------------
792
- RETURN SCRIPTS
793
- ----------------------------------------------------------- */
794
- const wrap_required = (
795
- input: ts.Expression,
796
- meta: Metadata,
797
- explore: FeatureProgrammer.IExplore,
798
- ): ((expression: ts.Expression) => ts.Expression) => {
799
- if (meta.isRequired() === true && meta.any === false)
800
- return (expression) => expression;
801
- return (expression) =>
802
- ts.factory.createConditionalExpression(
803
- ts.factory.createStrictInequality(
804
- ts.factory.createIdentifier("undefined"),
805
- input,
806
- ),
807
- undefined,
808
- expression,
809
- undefined,
810
- explore.from === "array"
811
- ? ts.factory.createStringLiteral("null")
812
- : ts.factory.createIdentifier("undefined"),
813
- );
814
- };
815
-
816
- const wrap_nullable = (
817
- input: ts.Expression,
818
- meta: Metadata,
819
- ): ((expression: ts.Expression) => ts.Expression) => {
820
- if (meta.nullable === false) return (expression) => expression;
821
- return (expression) =>
822
- ts.factory.createConditionalExpression(
823
- ts.factory.createStrictInequality(
824
- ts.factory.createNull(),
825
- input,
826
- ),
827
- undefined,
828
- expression,
829
- undefined,
830
- ts.factory.createStringLiteral("null"),
831
- );
832
- };
833
-
834
- const wrap_functional = (
835
- input: ts.Expression,
836
- meta: Metadata,
837
- explore: FeatureProgrammer.IExplore,
838
- ): ((expression: ts.Expression) => ts.Expression) => {
839
- if (meta.functional === false) return (expression) => expression;
840
- return (expression) =>
841
- ts.factory.createConditionalExpression(
842
- ts.factory.createStrictInequality(
843
- ts.factory.createStringLiteral("function"),
844
- ValueFactory.TYPEOF(input),
845
- ),
846
- undefined,
847
- expression,
848
- undefined,
849
- decode_functional(explore),
850
- );
851
- };
852
-
853
- const iterate = (
854
- importer: FunctionImporter,
855
- input: ts.Expression,
856
- unions: IUnion[],
857
- expected: string,
858
- ) =>
859
- ts.factory.createBlock(
860
- [
861
- ...unions.map((u) =>
862
- ts.factory.createIfStatement(
863
- u.is(),
864
- ts.factory.createReturnStatement(u.value()),
865
- ),
866
- ),
867
- create_throw_error(importer)(expected)(input),
868
- ],
869
- true,
870
- );
871
-
872
- /* -----------------------------------------------------------
873
- CONFIGURATIONS
874
- ----------------------------------------------------------- */
875
- const PREFIX = "$s";
876
-
877
- const configure =
878
- (project: IProject) =>
879
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
880
- const config: FeatureProgrammer.IConfig = {
881
- types: {
882
- input: (type, name) =>
883
- ts.factory.createTypeReferenceNode(
884
- name ??
885
- TypeFactory.getFullName(project.checker)(type),
886
- ),
887
- output: () => TypeFactory.keyword("string"),
888
- },
889
- prefix: PREFIX,
890
- trace: false,
891
- path: false,
892
- initializer,
893
- decoder: () => decode(project)(config)(importer),
894
- objector: {
895
- checker: () => (input, meta, explore) =>
896
- IsProgrammer.decode(project)(importer)(
897
- input,
898
- meta,
899
- explore,
900
- ),
901
- decoder: () => decode_object(importer),
902
- joiner: StringifyJoiner.object(importer),
903
- unionizer: decode_union_object(
904
- IsProgrammer.decode_object(project)(importer),
905
- )(decode_object(importer))((exp) => exp)(
906
- (value, expected) =>
907
- create_throw_error(importer)(expected)(value),
908
- ),
909
- failure: (input, expected) =>
910
- create_throw_error(importer)(expected)(input),
911
- },
912
- generator: {
913
- arrays: () => write_array_functions(config)(importer),
914
- tuples: () =>
915
- write_tuple_functions(project)(config)(importer),
916
- },
917
- };
918
- return config;
919
- };
920
-
921
- const initializer: FeatureProgrammer.IConfig["initializer"] =
922
- ({ checker }) =>
923
- (importer) =>
924
- (type) =>
925
- JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
926
- checker,
927
- )(type);
928
-
929
- const create_throw_error =
930
- (importer: FunctionImporter) =>
931
- (expected: string) =>
932
- (value: ts.Expression) =>
933
- ts.factory.createExpressionStatement(
934
- ts.factory.createCallExpression(
935
- importer.use("throws"),
936
- [],
937
- [
938
- ts.factory.createObjectLiteralExpression(
939
- [
940
- ts.factory.createPropertyAssignment(
941
- "expected",
942
- ts.factory.createStringLiteral(expected),
943
- ),
944
- ts.factory.createPropertyAssignment(
945
- "value",
946
- value,
947
- ),
948
- ],
949
- true,
950
- ),
951
- ],
952
- ),
953
- );
954
- }
955
-
956
- interface IUnion {
957
- type: string;
958
- is: () => ts.Expression;
959
- value: () => ts.Expression;
960
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { JsonMetadataFactory } from "../../factories/JsonMetadataFactory";
6
+ import { MetadataCollection } from "../../factories/MetadataCollection";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+ import { ValueFactory } from "../../factories/ValueFactory";
10
+
11
+ import { Metadata } from "../../schemas/metadata/Metadata";
12
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
13
+ import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
16
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
17
+
18
+ import { IProject } from "../../transformers/IProject";
19
+
20
+ import { Atomic } from "../../typings/Atomic";
21
+
22
+ import { ArrayUtil } from "../../utils/ArrayUtil";
23
+
24
+ import { FeatureProgrammer } from "../FeatureProgrammer";
25
+ import { IsProgrammer } from "../IsProgrammer";
26
+ import { AtomicPredicator } from "../helpers/AtomicPredicator";
27
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
28
+ import { IExpressionEntry } from "../helpers/IExpressionEntry";
29
+ import { OptionPredicator } from "../helpers/OptionPredicator";
30
+ import { StringifyJoiner } from "../helpers/StringifyJoinder";
31
+ import { StringifyPredicator } from "../helpers/StringifyPredicator";
32
+ import { UnionExplorer } from "../helpers/UnionExplorer";
33
+ import { check_native } from "../internal/check_native";
34
+ import { decode_union_object } from "../internal/decode_union_object";
35
+ import { feature_object_entries } from "../internal/feature_object_entries";
36
+ import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
37
+
38
+ export namespace JsonStringifyProgrammer {
39
+ /* -----------------------------------------------------------
40
+ WRITER
41
+ ----------------------------------------------------------- */
42
+
43
+ export const write =
44
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
+ const importer: FunctionImporter = new FunctionImporter(
46
+ modulo.getText(),
47
+ );
48
+ const config: FeatureProgrammer.IConfig =
49
+ configure(project)(importer);
50
+
51
+ return FeatureProgrammer.write(project)({
52
+ ...config,
53
+ addition: (collection) => [
54
+ ...IsProgrammer.write_function_statements(project)(
55
+ importer,
56
+ )(collection),
57
+ ...importer.declare(modulo),
58
+ ],
59
+ })(importer);
60
+ };
61
+
62
+ const write_array_functions =
63
+ (config: FeatureProgrammer.IConfig) =>
64
+ (importer: FunctionImporter) =>
65
+ (collection: MetadataCollection): ts.VariableStatement[] =>
66
+ collection
67
+ .arrays()
68
+ .filter((a) => a.recursive)
69
+ .map((type, i) =>
70
+ StatementFactory.constant(
71
+ `${config.prefix}a${i}`,
72
+ ts.factory.createArrowFunction(
73
+ undefined,
74
+ undefined,
75
+ FeatureProgrammer.parameterDeclarations(config)(
76
+ TypeFactory.keyword("any"),
77
+ )(ts.factory.createIdentifier("input")),
78
+ TypeFactory.keyword("any"),
79
+ undefined,
80
+ decode_array_inline(config)(importer)(
81
+ ts.factory.createIdentifier("input"),
82
+ MetadataArray.create({
83
+ type,
84
+ tags: [],
85
+ }),
86
+ {
87
+ tracable: config.trace,
88
+ source: "function",
89
+ from: "array",
90
+ postfix: "",
91
+ },
92
+ ),
93
+ ),
94
+ ),
95
+ );
96
+
97
+ const write_tuple_functions =
98
+ (project: IProject) =>
99
+ (config: FeatureProgrammer.IConfig) =>
100
+ (importer: FunctionImporter) =>
101
+ (collection: MetadataCollection): ts.VariableStatement[] =>
102
+ collection
103
+ .tuples()
104
+ .filter((t) => t.recursive)
105
+ .map((tuple, i) =>
106
+ StatementFactory.constant(
107
+ `${config.prefix}t${i}`,
108
+ ts.factory.createArrowFunction(
109
+ undefined,
110
+ undefined,
111
+ FeatureProgrammer.parameterDeclarations(config)(
112
+ TypeFactory.keyword("any"),
113
+ )(ts.factory.createIdentifier("input")),
114
+ TypeFactory.keyword("any"),
115
+ undefined,
116
+ decode_tuple_inline(project)(config)(importer)(
117
+ ts.factory.createIdentifier("input"),
118
+ tuple,
119
+ {
120
+ tracable: config.trace,
121
+ source: "function",
122
+ from: "array",
123
+ postfix: "",
124
+ },
125
+ ),
126
+ ),
127
+ ),
128
+ );
129
+
130
+ /* -----------------------------------------------------------
131
+ DECODERS
132
+ ----------------------------------------------------------- */
133
+ const decode =
134
+ (project: IProject) =>
135
+ (config: FeatureProgrammer.IConfig) =>
136
+ (importer: FunctionImporter) =>
137
+ (
138
+ input: ts.Expression,
139
+ meta: Metadata,
140
+ explore: FeatureProgrammer.IExplore,
141
+ ): ts.Expression => {
142
+ // ANY TYPE
143
+ if (meta.any === true)
144
+ return wrap_required(
145
+ input,
146
+ meta,
147
+ explore,
148
+ )(
149
+ wrap_functional(
150
+ input,
151
+ meta,
152
+ explore,
153
+ )(
154
+ ts.factory.createCallExpression(
155
+ ts.factory.createIdentifier("JSON.stringify"),
156
+ undefined,
157
+ [input],
158
+ ),
159
+ ),
160
+ );
161
+
162
+ // ONLY NULL OR UNDEFINED
163
+ const size: number = meta.size();
164
+ if (
165
+ size === 0 &&
166
+ (meta.isRequired() === false || meta.nullable === true)
167
+ ) {
168
+ if (meta.isRequired() === false && meta.nullable === true)
169
+ return explore.from === "array"
170
+ ? ts.factory.createStringLiteral("null")
171
+ : ts.factory.createConditionalExpression(
172
+ ts.factory.createStrictEquality(
173
+ ts.factory.createNull(),
174
+ input,
175
+ ),
176
+ undefined,
177
+ ts.factory.createStringLiteral("null"),
178
+ undefined,
179
+ ts.factory.createIdentifier("undefined"),
180
+ );
181
+ else if (meta.isRequired() === false)
182
+ return explore.from === "array"
183
+ ? ts.factory.createStringLiteral("null")
184
+ : ts.factory.createIdentifier("undefined");
185
+ else return ts.factory.createStringLiteral("null");
186
+ }
187
+
188
+ //----
189
+ // LIST UP UNION TYPES
190
+ //----
191
+ const unions: IUnion[] = [];
192
+
193
+ // toJSON() METHOD
194
+ if (meta.escaped !== null)
195
+ unions.push({
196
+ type: "resolved",
197
+ is: () => IsProgrammer.decode_to_json(false)(input),
198
+ value: () =>
199
+ decode_to_json(project)(config)(importer)(
200
+ input,
201
+ meta.escaped!.returns,
202
+ explore,
203
+ ),
204
+ });
205
+ else if (meta.functional === true)
206
+ unions.push({
207
+ type: "functional",
208
+ is: () => IsProgrammer.decode_functional(input),
209
+ value: () => decode_functional(explore),
210
+ });
211
+
212
+ // TEMPLATES
213
+ if (
214
+ meta.templates.length ||
215
+ ArrayUtil.has(meta.constants, (c) => c.type === "string")
216
+ )
217
+ if (AtomicPredicator.template(meta)) {
218
+ const partial = Metadata.initialize();
219
+ partial.atomics.push(
220
+ MetadataAtomic.create({ type: "string", tags: [] }),
221
+ ),
222
+ unions.push({
223
+ type: "template literal",
224
+ is: () =>
225
+ IsProgrammer.decode(project)(importer)(
226
+ input,
227
+ partial,
228
+ explore,
229
+ ),
230
+ value: () =>
231
+ decode_atomic(project)(importer)(
232
+ input,
233
+ "string",
234
+ explore,
235
+ ),
236
+ });
237
+ }
238
+
239
+ // CONSTANTS
240
+ for (const constant of meta.constants)
241
+ if (AtomicPredicator.constant(meta)(constant.type) === false)
242
+ continue;
243
+ else if (constant.type !== "string")
244
+ unions.push({
245
+ type: "atomic",
246
+ is: () =>
247
+ IsProgrammer.decode(project)(importer)(
248
+ input,
249
+ (() => {
250
+ const partial = Metadata.initialize();
251
+ partial.atomics.push(
252
+ MetadataAtomic.create({
253
+ type: constant.type,
254
+ tags: [],
255
+ }),
256
+ );
257
+ return partial;
258
+ })(),
259
+ explore,
260
+ ),
261
+ value: () =>
262
+ decode_atomic(project)(importer)(
263
+ input,
264
+ constant.type,
265
+ explore,
266
+ ),
267
+ });
268
+ else if (meta.templates.length === 0)
269
+ unions.push({
270
+ type: "const string",
271
+ is: () =>
272
+ IsProgrammer.decode(project)(importer)(
273
+ input,
274
+ (() => {
275
+ const partial = Metadata.initialize();
276
+ partial.atomics.push(
277
+ MetadataAtomic.create({
278
+ type: "string",
279
+ tags: [],
280
+ }),
281
+ );
282
+ return partial;
283
+ })(),
284
+ explore,
285
+ ),
286
+ value: () =>
287
+ decode_constant_string(project)(importer)(
288
+ input,
289
+ [...constant.values] as string[],
290
+ explore,
291
+ ),
292
+ });
293
+
294
+ /// ATOMICS
295
+ for (const a of meta.atomics)
296
+ if (AtomicPredicator.atomic(meta)(a.type))
297
+ unions.push({
298
+ type: "atomic",
299
+ is: () =>
300
+ IsProgrammer.decode(project)(importer)(
301
+ input,
302
+ (() => {
303
+ const partial = Metadata.initialize();
304
+ partial.atomics.push(a);
305
+ return partial;
306
+ })(),
307
+ explore,
308
+ ),
309
+ value: () =>
310
+ decode_atomic(project)(importer)(
311
+ input,
312
+ a.type,
313
+ explore,
314
+ ),
315
+ });
316
+
317
+ // TUPLES
318
+ for (const tuple of meta.tuples)
319
+ unions.push({
320
+ type: "tuple",
321
+ is: () =>
322
+ IsProgrammer.decode(project)(importer)(
323
+ input,
324
+ (() => {
325
+ const partial = Metadata.initialize();
326
+ partial.tuples.push(tuple);
327
+ return partial;
328
+ })(),
329
+ explore,
330
+ ),
331
+ value: () =>
332
+ decode_tuple(project)(config)(importer)(
333
+ input,
334
+ tuple,
335
+ explore,
336
+ ),
337
+ });
338
+
339
+ // ARRAYS
340
+ if (meta.arrays.length) {
341
+ const value: () => ts.Expression =
342
+ meta.arrays.length === 1
343
+ ? () =>
344
+ decode_array(config)(importer)(
345
+ input,
346
+ meta.arrays[0]!,
347
+ {
348
+ ...explore,
349
+ from: "array",
350
+ },
351
+ )
352
+ : meta.arrays.some((elem) => elem.type.value.any)
353
+ ? () =>
354
+ ts.factory.createCallExpression(
355
+ ts.factory.createIdentifier("JSON.stringify"),
356
+ undefined,
357
+ [input],
358
+ )
359
+ : () =>
360
+ explore_arrays(project)(config)(importer)(
361
+ input,
362
+ meta.arrays,
363
+ {
364
+ ...explore,
365
+ from: "array",
366
+ },
367
+ );
368
+
369
+ unions.push({
370
+ type: "array",
371
+ is: () => ExpressionFactory.isArray(input),
372
+ value,
373
+ });
374
+ }
375
+
376
+ // BUILT-IN CLASSES
377
+ if (meta.natives.length)
378
+ for (const native of meta.natives)
379
+ unions.push({
380
+ type: "object",
381
+ is: () => check_native(native)(input),
382
+ value: () =>
383
+ AtomicPredicator.native(native)
384
+ ? decode_atomic(project)(importer)(
385
+ input,
386
+ native.toLowerCase() as Atomic.Literal,
387
+ explore,
388
+ )
389
+ : ts.factory.createStringLiteral("{}"),
390
+ });
391
+
392
+ // SETS
393
+ if (meta.sets.length)
394
+ unions.push({
395
+ type: "object",
396
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
397
+ value: () => ts.factory.createStringLiteral("{}"),
398
+ });
399
+
400
+ // MAPS
401
+ if (meta.maps.length)
402
+ unions.push({
403
+ type: "object",
404
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
405
+ value: () => ts.factory.createStringLiteral("{}"),
406
+ });
407
+
408
+ // OBJECTS
409
+ if (meta.objects.length)
410
+ unions.push({
411
+ type: "object",
412
+ is: () =>
413
+ ExpressionFactory.isObject({
414
+ checkNull: true,
415
+ checkArray: meta.objects.some((obj) =>
416
+ obj.properties.every(
417
+ (prop) =>
418
+ !prop.key.isSoleLiteral() ||
419
+ !prop.value.isRequired(),
420
+ ),
421
+ ),
422
+ })(input),
423
+ value: () =>
424
+ meta.isParentResolved() === false &&
425
+ meta.objects.length === 1 &&
426
+ meta.objects[0]!._Is_simple(
427
+ explore.from === "top" ? 0 : 1,
428
+ )
429
+ ? (() => {
430
+ const obj: MetadataObject = meta.objects[0]!;
431
+ const entries: IExpressionEntry<ts.Expression>[] =
432
+ feature_object_entries({
433
+ decoder: () =>
434
+ decode(project)(config)(importer),
435
+ trace: false,
436
+ path: false,
437
+ })(importer)(obj)(
438
+ ts.factory.createAsExpression(
439
+ input,
440
+ TypeFactory.keyword("any"),
441
+ ),
442
+ );
443
+ return StringifyJoiner.object(importer)(
444
+ ts.factory.createAsExpression(
445
+ input,
446
+ TypeFactory.keyword("any"),
447
+ ),
448
+ entries,
449
+ );
450
+ })()
451
+ : explore_objects(config)(importer)(input, meta, {
452
+ ...explore,
453
+ from: "object",
454
+ }),
455
+ });
456
+
457
+ //----
458
+ // RETURNS
459
+ //----
460
+ // CHECK NULL AND UNDEFINED
461
+ const wrapper = (output: ts.Expression) =>
462
+ wrap_required(
463
+ input,
464
+ meta,
465
+ explore,
466
+ )(wrap_nullable(input, meta)(output));
467
+
468
+ // DIRECT RETURN
469
+ if (unions.length === 0)
470
+ return ts.factory.createCallExpression(
471
+ ts.factory.createIdentifier("JSON.stringify"),
472
+ undefined,
473
+ [input],
474
+ );
475
+ else if (unions.length === 1) return wrapper(unions[0]!.value());
476
+
477
+ // RETURN WITH TYPE CHECKING
478
+ return wrapper(
479
+ ts.factory.createCallExpression(
480
+ ts.factory.createArrowFunction(
481
+ undefined,
482
+ undefined,
483
+ [],
484
+ undefined,
485
+ undefined,
486
+ iterate(importer, input, unions, meta.getName()),
487
+ ),
488
+ undefined,
489
+ undefined,
490
+ ),
491
+ );
492
+ };
493
+
494
+ const decode_object = (importer: FunctionImporter) =>
495
+ FeatureProgrammer.decode_object({
496
+ trace: false,
497
+ path: false,
498
+ prefix: PREFIX,
499
+ })(importer);
500
+
501
+ const decode_array =
502
+ (config: FeatureProgrammer.IConfig) =>
503
+ (importer: FunctionImporter) =>
504
+ (
505
+ input: ts.Expression,
506
+ array: MetadataArray,
507
+ explore: FeatureProgrammer.IExplore,
508
+ ) =>
509
+ array.type.recursive
510
+ ? ts.factory.createCallExpression(
511
+ ts.factory.createIdentifier(
512
+ importer.useLocal(
513
+ `${config.prefix}a${array.type.index}`,
514
+ ),
515
+ ),
516
+ undefined,
517
+ FeatureProgrammer.argumentsArray(config)({
518
+ ...explore,
519
+ source: "function",
520
+ from: "array",
521
+ })(input),
522
+ )
523
+ : decode_array_inline(config)(importer)(input, array, explore);
524
+
525
+ const decode_array_inline =
526
+ (config: FeatureProgrammer.IConfig) =>
527
+ (importer: FunctionImporter) =>
528
+ (
529
+ input: ts.Expression,
530
+ array: MetadataArray,
531
+ explore: FeatureProgrammer.IExplore,
532
+ ) =>
533
+ FeatureProgrammer.decode_array(config)(importer)(
534
+ StringifyJoiner.array,
535
+ )(input, array, explore);
536
+
537
+ const decode_tuple =
538
+ (project: IProject) =>
539
+ (config: FeatureProgrammer.IConfig) =>
540
+ (importer: FunctionImporter) =>
541
+ (
542
+ input: ts.Expression,
543
+ tuple: MetadataTuple,
544
+ explore: FeatureProgrammer.IExplore,
545
+ ): ts.Expression =>
546
+ tuple.type.recursive
547
+ ? ts.factory.createCallExpression(
548
+ ts.factory.createIdentifier(
549
+ importer.useLocal(
550
+ `${config.prefix}t${tuple.type.index}`,
551
+ ),
552
+ ),
553
+ undefined,
554
+ FeatureProgrammer.argumentsArray(config)({
555
+ ...explore,
556
+ source: "function",
557
+ })(input),
558
+ )
559
+ : decode_tuple_inline(project)(config)(importer)(
560
+ input,
561
+ tuple.type,
562
+ explore,
563
+ );
564
+
565
+ const decode_tuple_inline =
566
+ (project: IProject) =>
567
+ (config: FeatureProgrammer.IConfig) =>
568
+ (importer: FunctionImporter) =>
569
+ (
570
+ input: ts.Expression,
571
+ tuple: MetadataTupleType,
572
+ explore: FeatureProgrammer.IExplore,
573
+ ): ts.Expression => {
574
+ const children: ts.Expression[] = tuple.elements
575
+ .filter((elem) => elem.rest === null)
576
+ .map((elem, index) =>
577
+ decode(project)(config)(importer)(
578
+ ts.factory.createElementAccessExpression(input, index),
579
+ elem,
580
+ {
581
+ ...explore,
582
+ from: "array",
583
+ postfix: explore.postfix.length
584
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
585
+ : `"[${index}]"`,
586
+ },
587
+ ),
588
+ );
589
+ const rest = (() => {
590
+ if (tuple.elements.length === 0) return null;
591
+ const last = tuple.elements.at(-1)!;
592
+ if (last.rest === null) return null;
593
+
594
+ const code = decode(project)(config)(importer)(
595
+ ts.factory.createCallExpression(
596
+ IdentifierFactory.access(input)("slice"),
597
+ undefined,
598
+ [ExpressionFactory.number(tuple.elements.length - 1)],
599
+ ),
600
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
601
+ {
602
+ ...explore,
603
+ start: tuple.elements.length - 1,
604
+ },
605
+ );
606
+ return ts.factory.createCallExpression(
607
+ importer.use("rest"),
608
+ undefined,
609
+ [code],
610
+ );
611
+ })();
612
+ return StringifyJoiner.tuple(children, rest);
613
+ };
614
+
615
+ const decode_atomic =
616
+ (project: IProject) =>
617
+ (importer: FunctionImporter) =>
618
+ (
619
+ input: ts.Expression,
620
+ type: string,
621
+ explore: FeatureProgrammer.IExplore,
622
+ ) => {
623
+ if (type === "string")
624
+ return ts.factory.createCallExpression(
625
+ importer.use("string"),
626
+ undefined,
627
+ [input],
628
+ );
629
+ else if (
630
+ type === "number" &&
631
+ OptionPredicator.numeric(project.options)
632
+ )
633
+ input = ts.factory.createCallExpression(
634
+ importer.use("number"),
635
+ undefined,
636
+ [input],
637
+ );
638
+
639
+ return explore.from !== "top"
640
+ ? input
641
+ : ts.factory.createCallExpression(
642
+ IdentifierFactory.access(input)("toString"),
643
+ undefined,
644
+ undefined,
645
+ );
646
+ };
647
+
648
+ const decode_constant_string =
649
+ (project: IProject) =>
650
+ (importer: FunctionImporter) =>
651
+ (
652
+ input: ts.Expression,
653
+ values: string[],
654
+ explore: FeatureProgrammer.IExplore,
655
+ ): ts.Expression => {
656
+ if (values.every((v) => !StringifyPredicator.require_escape(v)))
657
+ return [
658
+ ts.factory.createStringLiteral('"'),
659
+ input,
660
+ ts.factory.createStringLiteral('"'),
661
+ ].reduce((x, y) => ts.factory.createAdd(x, y));
662
+ else
663
+ return decode_atomic(project)(importer)(
664
+ input,
665
+ "string",
666
+ explore,
667
+ );
668
+ };
669
+
670
+ const decode_to_json =
671
+ (project: IProject) =>
672
+ (config: FeatureProgrammer.IConfig) =>
673
+ (importer: FunctionImporter) =>
674
+ (
675
+ input: ts.Expression,
676
+ resolved: Metadata,
677
+ explore: FeatureProgrammer.IExplore,
678
+ ): ts.Expression => {
679
+ return decode(project)(config)(importer)(
680
+ ts.factory.createCallExpression(
681
+ IdentifierFactory.access(input)("toJSON"),
682
+ undefined,
683
+ [],
684
+ ),
685
+ resolved,
686
+ explore,
687
+ );
688
+ };
689
+
690
+ const decode_functional = (explore: FeatureProgrammer.IExplore) =>
691
+ explore.from === "array"
692
+ ? ts.factory.createStringLiteral("null")
693
+ : ts.factory.createIdentifier("undefined");
694
+
695
+ /* -----------------------------------------------------------
696
+ EXPLORERS
697
+ ----------------------------------------------------------- */
698
+ const explore_objects =
699
+ (config: FeatureProgrammer.IConfig) =>
700
+ (importer: FunctionImporter) =>
701
+ (
702
+ input: ts.Expression,
703
+ meta: Metadata,
704
+ explore: FeatureProgrammer.IExplore,
705
+ ) =>
706
+ meta.objects.length === 1
707
+ ? decode_object(importer)(input, meta.objects[0]!, explore)
708
+ : ts.factory.createCallExpression(
709
+ ts.factory.createIdentifier(
710
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
711
+ ),
712
+ undefined,
713
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
714
+ );
715
+
716
+ const explore_arrays =
717
+ (project: IProject) =>
718
+ (config: FeatureProgrammer.IConfig) =>
719
+ (importer: FunctionImporter) =>
720
+ (
721
+ input: ts.Expression,
722
+ elements: MetadataArray[],
723
+ explore: FeatureProgrammer.IExplore,
724
+ ): ts.Expression =>
725
+ explore_array_like_union_types(config)(importer)(
726
+ UnionExplorer.array({
727
+ checker: IsProgrammer.decode(project)(importer),
728
+ decoder: decode_array(config)(importer),
729
+ empty: ts.factory.createStringLiteral("[]"),
730
+ success: ts.factory.createTrue(),
731
+ failure: (input, expected) =>
732
+ create_throw_error(importer)(expected)(input),
733
+ }),
734
+ )(input, elements, explore);
735
+
736
+ const explore_array_like_union_types =
737
+ (config: FeatureProgrammer.IConfig) =>
738
+ (importer: FunctionImporter) =>
739
+ <T extends MetadataArray | MetadataTuple>(
740
+ factory: (
741
+ parameters: ts.ParameterDeclaration[],
742
+ ) => (
743
+ input: ts.Expression,
744
+ elements: T[],
745
+ explore: FeatureProgrammer.IExplore,
746
+ ) => ts.ArrowFunction,
747
+ ) =>
748
+ (
749
+ input: ts.Expression,
750
+ elements: T[],
751
+ explore: FeatureProgrammer.IExplore,
752
+ ): ts.Expression => {
753
+ const arrow =
754
+ (parameters: ts.ParameterDeclaration[]) =>
755
+ (explore: FeatureProgrammer.IExplore) =>
756
+ (input: ts.Expression): ts.ArrowFunction =>
757
+ factory(parameters)(input, elements, explore);
758
+ if (elements.every((e) => e.type.recursive === false))
759
+ ts.factory.createCallExpression(
760
+ arrow([])(explore)(input),
761
+ undefined,
762
+ [],
763
+ );
764
+
765
+ explore = {
766
+ ...explore,
767
+ source: "function",
768
+ from: "array",
769
+ };
770
+ return ts.factory.createCallExpression(
771
+ ts.factory.createIdentifier(
772
+ importer.emplaceUnion(
773
+ config.prefix,
774
+ elements.map((e) => e.type.name).join(" | "),
775
+ () =>
776
+ arrow(
777
+ FeatureProgrammer.parameterDeclarations(config)(
778
+ TypeFactory.keyword("any"),
779
+ )(ts.factory.createIdentifier("input")),
780
+ )({
781
+ ...explore,
782
+ postfix: "",
783
+ })(ts.factory.createIdentifier("input")),
784
+ ),
785
+ ),
786
+ undefined,
787
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
788
+ );
789
+ };
790
+
791
+ /* -----------------------------------------------------------
792
+ RETURN SCRIPTS
793
+ ----------------------------------------------------------- */
794
+ const wrap_required = (
795
+ input: ts.Expression,
796
+ meta: Metadata,
797
+ explore: FeatureProgrammer.IExplore,
798
+ ): ((expression: ts.Expression) => ts.Expression) => {
799
+ if (meta.isRequired() === true && meta.any === false)
800
+ return (expression) => expression;
801
+ return (expression) =>
802
+ ts.factory.createConditionalExpression(
803
+ ts.factory.createStrictInequality(
804
+ ts.factory.createIdentifier("undefined"),
805
+ input,
806
+ ),
807
+ undefined,
808
+ expression,
809
+ undefined,
810
+ explore.from === "array"
811
+ ? ts.factory.createStringLiteral("null")
812
+ : ts.factory.createIdentifier("undefined"),
813
+ );
814
+ };
815
+
816
+ const wrap_nullable = (
817
+ input: ts.Expression,
818
+ meta: Metadata,
819
+ ): ((expression: ts.Expression) => ts.Expression) => {
820
+ if (meta.nullable === false) return (expression) => expression;
821
+ return (expression) =>
822
+ ts.factory.createConditionalExpression(
823
+ ts.factory.createStrictInequality(
824
+ ts.factory.createNull(),
825
+ input,
826
+ ),
827
+ undefined,
828
+ expression,
829
+ undefined,
830
+ ts.factory.createStringLiteral("null"),
831
+ );
832
+ };
833
+
834
+ const wrap_functional = (
835
+ input: ts.Expression,
836
+ meta: Metadata,
837
+ explore: FeatureProgrammer.IExplore,
838
+ ): ((expression: ts.Expression) => ts.Expression) => {
839
+ if (meta.functional === false) return (expression) => expression;
840
+ return (expression) =>
841
+ ts.factory.createConditionalExpression(
842
+ ts.factory.createStrictInequality(
843
+ ts.factory.createStringLiteral("function"),
844
+ ValueFactory.TYPEOF(input),
845
+ ),
846
+ undefined,
847
+ expression,
848
+ undefined,
849
+ decode_functional(explore),
850
+ );
851
+ };
852
+
853
+ const iterate = (
854
+ importer: FunctionImporter,
855
+ input: ts.Expression,
856
+ unions: IUnion[],
857
+ expected: string,
858
+ ) =>
859
+ ts.factory.createBlock(
860
+ [
861
+ ...unions.map((u) =>
862
+ ts.factory.createIfStatement(
863
+ u.is(),
864
+ ts.factory.createReturnStatement(u.value()),
865
+ ),
866
+ ),
867
+ create_throw_error(importer)(expected)(input),
868
+ ],
869
+ true,
870
+ );
871
+
872
+ /* -----------------------------------------------------------
873
+ CONFIGURATIONS
874
+ ----------------------------------------------------------- */
875
+ const PREFIX = "$s";
876
+
877
+ const configure =
878
+ (project: IProject) =>
879
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
880
+ const config: FeatureProgrammer.IConfig = {
881
+ types: {
882
+ input: (type, name) =>
883
+ ts.factory.createTypeReferenceNode(
884
+ name ??
885
+ TypeFactory.getFullName(project.checker)(type),
886
+ ),
887
+ output: () => TypeFactory.keyword("string"),
888
+ },
889
+ prefix: PREFIX,
890
+ trace: false,
891
+ path: false,
892
+ initializer,
893
+ decoder: () => decode(project)(config)(importer),
894
+ objector: {
895
+ checker: () => (input, meta, explore) =>
896
+ IsProgrammer.decode(project)(importer)(
897
+ input,
898
+ meta,
899
+ explore,
900
+ ),
901
+ decoder: () => decode_object(importer),
902
+ joiner: StringifyJoiner.object(importer),
903
+ unionizer: decode_union_object(
904
+ IsProgrammer.decode_object(project)(importer),
905
+ )(decode_object(importer))((exp) => exp)(
906
+ (value, expected) =>
907
+ create_throw_error(importer)(expected)(value),
908
+ ),
909
+ failure: (input, expected) =>
910
+ create_throw_error(importer)(expected)(input),
911
+ },
912
+ generator: {
913
+ arrays: () => write_array_functions(config)(importer),
914
+ tuples: () =>
915
+ write_tuple_functions(project)(config)(importer),
916
+ },
917
+ };
918
+ return config;
919
+ };
920
+
921
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
922
+ (project) => (importer) => (type) =>
923
+ JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
924
+ project.checker,
925
+ project.context,
926
+ )(type);
927
+
928
+ const create_throw_error =
929
+ (importer: FunctionImporter) =>
930
+ (expected: string) =>
931
+ (value: ts.Expression) =>
932
+ ts.factory.createExpressionStatement(
933
+ ts.factory.createCallExpression(
934
+ importer.use("throws"),
935
+ [],
936
+ [
937
+ ts.factory.createObjectLiteralExpression(
938
+ [
939
+ ts.factory.createPropertyAssignment(
940
+ "expected",
941
+ ts.factory.createStringLiteral(expected),
942
+ ),
943
+ ts.factory.createPropertyAssignment(
944
+ "value",
945
+ value,
946
+ ),
947
+ ],
948
+ true,
949
+ ),
950
+ ],
951
+ ),
952
+ );
953
+ }
954
+
955
+ interface IUnion {
956
+ type: string;
957
+ is: () => ts.Expression;
958
+ value: () => ts.Expression;
959
+ }