typia 5.2.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,964 +1,964 @@
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
- [
599
- ts.factory.createNumericLiteral(
600
- tuple.elements.length - 1,
601
- ),
602
- ],
603
- ),
604
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
605
- {
606
- ...explore,
607
- start: tuple.elements.length - 1,
608
- },
609
- );
610
- return ts.factory.createCallExpression(
611
- importer.use("rest"),
612
- undefined,
613
- [code],
614
- );
615
- })();
616
- return StringifyJoiner.tuple(children, rest);
617
- };
618
-
619
- const decode_atomic =
620
- (project: IProject) =>
621
- (importer: FunctionImporter) =>
622
- (
623
- input: ts.Expression,
624
- type: string,
625
- explore: FeatureProgrammer.IExplore,
626
- ) => {
627
- if (type === "string")
628
- return ts.factory.createCallExpression(
629
- importer.use("string"),
630
- undefined,
631
- [input],
632
- );
633
- else if (
634
- type === "number" &&
635
- OptionPredicator.numeric(project.options)
636
- )
637
- input = ts.factory.createCallExpression(
638
- importer.use("number"),
639
- undefined,
640
- [input],
641
- );
642
-
643
- return explore.from !== "top"
644
- ? input
645
- : ts.factory.createCallExpression(
646
- IdentifierFactory.access(input)("toString"),
647
- undefined,
648
- undefined,
649
- );
650
- };
651
-
652
- const decode_constant_string =
653
- (project: IProject) =>
654
- (importer: FunctionImporter) =>
655
- (
656
- input: ts.Expression,
657
- values: string[],
658
- explore: FeatureProgrammer.IExplore,
659
- ): ts.Expression => {
660
- if (values.every((v) => !StringifyPredicator.require_escape(v)))
661
- return [
662
- ts.factory.createStringLiteral('"'),
663
- input,
664
- ts.factory.createStringLiteral('"'),
665
- ].reduce((x, y) => ts.factory.createAdd(x, y));
666
- else
667
- return decode_atomic(project)(importer)(
668
- input,
669
- "string",
670
- explore,
671
- );
672
- };
673
-
674
- const decode_to_json =
675
- (project: IProject) =>
676
- (config: FeatureProgrammer.IConfig) =>
677
- (importer: FunctionImporter) =>
678
- (
679
- input: ts.Expression,
680
- resolved: Metadata,
681
- explore: FeatureProgrammer.IExplore,
682
- ): ts.Expression => {
683
- return decode(project)(config)(importer)(
684
- ts.factory.createCallExpression(
685
- IdentifierFactory.access(input)("toJSON"),
686
- undefined,
687
- [],
688
- ),
689
- resolved,
690
- explore,
691
- );
692
- };
693
-
694
- const decode_functional = (explore: FeatureProgrammer.IExplore) =>
695
- explore.from === "array"
696
- ? ts.factory.createStringLiteral("null")
697
- : ts.factory.createIdentifier("undefined");
698
-
699
- /* -----------------------------------------------------------
700
- EXPLORERS
701
- ----------------------------------------------------------- */
702
- const explore_objects =
703
- (config: FeatureProgrammer.IConfig) =>
704
- (importer: FunctionImporter) =>
705
- (
706
- input: ts.Expression,
707
- meta: Metadata,
708
- explore: FeatureProgrammer.IExplore,
709
- ) =>
710
- meta.objects.length === 1
711
- ? decode_object(importer)(input, meta.objects[0]!, explore)
712
- : ts.factory.createCallExpression(
713
- ts.factory.createIdentifier(
714
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
715
- ),
716
- undefined,
717
- FeatureProgrammer.argumentsArray(config)(explore)(input),
718
- );
719
-
720
- const explore_arrays =
721
- (project: IProject) =>
722
- (config: FeatureProgrammer.IConfig) =>
723
- (importer: FunctionImporter) =>
724
- (
725
- input: ts.Expression,
726
- elements: MetadataArray[],
727
- explore: FeatureProgrammer.IExplore,
728
- ): ts.Expression =>
729
- explore_array_like_union_types(config)(importer)(
730
- UnionExplorer.array({
731
- checker: IsProgrammer.decode(project)(importer),
732
- decoder: decode_array(config)(importer),
733
- empty: ts.factory.createStringLiteral("[]"),
734
- success: ts.factory.createTrue(),
735
- failure: (input, expected) =>
736
- create_throw_error(importer)(expected)(input),
737
- }),
738
- )(input, elements, explore);
739
-
740
- const explore_array_like_union_types =
741
- (config: FeatureProgrammer.IConfig) =>
742
- (importer: FunctionImporter) =>
743
- <T extends MetadataArray | MetadataTuple>(
744
- factory: (
745
- parameters: ts.ParameterDeclaration[],
746
- ) => (
747
- input: ts.Expression,
748
- elements: T[],
749
- explore: FeatureProgrammer.IExplore,
750
- ) => ts.ArrowFunction,
751
- ) =>
752
- (
753
- input: ts.Expression,
754
- elements: T[],
755
- explore: FeatureProgrammer.IExplore,
756
- ): ts.Expression => {
757
- const arrow =
758
- (parameters: ts.ParameterDeclaration[]) =>
759
- (explore: FeatureProgrammer.IExplore) =>
760
- (input: ts.Expression): ts.ArrowFunction =>
761
- factory(parameters)(input, elements, explore);
762
- if (elements.every((e) => e.type.recursive === false))
763
- ts.factory.createCallExpression(
764
- arrow([])(explore)(input),
765
- undefined,
766
- [],
767
- );
768
-
769
- explore = {
770
- ...explore,
771
- source: "function",
772
- from: "array",
773
- };
774
- return ts.factory.createCallExpression(
775
- ts.factory.createIdentifier(
776
- importer.emplaceUnion(
777
- config.prefix,
778
- elements.map((e) => e.type.name).join(" | "),
779
- () =>
780
- arrow(
781
- FeatureProgrammer.parameterDeclarations(config)(
782
- TypeFactory.keyword("any"),
783
- )(ts.factory.createIdentifier("input")),
784
- )({
785
- ...explore,
786
- postfix: "",
787
- })(ts.factory.createIdentifier("input")),
788
- ),
789
- ),
790
- undefined,
791
- FeatureProgrammer.argumentsArray(config)(explore)(input),
792
- );
793
- };
794
-
795
- /* -----------------------------------------------------------
796
- RETURN SCRIPTS
797
- ----------------------------------------------------------- */
798
- const wrap_required = (
799
- input: ts.Expression,
800
- meta: Metadata,
801
- explore: FeatureProgrammer.IExplore,
802
- ): ((expression: ts.Expression) => ts.Expression) => {
803
- if (meta.isRequired() === true && meta.any === false)
804
- return (expression) => expression;
805
- return (expression) =>
806
- ts.factory.createConditionalExpression(
807
- ts.factory.createStrictInequality(
808
- ts.factory.createIdentifier("undefined"),
809
- input,
810
- ),
811
- undefined,
812
- expression,
813
- undefined,
814
- explore.from === "array"
815
- ? ts.factory.createStringLiteral("null")
816
- : ts.factory.createIdentifier("undefined"),
817
- );
818
- };
819
-
820
- const wrap_nullable = (
821
- input: ts.Expression,
822
- meta: Metadata,
823
- ): ((expression: ts.Expression) => ts.Expression) => {
824
- if (meta.nullable === false) return (expression) => expression;
825
- return (expression) =>
826
- ts.factory.createConditionalExpression(
827
- ts.factory.createStrictInequality(
828
- ts.factory.createNull(),
829
- input,
830
- ),
831
- undefined,
832
- expression,
833
- undefined,
834
- ts.factory.createStringLiteral("null"),
835
- );
836
- };
837
-
838
- const wrap_functional = (
839
- input: ts.Expression,
840
- meta: Metadata,
841
- explore: FeatureProgrammer.IExplore,
842
- ): ((expression: ts.Expression) => ts.Expression) => {
843
- if (meta.functional === false) return (expression) => expression;
844
- return (expression) =>
845
- ts.factory.createConditionalExpression(
846
- ts.factory.createStrictInequality(
847
- ts.factory.createStringLiteral("function"),
848
- ValueFactory.TYPEOF(input),
849
- ),
850
- undefined,
851
- expression,
852
- undefined,
853
- decode_functional(explore),
854
- );
855
- };
856
-
857
- const iterate = (
858
- importer: FunctionImporter,
859
- input: ts.Expression,
860
- unions: IUnion[],
861
- expected: string,
862
- ) =>
863
- ts.factory.createBlock(
864
- [
865
- ...unions.map((u) =>
866
- ts.factory.createIfStatement(
867
- u.is(),
868
- ts.factory.createReturnStatement(u.value()),
869
- ),
870
- ),
871
- create_throw_error(importer)(expected)(input),
872
- ],
873
- true,
874
- );
875
-
876
- /* -----------------------------------------------------------
877
- CONFIGURATIONS
878
- ----------------------------------------------------------- */
879
- const PREFIX = "$s";
880
-
881
- const configure =
882
- (project: IProject) =>
883
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
884
- const config: FeatureProgrammer.IConfig = {
885
- types: {
886
- input: (type, name) =>
887
- ts.factory.createTypeReferenceNode(
888
- name ??
889
- TypeFactory.getFullName(project.checker)(type),
890
- ),
891
- output: () => TypeFactory.keyword("string"),
892
- },
893
- prefix: PREFIX,
894
- trace: false,
895
- path: false,
896
- initializer,
897
- decoder: () => decode(project)(config)(importer),
898
- objector: {
899
- checker: () => (input, meta, explore) =>
900
- IsProgrammer.decode(project)(importer)(
901
- input,
902
- meta,
903
- explore,
904
- ),
905
- decoder: () => decode_object(importer),
906
- joiner: StringifyJoiner.object(importer),
907
- unionizer: decode_union_object(
908
- IsProgrammer.decode_object(importer),
909
- )(decode_object(importer))((exp) => exp)(
910
- (value, expected) =>
911
- create_throw_error(importer)(expected)(value),
912
- ),
913
- failure: (input, expected) =>
914
- create_throw_error(importer)(expected)(input),
915
- },
916
- generator: {
917
- arrays: () => write_array_functions(config)(importer),
918
- tuples: () =>
919
- write_tuple_functions(project)(config)(importer),
920
- },
921
- };
922
- return config;
923
- };
924
-
925
- const initializer: FeatureProgrammer.IConfig["initializer"] =
926
- ({ checker }) =>
927
- (importer) =>
928
- (type) =>
929
- JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
930
- checker,
931
- )(type);
932
-
933
- const create_throw_error =
934
- (importer: FunctionImporter) =>
935
- (expected: string) =>
936
- (value: ts.Expression) =>
937
- ts.factory.createExpressionStatement(
938
- ts.factory.createCallExpression(
939
- importer.use("throws"),
940
- [],
941
- [
942
- ts.factory.createObjectLiteralExpression(
943
- [
944
- ts.factory.createPropertyAssignment(
945
- "expected",
946
- ts.factory.createStringLiteral(expected),
947
- ),
948
- ts.factory.createPropertyAssignment(
949
- "value",
950
- value,
951
- ),
952
- ],
953
- true,
954
- ),
955
- ],
956
- ),
957
- );
958
- }
959
-
960
- interface IUnion {
961
- type: string;
962
- is: () => ts.Expression;
963
- value: () => ts.Expression;
964
- }
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
+ [
599
+ ts.factory.createNumericLiteral(
600
+ tuple.elements.length - 1,
601
+ ),
602
+ ],
603
+ ),
604
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
605
+ {
606
+ ...explore,
607
+ start: tuple.elements.length - 1,
608
+ },
609
+ );
610
+ return ts.factory.createCallExpression(
611
+ importer.use("rest"),
612
+ undefined,
613
+ [code],
614
+ );
615
+ })();
616
+ return StringifyJoiner.tuple(children, rest);
617
+ };
618
+
619
+ const decode_atomic =
620
+ (project: IProject) =>
621
+ (importer: FunctionImporter) =>
622
+ (
623
+ input: ts.Expression,
624
+ type: string,
625
+ explore: FeatureProgrammer.IExplore,
626
+ ) => {
627
+ if (type === "string")
628
+ return ts.factory.createCallExpression(
629
+ importer.use("string"),
630
+ undefined,
631
+ [input],
632
+ );
633
+ else if (
634
+ type === "number" &&
635
+ OptionPredicator.numeric(project.options)
636
+ )
637
+ input = ts.factory.createCallExpression(
638
+ importer.use("number"),
639
+ undefined,
640
+ [input],
641
+ );
642
+
643
+ return explore.from !== "top"
644
+ ? input
645
+ : ts.factory.createCallExpression(
646
+ IdentifierFactory.access(input)("toString"),
647
+ undefined,
648
+ undefined,
649
+ );
650
+ };
651
+
652
+ const decode_constant_string =
653
+ (project: IProject) =>
654
+ (importer: FunctionImporter) =>
655
+ (
656
+ input: ts.Expression,
657
+ values: string[],
658
+ explore: FeatureProgrammer.IExplore,
659
+ ): ts.Expression => {
660
+ if (values.every((v) => !StringifyPredicator.require_escape(v)))
661
+ return [
662
+ ts.factory.createStringLiteral('"'),
663
+ input,
664
+ ts.factory.createStringLiteral('"'),
665
+ ].reduce((x, y) => ts.factory.createAdd(x, y));
666
+ else
667
+ return decode_atomic(project)(importer)(
668
+ input,
669
+ "string",
670
+ explore,
671
+ );
672
+ };
673
+
674
+ const decode_to_json =
675
+ (project: IProject) =>
676
+ (config: FeatureProgrammer.IConfig) =>
677
+ (importer: FunctionImporter) =>
678
+ (
679
+ input: ts.Expression,
680
+ resolved: Metadata,
681
+ explore: FeatureProgrammer.IExplore,
682
+ ): ts.Expression => {
683
+ return decode(project)(config)(importer)(
684
+ ts.factory.createCallExpression(
685
+ IdentifierFactory.access(input)("toJSON"),
686
+ undefined,
687
+ [],
688
+ ),
689
+ resolved,
690
+ explore,
691
+ );
692
+ };
693
+
694
+ const decode_functional = (explore: FeatureProgrammer.IExplore) =>
695
+ explore.from === "array"
696
+ ? ts.factory.createStringLiteral("null")
697
+ : ts.factory.createIdentifier("undefined");
698
+
699
+ /* -----------------------------------------------------------
700
+ EXPLORERS
701
+ ----------------------------------------------------------- */
702
+ const explore_objects =
703
+ (config: FeatureProgrammer.IConfig) =>
704
+ (importer: FunctionImporter) =>
705
+ (
706
+ input: ts.Expression,
707
+ meta: Metadata,
708
+ explore: FeatureProgrammer.IExplore,
709
+ ) =>
710
+ meta.objects.length === 1
711
+ ? decode_object(importer)(input, meta.objects[0]!, explore)
712
+ : ts.factory.createCallExpression(
713
+ ts.factory.createIdentifier(
714
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
715
+ ),
716
+ undefined,
717
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
718
+ );
719
+
720
+ const explore_arrays =
721
+ (project: IProject) =>
722
+ (config: FeatureProgrammer.IConfig) =>
723
+ (importer: FunctionImporter) =>
724
+ (
725
+ input: ts.Expression,
726
+ elements: MetadataArray[],
727
+ explore: FeatureProgrammer.IExplore,
728
+ ): ts.Expression =>
729
+ explore_array_like_union_types(config)(importer)(
730
+ UnionExplorer.array({
731
+ checker: IsProgrammer.decode(project)(importer),
732
+ decoder: decode_array(config)(importer),
733
+ empty: ts.factory.createStringLiteral("[]"),
734
+ success: ts.factory.createTrue(),
735
+ failure: (input, expected) =>
736
+ create_throw_error(importer)(expected)(input),
737
+ }),
738
+ )(input, elements, explore);
739
+
740
+ const explore_array_like_union_types =
741
+ (config: FeatureProgrammer.IConfig) =>
742
+ (importer: FunctionImporter) =>
743
+ <T extends MetadataArray | MetadataTuple>(
744
+ factory: (
745
+ parameters: ts.ParameterDeclaration[],
746
+ ) => (
747
+ input: ts.Expression,
748
+ elements: T[],
749
+ explore: FeatureProgrammer.IExplore,
750
+ ) => ts.ArrowFunction,
751
+ ) =>
752
+ (
753
+ input: ts.Expression,
754
+ elements: T[],
755
+ explore: FeatureProgrammer.IExplore,
756
+ ): ts.Expression => {
757
+ const arrow =
758
+ (parameters: ts.ParameterDeclaration[]) =>
759
+ (explore: FeatureProgrammer.IExplore) =>
760
+ (input: ts.Expression): ts.ArrowFunction =>
761
+ factory(parameters)(input, elements, explore);
762
+ if (elements.every((e) => e.type.recursive === false))
763
+ ts.factory.createCallExpression(
764
+ arrow([])(explore)(input),
765
+ undefined,
766
+ [],
767
+ );
768
+
769
+ explore = {
770
+ ...explore,
771
+ source: "function",
772
+ from: "array",
773
+ };
774
+ return ts.factory.createCallExpression(
775
+ ts.factory.createIdentifier(
776
+ importer.emplaceUnion(
777
+ config.prefix,
778
+ elements.map((e) => e.type.name).join(" | "),
779
+ () =>
780
+ arrow(
781
+ FeatureProgrammer.parameterDeclarations(config)(
782
+ TypeFactory.keyword("any"),
783
+ )(ts.factory.createIdentifier("input")),
784
+ )({
785
+ ...explore,
786
+ postfix: "",
787
+ })(ts.factory.createIdentifier("input")),
788
+ ),
789
+ ),
790
+ undefined,
791
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
792
+ );
793
+ };
794
+
795
+ /* -----------------------------------------------------------
796
+ RETURN SCRIPTS
797
+ ----------------------------------------------------------- */
798
+ const wrap_required = (
799
+ input: ts.Expression,
800
+ meta: Metadata,
801
+ explore: FeatureProgrammer.IExplore,
802
+ ): ((expression: ts.Expression) => ts.Expression) => {
803
+ if (meta.isRequired() === true && meta.any === false)
804
+ return (expression) => expression;
805
+ return (expression) =>
806
+ ts.factory.createConditionalExpression(
807
+ ts.factory.createStrictInequality(
808
+ ts.factory.createIdentifier("undefined"),
809
+ input,
810
+ ),
811
+ undefined,
812
+ expression,
813
+ undefined,
814
+ explore.from === "array"
815
+ ? ts.factory.createStringLiteral("null")
816
+ : ts.factory.createIdentifier("undefined"),
817
+ );
818
+ };
819
+
820
+ const wrap_nullable = (
821
+ input: ts.Expression,
822
+ meta: Metadata,
823
+ ): ((expression: ts.Expression) => ts.Expression) => {
824
+ if (meta.nullable === false) return (expression) => expression;
825
+ return (expression) =>
826
+ ts.factory.createConditionalExpression(
827
+ ts.factory.createStrictInequality(
828
+ ts.factory.createNull(),
829
+ input,
830
+ ),
831
+ undefined,
832
+ expression,
833
+ undefined,
834
+ ts.factory.createStringLiteral("null"),
835
+ );
836
+ };
837
+
838
+ const wrap_functional = (
839
+ input: ts.Expression,
840
+ meta: Metadata,
841
+ explore: FeatureProgrammer.IExplore,
842
+ ): ((expression: ts.Expression) => ts.Expression) => {
843
+ if (meta.functional === false) return (expression) => expression;
844
+ return (expression) =>
845
+ ts.factory.createConditionalExpression(
846
+ ts.factory.createStrictInequality(
847
+ ts.factory.createStringLiteral("function"),
848
+ ValueFactory.TYPEOF(input),
849
+ ),
850
+ undefined,
851
+ expression,
852
+ undefined,
853
+ decode_functional(explore),
854
+ );
855
+ };
856
+
857
+ const iterate = (
858
+ importer: FunctionImporter,
859
+ input: ts.Expression,
860
+ unions: IUnion[],
861
+ expected: string,
862
+ ) =>
863
+ ts.factory.createBlock(
864
+ [
865
+ ...unions.map((u) =>
866
+ ts.factory.createIfStatement(
867
+ u.is(),
868
+ ts.factory.createReturnStatement(u.value()),
869
+ ),
870
+ ),
871
+ create_throw_error(importer)(expected)(input),
872
+ ],
873
+ true,
874
+ );
875
+
876
+ /* -----------------------------------------------------------
877
+ CONFIGURATIONS
878
+ ----------------------------------------------------------- */
879
+ const PREFIX = "$s";
880
+
881
+ const configure =
882
+ (project: IProject) =>
883
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
884
+ const config: FeatureProgrammer.IConfig = {
885
+ types: {
886
+ input: (type, name) =>
887
+ ts.factory.createTypeReferenceNode(
888
+ name ??
889
+ TypeFactory.getFullName(project.checker)(type),
890
+ ),
891
+ output: () => TypeFactory.keyword("string"),
892
+ },
893
+ prefix: PREFIX,
894
+ trace: false,
895
+ path: false,
896
+ initializer,
897
+ decoder: () => decode(project)(config)(importer),
898
+ objector: {
899
+ checker: () => (input, meta, explore) =>
900
+ IsProgrammer.decode(project)(importer)(
901
+ input,
902
+ meta,
903
+ explore,
904
+ ),
905
+ decoder: () => decode_object(importer),
906
+ joiner: StringifyJoiner.object(importer),
907
+ unionizer: decode_union_object(
908
+ IsProgrammer.decode_object(importer),
909
+ )(decode_object(importer))((exp) => exp)(
910
+ (value, expected) =>
911
+ create_throw_error(importer)(expected)(value),
912
+ ),
913
+ failure: (input, expected) =>
914
+ create_throw_error(importer)(expected)(input),
915
+ },
916
+ generator: {
917
+ arrays: () => write_array_functions(config)(importer),
918
+ tuples: () =>
919
+ write_tuple_functions(project)(config)(importer),
920
+ },
921
+ };
922
+ return config;
923
+ };
924
+
925
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
926
+ ({ checker }) =>
927
+ (importer) =>
928
+ (type) =>
929
+ JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
930
+ checker,
931
+ )(type);
932
+
933
+ const create_throw_error =
934
+ (importer: FunctionImporter) =>
935
+ (expected: string) =>
936
+ (value: ts.Expression) =>
937
+ ts.factory.createExpressionStatement(
938
+ ts.factory.createCallExpression(
939
+ importer.use("throws"),
940
+ [],
941
+ [
942
+ ts.factory.createObjectLiteralExpression(
943
+ [
944
+ ts.factory.createPropertyAssignment(
945
+ "expected",
946
+ ts.factory.createStringLiteral(expected),
947
+ ),
948
+ ts.factory.createPropertyAssignment(
949
+ "value",
950
+ value,
951
+ ),
952
+ ],
953
+ true,
954
+ ),
955
+ ],
956
+ ),
957
+ );
958
+ }
959
+
960
+ interface IUnion {
961
+ type: string;
962
+ is: () => ts.Expression;
963
+ value: () => ts.Expression;
964
+ }