typia 5.0.0-dev.20230823-2 → 5.0.0-dev.2023084

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