typia 3.7.3-dev.20230408 → 3.7.4-dev.20230410

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 (215) hide show
  1. package/README.md +5 -56
  2. package/package.json +8 -8
  3. package/src/CustomValidatorMap.ts +126 -126
  4. package/src/IRandomGenerator.ts +33 -33
  5. package/src/IValidation.ts +21 -21
  6. package/src/Primitive.ts +104 -104
  7. package/src/TypeGuardError.ts +36 -36
  8. package/src/executable/TypiaGenerateWizard.ts +87 -87
  9. package/src/executable/TypiaSetupWizard.ts +152 -152
  10. package/src/executable/setup/ArgumentParser.ts +91 -91
  11. package/src/executable/setup/CommandExecutor.ts +8 -8
  12. package/src/executable/setup/FileRetriever.ts +33 -33
  13. package/src/executable/setup/PackageManager.ts +92 -92
  14. package/src/executable/setup/PluginConfigurator.ts +99 -99
  15. package/src/executable/typia.ts +38 -38
  16. package/src/factories/CommentFactory.ts +10 -10
  17. package/src/factories/ExpressionFactory.ts +77 -77
  18. package/src/factories/IdentifierFactory.ts +81 -81
  19. package/src/factories/LiteralFactory.ts +44 -44
  20. package/src/factories/MetadataCollection.ts +122 -122
  21. package/src/factories/MetadataFactory.ts +51 -51
  22. package/src/factories/MetadataTagFactory.ts +302 -302
  23. package/src/factories/StatementFactory.ts +60 -60
  24. package/src/factories/TemplateFactory.ts +56 -56
  25. package/src/factories/TypeFactory.ts +129 -129
  26. package/src/factories/TypiaFileFactory.ts +120 -120
  27. package/src/factories/ValueFactory.ts +12 -12
  28. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  29. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  30. package/src/factories/internal/metadata/explore_metadata.ts +92 -92
  31. package/src/factories/internal/metadata/iterate_metadata.ts +80 -80
  32. package/src/factories/internal/metadata/iterate_metadata_array.ts +29 -29
  33. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  34. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  35. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  36. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  37. package/src/factories/internal/metadata/iterate_metadata_native.ts +222 -222
  38. package/src/factories/internal/metadata/iterate_metadata_object.ts +48 -48
  39. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +27 -27
  40. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  41. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  42. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +45 -45
  43. package/src/factories/internal/metadata/iterate_metadata_union.ts +59 -59
  44. package/src/functional/$any.ts +3 -3
  45. package/src/functional/$dictionary.ts +20 -20
  46. package/src/functional/$every.ts +11 -11
  47. package/src/functional/$guard.ts +35 -35
  48. package/src/functional/$is_between.ts +7 -7
  49. package/src/functional/$is_custom.ts +14 -14
  50. package/src/functional/$is_date.ts +4 -4
  51. package/src/functional/$is_datetime.ts +3 -3
  52. package/src/functional/$is_email.ts +5 -5
  53. package/src/functional/$is_ipv4.ts +5 -5
  54. package/src/functional/$is_ipv6.ts +5 -5
  55. package/src/functional/$is_url.ts +5 -5
  56. package/src/functional/$is_uuid.ts +5 -5
  57. package/src/functional/$join.ts +50 -50
  58. package/src/functional/$number.ts +12 -12
  59. package/src/functional/$report.ts +15 -15
  60. package/src/functional/$rest.ts +3 -3
  61. package/src/functional/$string.ts +37 -37
  62. package/src/functional/$tail.ts +6 -6
  63. package/src/functional/Namespace.ts +127 -127
  64. package/src/index.ts +4 -4
  65. package/src/metadata/ICommentTag.ts +4 -4
  66. package/src/metadata/IJsDocTagInfo.ts +10 -10
  67. package/src/metadata/IMetadata.ts +25 -25
  68. package/src/metadata/IMetadataApplication.ts +7 -7
  69. package/src/metadata/IMetadataConstant.ts +16 -16
  70. package/src/metadata/IMetadataEntry.ts +6 -6
  71. package/src/metadata/IMetadataObject.ts +29 -29
  72. package/src/metadata/IMetadataProperty.ts +11 -11
  73. package/src/metadata/IMetadataTag.ts +105 -105
  74. package/src/metadata/Metadata.ts +534 -534
  75. package/src/metadata/MetadataConstant.ts +3 -3
  76. package/src/metadata/MetadataObject.ts +131 -131
  77. package/src/metadata/MetadataProperty.ts +64 -64
  78. package/src/module.ts +2043 -2043
  79. package/src/programmers/ApplicationProgrammer.ts +55 -55
  80. package/src/programmers/AssertCloneProgrammer.ts +70 -70
  81. package/src/programmers/AssertParseProgrammer.ts +65 -65
  82. package/src/programmers/AssertProgrammer.ts +284 -284
  83. package/src/programmers/AssertPruneProgrammer.ts +67 -67
  84. package/src/programmers/AssertStringifyProgrammer.ts +71 -71
  85. package/src/programmers/CheckerProgrammer.ts +922 -922
  86. package/src/programmers/CloneProgrammer.ts +388 -388
  87. package/src/programmers/FeatureProgrammer.ts +512 -512
  88. package/src/programmers/IsCloneProgrammer.ts +80 -80
  89. package/src/programmers/IsParseProgrammer.ts +74 -74
  90. package/src/programmers/IsProgrammer.ts +206 -206
  91. package/src/programmers/IsPruneProgrammer.ts +75 -75
  92. package/src/programmers/IsStringifyProgrammer.ts +81 -81
  93. package/src/programmers/LiteralsProgrammer.ts +65 -65
  94. package/src/programmers/PruneProgrammer.ts +343 -343
  95. package/src/programmers/RandomProgrammer.ts +413 -413
  96. package/src/programmers/StringifyProgrammer.ts +801 -801
  97. package/src/programmers/ValidateCloneProgrammer.ts +90 -90
  98. package/src/programmers/ValidateParseProgrammer.ts +69 -69
  99. package/src/programmers/ValidateProgrammer.ts +317 -317
  100. package/src/programmers/ValidatePruneProgrammer.ts +83 -83
  101. package/src/programmers/ValidateStringifyProgrammer.ts +89 -89
  102. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  103. package/src/programmers/helpers/CloneJoiner.ts +134 -134
  104. package/src/programmers/helpers/FunctionImporeter.ts +55 -55
  105. package/src/programmers/helpers/ICheckEntry.ts +12 -12
  106. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  107. package/src/programmers/helpers/OptionPredicator.ts +19 -19
  108. package/src/programmers/helpers/PruneJoiner.ts +52 -52
  109. package/src/programmers/helpers/RandomJoiner.ts +161 -161
  110. package/src/programmers/helpers/RandomRanger.ts +216 -216
  111. package/src/programmers/helpers/StringifyJoinder.ts +114 -114
  112. package/src/programmers/helpers/StringifyPredicator.ts +18 -18
  113. package/src/programmers/helpers/UnionExplorer.ts +281 -281
  114. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  115. package/src/programmers/internal/application_array.ts +37 -37
  116. package/src/programmers/internal/application_boolean.ts +17 -17
  117. package/src/programmers/internal/application_constant.ts +29 -29
  118. package/src/programmers/internal/application_default.ts +17 -17
  119. package/src/programmers/internal/application_default_string.ts +32 -32
  120. package/src/programmers/internal/application_native.ts +32 -32
  121. package/src/programmers/internal/application_number.ts +73 -73
  122. package/src/programmers/internal/application_object.ts +153 -153
  123. package/src/programmers/internal/application_schema.ts +184 -184
  124. package/src/programmers/internal/application_string.ts +45 -45
  125. package/src/programmers/internal/application_templates.ts +27 -27
  126. package/src/programmers/internal/application_tuple.ts +29 -29
  127. package/src/programmers/internal/check_array.ts +30 -30
  128. package/src/programmers/internal/check_array_length.ts +35 -35
  129. package/src/programmers/internal/check_bigint.ts +82 -82
  130. package/src/programmers/internal/check_custom.ts +33 -33
  131. package/src/programmers/internal/check_dynamic_properties.ts +197 -197
  132. package/src/programmers/internal/check_everything.ts +28 -28
  133. package/src/programmers/internal/check_native.ts +21 -21
  134. package/src/programmers/internal/check_number.ts +177 -177
  135. package/src/programmers/internal/check_object.ts +55 -55
  136. package/src/programmers/internal/check_string.ts +25 -25
  137. package/src/programmers/internal/check_string_tags.ts +73 -73
  138. package/src/programmers/internal/check_template.ts +59 -59
  139. package/src/programmers/internal/check_union_array_like.ts +272 -272
  140. package/src/programmers/internal/check_union_tuple.ts +35 -35
  141. package/src/programmers/internal/decode_union_object.ts +73 -73
  142. package/src/programmers/internal/feature_object_entries.ts +63 -63
  143. package/src/programmers/internal/get_comment_tags.ts +23 -23
  144. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  145. package/src/programmers/internal/prune_object_properties.ts +60 -60
  146. package/src/programmers/internal/random_custom.ts +30 -30
  147. package/src/programmers/internal/stringify_dynamic_properties.ts +168 -168
  148. package/src/programmers/internal/stringify_native.ts +8 -8
  149. package/src/programmers/internal/stringify_regular_properties.ts +84 -84
  150. package/src/programmers/internal/template_to_pattern.ts +15 -15
  151. package/src/schemas/IJsonApplication.ts +9 -9
  152. package/src/schemas/IJsonComponents.ts +26 -26
  153. package/src/schemas/IJsonSchema.ts +121 -121
  154. package/src/transform.ts +21 -21
  155. package/src/transformers/CallExpressionTransformer.ts +174 -174
  156. package/src/transformers/ExpressionWithArgumentTransformer.ts +66 -66
  157. package/src/transformers/FileTransformer.ts +49 -49
  158. package/src/transformers/IProject.ts +11 -11
  159. package/src/transformers/ITransformOptions.ts +62 -62
  160. package/src/transformers/ImportTransformer.ts +66 -66
  161. package/src/transformers/NodeTransformer.ts +19 -19
  162. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +119 -119
  163. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  164. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  165. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  166. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  167. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  168. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  169. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  170. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  171. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  172. package/src/transformers/features/miscellaneous/CreateRandomTransformer.ts +41 -41
  173. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  174. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  175. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  176. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  177. package/src/transformers/features/miscellaneous/LiteralsTransformer.ts +30 -30
  178. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +54 -54
  179. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  180. package/src/transformers/features/miscellaneous/RandomTransformer.ts +46 -46
  181. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  182. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  183. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  184. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  185. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  186. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  187. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  188. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  189. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  190. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +9 -9
  191. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  192. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  193. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +11 -11
  194. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  195. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  196. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  197. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  198. package/src/transformers/features/validators/CreateAssertTransformer.ts +12 -12
  199. package/src/transformers/features/validators/CreateIsTransformer.ts +10 -10
  200. package/src/transformers/features/validators/CreateValidateTransformer.ts +12 -12
  201. package/src/transformers/features/validators/IsTransformer.ts +10 -10
  202. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  203. package/src/transformers/internal/GenericTransformer.ts +99 -99
  204. package/src/typings/Atomic.ts +17 -17
  205. package/src/typings/ClassProperties.ts +5 -5
  206. package/src/typings/Customizable.ts +5 -5
  207. package/src/typings/OmitNever.ts +3 -3
  208. package/src/typings/SpecialFields.ts +3 -3
  209. package/src/typings/Writable.ts +11 -11
  210. package/src/utils/ArrayUtil.ts +49 -49
  211. package/src/utils/Escaper.ts +50 -50
  212. package/src/utils/MapUtil.ts +14 -14
  213. package/src/utils/PatternUtil.ts +30 -30
  214. package/src/utils/RandomGenerator.ts +96 -96
  215. package/src/utils/Singleton.ts +17 -17
@@ -1,512 +1,512 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../factories/IdentifierFactory";
4
- import { MetadataCollection } from "../factories/MetadataCollection";
5
- import { StatementFactory } from "../factories/StatementFactory";
6
- import { TypeFactory } from "../factories/TypeFactory";
7
- import { ValueFactory } from "../factories/ValueFactory";
8
-
9
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
- import { IMetadataTag } from "../metadata/IMetadataTag";
11
- import { Metadata } from "../metadata/Metadata";
12
- import { MetadataObject } from "../metadata/MetadataObject";
13
-
14
- import { IProject } from "../transformers/IProject";
15
-
16
- import { CheckerProgrammer } from "./CheckerProgrammer";
17
- import { FunctionImporter } from "./helpers/FunctionImporeter";
18
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
- import { UnionExplorer } from "./helpers/UnionExplorer";
20
- import { feature_object_entries } from "./internal/feature_object_entries";
21
-
22
- export namespace FeatureProgrammer {
23
- /* -----------------------------------------------------------
24
- PARAMETERS
25
- ----------------------------------------------------------- */
26
- export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
- types: IConfig.ITypes;
28
-
29
- /**
30
- * Prefix name of functions for specific object types.
31
- */
32
- functors: string;
33
-
34
- /**
35
- * Prefix name of functions for union object types.
36
- */
37
- unioners: string;
38
-
39
- /**
40
- * Whether to archive access path or not.
41
- */
42
- path: boolean;
43
-
44
- /**
45
- * Whether to trace exception or not.
46
- */
47
- trace: boolean;
48
-
49
- /**
50
- * Initializer of metadata.
51
- */
52
- initializer(
53
- project: IProject,
54
- type: ts.Type,
55
- ): [MetadataCollection, Metadata];
56
-
57
- /**
58
- * Decoder, station of every types.
59
- */
60
- decoder: Decoder<Metadata, Output>;
61
-
62
- /**
63
- * Object configurator.
64
- */
65
- objector: IConfig.IObjector;
66
-
67
- /**
68
- * Generator of functions for object types.
69
- */
70
- generator?: Partial<IConfig.IGenerator>;
71
- }
72
- export namespace IConfig {
73
- export interface ITypes {
74
- input: (type: ts.Type, name?: string) => ts.TypeNode;
75
- output: (type: ts.Type, name?: string) => ts.TypeNode;
76
- }
77
-
78
- export interface IObjector {
79
- /**
80
- * Type checker when union object type comes.
81
- */
82
- checker: Decoder<Metadata, ts.Expression>;
83
-
84
- /**
85
- * Decoder, function call expression generator of specific typed objects.
86
- */
87
- decoder: Decoder<MetadataObject, ts.Expression>;
88
-
89
- /**
90
- * Joiner of expressions from properties.
91
- */
92
- joiner(
93
- input: ts.Expression,
94
- entries: IExpressionEntry[],
95
- parent: MetadataObject,
96
- ): ts.ConciseBody;
97
-
98
- /**
99
- * Union type specificator.
100
- *
101
- * Expression of an algorithm specifying object type and calling
102
- * the `decoder` function of the specified object type.
103
- */
104
- unionizer: Decoder<MetadataObject[], ts.Expression>;
105
-
106
- /**
107
- * Handler of union type specification failure.
108
- *
109
- * @param value Expression of input parameter
110
- * @param expected Expected type name
111
- * @param explore Exploration info
112
- * @returns Statement of failure
113
- */
114
- failure(
115
- value: ts.Expression,
116
- expected: string,
117
- explore?: IExplore,
118
- ): ts.Statement;
119
-
120
- /**
121
- * Transformer of type checking expression by discrimination.
122
- *
123
- * When an object type has been specified by a discrimination without full
124
- * iteration, the `unionizer` will decode the object instance after
125
- * the last type checking.
126
- *
127
- * In such circumtance, you can transform the last type checking function.
128
- *
129
- * @param exp Current expression about type checking
130
- * @returns Transformed expression
131
- * @deprecated
132
- */
133
- is?(exp: ts.Expression): ts.Expression;
134
-
135
- /**
136
- * Transformer of non-undefined type checking by discrimination.
137
- *
138
- * When specifying an union type of objects, `typia` tries to find
139
- * descrimination way just by checking only one property type.
140
- * If succeeded to find the discrimination way, `typia` will check the target
141
- * property type and in the checking, non-undefined type checking would be
142
- * done.
143
- *
144
- * In such process, you can transform the non-undefined type checking.
145
- *
146
- * @param exp
147
- * @returns Transformed expression
148
- * @deprecated
149
- */
150
- required?(exp: ts.Expression): ts.Expression;
151
-
152
- /**
153
- * Conditon wrapper when unable to specify any object type.
154
- *
155
- * When failed to specify an object type through discrimination, full
156
- * iteration type checking would be happend. In such circumstance, you
157
- * can wrap the condition with additional function.
158
- *
159
- * @param condition Current condition
160
- * @returns A function wrapped current condition
161
- */
162
- full?: (
163
- condition: ts.Expression,
164
- ) => (
165
- input: ts.Expression,
166
- expected: string,
167
- explore: IExplore,
168
- ) => ts.Expression;
169
-
170
- /**
171
- * Return type.
172
- */
173
- type?: ts.TypeNode;
174
- }
175
- export interface IGenerator {
176
- /**
177
- *
178
- * @param col
179
- */
180
- functors(col: MetadataCollection): ts.VariableStatement[];
181
-
182
- /**
183
- *
184
- * @param col
185
- */
186
- unioners(col: MetadataCollection): ts.VariableStatement[];
187
- }
188
- }
189
-
190
- export interface IExplore {
191
- tracable: boolean;
192
- source: "top" | "object";
193
- from: "top" | "array" | "object";
194
- postfix: string;
195
- start?: number;
196
- }
197
-
198
- export interface Decoder<
199
- T,
200
- Output extends ts.ConciseBody = ts.ConciseBody,
201
- > {
202
- (
203
- input: ts.Expression,
204
- target: T,
205
- explore: IExplore,
206
- metaTags: IMetadataTag[],
207
- jsDocTags: ts.JSDocTagInfo[],
208
- ): Output;
209
- }
210
-
211
- /* -----------------------------------------------------------
212
- GENERATORS
213
- ----------------------------------------------------------- */
214
- /**
215
- * Generates a decoder function for a specific type.
216
- *
217
- * @param project Project configuration
218
- * @param config Detailed configuration for programming
219
- * @param importer Function importer
220
- * @param addition Generator of additinal statements in the top of function
221
- * @returns Currying function generating type decoder function
222
- */
223
- export const generate =
224
- (
225
- project: IProject,
226
- config: IConfig,
227
- importer: FunctionImporter,
228
- addition: (
229
- collection: MetadataCollection,
230
- ) => ts.Statement[] | undefined,
231
- ) =>
232
- (type: ts.Type, name?: string) => {
233
- const [collection, meta] = config.initializer(project, type);
234
-
235
- // ITERATE OVER ALL METADATA
236
- const output: ts.ConciseBody = config.decoder(
237
- ValueFactory.INPUT(),
238
- meta,
239
- {
240
- tracable: config.path || config.trace,
241
- source: "top",
242
- from: "top",
243
- postfix: '""',
244
- },
245
- [],
246
- [],
247
- );
248
-
249
- // RETURNS THE OPTIMAL ARROW FUNCTION
250
- const functors: ts.VariableStatement[] = (
251
- config.generator?.functors ??
252
- generate_functors(config)(importer)
253
- )(collection);
254
- const unioners: ts.VariableStatement[] = (
255
- config.generator?.unioners ??
256
- generate_unioners(config)(importer)
257
- )(collection);
258
- const added: ts.Statement[] | undefined = addition(collection);
259
-
260
- return ts.factory.createArrowFunction(
261
- undefined,
262
- undefined,
263
- PARAMETERS(config)(config.types.input(type, name))(
264
- ValueFactory.INPUT(),
265
- ),
266
- config.types.output(type, name),
267
- undefined,
268
- ts.factory.createBlock(
269
- [
270
- ...(added || []),
271
- ...functors.filter((_, i) =>
272
- importer.hasLocal(`${config.functors}${i}`),
273
- ),
274
- ...unioners.filter((_, i) =>
275
- importer.hasLocal(`${config.unioners}${i}`),
276
- ),
277
- ...(ts.isBlock(output)
278
- ? output.statements
279
- : [ts.factory.createReturnStatement(output)]),
280
- ],
281
- true,
282
- ),
283
- );
284
- };
285
-
286
- export const generate_functors =
287
- (config: IConfig) =>
288
- (importer: FunctionImporter) =>
289
- (collection: MetadataCollection) =>
290
- collection
291
- .objects()
292
- .map((obj, i) =>
293
- StatementFactory.constant(
294
- `${config.functors}${i}`,
295
- generate_object(config)(importer)(obj),
296
- ),
297
- );
298
-
299
- export const generate_unioners =
300
- (config: IConfig) =>
301
- (importer: FunctionImporter) =>
302
- (collection: MetadataCollection) =>
303
- collection
304
- .unions()
305
- .map((union, i) =>
306
- StatementFactory.constant(
307
- importer.useLocal(`${config.unioners}${i}`),
308
- generate_union(config)(union),
309
- ),
310
- );
311
-
312
- const generate_object =
313
- (config: IConfig) =>
314
- (importer: FunctionImporter) =>
315
- (obj: MetadataObject) =>
316
- ts.factory.createArrowFunction(
317
- undefined,
318
- undefined,
319
- PARAMETERS(config)(TypeFactory.keyword("any"))(
320
- ValueFactory.INPUT(),
321
- ),
322
- config.objector.type ?? TypeFactory.keyword("any"),
323
- undefined,
324
- config.objector.joiner(
325
- ts.factory.createIdentifier("input"),
326
- feature_object_entries(config)(importer)(obj)(
327
- ts.factory.createIdentifier("input"),
328
- ),
329
- obj,
330
- ),
331
- );
332
-
333
- function generate_union(config: IConfig) {
334
- const explorer = UnionExplorer.object(config);
335
- const input = ValueFactory.INPUT();
336
-
337
- return (meta: MetadataObject[]) =>
338
- ts.factory.createArrowFunction(
339
- undefined,
340
- undefined,
341
- PARAMETERS(config)(TypeFactory.keyword("any"))(
342
- ValueFactory.INPUT(),
343
- ),
344
- TypeFactory.keyword("any"),
345
- undefined,
346
- explorer(
347
- input,
348
- meta,
349
- {
350
- tracable: config.path || config.trace,
351
- source: "object",
352
- from: "object",
353
- postfix: "",
354
- },
355
- [],
356
- [],
357
- ),
358
- );
359
- }
360
-
361
- /* -----------------------------------------------------------
362
- DECODERS
363
- ----------------------------------------------------------- */
364
- export function decode_array(
365
- config: Pick<IConfig, "trace" | "path" | "decoder">,
366
- importer: FunctionImporter,
367
- combiner: (
368
- input: ts.Expression,
369
- arrow: ts.ArrowFunction,
370
- metaTags: IMetadataTag[],
371
- jsDocTags: ts.JSDocTagInfo[],
372
- ) => ts.Expression,
373
- ) {
374
- const rand: string = importer.increment().toString();
375
- const tail =
376
- config.path || config.trace
377
- ? [
378
- IdentifierFactory.parameter(
379
- "_index" + rand,
380
- TypeFactory.keyword("number"),
381
- ),
382
- ]
383
- : [];
384
-
385
- return (
386
- input: ts.Expression,
387
- meta: Metadata,
388
- explore: IExplore,
389
- metaTags: IMetadataTag[],
390
- jsDocTags: IJsDocTagInfo[],
391
- ) => {
392
- const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
393
- undefined,
394
- undefined,
395
- [
396
- IdentifierFactory.parameter(
397
- "elem",
398
- TypeFactory.keyword("any"),
399
- ),
400
- ...tail,
401
- ],
402
- undefined,
403
- undefined,
404
- config.decoder(
405
- ValueFactory.INPUT("elem"),
406
- meta,
407
- {
408
- tracable: explore.tracable,
409
- source: explore.source,
410
- from: "array",
411
- postfix: INDEX_SYMBOL(explore.start ?? null)(
412
- explore.postfix,
413
- )(rand),
414
- },
415
- metaTags,
416
- jsDocTags,
417
- ),
418
- );
419
- return combiner(input, arrow, metaTags, jsDocTags);
420
- };
421
- }
422
-
423
- export const decode_object =
424
- (config: Pick<IConfig, "trace" | "path" | "functors">) =>
425
- (importer: FunctionImporter) =>
426
- (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
427
- ts.factory.createCallExpression(
428
- ts.factory.createIdentifier(
429
- importer.useLocal(`${config.functors}${obj.index}`),
430
- ),
431
- undefined,
432
- get_object_arguments(config)(explore)(input),
433
- );
434
-
435
- export const get_object_arguments =
436
- (config: Pick<IConfig, "path" | "trace">) =>
437
- (explore: FeatureProgrammer.IExplore) => {
438
- const tail: ts.Expression[] =
439
- config.path === false && config.trace === false
440
- ? []
441
- : config.path === true && config.trace === true
442
- ? [
443
- ts.factory.createIdentifier(
444
- explore.postfix
445
- ? `_path + ${explore.postfix}`
446
- : "_path",
447
- ),
448
- explore.source === "object"
449
- ? ts.factory.createIdentifier(
450
- `${explore.tracable} && _exceptionable`,
451
- )
452
- : explore.tracable
453
- ? ts.factory.createTrue()
454
- : ts.factory.createFalse(),
455
- ]
456
- : config.path === true
457
- ? [
458
- ts.factory.createIdentifier(
459
- explore.postfix
460
- ? `_path + ${explore.postfix}`
461
- : "_path",
462
- ),
463
- ]
464
- : [
465
- explore.source === "object"
466
- ? ts.factory.createIdentifier(
467
- `${explore.tracable} && _exceptionable`,
468
- )
469
- : explore.tracable
470
- ? ts.factory.createTrue()
471
- : ts.factory.createFalse(),
472
- ];
473
- return (input: ts.Expression) => [input, ...tail];
474
- };
475
- }
476
-
477
- const INDEX_SYMBOL =
478
- (start: number | null) => (prev: string) => (rand: string) => {
479
- const tail: string =
480
- start !== null
481
- ? `"[" + (${start} + _index${rand}) + "]"`
482
- : `"[" + _index${rand} + "]"`;
483
- if (prev === "") return tail;
484
- else if (prev[prev.length - 1] === `"`)
485
- return prev.substring(0, prev.length - 1) + tail.substring(1);
486
- return prev + ` + ${tail}`;
487
- };
488
-
489
- const PARAMETERS =
490
- (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
491
- (type: ts.TypeNode) => {
492
- const tail: ts.ParameterDeclaration[] = [];
493
- if (props.path)
494
- tail.push(
495
- IdentifierFactory.parameter(
496
- "_path",
497
- TypeFactory.keyword("string"),
498
- ),
499
- );
500
- if (props.trace)
501
- tail.push(
502
- IdentifierFactory.parameter(
503
- "_exceptionable",
504
- TypeFactory.keyword("boolean"),
505
- ts.factory.createTrue(),
506
- ),
507
- );
508
- return (input: ts.Identifier) => [
509
- IdentifierFactory.parameter(input, type),
510
- ...tail,
511
- ];
512
- };
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
4
+ import { MetadataCollection } from "../factories/MetadataCollection";
5
+ import { StatementFactory } from "../factories/StatementFactory";
6
+ import { TypeFactory } from "../factories/TypeFactory";
7
+ import { ValueFactory } from "../factories/ValueFactory";
8
+
9
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
+ import { IMetadataTag } from "../metadata/IMetadataTag";
11
+ import { Metadata } from "../metadata/Metadata";
12
+ import { MetadataObject } from "../metadata/MetadataObject";
13
+
14
+ import { IProject } from "../transformers/IProject";
15
+
16
+ import { CheckerProgrammer } from "./CheckerProgrammer";
17
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
18
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
+ import { UnionExplorer } from "./helpers/UnionExplorer";
20
+ import { feature_object_entries } from "./internal/feature_object_entries";
21
+
22
+ export namespace FeatureProgrammer {
23
+ /* -----------------------------------------------------------
24
+ PARAMETERS
25
+ ----------------------------------------------------------- */
26
+ export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
+ types: IConfig.ITypes;
28
+
29
+ /**
30
+ * Prefix name of functions for specific object types.
31
+ */
32
+ functors: string;
33
+
34
+ /**
35
+ * Prefix name of functions for union object types.
36
+ */
37
+ unioners: string;
38
+
39
+ /**
40
+ * Whether to archive access path or not.
41
+ */
42
+ path: boolean;
43
+
44
+ /**
45
+ * Whether to trace exception or not.
46
+ */
47
+ trace: boolean;
48
+
49
+ /**
50
+ * Initializer of metadata.
51
+ */
52
+ initializer(
53
+ project: IProject,
54
+ type: ts.Type,
55
+ ): [MetadataCollection, Metadata];
56
+
57
+ /**
58
+ * Decoder, station of every types.
59
+ */
60
+ decoder: Decoder<Metadata, Output>;
61
+
62
+ /**
63
+ * Object configurator.
64
+ */
65
+ objector: IConfig.IObjector;
66
+
67
+ /**
68
+ * Generator of functions for object types.
69
+ */
70
+ generator?: Partial<IConfig.IGenerator>;
71
+ }
72
+ export namespace IConfig {
73
+ export interface ITypes {
74
+ input: (type: ts.Type, name?: string) => ts.TypeNode;
75
+ output: (type: ts.Type, name?: string) => ts.TypeNode;
76
+ }
77
+
78
+ export interface IObjector {
79
+ /**
80
+ * Type checker when union object type comes.
81
+ */
82
+ checker: Decoder<Metadata, ts.Expression>;
83
+
84
+ /**
85
+ * Decoder, function call expression generator of specific typed objects.
86
+ */
87
+ decoder: Decoder<MetadataObject, ts.Expression>;
88
+
89
+ /**
90
+ * Joiner of expressions from properties.
91
+ */
92
+ joiner(
93
+ input: ts.Expression,
94
+ entries: IExpressionEntry[],
95
+ parent: MetadataObject,
96
+ ): ts.ConciseBody;
97
+
98
+ /**
99
+ * Union type specificator.
100
+ *
101
+ * Expression of an algorithm specifying object type and calling
102
+ * the `decoder` function of the specified object type.
103
+ */
104
+ unionizer: Decoder<MetadataObject[], ts.Expression>;
105
+
106
+ /**
107
+ * Handler of union type specification failure.
108
+ *
109
+ * @param value Expression of input parameter
110
+ * @param expected Expected type name
111
+ * @param explore Exploration info
112
+ * @returns Statement of failure
113
+ */
114
+ failure(
115
+ value: ts.Expression,
116
+ expected: string,
117
+ explore?: IExplore,
118
+ ): ts.Statement;
119
+
120
+ /**
121
+ * Transformer of type checking expression by discrimination.
122
+ *
123
+ * When an object type has been specified by a discrimination without full
124
+ * iteration, the `unionizer` will decode the object instance after
125
+ * the last type checking.
126
+ *
127
+ * In such circumtance, you can transform the last type checking function.
128
+ *
129
+ * @param exp Current expression about type checking
130
+ * @returns Transformed expression
131
+ * @deprecated
132
+ */
133
+ is?(exp: ts.Expression): ts.Expression;
134
+
135
+ /**
136
+ * Transformer of non-undefined type checking by discrimination.
137
+ *
138
+ * When specifying an union type of objects, `typia` tries to find
139
+ * descrimination way just by checking only one property type.
140
+ * If succeeded to find the discrimination way, `typia` will check the target
141
+ * property type and in the checking, non-undefined type checking would be
142
+ * done.
143
+ *
144
+ * In such process, you can transform the non-undefined type checking.
145
+ *
146
+ * @param exp
147
+ * @returns Transformed expression
148
+ * @deprecated
149
+ */
150
+ required?(exp: ts.Expression): ts.Expression;
151
+
152
+ /**
153
+ * Conditon wrapper when unable to specify any object type.
154
+ *
155
+ * When failed to specify an object type through discrimination, full
156
+ * iteration type checking would be happend. In such circumstance, you
157
+ * can wrap the condition with additional function.
158
+ *
159
+ * @param condition Current condition
160
+ * @returns A function wrapped current condition
161
+ */
162
+ full?: (
163
+ condition: ts.Expression,
164
+ ) => (
165
+ input: ts.Expression,
166
+ expected: string,
167
+ explore: IExplore,
168
+ ) => ts.Expression;
169
+
170
+ /**
171
+ * Return type.
172
+ */
173
+ type?: ts.TypeNode;
174
+ }
175
+ export interface IGenerator {
176
+ /**
177
+ *
178
+ * @param col
179
+ */
180
+ functors(col: MetadataCollection): ts.VariableStatement[];
181
+
182
+ /**
183
+ *
184
+ * @param col
185
+ */
186
+ unioners(col: MetadataCollection): ts.VariableStatement[];
187
+ }
188
+ }
189
+
190
+ export interface IExplore {
191
+ tracable: boolean;
192
+ source: "top" | "object";
193
+ from: "top" | "array" | "object";
194
+ postfix: string;
195
+ start?: number;
196
+ }
197
+
198
+ export interface Decoder<
199
+ T,
200
+ Output extends ts.ConciseBody = ts.ConciseBody,
201
+ > {
202
+ (
203
+ input: ts.Expression,
204
+ target: T,
205
+ explore: IExplore,
206
+ metaTags: IMetadataTag[],
207
+ jsDocTags: ts.JSDocTagInfo[],
208
+ ): Output;
209
+ }
210
+
211
+ /* -----------------------------------------------------------
212
+ GENERATORS
213
+ ----------------------------------------------------------- */
214
+ /**
215
+ * Generates a decoder function for a specific type.
216
+ *
217
+ * @param project Project configuration
218
+ * @param config Detailed configuration for programming
219
+ * @param importer Function importer
220
+ * @param addition Generator of additinal statements in the top of function
221
+ * @returns Currying function generating type decoder function
222
+ */
223
+ export const generate =
224
+ (
225
+ project: IProject,
226
+ config: IConfig,
227
+ importer: FunctionImporter,
228
+ addition: (
229
+ collection: MetadataCollection,
230
+ ) => ts.Statement[] | undefined,
231
+ ) =>
232
+ (type: ts.Type, name?: string) => {
233
+ const [collection, meta] = config.initializer(project, type);
234
+
235
+ // ITERATE OVER ALL METADATA
236
+ const output: ts.ConciseBody = config.decoder(
237
+ ValueFactory.INPUT(),
238
+ meta,
239
+ {
240
+ tracable: config.path || config.trace,
241
+ source: "top",
242
+ from: "top",
243
+ postfix: '""',
244
+ },
245
+ [],
246
+ [],
247
+ );
248
+
249
+ // RETURNS THE OPTIMAL ARROW FUNCTION
250
+ const functors: ts.VariableStatement[] = (
251
+ config.generator?.functors ??
252
+ generate_functors(config)(importer)
253
+ )(collection);
254
+ const unioners: ts.VariableStatement[] = (
255
+ config.generator?.unioners ??
256
+ generate_unioners(config)(importer)
257
+ )(collection);
258
+ const added: ts.Statement[] | undefined = addition(collection);
259
+
260
+ return ts.factory.createArrowFunction(
261
+ undefined,
262
+ undefined,
263
+ PARAMETERS(config)(config.types.input(type, name))(
264
+ ValueFactory.INPUT(),
265
+ ),
266
+ config.types.output(type, name),
267
+ undefined,
268
+ ts.factory.createBlock(
269
+ [
270
+ ...(added || []),
271
+ ...functors.filter((_, i) =>
272
+ importer.hasLocal(`${config.functors}${i}`),
273
+ ),
274
+ ...unioners.filter((_, i) =>
275
+ importer.hasLocal(`${config.unioners}${i}`),
276
+ ),
277
+ ...(ts.isBlock(output)
278
+ ? output.statements
279
+ : [ts.factory.createReturnStatement(output)]),
280
+ ],
281
+ true,
282
+ ),
283
+ );
284
+ };
285
+
286
+ export const generate_functors =
287
+ (config: IConfig) =>
288
+ (importer: FunctionImporter) =>
289
+ (collection: MetadataCollection) =>
290
+ collection
291
+ .objects()
292
+ .map((obj, i) =>
293
+ StatementFactory.constant(
294
+ `${config.functors}${i}`,
295
+ generate_object(config)(importer)(obj),
296
+ ),
297
+ );
298
+
299
+ export const generate_unioners =
300
+ (config: IConfig) =>
301
+ (importer: FunctionImporter) =>
302
+ (collection: MetadataCollection) =>
303
+ collection
304
+ .unions()
305
+ .map((union, i) =>
306
+ StatementFactory.constant(
307
+ importer.useLocal(`${config.unioners}${i}`),
308
+ generate_union(config)(union),
309
+ ),
310
+ );
311
+
312
+ const generate_object =
313
+ (config: IConfig) =>
314
+ (importer: FunctionImporter) =>
315
+ (obj: MetadataObject) =>
316
+ ts.factory.createArrowFunction(
317
+ undefined,
318
+ undefined,
319
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
320
+ ValueFactory.INPUT(),
321
+ ),
322
+ config.objector.type ?? TypeFactory.keyword("any"),
323
+ undefined,
324
+ config.objector.joiner(
325
+ ts.factory.createIdentifier("input"),
326
+ feature_object_entries(config)(importer)(obj)(
327
+ ts.factory.createIdentifier("input"),
328
+ ),
329
+ obj,
330
+ ),
331
+ );
332
+
333
+ function generate_union(config: IConfig) {
334
+ const explorer = UnionExplorer.object(config);
335
+ const input = ValueFactory.INPUT();
336
+
337
+ return (meta: MetadataObject[]) =>
338
+ ts.factory.createArrowFunction(
339
+ undefined,
340
+ undefined,
341
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
342
+ ValueFactory.INPUT(),
343
+ ),
344
+ TypeFactory.keyword("any"),
345
+ undefined,
346
+ explorer(
347
+ input,
348
+ meta,
349
+ {
350
+ tracable: config.path || config.trace,
351
+ source: "object",
352
+ from: "object",
353
+ postfix: "",
354
+ },
355
+ [],
356
+ [],
357
+ ),
358
+ );
359
+ }
360
+
361
+ /* -----------------------------------------------------------
362
+ DECODERS
363
+ ----------------------------------------------------------- */
364
+ export function decode_array(
365
+ config: Pick<IConfig, "trace" | "path" | "decoder">,
366
+ importer: FunctionImporter,
367
+ combiner: (
368
+ input: ts.Expression,
369
+ arrow: ts.ArrowFunction,
370
+ metaTags: IMetadataTag[],
371
+ jsDocTags: ts.JSDocTagInfo[],
372
+ ) => ts.Expression,
373
+ ) {
374
+ const rand: string = importer.increment().toString();
375
+ const tail =
376
+ config.path || config.trace
377
+ ? [
378
+ IdentifierFactory.parameter(
379
+ "_index" + rand,
380
+ TypeFactory.keyword("number"),
381
+ ),
382
+ ]
383
+ : [];
384
+
385
+ return (
386
+ input: ts.Expression,
387
+ meta: Metadata,
388
+ explore: IExplore,
389
+ metaTags: IMetadataTag[],
390
+ jsDocTags: IJsDocTagInfo[],
391
+ ) => {
392
+ const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
393
+ undefined,
394
+ undefined,
395
+ [
396
+ IdentifierFactory.parameter(
397
+ "elem",
398
+ TypeFactory.keyword("any"),
399
+ ),
400
+ ...tail,
401
+ ],
402
+ undefined,
403
+ undefined,
404
+ config.decoder(
405
+ ValueFactory.INPUT("elem"),
406
+ meta,
407
+ {
408
+ tracable: explore.tracable,
409
+ source: explore.source,
410
+ from: "array",
411
+ postfix: INDEX_SYMBOL(explore.start ?? null)(
412
+ explore.postfix,
413
+ )(rand),
414
+ },
415
+ metaTags,
416
+ jsDocTags,
417
+ ),
418
+ );
419
+ return combiner(input, arrow, metaTags, jsDocTags);
420
+ };
421
+ }
422
+
423
+ export const decode_object =
424
+ (config: Pick<IConfig, "trace" | "path" | "functors">) =>
425
+ (importer: FunctionImporter) =>
426
+ (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
427
+ ts.factory.createCallExpression(
428
+ ts.factory.createIdentifier(
429
+ importer.useLocal(`${config.functors}${obj.index}`),
430
+ ),
431
+ undefined,
432
+ get_object_arguments(config)(explore)(input),
433
+ );
434
+
435
+ export const get_object_arguments =
436
+ (config: Pick<IConfig, "path" | "trace">) =>
437
+ (explore: FeatureProgrammer.IExplore) => {
438
+ const tail: ts.Expression[] =
439
+ config.path === false && config.trace === false
440
+ ? []
441
+ : config.path === true && config.trace === true
442
+ ? [
443
+ ts.factory.createIdentifier(
444
+ explore.postfix
445
+ ? `_path + ${explore.postfix}`
446
+ : "_path",
447
+ ),
448
+ explore.source === "object"
449
+ ? ts.factory.createIdentifier(
450
+ `${explore.tracable} && _exceptionable`,
451
+ )
452
+ : explore.tracable
453
+ ? ts.factory.createTrue()
454
+ : ts.factory.createFalse(),
455
+ ]
456
+ : config.path === true
457
+ ? [
458
+ ts.factory.createIdentifier(
459
+ explore.postfix
460
+ ? `_path + ${explore.postfix}`
461
+ : "_path",
462
+ ),
463
+ ]
464
+ : [
465
+ explore.source === "object"
466
+ ? ts.factory.createIdentifier(
467
+ `${explore.tracable} && _exceptionable`,
468
+ )
469
+ : explore.tracable
470
+ ? ts.factory.createTrue()
471
+ : ts.factory.createFalse(),
472
+ ];
473
+ return (input: ts.Expression) => [input, ...tail];
474
+ };
475
+ }
476
+
477
+ const INDEX_SYMBOL =
478
+ (start: number | null) => (prev: string) => (rand: string) => {
479
+ const tail: string =
480
+ start !== null
481
+ ? `"[" + (${start} + _index${rand}) + "]"`
482
+ : `"[" + _index${rand} + "]"`;
483
+ if (prev === "") return tail;
484
+ else if (prev[prev.length - 1] === `"`)
485
+ return prev.substring(0, prev.length - 1) + tail.substring(1);
486
+ return prev + ` + ${tail}`;
487
+ };
488
+
489
+ const PARAMETERS =
490
+ (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
491
+ (type: ts.TypeNode) => {
492
+ const tail: ts.ParameterDeclaration[] = [];
493
+ if (props.path)
494
+ tail.push(
495
+ IdentifierFactory.parameter(
496
+ "_path",
497
+ TypeFactory.keyword("string"),
498
+ ),
499
+ );
500
+ if (props.trace)
501
+ tail.push(
502
+ IdentifierFactory.parameter(
503
+ "_exceptionable",
504
+ TypeFactory.keyword("boolean"),
505
+ ts.factory.createTrue(),
506
+ ),
507
+ );
508
+ return (input: ts.Identifier) => [
509
+ IdentifierFactory.parameter(input, type),
510
+ ...tail,
511
+ ];
512
+ };