typia 5.0.0-dev.20230823 → 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 (217) hide show
  1. package/lib/Primitive.d.ts +14 -12
  2. package/lib/Resolved.d.ts +46 -0
  3. package/lib/{schemas/metadata/IMetadataResolved.js → Resolved.js} +1 -1
  4. package/lib/Resolved.js.map +1 -0
  5. package/lib/factories/MetadataFactory.d.ts +1 -1
  6. package/lib/factories/MetadataTagFactory.js +10 -8
  7. package/lib/factories/MetadataTagFactory.js.map +1 -1
  8. package/lib/factories/ProtobufFactory.js +4 -3
  9. package/lib/factories/ProtobufFactory.js.map +1 -1
  10. package/lib/factories/internal/metadata/emend_metadata_atomics.js +6 -6
  11. package/lib/factories/internal/metadata/emend_metadata_atomics.js.map +1 -1
  12. package/lib/factories/internal/metadata/explore_metadata.js +3 -3
  13. package/lib/factories/internal/metadata/explore_metadata.js.map +1 -1
  14. package/lib/factories/internal/metadata/iterate_metadata_atomic.js +1 -1
  15. package/lib/factories/internal/metadata/iterate_metadata_atomic.js.map +1 -1
  16. package/lib/factories/internal/metadata/iterate_metadata_collection.js +6 -6
  17. package/lib/factories/internal/metadata/iterate_metadata_collection.js.map +1 -1
  18. package/lib/factories/internal/metadata/iterate_metadata_native.js +2 -10
  19. package/lib/factories/internal/metadata/iterate_metadata_native.js.map +1 -1
  20. package/lib/factories/internal/metadata/iterate_metadata_resolve.js +6 -6
  21. package/lib/factories/internal/metadata/iterate_metadata_resolve.js.map +1 -1
  22. package/lib/factories/internal/metadata/iterate_metadata_sort.js +2 -2
  23. package/lib/factories/internal/metadata/iterate_metadata_sort.js.map +1 -1
  24. package/lib/functional/$ProtobufWriter.js.map +1 -1
  25. package/lib/json.d.ts +15 -15
  26. package/lib/misc.d.ts +42 -42
  27. package/lib/module.d.ts +18 -17
  28. package/lib/module.js +1 -0
  29. package/lib/module.js.map +1 -1
  30. package/lib/programmers/AssertProgrammer.js +1 -1
  31. package/lib/programmers/AssertProgrammer.js.map +1 -1
  32. package/lib/programmers/CheckerProgrammer.js +7 -7
  33. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  34. package/lib/programmers/IsProgrammer.js +2 -2
  35. package/lib/programmers/IsProgrammer.js.map +1 -1
  36. package/lib/programmers/RandomProgrammer.js +311 -136
  37. package/lib/programmers/RandomProgrammer.js.map +1 -1
  38. package/lib/programmers/ValidateProgrammer.js +1 -1
  39. package/lib/programmers/ValidateProgrammer.js.map +1 -1
  40. package/lib/programmers/helpers/AtomicPredicator.js +2 -2
  41. package/lib/programmers/helpers/AtomicPredicator.js.map +1 -1
  42. package/lib/programmers/helpers/FunctionImporeter.d.ts +2 -0
  43. package/lib/programmers/helpers/FunctionImporeter.js +2 -1
  44. package/lib/programmers/helpers/FunctionImporeter.js.map +1 -1
  45. package/lib/programmers/helpers/ProtobufUtil.js +2 -2
  46. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
  47. package/lib/programmers/helpers/StringifyPredicator.js +1 -2
  48. package/lib/programmers/helpers/StringifyPredicator.js.map +1 -1
  49. package/lib/programmers/helpers/disable_function_importer_declare.js +1 -0
  50. package/lib/programmers/helpers/disable_function_importer_declare.js.map +1 -1
  51. package/lib/programmers/internal/application_default_string.js +2 -2
  52. package/lib/programmers/internal/application_default_string.js.map +1 -1
  53. package/lib/programmers/internal/application_resolved.d.ts +2 -2
  54. package/lib/programmers/internal/application_resolved.js.map +1 -1
  55. package/lib/programmers/internal/application_schema.js +7 -7
  56. package/lib/programmers/internal/application_schema.js.map +1 -1
  57. package/lib/programmers/internal/metadata_to_pattern.js +4 -4
  58. package/lib/programmers/internal/metadata_to_pattern.js.map +1 -1
  59. package/lib/programmers/internal/stringify_dynamic_properties.js +2 -1
  60. package/lib/programmers/internal/stringify_dynamic_properties.js.map +1 -1
  61. package/lib/programmers/json/JsonStringifyProgrammer.js +20 -14
  62. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  63. package/lib/programmers/misc/MiscAssertCloneProgrammer.js +1 -1
  64. package/lib/programmers/misc/MiscAssertCloneProgrammer.js.map +1 -1
  65. package/lib/programmers/misc/MiscCloneProgrammer.js +114 -33
  66. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  67. package/lib/programmers/misc/MiscIsCloneProgrammer.js +1 -1
  68. package/lib/programmers/misc/MiscIsCloneProgrammer.js.map +1 -1
  69. package/lib/programmers/misc/MiscLiteralsProgrammer.js +3 -3
  70. package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
  71. package/lib/programmers/misc/MiscPruneProgrammer.js +2 -2
  72. package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
  73. package/lib/programmers/misc/MiscValidateCloneProgrammer.js +1 -1
  74. package/lib/programmers/misc/MiscValidateCloneProgrammer.js.map +1 -1
  75. package/lib/programmers/protobuf/ProtobufAssertDecodeProgrammer.js +3 -3
  76. package/lib/programmers/protobuf/ProtobufAssertDecodeProgrammer.js.map +1 -1
  77. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +3 -3
  78. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  79. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +1 -1
  80. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  81. package/lib/programmers/protobuf/ProtobufIsDecodeProgrammer.js +2 -2
  82. package/lib/programmers/protobuf/ProtobufIsDecodeProgrammer.js.map +1 -1
  83. package/lib/programmers/protobuf/ProtobufValidateDecodeProgrammer.js +1 -1
  84. package/lib/programmers/protobuf/ProtobufValidateDecodeProgrammer.js.map +1 -1
  85. package/lib/protobuf.d.ts +631 -10
  86. package/lib/protobuf.js +13 -13
  87. package/lib/protobuf.js.map +1 -1
  88. package/lib/schemas/metadata/IMetadata.d.ts +4 -4
  89. package/lib/schemas/metadata/IMetadataAtomic.d.ts +12 -0
  90. package/lib/schemas/metadata/IMetadataAtomic.js +3 -0
  91. package/lib/schemas/metadata/IMetadataAtomic.js.map +1 -0
  92. package/lib/schemas/metadata/{IMetadataResolved.d.ts → IMetadataEscaped.d.ts} +1 -1
  93. package/lib/schemas/metadata/IMetadataEscaped.js +3 -0
  94. package/lib/schemas/metadata/IMetadataEscaped.js.map +1 -0
  95. package/lib/schemas/metadata/Metadata.d.ts +4 -4
  96. package/lib/schemas/metadata/Metadata.js +64 -59
  97. package/lib/schemas/metadata/Metadata.js.map +1 -1
  98. package/lib/schemas/metadata/{MetadataResolved.d.ts → MetadataEscaped.d.ts} +3 -3
  99. package/lib/schemas/metadata/{MetadataResolved.js → MetadataEscaped.js} +11 -11
  100. package/lib/schemas/metadata/MetadataEscaped.js.map +1 -0
  101. package/lib/tags/ExclusiveMaximum.d.ts +8 -0
  102. package/lib/tags/ExclusiveMaximum.js +3 -0
  103. package/lib/tags/ExclusiveMaximum.js.map +1 -0
  104. package/lib/tags/ExclusiveMinimum.d.ts +8 -0
  105. package/lib/tags/ExclusiveMinimum.js +3 -0
  106. package/lib/tags/ExclusiveMinimum.js.map +1 -0
  107. package/lib/tags/Format.d.ts +8 -0
  108. package/lib/tags/Format.js +3 -0
  109. package/lib/tags/Format.js.map +1 -0
  110. package/lib/tags/MaxItems.d.ts +8 -0
  111. package/lib/tags/MaxItems.js +3 -0
  112. package/lib/tags/MaxItems.js.map +1 -0
  113. package/lib/tags/MaxLength.d.ts +8 -0
  114. package/lib/tags/MaxLength.js +3 -0
  115. package/lib/tags/MaxLength.js.map +1 -0
  116. package/lib/tags/Maximum.d.ts +8 -0
  117. package/lib/tags/Maximum.js +3 -0
  118. package/lib/tags/Maximum.js.map +1 -0
  119. package/lib/tags/MinItems.d.ts +8 -0
  120. package/lib/tags/MinItems.js +3 -0
  121. package/lib/tags/MinItems.js.map +1 -0
  122. package/lib/tags/MinLength.d.ts +8 -0
  123. package/lib/tags/MinLength.js +3 -0
  124. package/lib/tags/MinLength.js.map +1 -0
  125. package/lib/tags/Minimum.d.ts +8 -0
  126. package/lib/tags/Minimum.js +3 -0
  127. package/lib/tags/Minimum.js.map +1 -0
  128. package/lib/tags/MultipleOf.d.ts +8 -0
  129. package/lib/tags/MultipleOf.js +3 -0
  130. package/lib/tags/MultipleOf.js.map +1 -0
  131. package/lib/tags/Pattern.d.ts +8 -0
  132. package/lib/tags/Pattern.js +3 -0
  133. package/lib/tags/Pattern.js.map +1 -0
  134. package/lib/tags/TagBase.d.ts +10 -0
  135. package/lib/tags/TagBase.js +3 -0
  136. package/lib/tags/TagBase.js.map +1 -0
  137. package/lib/tags/Type.d.ts +13 -0
  138. package/lib/tags/Type.js +3 -0
  139. package/lib/tags/Type.js.map +1 -0
  140. package/lib/tags/index.d.ts +12 -0
  141. package/lib/tags/index.js +29 -0
  142. package/lib/tags/index.js.map +1 -0
  143. package/lib/transformers/features/json/JsonApplicationTransformer.js +2 -2
  144. package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
  145. package/lib/transformers/features/misc/MetadataTransformer.js +1 -1
  146. package/lib/transformers/features/misc/MetadataTransformer.js.map +1 -1
  147. package/lib/utils/RandomGenerator.js +3 -2
  148. package/lib/utils/RandomGenerator.js.map +1 -1
  149. package/package.json +2 -1
  150. package/src/Primitive.ts +135 -131
  151. package/src/Resolved.ts +116 -0
  152. package/src/factories/MetadataFactory.ts +46 -46
  153. package/src/factories/MetadataTagFactory.ts +366 -364
  154. package/src/factories/ProtobufFactory.ts +268 -266
  155. package/src/factories/internal/metadata/emend_metadata_atomics.ts +35 -33
  156. package/src/factories/internal/metadata/explore_metadata.ts +38 -38
  157. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +63 -59
  158. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  159. package/src/factories/internal/metadata/iterate_metadata_native.ts +210 -219
  160. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  161. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  162. package/src/functional/$ProtobufWriter.ts +151 -151
  163. package/src/json.ts +648 -648
  164. package/src/misc.ts +651 -651
  165. package/src/module.ts +709 -708
  166. package/src/programmers/AssertProgrammer.ts +281 -279
  167. package/src/programmers/CheckerProgrammer.ts +1174 -1173
  168. package/src/programmers/IsProgrammer.ts +241 -239
  169. package/src/programmers/RandomProgrammer.ts +874 -578
  170. package/src/programmers/ValidateProgrammer.ts +307 -305
  171. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  172. package/src/programmers/helpers/FunctionImporeter.ts +91 -89
  173. package/src/programmers/helpers/ProtobufUtil.ts +29 -29
  174. package/src/programmers/helpers/StringifyPredicator.ts +12 -13
  175. package/src/programmers/helpers/disable_function_importer_declare.ts +32 -27
  176. package/src/programmers/internal/application_default_string.ts +37 -33
  177. package/src/programmers/internal/application_resolved.ts +55 -55
  178. package/src/programmers/internal/application_schema.ts +157 -157
  179. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  180. package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
  181. package/src/programmers/json/JsonStringifyProgrammer.ts +995 -987
  182. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  183. package/src/programmers/misc/MiscCloneProgrammer.ts +775 -587
  184. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  185. package/src/programmers/misc/MiscLiteralsProgrammer.ts +64 -64
  186. package/src/programmers/misc/MiscPruneProgrammer.ts +544 -542
  187. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  188. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +75 -66
  189. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +673 -669
  190. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +814 -814
  191. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +85 -75
  192. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +75 -75
  193. package/src/protobuf.ts +881 -249
  194. package/src/schemas/metadata/IMetadata.ts +27 -28
  195. package/src/schemas/metadata/IMetadataAtomic.ts +13 -0
  196. package/src/schemas/metadata/{IMetadataResolved.ts → IMetadataEscaped.ts} +6 -6
  197. package/src/schemas/metadata/Metadata.ts +643 -637
  198. package/src/schemas/metadata/{MetadataResolved.ts → MetadataEscaped.ts} +51 -51
  199. package/src/tags/ExclusiveMaximum.ts +8 -0
  200. package/src/tags/ExclusiveMinimum.ts +8 -0
  201. package/src/tags/Format.ts +29 -0
  202. package/src/tags/MaxItems.ts +8 -0
  203. package/src/tags/MaxLength.ts +8 -0
  204. package/src/tags/Maximum.ts +8 -0
  205. package/src/tags/MinItems.ts +8 -0
  206. package/src/tags/MinLength.ts +8 -0
  207. package/src/tags/Minimum.ts +8 -0
  208. package/src/tags/MultipleOf.ts +10 -0
  209. package/src/tags/Pattern.ts +8 -0
  210. package/src/tags/TagBase.ts +17 -0
  211. package/src/tags/Type.ts +30 -0
  212. package/src/tags/index.ts +12 -0
  213. package/src/transformers/features/json/JsonApplicationTransformer.ts +111 -111
  214. package/src/transformers/features/misc/MetadataTransformer.ts +53 -53
  215. package/src/utils/RandomGenerator.ts +83 -81
  216. package/lib/schemas/metadata/IMetadataResolved.js.map +0 -1
  217. package/lib/schemas/metadata/MetadataResolved.js.map +0 -1
@@ -1,587 +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
- return FeatureProgrammer.write(project)({
31
- ...configure(project)(importer),
32
- addition: (collection) => [
33
- ...IsProgrammer.write_function_statements(project)(
34
- importer,
35
- )(collection),
36
- ...importer.declare(modulo),
37
- ],
38
- })(importer);
39
- };
40
-
41
- const write_array_functions =
42
- (config: FeatureProgrammer.IConfig) =>
43
- (importer: FunctionImporter) =>
44
- (collection: MetadataCollection): ts.VariableStatement[] =>
45
- collection
46
- .arrays()
47
- .filter((a) => a.recursive)
48
- .map((array, i) =>
49
- StatementFactory.constant(
50
- `${config.prefix}a${i}`,
51
- ts.factory.createArrowFunction(
52
- undefined,
53
- undefined,
54
- FeatureProgrammer.parameterDeclarations(config)(
55
- TypeFactory.keyword("any"),
56
- )(ts.factory.createIdentifier("input")),
57
- TypeFactory.keyword("any"),
58
- undefined,
59
- decode_array_inline(config)(importer)(
60
- ts.factory.createIdentifier("input"),
61
- array,
62
- {
63
- tracable: config.trace,
64
- source: "function",
65
- from: "array",
66
- postfix: "",
67
- },
68
- ),
69
- ),
70
- ),
71
- );
72
-
73
- const write_tuple_functions =
74
- (project: IProject) =>
75
- (config: FeatureProgrammer.IConfig) =>
76
- (importer: FunctionImporter) =>
77
- (collection: MetadataCollection): ts.VariableStatement[] =>
78
- collection
79
- .tuples()
80
- .filter((t) => t.recursive)
81
- .map((tuple, i) =>
82
- StatementFactory.constant(
83
- `${config.prefix}t${i}`,
84
- ts.factory.createArrowFunction(
85
- undefined,
86
- undefined,
87
- FeatureProgrammer.parameterDeclarations(config)(
88
- TypeFactory.keyword("any"),
89
- )(ts.factory.createIdentifier("input")),
90
- TypeFactory.keyword("any"),
91
- undefined,
92
- decode_tuple_inline(project)(config)(importer)(
93
- ts.factory.createIdentifier("input"),
94
- tuple,
95
- {
96
- tracable: config.trace,
97
- source: "function",
98
- from: "array",
99
- postfix: "",
100
- },
101
- ),
102
- ),
103
- ),
104
- );
105
-
106
- /* -----------------------------------------------------------
107
- DECODERS
108
- ----------------------------------------------------------- */
109
- const decode =
110
- (project: IProject) =>
111
- (config: FeatureProgrammer.IConfig) =>
112
- (importer: FunctionImporter) =>
113
- (
114
- input: ts.Expression,
115
- meta: Metadata,
116
- explore: FeatureProgrammer.IExplore,
117
- ): ts.Expression => {
118
- // ANY TYPE
119
- if (
120
- meta.any ||
121
- meta.arrays.some((a) => a.value.any) ||
122
- meta.tuples.some((t) => t.elements.every((e) => e.any))
123
- )
124
- return ts.factory.createCallExpression(
125
- importer.use("any"),
126
- undefined,
127
- [input],
128
- );
129
-
130
- interface IUnion {
131
- type: string;
132
- is: () => ts.Expression;
133
- value: () => ts.Expression;
134
- }
135
- const unions: IUnion[] = [];
136
-
137
- //----
138
- // LIST UP UNION TYPES
139
- //----
140
- // toJSON() METHOD
141
- if (meta.resolved !== null)
142
- unions.push({
143
- type: "resolved",
144
- is: () => IsProgrammer.decode_to_json(true)(input),
145
- value: () =>
146
- decode_to_json(project)(config)(importer)(
147
- input,
148
- meta.resolved!.returns,
149
- explore,
150
- ),
151
- });
152
-
153
- // TUPLES
154
- for (const tuple of meta.tuples)
155
- unions.push({
156
- type: "tuple",
157
- is: () =>
158
- IsProgrammer.decode(project)(importer)(
159
- input,
160
- (() => {
161
- const partial = Metadata.initialize();
162
- partial.tuples.push(tuple);
163
- return partial;
164
- })(),
165
- explore,
166
- [],
167
- [],
168
- ),
169
- value: () =>
170
- decode_tuple(project)(config)(importer)(
171
- input,
172
- tuple,
173
- explore,
174
- ),
175
- });
176
-
177
- // ARRAYS
178
- if (meta.arrays.length)
179
- unions.push({
180
- type: "array",
181
- is: () => ExpressionFactory.isArray(input),
182
- value: () =>
183
- explore_arrays(project)(config)(importer)(
184
- input,
185
- meta.arrays,
186
- {
187
- ...explore,
188
- from: "array",
189
- },
190
- ),
191
- });
192
-
193
- // NATIVE TYPES
194
- if (meta.sets.length)
195
- unions.push({
196
- type: "set",
197
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
198
- value: () => ts.factory.createIdentifier("{}"),
199
- });
200
- if (meta.maps.length)
201
- unions.push({
202
- type: "map",
203
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
204
- value: () => ts.factory.createIdentifier("{}"),
205
- });
206
- for (const native of meta.natives)
207
- unions.push({
208
- type: "native",
209
- is: () => ExpressionFactory.isInstanceOf(native)(input),
210
- value: () =>
211
- native === "Boolean" ||
212
- native === "Number" ||
213
- native === "String"
214
- ? ts.factory.createCallExpression(
215
- IdentifierFactory.access(input)("valueOf"),
216
- undefined,
217
- undefined,
218
- )
219
- : ts.factory.createIdentifier("{}"),
220
- });
221
-
222
- // OBJECTS
223
- if (meta.objects.length)
224
- unions.push({
225
- type: "object",
226
- is: () =>
227
- ExpressionFactory.isObject({
228
- checkNull: true,
229
- checkArray: false,
230
- })(input),
231
- value: () =>
232
- explore_objects(config)(importer)(input, meta, {
233
- ...explore,
234
- from: "object",
235
- }),
236
- });
237
-
238
- // COMPOSITION
239
- let last: ts.Expression = input;
240
- for (const u of unions.reverse())
241
- last = ts.factory.createConditionalExpression(
242
- u.is(),
243
- undefined,
244
- u.value(),
245
- undefined,
246
- last,
247
- );
248
- return ts.factory.createAsExpression(
249
- last,
250
- TypeFactory.keyword("any"),
251
- );
252
- };
253
-
254
- const decode_to_json =
255
- (project: IProject) =>
256
- (config: FeatureProgrammer.IConfig) =>
257
- (importer: FunctionImporter) =>
258
- (
259
- input: ts.Expression,
260
- resolved: Metadata,
261
- explore: FeatureProgrammer.IExplore,
262
- ): ts.Expression => {
263
- return decode(project)(config)(importer)(
264
- ts.factory.createCallExpression(
265
- IdentifierFactory.access(input)("toJSON"),
266
- undefined,
267
- [],
268
- ),
269
- resolved,
270
- explore,
271
- );
272
- };
273
-
274
- const decode_object = (importer: FunctionImporter) =>
275
- FeatureProgrammer.decode_object({
276
- trace: false,
277
- path: false,
278
- prefix: PREFIX,
279
- })(importer);
280
-
281
- const decode_array =
282
- (config: FeatureProgrammer.IConfig) =>
283
- (importer: FunctionImporter) =>
284
- (
285
- input: ts.Expression,
286
- array: MetadataArray,
287
- explore: FeatureProgrammer.IExplore,
288
- ) =>
289
- array.recursive
290
- ? ts.factory.createCallExpression(
291
- ts.factory.createIdentifier(
292
- importer.useLocal(`${config.prefix}a${array.index}`),
293
- ),
294
- undefined,
295
- FeatureProgrammer.argumentsArray(config)({
296
- ...explore,
297
- source: "function",
298
- from: "array",
299
- })(input),
300
- )
301
- : decode_array_inline(config)(importer)(input, array, explore);
302
-
303
- const decode_array_inline =
304
- (config: FeatureProgrammer.IConfig) =>
305
- (importer: FunctionImporter) =>
306
- (
307
- input: ts.Expression,
308
- array: MetadataArray,
309
- explore: FeatureProgrammer.IExplore,
310
- ) =>
311
- FeatureProgrammer.decode_array(config)(importer)(CloneJoiner.array)(
312
- input,
313
- array,
314
- explore,
315
- [],
316
- [],
317
- );
318
-
319
- const decode_tuple =
320
- (project: IProject) =>
321
- (config: FeatureProgrammer.IConfig) =>
322
- (importer: FunctionImporter) =>
323
- (
324
- input: ts.Expression,
325
- tuple: MetadataTuple,
326
- explore: FeatureProgrammer.IExplore,
327
- ): ts.Expression =>
328
- tuple.recursive
329
- ? ts.factory.createCallExpression(
330
- ts.factory.createIdentifier(
331
- importer.useLocal(`${config.prefix}t${tuple.index}`),
332
- ),
333
- undefined,
334
- FeatureProgrammer.argumentsArray(config)({
335
- ...explore,
336
- source: "function",
337
- })(input),
338
- )
339
- : decode_tuple_inline(project)(config)(importer)(
340
- input,
341
- tuple,
342
- explore,
343
- );
344
-
345
- const decode_tuple_inline =
346
- (project: IProject) =>
347
- (config: FeatureProgrammer.IConfig) =>
348
- (importer: FunctionImporter) =>
349
- (
350
- input: ts.Expression,
351
- tuple: MetadataTuple,
352
- explore: FeatureProgrammer.IExplore,
353
- ): ts.Expression => {
354
- const children: ts.Expression[] = tuple.elements
355
- .filter((m) => m.rest === null)
356
- .map((elem, index) =>
357
- decode(project)(config)(importer)(
358
- ts.factory.createElementAccessExpression(input, index),
359
- elem,
360
- {
361
- ...explore,
362
- from: "array",
363
- postfix: explore.postfix.length
364
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
365
- : `"[${index}]"`,
366
- },
367
- ),
368
- );
369
- const rest = (() => {
370
- if (tuple.elements.length === 0) return null;
371
-
372
- const last: Metadata = tuple.elements.at(-1)!;
373
- const rest: Metadata | null = last.rest;
374
- if (rest === null) return null;
375
-
376
- return decode(project)(config)(importer)(
377
- ts.factory.createCallExpression(
378
- IdentifierFactory.access(input)("slice"),
379
- undefined,
380
- [
381
- ts.factory.createNumericLiteral(
382
- tuple.elements.length - 1,
383
- ),
384
- ],
385
- ),
386
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
387
- {
388
- ...explore,
389
- start: tuple.elements.length - 1,
390
- },
391
- );
392
- })();
393
- return CloneJoiner.tuple(children, rest);
394
- };
395
-
396
- /* -----------------------------------------------------------
397
- EXPLORERS FOR UNION TYPES
398
- ----------------------------------------------------------- */
399
- const explore_objects =
400
- (config: FeatureProgrammer.IConfig) =>
401
- (importer: FunctionImporter) =>
402
- (
403
- input: ts.Expression,
404
- meta: Metadata,
405
- explore: FeatureProgrammer.IExplore,
406
- ) => {
407
- if (meta.objects.length === 1)
408
- return decode_object(importer)(
409
- input,
410
- meta.objects[0]!,
411
- explore,
412
- );
413
-
414
- return ts.factory.createCallExpression(
415
- ts.factory.createIdentifier(
416
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
417
- ),
418
- undefined,
419
- FeatureProgrammer.argumentsArray(config)(explore)(input),
420
- );
421
- };
422
-
423
- const explore_arrays =
424
- (project: IProject) =>
425
- (config: FeatureProgrammer.IConfig) =>
426
- (importer: FunctionImporter) =>
427
- (
428
- input: ts.Expression,
429
- elements: MetadataArray[],
430
- explore: FeatureProgrammer.IExplore,
431
- ): ts.Expression =>
432
- explore_array_like_union_types(config)(importer)(
433
- UnionExplorer.array({
434
- checker: IsProgrammer.decode(project)(importer),
435
- decoder: decode_array(config)(importer),
436
- empty: ts.factory.createIdentifier("[]"),
437
- success: ts.factory.createTrue(),
438
- failure: (input, expected) =>
439
- create_throw_error(importer)(expected)(input),
440
- }),
441
- )(input, elements, explore);
442
-
443
- const explore_array_like_union_types =
444
- (config: FeatureProgrammer.IConfig) =>
445
- (importer: FunctionImporter) =>
446
- <T extends MetadataArray | MetadataTuple>(
447
- factory: (
448
- parameters: ts.ParameterDeclaration[],
449
- ) => (
450
- input: ts.Expression,
451
- elements: T[],
452
- explore: FeatureProgrammer.IExplore,
453
- tags: IMetadataTag[],
454
- jsDocTags: IJsDocTagInfo[],
455
- ) => ts.ArrowFunction,
456
- ) =>
457
- (
458
- input: ts.Expression,
459
- elements: T[],
460
- explore: FeatureProgrammer.IExplore,
461
- ): ts.Expression => {
462
- const arrow =
463
- (parameters: ts.ParameterDeclaration[]) =>
464
- (explore: FeatureProgrammer.IExplore) =>
465
- (input: ts.Expression): ts.ArrowFunction =>
466
- factory(parameters)(input, elements, explore, [], []);
467
- if (elements.every((e) => e.recursive === false))
468
- ts.factory.createCallExpression(
469
- arrow([])(explore)(input),
470
- undefined,
471
- [],
472
- );
473
-
474
- explore = {
475
- ...explore,
476
- source: "function",
477
- from: "array",
478
- };
479
- return ts.factory.createCallExpression(
480
- ts.factory.createIdentifier(
481
- importer.emplaceUnion(
482
- config.prefix,
483
- elements.map((e) => e.name).join(" | "),
484
- () =>
485
- arrow(
486
- FeatureProgrammer.parameterDeclarations(config)(
487
- TypeFactory.keyword("any"),
488
- )(ts.factory.createIdentifier("input")),
489
- )({
490
- ...explore,
491
- postfix: "",
492
- })(ts.factory.createIdentifier("input")),
493
- ),
494
- ),
495
- undefined,
496
- FeatureProgrammer.argumentsArray(config)(explore)(input),
497
- );
498
- };
499
-
500
- /* -----------------------------------------------------------
501
- CONFIGURATIONS
502
- ----------------------------------------------------------- */
503
- const PREFIX = "$c";
504
-
505
- const configure =
506
- (project: IProject) =>
507
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
508
- const config: FeatureProgrammer.IConfig = {
509
- types: {
510
- input: (type, name) =>
511
- ts.factory.createTypeReferenceNode(
512
- name ??
513
- TypeFactory.getFullName(project.checker)(type),
514
- ),
515
- output: (type, name) =>
516
- ts.factory.createTypeReferenceNode(
517
- `typia.Primitive<${
518
- name ??
519
- TypeFactory.getFullName(project.checker)(type)
520
- }>`,
521
- ),
522
- },
523
- prefix: PREFIX,
524
- trace: false,
525
- path: false,
526
- initializer,
527
- decoder: () => decode(project)(config)(importer),
528
- objector: {
529
- checker: () => IsProgrammer.decode(project)(importer),
530
- decoder: () => decode_object(importer),
531
- joiner: CloneJoiner.object,
532
- unionizer: decode_union_object(
533
- IsProgrammer.decode_object(importer),
534
- )(decode_object(importer))((exp) => exp)(
535
- (input, expected) =>
536
- create_throw_error(importer)(expected)(input),
537
- ),
538
- failure: (input, expected) =>
539
- create_throw_error(importer)(expected)(input),
540
- },
541
- generator: {
542
- arrays: () => write_array_functions(config)(importer),
543
- tuples: () =>
544
- write_tuple_functions(project)(config)(importer),
545
- },
546
- };
547
- return config;
548
- };
549
-
550
- const initializer: FeatureProgrammer.IConfig["initializer"] =
551
- ({ checker }) =>
552
- (type) => {
553
- const collection = new MetadataCollection();
554
- const meta = MetadataFactory.analyze(checker)({
555
- resolve: true,
556
- constant: true,
557
- absorb: true,
558
- })(collection)(type);
559
- return [collection, meta];
560
- };
561
-
562
- const create_throw_error =
563
- (importer: FunctionImporter) =>
564
- (expected: string) =>
565
- (value: ts.Expression) =>
566
- ts.factory.createExpressionStatement(
567
- ts.factory.createCallExpression(
568
- importer.use("throws"),
569
- [],
570
- [
571
- ts.factory.createObjectLiteralExpression(
572
- [
573
- ts.factory.createPropertyAssignment(
574
- "expected",
575
- ts.factory.createStringLiteral(expected),
576
- ),
577
- ts.factory.createPropertyAssignment(
578
- "value",
579
- value,
580
- ),
581
- ],
582
- true,
583
- ),
584
- ],
585
- ),
586
- );
587
- }
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
+ }