typia 5.0.4 → 5.0.5-dev.20230921

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 (291) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/IRandomGenerator.ts +35 -35
  4. package/src/IValidation.ts +21 -21
  5. package/src/Primitive.ts +135 -135
  6. package/src/Resolved.ts +116 -116
  7. package/src/TypeGuardError.ts +36 -36
  8. package/src/executable/TypiaGenerateWizard.ts +85 -85
  9. package/src/executable/TypiaSetupWizard.ts +153 -153
  10. package/src/executable/setup/ArgumentParser.ts +45 -45
  11. package/src/executable/setup/CommandExecutor.ts +8 -8
  12. package/src/executable/setup/FileRetriever.ts +22 -22
  13. package/src/executable/setup/PackageManager.ts +71 -71
  14. package/src/executable/setup/PluginConfigurator.ts +70 -70
  15. package/src/executable/typia.ts +52 -52
  16. package/src/factories/CommentFactory.ts +84 -84
  17. package/src/factories/ExpressionFactory.ts +159 -159
  18. package/src/factories/IdentifierFactory.ts +74 -74
  19. package/src/factories/JsonMetadataFactory.ts +43 -43
  20. package/src/factories/LiteralFactory.ts +47 -47
  21. package/src/factories/MetadataCollection.ts +269 -269
  22. package/src/factories/MetadataCommentTagFactory.ts +629 -629
  23. package/src/factories/MetadataFactory.ts +214 -214
  24. package/src/factories/MetadataTypeTagFactory.ts +313 -313
  25. package/src/factories/NumericRangeFactory.ts +33 -33
  26. package/src/factories/ProtobufFactory.ts +272 -272
  27. package/src/factories/StatementFactory.ts +72 -72
  28. package/src/factories/TemplateFactory.ts +58 -58
  29. package/src/factories/TypeFactory.ts +119 -119
  30. package/src/factories/ValueFactory.ts +12 -12
  31. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  32. package/src/factories/internal/metadata/emend_metadata_atomics.ts +40 -40
  33. package/src/factories/internal/metadata/emplace_metadata_alias.ts +41 -41
  34. package/src/factories/internal/metadata/emplace_metadata_array_type.ts +41 -41
  35. package/src/factories/internal/metadata/emplace_metadata_object.ts +150 -150
  36. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +60 -60
  37. package/src/factories/internal/metadata/explore_metadata.ts +32 -32
  38. package/src/factories/internal/metadata/iterate_metadata.ts +101 -101
  39. package/src/factories/internal/metadata/iterate_metadata_alias.ts +35 -35
  40. package/src/factories/internal/metadata/iterate_metadata_array.ts +37 -37
  41. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +62 -62
  42. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  43. package/src/factories/internal/metadata/iterate_metadata_collection.ts +141 -141
  44. package/src/factories/internal/metadata/iterate_metadata_comment_tags.ts +26 -26
  45. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  46. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +197 -197
  47. package/src/factories/internal/metadata/iterate_metadata_map.ts +57 -57
  48. package/src/factories/internal/metadata/iterate_metadata_native.ts +210 -210
  49. package/src/factories/internal/metadata/iterate_metadata_object.ts +44 -44
  50. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +52 -52
  51. package/src/factories/internal/metadata/iterate_metadata_set.ts +42 -42
  52. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  53. package/src/factories/internal/metadata/iterate_metadata_template.ts +47 -47
  54. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +37 -37
  55. package/src/factories/internal/metadata/iterate_metadata_union.ts +27 -27
  56. package/src/functional/$ProtobufReader.ts +201 -201
  57. package/src/functional/$ProtobufSizer.ts +147 -147
  58. package/src/functional/$ProtobufWriter.ts +151 -151
  59. package/src/functional/$any.ts +4 -4
  60. package/src/functional/$clone.ts +4 -4
  61. package/src/functional/$dictionary.ts +25 -25
  62. package/src/functional/$every.ts +11 -11
  63. package/src/functional/$guard.ts +35 -35
  64. package/src/functional/$is_between.ts +2 -2
  65. package/src/functional/$join.ts +46 -46
  66. package/src/functional/$number.ts +13 -13
  67. package/src/functional/$report.ts +15 -15
  68. package/src/functional/$rest.ts +3 -3
  69. package/src/functional/$string.ts +50 -50
  70. package/src/functional/$strlen.ts +7 -7
  71. package/src/functional/$tail.ts +5 -5
  72. package/src/functional/$varint.ts +130 -130
  73. package/src/functional/$zigzag.ts +39 -39
  74. package/src/functional/IProtobufWriter.ts +18 -18
  75. package/src/functional/Namespace.ts +133 -133
  76. package/src/index.ts +4 -4
  77. package/src/json.ts +648 -648
  78. package/src/misc.ts +651 -651
  79. package/src/module.ts +656 -656
  80. package/src/programmers/AssertProgrammer.ts +317 -317
  81. package/src/programmers/CheckerProgrammer.ts +1137 -1137
  82. package/src/programmers/FeatureProgrammer.ts +478 -478
  83. package/src/programmers/IsProgrammer.ts +252 -252
  84. package/src/programmers/RandomProgrammer.ts +878 -878
  85. package/src/programmers/TypiaProgrammer.ts +163 -163
  86. package/src/programmers/ValidateProgrammer.ts +346 -346
  87. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  88. package/src/programmers/helpers/CloneJoiner.ts +144 -144
  89. package/src/programmers/helpers/FunctionImporeter.ts +91 -91
  90. package/src/programmers/helpers/ICheckEntry.ts +13 -13
  91. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  92. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  93. package/src/programmers/helpers/ProtobufUtil.ts +125 -125
  94. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  95. package/src/programmers/helpers/PruneJoiner.ts +143 -143
  96. package/src/programmers/helpers/RandomJoiner.ts +151 -151
  97. package/src/programmers/helpers/RandomRanger.ts +173 -173
  98. package/src/programmers/helpers/StringifyJoinder.ts +113 -113
  99. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  100. package/src/programmers/helpers/UnionExplorer.ts +305 -305
  101. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  102. package/src/programmers/helpers/disable_function_importer_declare.ts +32 -32
  103. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  104. package/src/programmers/internal/application_alias.ts +65 -65
  105. package/src/programmers/internal/application_array.ts +53 -53
  106. package/src/programmers/internal/application_boolean.ts +15 -15
  107. package/src/programmers/internal/application_constant.ts +26 -26
  108. package/src/programmers/internal/application_default.ts +17 -17
  109. package/src/programmers/internal/application_default_string.ts +37 -37
  110. package/src/programmers/internal/application_escaped.ts +55 -55
  111. package/src/programmers/internal/application_native.ts +39 -39
  112. package/src/programmers/internal/application_number.ts +88 -88
  113. package/src/programmers/internal/application_object.ts +162 -162
  114. package/src/programmers/internal/application_schema.ts +175 -175
  115. package/src/programmers/internal/application_string.ts +51 -51
  116. package/src/programmers/internal/application_templates.ts +25 -25
  117. package/src/programmers/internal/application_tuple.ts +58 -58
  118. package/src/programmers/internal/check_array_length.ts +41 -41
  119. package/src/programmers/internal/check_bigint.ts +45 -45
  120. package/src/programmers/internal/check_dynamic_properties.ts +194 -194
  121. package/src/programmers/internal/check_everything.ts +28 -28
  122. package/src/programmers/internal/check_native.ts +21 -21
  123. package/src/programmers/internal/check_number.ts +105 -105
  124. package/src/programmers/internal/check_object.ts +55 -55
  125. package/src/programmers/internal/check_string.ts +45 -45
  126. package/src/programmers/internal/check_template.ts +61 -61
  127. package/src/programmers/internal/check_union_array_like.ts +323 -323
  128. package/src/programmers/internal/decode_union_object.ts +84 -84
  129. package/src/programmers/internal/feature_object_entries.ts +58 -58
  130. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  131. package/src/programmers/internal/prune_object_properties.ts +60 -60
  132. package/src/programmers/internal/random_custom.ts +37 -37
  133. package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
  134. package/src/programmers/internal/stringify_native.ts +7 -7
  135. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  136. package/src/programmers/internal/template_to_pattern.ts +15 -15
  137. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +20 -20
  138. package/src/programmers/json/JsonApplicationProgrammer.ts +50 -50
  139. package/src/programmers/json/JsonAssertParseProgrammer.ts +71 -71
  140. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +66 -66
  141. package/src/programmers/json/JsonIsParseProgrammer.ts +77 -77
  142. package/src/programmers/json/JsonIsStringifyProgrammer.ts +76 -76
  143. package/src/programmers/json/JsonStringifyProgrammer.ts +964 -964
  144. package/src/programmers/json/JsonValidateParseProgrammer.ts +68 -68
  145. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +84 -84
  146. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  147. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +68 -68
  148. package/src/programmers/misc/MiscCloneProgrammer.ts +774 -774
  149. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  150. package/src/programmers/misc/MiscIsPruneProgrammer.ts +73 -73
  151. package/src/programmers/misc/MiscLiteralsProgrammer.ts +69 -69
  152. package/src/programmers/misc/MiscPruneProgrammer.ts +550 -550
  153. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  154. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +78 -78
  155. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +75 -75
  156. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +66 -66
  157. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +655 -655
  158. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +883 -883
  159. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +85 -85
  160. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +76 -76
  161. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +165 -165
  162. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +75 -75
  163. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +86 -86
  164. package/src/protobuf.ts +881 -881
  165. package/src/schemas/json/IJsonApplication.ts +8 -8
  166. package/src/schemas/json/IJsonComponents.ts +33 -33
  167. package/src/schemas/json/IJsonSchema.ts +110 -110
  168. package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
  169. package/src/schemas/metadata/IMetadata.ts +34 -34
  170. package/src/schemas/metadata/IMetadataAlias.ts +12 -12
  171. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  172. package/src/schemas/metadata/IMetadataArray.ts +7 -7
  173. package/src/schemas/metadata/IMetadataArrayType.ts +10 -10
  174. package/src/schemas/metadata/IMetadataAtomic.ts +6 -6
  175. package/src/schemas/metadata/IMetadataCollection.ts +11 -11
  176. package/src/schemas/metadata/IMetadataConstant.ts +16 -16
  177. package/src/schemas/metadata/IMetadataDictionary.ts +14 -14
  178. package/src/schemas/metadata/IMetadataEntry.ts +6 -6
  179. package/src/schemas/metadata/IMetadataEscaped.ts +6 -6
  180. package/src/schemas/metadata/IMetadataObject.ts +18 -18
  181. package/src/schemas/metadata/IMetadataProperty.ts +9 -9
  182. package/src/schemas/metadata/IMetadataTuple.ts +7 -7
  183. package/src/schemas/metadata/IMetadataTupleType.ts +10 -10
  184. package/src/schemas/metadata/IMetadataTypeTag.ts +8 -8
  185. package/src/schemas/metadata/Metadata.ts +685 -685
  186. package/src/schemas/metadata/MetadataAlias.ts +61 -61
  187. package/src/schemas/metadata/MetadataArray.ts +49 -49
  188. package/src/schemas/metadata/MetadataArrayType.ts +57 -57
  189. package/src/schemas/metadata/MetadataAtomic.ts +42 -42
  190. package/src/schemas/metadata/MetadataConstant.ts +3 -3
  191. package/src/schemas/metadata/MetadataEscaped.ts +51 -51
  192. package/src/schemas/metadata/MetadataObject.ts +139 -139
  193. package/src/schemas/metadata/MetadataProperty.ts +59 -59
  194. package/src/schemas/metadata/MetadataTuple.ts +32 -32
  195. package/src/schemas/metadata/MetadataTupleType.ts +67 -67
  196. package/src/tags/ExclusiveMaximum.ts +13 -13
  197. package/src/tags/ExclusiveMinimum.ts +13 -13
  198. package/src/tags/Format.ts +30 -30
  199. package/src/tags/MaxItems.ts +9 -9
  200. package/src/tags/MaxLength.ts +9 -9
  201. package/src/tags/Maximum.ts +13 -13
  202. package/src/tags/MinItems.ts +9 -9
  203. package/src/tags/MinLength.ts +9 -9
  204. package/src/tags/Minimum.ts +13 -13
  205. package/src/tags/MultipleOf.ts +15 -15
  206. package/src/tags/Pattern.ts +8 -8
  207. package/src/tags/TagBase.ts +68 -68
  208. package/src/tags/Type.ts +27 -27
  209. package/src/tags/index.ts +13 -13
  210. package/src/transform.ts +35 -35
  211. package/src/transformers/CallExpressionTransformer.ts +243 -243
  212. package/src/transformers/FileTransformer.ts +57 -57
  213. package/src/transformers/IProject.ts +15 -15
  214. package/src/transformers/ITransformOptions.ts +62 -62
  215. package/src/transformers/ImportTransformer.ts +66 -66
  216. package/src/transformers/NodeTransformer.ts +13 -13
  217. package/src/transformers/TransformerError.ts +55 -55
  218. package/src/transformers/features/AssertTransformer.ts +11 -11
  219. package/src/transformers/features/CreateAssertTransformer.ts +13 -13
  220. package/src/transformers/features/CreateIsTransformer.ts +11 -11
  221. package/src/transformers/features/CreateRandomTransformer.ts +43 -43
  222. package/src/transformers/features/CreateValidateTransformer.ts +13 -13
  223. package/src/transformers/features/IsTransformer.ts +11 -11
  224. package/src/transformers/features/RandomTransformer.ts +46 -46
  225. package/src/transformers/features/ValidateTransformer.ts +11 -11
  226. package/src/transformers/features/json/JsonApplicationTransformer.ts +118 -118
  227. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  228. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  229. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +10 -10
  230. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  231. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  232. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +10 -10
  233. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  234. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +10 -10
  235. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  236. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  237. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  238. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  239. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  240. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  241. package/src/transformers/features/misc/MetadataTransformer.ts +61 -61
  242. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  243. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  244. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  245. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +10 -10
  246. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +10 -10
  247. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  248. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  249. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  250. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  251. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +10 -10
  252. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +10 -10
  253. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  254. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  255. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +32 -32
  256. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  257. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  258. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  259. package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +12 -12
  260. package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +12 -12
  261. package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +9 -9
  262. package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +9 -9
  263. package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +12 -12
  264. package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +12 -12
  265. package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +12 -12
  266. package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +12 -12
  267. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
  268. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
  269. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +10 -10
  270. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +10 -10
  271. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +10 -10
  272. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +10 -10
  273. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +33 -33
  274. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +12 -12
  275. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +12 -12
  276. package/src/transformers/internal/GenericTransformer.ts +104 -104
  277. package/src/typings/Atomic.ts +18 -18
  278. package/src/typings/ClassProperties.ts +5 -5
  279. package/src/typings/Customizable.ts +5 -5
  280. package/src/typings/OmitNever.ts +3 -3
  281. package/src/typings/ProtobufAtomic.ts +19 -19
  282. package/src/typings/SpecialFields.ts +3 -3
  283. package/src/typings/ValidationPipe.ts +9 -9
  284. package/src/typings/Writable.ts +11 -11
  285. package/src/utils/ArrayUtil.ts +45 -45
  286. package/src/utils/Escaper.ts +46 -46
  287. package/src/utils/MapUtil.ts +12 -12
  288. package/src/utils/NameEncoder.ts +32 -32
  289. package/src/utils/PatternUtil.ts +33 -33
  290. package/src/utils/RandomGenerator.ts +83 -83
  291. package/src/utils/Singleton.ts +17 -17
@@ -1,1137 +1,1137 @@
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
- import { ValueFactory } from "../factories/ValueFactory";
10
-
11
- import { Metadata } from "../schemas/metadata/Metadata";
12
- import { MetadataArray } from "../schemas/metadata/MetadataArray";
13
- import { MetadataObject } from "../schemas/metadata/MetadataObject";
14
- import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
15
- import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
16
-
17
- import { IProject } from "../transformers/IProject";
18
- import { TransformerError } from "../transformers/TransformerError";
19
-
20
- import { FeatureProgrammer } from "./FeatureProgrammer";
21
- import { AtomicPredicator } from "./helpers/AtomicPredicator";
22
- import { FunctionImporter } from "./helpers/FunctionImporeter";
23
- import { ICheckEntry } from "./helpers/ICheckEntry";
24
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
25
- import { OptionPredicator } from "./helpers/OptionPredicator";
26
- import { UnionExplorer } from "./helpers/UnionExplorer";
27
- import { check_array_length } from "./internal/check_array_length";
28
- import { check_bigint } from "./internal/check_bigint";
29
- import { check_native } from "./internal/check_native";
30
- import { check_number } from "./internal/check_number";
31
- import { check_string } from "./internal/check_string";
32
- import { check_template } from "./internal/check_template";
33
- import { decode_union_object } from "./internal/decode_union_object";
34
- import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
35
-
36
- export namespace CheckerProgrammer {
37
- export interface IConfig {
38
- prefix: string;
39
- path: boolean;
40
- trace: boolean;
41
- equals: boolean;
42
- numeric: boolean;
43
- addition?: () => ts.Statement[];
44
- decoder?: () => FeatureProgrammer.Decoder<Metadata, ts.Expression>;
45
- combiner: IConfig.Combiner;
46
- atomist: (
47
- explore: IExplore,
48
- ) => (check: ICheckEntry) => (input: ts.Expression) => ts.Expression;
49
- joiner: IConfig.IJoiner;
50
- success: ts.Expression;
51
- }
52
- export namespace IConfig {
53
- export interface Combiner {
54
- (explorer: IExplore): {
55
- (logic: "and" | "or"): {
56
- (
57
- input: ts.Expression,
58
- binaries: IBinary[],
59
- expected: string,
60
- ): ts.Expression;
61
- };
62
- };
63
- }
64
- export interface IJoiner {
65
- object(
66
- input: ts.Expression,
67
- entries: IExpressionEntry[],
68
- ): ts.Expression;
69
- array(input: ts.Expression, arrow: ts.ArrowFunction): ts.Expression;
70
- tuple?(exprs: ts.Expression[]): ts.Expression;
71
-
72
- failure(
73
- value: ts.Expression,
74
- expected: string,
75
- explore?: FeatureProgrammer.IExplore,
76
- ): ts.Expression;
77
- is?(expression: ts.Expression): ts.Expression;
78
- required?(exp: ts.Expression): ts.Expression;
79
- full?: (
80
- condition: ts.Expression,
81
- ) => (
82
- input: ts.Expression,
83
- expected: string,
84
- explore: IExplore,
85
- ) => ts.Expression;
86
- }
87
- }
88
- export type IExplore = FeatureProgrammer.IExplore;
89
-
90
- export interface IBinary {
91
- expression: ts.Expression;
92
- combined: boolean;
93
- }
94
-
95
- /* -----------------------------------------------------------
96
- WRITERS
97
- ----------------------------------------------------------- */
98
- export const write =
99
- (project: IProject) =>
100
- (config: IConfig) =>
101
- (importer: FunctionImporter) =>
102
- FeatureProgrammer.write(project)(
103
- configure(project)(config)(importer),
104
- )(importer);
105
-
106
- export const write_object_functions =
107
- (project: IProject) =>
108
- (config: IConfig) =>
109
- (importer: FunctionImporter) =>
110
- FeatureProgrammer.write_object_functions(
111
- configure(project)(config)(importer),
112
- )(importer);
113
-
114
- export const write_union_functions =
115
- (project: IProject) =>
116
- (config: IConfig) =>
117
- (importer: FunctionImporter) =>
118
- FeatureProgrammer.write_union_functions(
119
- configure(project)({ ...config, numeric: false })(importer),
120
- );
121
-
122
- export const write_array_functions =
123
- (project: IProject) =>
124
- (config: IConfig) =>
125
- (importer: FunctionImporter) =>
126
- (collection: MetadataCollection): ts.VariableStatement[] =>
127
- collection
128
- .arrays()
129
- .filter((a) => a.recursive)
130
- .map((type, i) =>
131
- StatementFactory.constant(
132
- `${config.prefix}a${i}`,
133
- ts.factory.createArrowFunction(
134
- undefined,
135
- undefined,
136
- FeatureProgrammer.parameterDeclarations(config)(
137
- TypeFactory.keyword("any"),
138
- )(ts.factory.createIdentifier("input")),
139
- TypeFactory.keyword("any"),
140
- undefined,
141
- decode_array_inline(project)(config)(importer)(
142
- ts.factory.createIdentifier("input"),
143
- MetadataArray.create({
144
- type,
145
- tags: [],
146
- }),
147
- {
148
- tracable: config.trace,
149
- source: "function",
150
- from: "array",
151
- postfix: "",
152
- },
153
- ),
154
- ),
155
- ),
156
- );
157
-
158
- export const write_tuple_functions =
159
- (project: IProject) =>
160
- (config: IConfig) =>
161
- (importer: FunctionImporter) =>
162
- (collection: MetadataCollection): ts.VariableStatement[] =>
163
- collection
164
- .tuples()
165
- .filter((t) => t.recursive)
166
- .map((tuple, i) =>
167
- StatementFactory.constant(
168
- `${config.prefix}t${i}`,
169
- ts.factory.createArrowFunction(
170
- undefined,
171
- undefined,
172
- FeatureProgrammer.parameterDeclarations(config)(
173
- TypeFactory.keyword("any"),
174
- )(ts.factory.createIdentifier("input")),
175
- TypeFactory.keyword("any"),
176
- undefined,
177
- decode_tuple_inline(project)(config)(importer)(
178
- ts.factory.createIdentifier("input"),
179
- tuple,
180
- {
181
- tracable: config.trace,
182
- source: "function",
183
- from: "array",
184
- postfix: "",
185
- },
186
- ),
187
- ),
188
- ),
189
- );
190
-
191
- const configure =
192
- (project: IProject) =>
193
- (config: IConfig) =>
194
- (importer: FunctionImporter): FeatureProgrammer.IConfig => ({
195
- types: {
196
- input: () => TypeFactory.keyword("any"),
197
- output: (type, name) =>
198
- ts.factory.createTypePredicateNode(
199
- undefined,
200
- "input",
201
- ts.factory.createTypeReferenceNode(
202
- name ??
203
- TypeFactory.getFullName(project.checker)(type),
204
- ),
205
- ),
206
- },
207
- trace: config.trace,
208
- path: config.path,
209
- prefix: config.prefix,
210
- initializer:
211
- ({ checker }) =>
212
- (importer) =>
213
- (type) => {
214
- const collection: MetadataCollection =
215
- new MetadataCollection();
216
- const result = MetadataFactory.analyze(checker)({
217
- escape: false,
218
- constant: true,
219
- absorb: true,
220
- })(collection)(type);
221
- if (result.success === false)
222
- throw TransformerError.from(`typia.${importer.method}`)(
223
- result.errors,
224
- );
225
- return [collection, result.data];
226
- },
227
- addition: config.addition,
228
- decoder: () =>
229
- config.decoder?.() ?? decode(project)(config)(importer),
230
- objector: {
231
- checker: () =>
232
- config.decoder?.() ?? decode(project)(config)(importer),
233
- decoder: () => decode_object(config)(importer),
234
- joiner: config.joiner.object,
235
- unionizer: config.equals
236
- ? decode_union_object(decode_object(config)(importer))(
237
- (input, obj, explore) =>
238
- decode_object(config)(importer)(input, obj, {
239
- ...explore,
240
- tracable: true,
241
- }),
242
- )(config.joiner.is ?? ((expr) => expr))(
243
- (value, expected) =>
244
- ts.factory.createReturnStatement(
245
- config.joiner.failure(value, expected),
246
- ),
247
- )
248
- : (input, targets, explore) =>
249
- config.combiner(explore)("or")(
250
- input,
251
- targets.map((obj) => ({
252
- expression: decode_object(config)(importer)(
253
- input,
254
- obj,
255
- explore,
256
- ),
257
- combined: true,
258
- })),
259
- `(${targets.map((t) => t.name).join(" | ")})`,
260
- ),
261
- failure: (value, expected) =>
262
- ts.factory.createReturnStatement(
263
- config.joiner.failure(value, expected),
264
- ),
265
- is: config.joiner.is,
266
- required: config.joiner.required,
267
- full: config.joiner.full,
268
- type: TypeFactory.keyword("boolean"),
269
- },
270
- generator: {
271
- unions: config.numeric
272
- ? () =>
273
- FeatureProgrammer.write_union_functions(
274
- configure(project)({ ...config, numeric: false })(
275
- importer,
276
- ),
277
- )
278
- : undefined,
279
- arrays: () => write_array_functions(project)(config)(importer),
280
- tuples: () => write_tuple_functions(project)(config)(importer),
281
- },
282
- });
283
-
284
- /* -----------------------------------------------------------
285
- DECODERS
286
- ----------------------------------------------------------- */
287
- /**
288
- * @internal
289
- */
290
- export const decode =
291
- (project: IProject) =>
292
- (config: IConfig) =>
293
- (importer: FunctionImporter) =>
294
- (
295
- input: ts.Expression,
296
- meta: Metadata,
297
- explore: IExplore,
298
- ): ts.Expression => {
299
- if (meta.any) return config.success;
300
-
301
- const top: IBinary[] = [];
302
- const binaries: IBinary[] = [];
303
- const add = create_add(binaries)(input);
304
- const getConstantValue = (
305
- value: number | string | bigint | boolean,
306
- ) => {
307
- if (typeof value === "string")
308
- return ts.factory.createStringLiteral(value);
309
- else if (typeof value === "bigint")
310
- return ExpressionFactory.bigint(value);
311
- return ts.factory.createIdentifier(value.toString());
312
- };
313
-
314
- //----
315
- // CHECK OPTIONAL
316
- //----
317
- // @todo -> should be elaborated
318
- const checkOptional: boolean = meta.empty() || meta.isUnionBucket();
319
-
320
- // NULLABLE
321
- if (checkOptional || meta.nullable)
322
- (meta.nullable ? add : create_add(top)(input))(
323
- meta.nullable,
324
- ValueFactory.NULL(),
325
- );
326
-
327
- // UNDEFINDABLE
328
- if (checkOptional || !meta.isRequired())
329
- (meta.isRequired() ? create_add(top)(input) : add)(
330
- !meta.isRequired(),
331
- ValueFactory.UNDEFINED(),
332
- );
333
-
334
- // FUNCTIONAL
335
- if (meta.functional === true)
336
- if (
337
- OptionPredicator.functional(project.options) ||
338
- meta.size() !== 1
339
- )
340
- add(
341
- true,
342
- ts.factory.createStringLiteral("function"),
343
- ValueFactory.TYPEOF(input),
344
- );
345
- else
346
- binaries.push({
347
- combined: false,
348
- expression: config.success,
349
- });
350
-
351
- //----
352
- // VALUES
353
- //----
354
- // CONSTANT VALUES
355
- for (const constant of meta.constants)
356
- if (AtomicPredicator.constant(meta)(constant.type))
357
- for (const val of constant.values)
358
- add(true, getConstantValue(val));
359
-
360
- // ATOMIC VALUES
361
- for (const atom of meta.atomics)
362
- if (AtomicPredicator.atomic(meta)(atom.type) === false)
363
- continue;
364
- else if (atom.type === "number")
365
- binaries.push({
366
- expression: config.atomist(explore)(
367
- check_number(project, config.numeric)(atom)(input),
368
- )(input),
369
- combined: false,
370
- });
371
- else if (atom.type === "bigint")
372
- binaries.push({
373
- expression: config.atomist(explore)(
374
- check_bigint(project)(atom)(input),
375
- )(input),
376
- combined: false,
377
- });
378
- else if (atom.type === "string")
379
- binaries.push({
380
- expression: config.atomist(explore)(
381
- check_string(project)(atom)(input),
382
- )(input),
383
- combined: false,
384
- });
385
- else
386
- add(
387
- true,
388
- ts.factory.createStringLiteral(atom.type),
389
- ValueFactory.TYPEOF(input),
390
- );
391
-
392
- // TEMPLATE LITERAL VALUES
393
- if (meta.templates.length)
394
- if (AtomicPredicator.template(meta))
395
- binaries.push({
396
- expression: config.atomist(explore)(
397
- check_template(meta.templates)(input),
398
- )(input),
399
- combined: false,
400
- });
401
-
402
- // NATIVE CLASSES
403
- for (const native of meta.natives)
404
- binaries.push({
405
- expression: check_native(native)(input),
406
- combined: false,
407
- });
408
-
409
- //----
410
- // INSTANCES
411
- //----
412
- interface IInstance {
413
- pre: ts.Expression;
414
- body: ts.Expression | null;
415
- expected: string;
416
- }
417
- const instances: IInstance[] = [];
418
- const prepare =
419
- (pre: ts.Expression, expected: string) =>
420
- (body: ts.Expression | null) =>
421
- instances.push({
422
- pre,
423
- expected,
424
- body,
425
- });
426
-
427
- // SETS
428
- if (meta.sets.length) {
429
- const install = prepare(
430
- check_native("Set")(input),
431
- meta.sets
432
- .map((elem) => `Set<${elem.getName()}>`)
433
- .join(" | "),
434
- );
435
- if (meta.sets.some((elem) => elem.any)) install(null);
436
- else
437
- install(
438
- explore_sets(project)(config)(importer)(
439
- input,
440
- meta.sets,
441
- {
442
- ...explore,
443
- from: "array",
444
- },
445
- ),
446
- );
447
- }
448
-
449
- // MAPS
450
- if (meta.maps.length) {
451
- const install = prepare(
452
- check_native("Map")(input),
453
- meta.maps
454
- .map(({ key, value }) => `Map<${key}, ${value}>`)
455
- .join(" | "),
456
- );
457
- if (meta.maps.some((elem) => elem.key.any && elem.value.any))
458
- install(null);
459
- else
460
- install(
461
- explore_maps(project)(config)(importer)(
462
- input,
463
- meta.maps,
464
- {
465
- ...explore,
466
- from: "array",
467
- },
468
- ),
469
- );
470
- }
471
-
472
- // ARRAYS AND TUPLES
473
- if (meta.tuples.length + meta.arrays.length > 0) {
474
- const install = prepare(
475
- config.atomist(explore)({
476
- expected: [
477
- ...meta.tuples.map((t) => t.type.name),
478
- ...meta.arrays.map((a) => a.getName()),
479
- ].join(" | "),
480
- expression: ExpressionFactory.isArray(input),
481
- conditions: [],
482
- })(input),
483
- [...meta.tuples, ...meta.arrays]
484
- .map((elem) => elem.type.name)
485
- .join(" | "),
486
- );
487
- if (meta.arrays.length === 0)
488
- if (meta.tuples.length === 1)
489
- install(
490
- decode_tuple(project)(config)(importer)(
491
- input,
492
- meta.tuples[0]!,
493
- {
494
- ...explore,
495
- from: "array",
496
- },
497
- ),
498
- );
499
- // TUPLE ONLY
500
- else
501
- install(
502
- explore_tuples(project)(config)(importer)(
503
- input,
504
- meta.tuples,
505
- {
506
- ...explore,
507
- from: "array",
508
- },
509
- ),
510
- );
511
- else if (meta.arrays.some((elem) => elem.type.value.any))
512
- install(null);
513
- else if (meta.tuples.length === 0)
514
- if (meta.arrays.length === 1)
515
- // ARRAY ONLY
516
- install(
517
- decode_array(project)(config)(importer)(
518
- input,
519
- meta.arrays[0]!,
520
- {
521
- ...explore,
522
- from: "array",
523
- },
524
- ),
525
- );
526
- else
527
- install(
528
- explore_arrays(project)(config)(importer)(
529
- input,
530
- meta.arrays,
531
- {
532
- ...explore,
533
- from: "array",
534
- },
535
- ),
536
- );
537
- else
538
- install(
539
- explore_arrays_and_tuples(project)(config)(importer)(
540
- input,
541
- [...meta.tuples, ...meta.arrays],
542
- explore,
543
- ),
544
- );
545
- }
546
-
547
- // OBJECT
548
- if (meta.objects.length > 0)
549
- prepare(
550
- ExpressionFactory.isObject({
551
- checkNull: true,
552
- checkArray: meta.objects.some((obj) =>
553
- obj.properties.every(
554
- (prop) =>
555
- !prop.key.isSoleLiteral() ||
556
- !prop.value.isRequired(),
557
- ),
558
- ),
559
- })(input),
560
- meta.objects.map((obj) => obj.name).join(" | "),
561
- )(
562
- explore_objects(config)(importer)(input, meta, {
563
- ...explore,
564
- from: "object",
565
- }),
566
- );
567
-
568
- if (instances.length) {
569
- const transformer =
570
- (
571
- merger: (
572
- x: ts.Expression,
573
- y: ts.Expression,
574
- ) => ts.Expression,
575
- ) =>
576
- (ins: IInstance) =>
577
- ins.body
578
- ? {
579
- expression: merger(ins.pre, ins.body),
580
- combined: true,
581
- }
582
- : {
583
- expression: ins.pre,
584
- combined: false,
585
- };
586
- if (instances.length === 1)
587
- binaries.push(
588
- transformer((pre, body) =>
589
- config.combiner(explore)("and")(
590
- input,
591
- [pre, body].map((expression) => ({
592
- expression,
593
- combined: expression !== pre,
594
- })),
595
- meta.getName(),
596
- ),
597
- )(instances[0]!),
598
- );
599
- else
600
- binaries.push({
601
- expression: config.combiner(explore)("or")(
602
- input,
603
- instances.map(
604
- transformer(ts.factory.createLogicalAnd),
605
- ),
606
- meta.getName(),
607
- ),
608
- combined: true,
609
- });
610
- }
611
-
612
- //----
613
- // COMBINE CONDITIONS
614
- //----
615
- return top.length && binaries.length
616
- ? config.combiner(explore)("and")(
617
- input,
618
- [
619
- ...top,
620
- {
621
- expression: config.combiner(explore)("or")(
622
- input,
623
- binaries,
624
- meta.getName(),
625
- ),
626
- combined: true,
627
- },
628
- ],
629
- meta.getName(),
630
- )
631
- : binaries.length
632
- ? config.combiner(explore)("or")(
633
- input,
634
- binaries,
635
- meta.getName(),
636
- )
637
- : config.success;
638
- };
639
-
640
- export const decode_object =
641
- (config: IConfig) => (importer: FunctionImporter) => {
642
- const func = FeatureProgrammer.decode_object(config)(importer);
643
- return (
644
- input: ts.Expression,
645
- obj: MetadataObject,
646
- explore: IExplore,
647
- ) => {
648
- obj.validated = true;
649
- return func(input, obj, explore);
650
- };
651
- };
652
-
653
- const decode_array =
654
- (project: IProject) =>
655
- (config: IConfig) =>
656
- (importer: FunctionImporter) =>
657
- (input: ts.Expression, array: MetadataArray, explore: IExplore) => {
658
- if (array.type.recursive === false)
659
- return decode_array_inline(project)(config)(importer)(
660
- input,
661
- array,
662
- explore,
663
- );
664
-
665
- explore = {
666
- ...explore,
667
- source: "function",
668
- from: "array",
669
- };
670
- return ts.factory.createLogicalOr(
671
- ts.factory.createCallExpression(
672
- ts.factory.createIdentifier(
673
- importer.useLocal(
674
- `${config.prefix}a${array.type.index}`,
675
- ),
676
- ),
677
- undefined,
678
- FeatureProgrammer.argumentsArray(config)({
679
- ...explore,
680
- source: "function",
681
- from: "array",
682
- })(input),
683
- ),
684
- config.joiner.failure(input, array.type.name, explore),
685
- );
686
- };
687
-
688
- const decode_array_inline =
689
- (project: IProject) =>
690
- (config: IConfig) =>
691
- (importer: FunctionImporter) =>
692
- (
693
- input: ts.Expression,
694
- array: MetadataArray,
695
- explore: IExplore,
696
- ): ts.Expression => {
697
- const length = check_array_length(project)(array)(input);
698
- const main = FeatureProgrammer.decode_array({
699
- prefix: config.prefix,
700
- trace: config.trace,
701
- path: config.path,
702
- decoder: () => decode(project)(config)(importer),
703
- })(importer)(config.joiner.array)(input, array, explore);
704
- return length.expression === null && length.conditions.length === 0
705
- ? main
706
- : ts.factory.createLogicalAnd(
707
- config.atomist(explore)(length)(input),
708
- main,
709
- );
710
- };
711
-
712
- const decode_tuple =
713
- (project: IProject) =>
714
- (config: IConfig) =>
715
- (importer: FunctionImporter) =>
716
- (
717
- input: ts.Expression,
718
- tuple: MetadataTuple,
719
- explore: IExplore,
720
- ): ts.Expression => {
721
- if (tuple.type.recursive === false)
722
- return decode_tuple_inline(project)(config)(importer)(
723
- input,
724
- tuple.type,
725
- explore,
726
- );
727
- explore = {
728
- ...explore,
729
- source: "function",
730
- from: "array",
731
- };
732
- return ts.factory.createLogicalOr(
733
- ts.factory.createCallExpression(
734
- ts.factory.createIdentifier(
735
- importer.useLocal(
736
- `${config.prefix}t${tuple.type.index}`,
737
- ),
738
- ),
739
- undefined,
740
- FeatureProgrammer.argumentsArray(config)({
741
- ...explore,
742
- source: "function",
743
- })(input),
744
- ),
745
- config.joiner.failure(input, tuple.type.name, explore),
746
- );
747
- };
748
-
749
- const decode_tuple_inline =
750
- (project: IProject) =>
751
- (config: IConfig) =>
752
- (importer: FunctionImporter) =>
753
- (
754
- input: ts.Expression,
755
- tuple: MetadataTupleType,
756
- explore: IExplore,
757
- ): ts.Expression => {
758
- const binaries: ts.Expression[] = tuple.elements
759
- .filter((meta) => meta.rest === null)
760
- .map((meta, index) =>
761
- decode(project)(config)(importer)(
762
- ts.factory.createElementAccessExpression(input, index),
763
- meta,
764
- {
765
- ...explore,
766
- from: "array",
767
- postfix: explore.postfix.length
768
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
769
- : `"[${index}]"`,
770
- },
771
- ),
772
- );
773
- const rest: ts.Expression | null =
774
- tuple.elements.length && tuple.elements.at(-1)!.rest !== null
775
- ? decode(project)(config)(importer)(
776
- ts.factory.createCallExpression(
777
- IdentifierFactory.access(input)("slice"),
778
- undefined,
779
- [
780
- ts.factory.createNumericLiteral(
781
- tuple.elements.length - 1,
782
- ),
783
- ],
784
- ),
785
- wrap_metadata_rest_tuple(
786
- tuple.elements.at(-1)!.rest!,
787
- ),
788
- {
789
- ...explore,
790
- start: tuple.elements.length - 1,
791
- },
792
- )
793
- : null;
794
-
795
- const arrayLength = ts.factory.createPropertyAccessExpression(
796
- input,
797
- "length",
798
- );
799
- return config.combiner(explore)("and")(
800
- input,
801
- [
802
- ...(rest === null
803
- ? tuple.elements.every((t) => t.optional === false)
804
- ? [
805
- {
806
- combined: false,
807
- expression:
808
- ts.factory.createStrictEquality(
809
- arrayLength,
810
- ts.factory.createNumericLiteral(
811
- tuple.elements.length,
812
- ),
813
- ),
814
- },
815
- ]
816
- : [
817
- {
818
- combined: false,
819
- expression: ts.factory.createLogicalAnd(
820
- ts.factory.createLessThanEquals(
821
- ts.factory.createNumericLiteral(
822
- tuple.elements.filter(
823
- (t) =>
824
- t.optional === false,
825
- ).length,
826
- ),
827
- arrayLength,
828
- ),
829
- ts.factory.createGreaterThanEquals(
830
- ts.factory.createNumericLiteral(
831
- tuple.elements.length,
832
- ),
833
- arrayLength,
834
- ),
835
- ),
836
- },
837
- ]
838
- : []),
839
- ...(config.joiner.tuple
840
- ? [
841
- {
842
- expression: config.joiner.tuple(binaries),
843
- combined: true,
844
- },
845
- ]
846
- : binaries.map((expression) => ({
847
- expression,
848
- combined: true,
849
- }))),
850
- ...(rest !== null
851
- ? [
852
- {
853
- expression: rest,
854
- combined: true,
855
- },
856
- ]
857
- : []),
858
- ],
859
- `[${tuple.elements.map((t) => t.getName()).join(", ")}]`,
860
- );
861
- };
862
-
863
- /* -----------------------------------------------------------
864
- UNION TYPE EXPLORERS
865
- ----------------------------------------------------------- */
866
- const explore_sets =
867
- (project: IProject) =>
868
- (config: IConfig) =>
869
- (importer: FunctionImporter) =>
870
- (
871
- input: ts.Expression,
872
- sets: Metadata[],
873
- explore: IExplore,
874
- ): ts.Expression =>
875
- ts.factory.createCallExpression(
876
- UnionExplorer.set({
877
- checker: decode(project)(config)(importer),
878
- decoder: decode_array(project)(config)(importer),
879
- empty: config.success,
880
- success: config.success,
881
- failure: (input, expected, explore) =>
882
- ts.factory.createReturnStatement(
883
- config.joiner.failure(input, expected, explore),
884
- ),
885
- })([])(input, sets, explore),
886
- undefined,
887
- undefined,
888
- );
889
-
890
- const explore_maps =
891
- (project: IProject) =>
892
- (config: IConfig) =>
893
- (importer: FunctionImporter) =>
894
- (
895
- input: ts.Expression,
896
- maps: Metadata.Entry[],
897
- explore: IExplore,
898
- ): ts.Expression =>
899
- ts.factory.createCallExpression(
900
- UnionExplorer.map({
901
- checker: (input, entry, explore) => {
902
- const func = decode(project)(config)(importer);
903
- return ts.factory.createLogicalAnd(
904
- func(
905
- ts.factory.createElementAccessExpression(
906
- input,
907
- 0,
908
- ),
909
- entry[0],
910
- {
911
- ...explore,
912
- postfix: `${explore.postfix}[0]`,
913
- },
914
- ),
915
- func(
916
- ts.factory.createElementAccessExpression(
917
- input,
918
- 1,
919
- ),
920
- entry[1],
921
- {
922
- ...explore,
923
- postfix: `${explore.postfix}[1]`,
924
- },
925
- ),
926
- );
927
- },
928
- decoder: decode_array(project)(config)(importer),
929
- empty: config.success,
930
- success: config.success,
931
- failure: (input, expected, explore) =>
932
- ts.factory.createReturnStatement(
933
- config.joiner.failure(input, expected, explore),
934
- ),
935
- })([])(input, maps, explore),
936
- undefined,
937
- undefined,
938
- );
939
-
940
- const explore_tuples =
941
- (project: IProject) =>
942
- (config: IConfig) =>
943
- (importer: FunctionImporter) =>
944
- (
945
- input: ts.Expression,
946
- tuples: MetadataTuple[],
947
- explore: IExplore,
948
- ): ts.Expression =>
949
- explore_array_like_union_types(config)(importer)(
950
- UnionExplorer.tuple({
951
- checker: decode_tuple(project)(config)(importer),
952
- decoder: decode_tuple(project)(config)(importer),
953
- empty: config.success,
954
- success: config.success,
955
- failure: (input, expected, explore) =>
956
- ts.factory.createReturnStatement(
957
- config.joiner.failure(input, expected, explore),
958
- ),
959
- }),
960
- )(input, tuples, explore);
961
-
962
- const explore_arrays =
963
- (project: IProject) =>
964
- (config: IConfig) =>
965
- (importer: FunctionImporter) =>
966
- (
967
- input: ts.Expression,
968
- arrays: MetadataArray[],
969
- explore: IExplore,
970
- ): ts.Expression =>
971
- explore_array_like_union_types(config)(importer)(
972
- UnionExplorer.array({
973
- checker: decode(project)(config)(importer),
974
- decoder: decode_array(project)(config)(importer),
975
- empty: config.success,
976
- success: config.success,
977
- failure: (input, expected, explore) =>
978
- ts.factory.createReturnStatement(
979
- config.joiner.failure(input, expected, explore),
980
- ),
981
- }),
982
- )(input, arrays, explore);
983
-
984
- const explore_arrays_and_tuples =
985
- (project: IProject) =>
986
- (config: IConfig) =>
987
- (importer: FunctionImporter) =>
988
- (
989
- input: ts.Expression,
990
- elements: Array<MetadataArray | MetadataTuple>,
991
- explore: IExplore,
992
- ): ts.Expression =>
993
- explore_array_like_union_types(config)(importer)(
994
- UnionExplorer.array_or_tuple({
995
- checker: (front, target, explore, array) =>
996
- target instanceof MetadataTuple
997
- ? decode_tuple(project)(config)(importer)(
998
- front,
999
- target,
1000
- explore,
1001
- )
1002
- : config.atomist(explore)({
1003
- expected: elements
1004
- .map((elem) =>
1005
- elem instanceof MetadataArray
1006
- ? elem.getName()
1007
- : elem.type.name,
1008
- )
1009
- .join(" | "),
1010
- expression: decode(project)(config)(importer)(
1011
- front,
1012
- target,
1013
- explore,
1014
- ),
1015
- conditions: [],
1016
- })(array),
1017
- decoder: (input, target, explore) =>
1018
- target instanceof MetadataTuple
1019
- ? decode_tuple(project)(config)(importer)(
1020
- input,
1021
- target,
1022
- explore,
1023
- )
1024
- : decode_array(project)(config)(importer)(
1025
- input,
1026
- target,
1027
- explore,
1028
- ),
1029
- empty: config.success,
1030
- success: config.success,
1031
- failure: (input, expected, explore) =>
1032
- ts.factory.createReturnStatement(
1033
- config.joiner.failure(input, expected, explore),
1034
- ),
1035
- }),
1036
- )(input, elements, explore);
1037
-
1038
- const explore_array_like_union_types =
1039
- (config: IConfig) =>
1040
- (importer: FunctionImporter) =>
1041
- <T extends MetadataArray | MetadataTuple>(
1042
- factory: (
1043
- parameters: ts.ParameterDeclaration[],
1044
- ) => (
1045
- input: ts.Expression,
1046
- elements: T[],
1047
- explore: IExplore,
1048
- ) => ts.ArrowFunction,
1049
- ) =>
1050
- (
1051
- input: ts.Expression,
1052
- elements: T[],
1053
- explore: IExplore,
1054
- ): ts.Expression => {
1055
- const arrow =
1056
- (parameters: ts.ParameterDeclaration[]) =>
1057
- (explore: IExplore) =>
1058
- (input: ts.Expression): ts.ArrowFunction =>
1059
- factory(parameters)(input, elements, explore);
1060
- if (elements.every((e) => e.type.recursive === false))
1061
- ts.factory.createCallExpression(
1062
- arrow([])(explore)(input),
1063
- undefined,
1064
- [],
1065
- );
1066
- explore = {
1067
- ...explore,
1068
- source: "function",
1069
- from: "array",
1070
- };
1071
- return ts.factory.createLogicalOr(
1072
- ts.factory.createCallExpression(
1073
- ts.factory.createIdentifier(
1074
- importer.emplaceUnion(
1075
- config.prefix,
1076
- elements.map((e) => e.type.name).join(" | "),
1077
- () =>
1078
- arrow(
1079
- FeatureProgrammer.parameterDeclarations(
1080
- config,
1081
- )(TypeFactory.keyword("any"))(
1082
- ts.factory.createIdentifier("input"),
1083
- ),
1084
- )({
1085
- ...explore,
1086
- postfix: "",
1087
- })(ts.factory.createIdentifier("input")),
1088
- ),
1089
- ),
1090
- undefined,
1091
- FeatureProgrammer.argumentsArray(config)(explore)(input),
1092
- ),
1093
- config.joiner.failure(
1094
- input,
1095
- elements.map((e) => e.type.name).join(" | "),
1096
- explore,
1097
- ),
1098
- );
1099
- };
1100
-
1101
- const explore_objects =
1102
- (config: IConfig) =>
1103
- (importer: FunctionImporter) =>
1104
- (input: ts.Expression, meta: Metadata, explore: IExplore) =>
1105
- meta.objects.length === 1
1106
- ? decode_object(config)(importer)(
1107
- input,
1108
- meta.objects[0]!,
1109
- explore,
1110
- )
1111
- : ts.factory.createCallExpression(
1112
- ts.factory.createIdentifier(
1113
- importer.useLocal(
1114
- `${config.prefix}u${meta.union_index!}`,
1115
- ),
1116
- ),
1117
- undefined,
1118
- FeatureProgrammer.argumentsArray(config)(explore)(input),
1119
- );
1120
- }
1121
-
1122
- const create_add =
1123
- (binaries: CheckerProgrammer.IBinary[]) =>
1124
- (defaultInput: ts.Expression) =>
1125
- (
1126
- exact: boolean,
1127
- left: ts.Expression,
1128
- right: ts.Expression = defaultInput,
1129
- ) => {
1130
- const factory = exact
1131
- ? ts.factory.createStrictEquality
1132
- : ts.factory.createStrictInequality;
1133
- binaries.push({
1134
- expression: factory(left, right),
1135
- combined: false,
1136
- });
1137
- };
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
+ import { ValueFactory } from "../factories/ValueFactory";
10
+
11
+ import { Metadata } from "../schemas/metadata/Metadata";
12
+ import { MetadataArray } from "../schemas/metadata/MetadataArray";
13
+ import { MetadataObject } from "../schemas/metadata/MetadataObject";
14
+ import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
15
+ import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
16
+
17
+ import { IProject } from "../transformers/IProject";
18
+ import { TransformerError } from "../transformers/TransformerError";
19
+
20
+ import { FeatureProgrammer } from "./FeatureProgrammer";
21
+ import { AtomicPredicator } from "./helpers/AtomicPredicator";
22
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
23
+ import { ICheckEntry } from "./helpers/ICheckEntry";
24
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
25
+ import { OptionPredicator } from "./helpers/OptionPredicator";
26
+ import { UnionExplorer } from "./helpers/UnionExplorer";
27
+ import { check_array_length } from "./internal/check_array_length";
28
+ import { check_bigint } from "./internal/check_bigint";
29
+ import { check_native } from "./internal/check_native";
30
+ import { check_number } from "./internal/check_number";
31
+ import { check_string } from "./internal/check_string";
32
+ import { check_template } from "./internal/check_template";
33
+ import { decode_union_object } from "./internal/decode_union_object";
34
+ import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
35
+
36
+ export namespace CheckerProgrammer {
37
+ export interface IConfig {
38
+ prefix: string;
39
+ path: boolean;
40
+ trace: boolean;
41
+ equals: boolean;
42
+ numeric: boolean;
43
+ addition?: () => ts.Statement[];
44
+ decoder?: () => FeatureProgrammer.Decoder<Metadata, ts.Expression>;
45
+ combiner: IConfig.Combiner;
46
+ atomist: (
47
+ explore: IExplore,
48
+ ) => (check: ICheckEntry) => (input: ts.Expression) => ts.Expression;
49
+ joiner: IConfig.IJoiner;
50
+ success: ts.Expression;
51
+ }
52
+ export namespace IConfig {
53
+ export interface Combiner {
54
+ (explorer: IExplore): {
55
+ (logic: "and" | "or"): {
56
+ (
57
+ input: ts.Expression,
58
+ binaries: IBinary[],
59
+ expected: string,
60
+ ): ts.Expression;
61
+ };
62
+ };
63
+ }
64
+ export interface IJoiner {
65
+ object(
66
+ input: ts.Expression,
67
+ entries: IExpressionEntry[],
68
+ ): ts.Expression;
69
+ array(input: ts.Expression, arrow: ts.ArrowFunction): ts.Expression;
70
+ tuple?(exprs: ts.Expression[]): ts.Expression;
71
+
72
+ failure(
73
+ value: ts.Expression,
74
+ expected: string,
75
+ explore?: FeatureProgrammer.IExplore,
76
+ ): ts.Expression;
77
+ is?(expression: ts.Expression): ts.Expression;
78
+ required?(exp: ts.Expression): ts.Expression;
79
+ full?: (
80
+ condition: ts.Expression,
81
+ ) => (
82
+ input: ts.Expression,
83
+ expected: string,
84
+ explore: IExplore,
85
+ ) => ts.Expression;
86
+ }
87
+ }
88
+ export type IExplore = FeatureProgrammer.IExplore;
89
+
90
+ export interface IBinary {
91
+ expression: ts.Expression;
92
+ combined: boolean;
93
+ }
94
+
95
+ /* -----------------------------------------------------------
96
+ WRITERS
97
+ ----------------------------------------------------------- */
98
+ export const write =
99
+ (project: IProject) =>
100
+ (config: IConfig) =>
101
+ (importer: FunctionImporter) =>
102
+ FeatureProgrammer.write(project)(
103
+ configure(project)(config)(importer),
104
+ )(importer);
105
+
106
+ export const write_object_functions =
107
+ (project: IProject) =>
108
+ (config: IConfig) =>
109
+ (importer: FunctionImporter) =>
110
+ FeatureProgrammer.write_object_functions(
111
+ configure(project)(config)(importer),
112
+ )(importer);
113
+
114
+ export const write_union_functions =
115
+ (project: IProject) =>
116
+ (config: IConfig) =>
117
+ (importer: FunctionImporter) =>
118
+ FeatureProgrammer.write_union_functions(
119
+ configure(project)({ ...config, numeric: false })(importer),
120
+ );
121
+
122
+ export const write_array_functions =
123
+ (project: IProject) =>
124
+ (config: IConfig) =>
125
+ (importer: FunctionImporter) =>
126
+ (collection: MetadataCollection): ts.VariableStatement[] =>
127
+ collection
128
+ .arrays()
129
+ .filter((a) => a.recursive)
130
+ .map((type, i) =>
131
+ StatementFactory.constant(
132
+ `${config.prefix}a${i}`,
133
+ ts.factory.createArrowFunction(
134
+ undefined,
135
+ undefined,
136
+ FeatureProgrammer.parameterDeclarations(config)(
137
+ TypeFactory.keyword("any"),
138
+ )(ts.factory.createIdentifier("input")),
139
+ TypeFactory.keyword("any"),
140
+ undefined,
141
+ decode_array_inline(project)(config)(importer)(
142
+ ts.factory.createIdentifier("input"),
143
+ MetadataArray.create({
144
+ type,
145
+ tags: [],
146
+ }),
147
+ {
148
+ tracable: config.trace,
149
+ source: "function",
150
+ from: "array",
151
+ postfix: "",
152
+ },
153
+ ),
154
+ ),
155
+ ),
156
+ );
157
+
158
+ export const write_tuple_functions =
159
+ (project: IProject) =>
160
+ (config: IConfig) =>
161
+ (importer: FunctionImporter) =>
162
+ (collection: MetadataCollection): ts.VariableStatement[] =>
163
+ collection
164
+ .tuples()
165
+ .filter((t) => t.recursive)
166
+ .map((tuple, i) =>
167
+ StatementFactory.constant(
168
+ `${config.prefix}t${i}`,
169
+ ts.factory.createArrowFunction(
170
+ undefined,
171
+ undefined,
172
+ FeatureProgrammer.parameterDeclarations(config)(
173
+ TypeFactory.keyword("any"),
174
+ )(ts.factory.createIdentifier("input")),
175
+ TypeFactory.keyword("any"),
176
+ undefined,
177
+ decode_tuple_inline(project)(config)(importer)(
178
+ ts.factory.createIdentifier("input"),
179
+ tuple,
180
+ {
181
+ tracable: config.trace,
182
+ source: "function",
183
+ from: "array",
184
+ postfix: "",
185
+ },
186
+ ),
187
+ ),
188
+ ),
189
+ );
190
+
191
+ const configure =
192
+ (project: IProject) =>
193
+ (config: IConfig) =>
194
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => ({
195
+ types: {
196
+ input: () => TypeFactory.keyword("any"),
197
+ output: (type, name) =>
198
+ ts.factory.createTypePredicateNode(
199
+ undefined,
200
+ "input",
201
+ ts.factory.createTypeReferenceNode(
202
+ name ??
203
+ TypeFactory.getFullName(project.checker)(type),
204
+ ),
205
+ ),
206
+ },
207
+ trace: config.trace,
208
+ path: config.path,
209
+ prefix: config.prefix,
210
+ initializer:
211
+ ({ checker }) =>
212
+ (importer) =>
213
+ (type) => {
214
+ const collection: MetadataCollection =
215
+ new MetadataCollection();
216
+ const result = MetadataFactory.analyze(checker)({
217
+ escape: false,
218
+ constant: true,
219
+ absorb: true,
220
+ })(collection)(type);
221
+ if (result.success === false)
222
+ throw TransformerError.from(`typia.${importer.method}`)(
223
+ result.errors,
224
+ );
225
+ return [collection, result.data];
226
+ },
227
+ addition: config.addition,
228
+ decoder: () =>
229
+ config.decoder?.() ?? decode(project)(config)(importer),
230
+ objector: {
231
+ checker: () =>
232
+ config.decoder?.() ?? decode(project)(config)(importer),
233
+ decoder: () => decode_object(config)(importer),
234
+ joiner: config.joiner.object,
235
+ unionizer: config.equals
236
+ ? decode_union_object(decode_object(config)(importer))(
237
+ (input, obj, explore) =>
238
+ decode_object(config)(importer)(input, obj, {
239
+ ...explore,
240
+ tracable: true,
241
+ }),
242
+ )(config.joiner.is ?? ((expr) => expr))(
243
+ (value, expected) =>
244
+ ts.factory.createReturnStatement(
245
+ config.joiner.failure(value, expected),
246
+ ),
247
+ )
248
+ : (input, targets, explore) =>
249
+ config.combiner(explore)("or")(
250
+ input,
251
+ targets.map((obj) => ({
252
+ expression: decode_object(config)(importer)(
253
+ input,
254
+ obj,
255
+ explore,
256
+ ),
257
+ combined: true,
258
+ })),
259
+ `(${targets.map((t) => t.name).join(" | ")})`,
260
+ ),
261
+ failure: (value, expected) =>
262
+ ts.factory.createReturnStatement(
263
+ config.joiner.failure(value, expected),
264
+ ),
265
+ is: config.joiner.is,
266
+ required: config.joiner.required,
267
+ full: config.joiner.full,
268
+ type: TypeFactory.keyword("boolean"),
269
+ },
270
+ generator: {
271
+ unions: config.numeric
272
+ ? () =>
273
+ FeatureProgrammer.write_union_functions(
274
+ configure(project)({ ...config, numeric: false })(
275
+ importer,
276
+ ),
277
+ )
278
+ : undefined,
279
+ arrays: () => write_array_functions(project)(config)(importer),
280
+ tuples: () => write_tuple_functions(project)(config)(importer),
281
+ },
282
+ });
283
+
284
+ /* -----------------------------------------------------------
285
+ DECODERS
286
+ ----------------------------------------------------------- */
287
+ /**
288
+ * @internal
289
+ */
290
+ export const decode =
291
+ (project: IProject) =>
292
+ (config: IConfig) =>
293
+ (importer: FunctionImporter) =>
294
+ (
295
+ input: ts.Expression,
296
+ meta: Metadata,
297
+ explore: IExplore,
298
+ ): ts.Expression => {
299
+ if (meta.any) return config.success;
300
+
301
+ const top: IBinary[] = [];
302
+ const binaries: IBinary[] = [];
303
+ const add = create_add(binaries)(input);
304
+ const getConstantValue = (
305
+ value: number | string | bigint | boolean,
306
+ ) => {
307
+ if (typeof value === "string")
308
+ return ts.factory.createStringLiteral(value);
309
+ else if (typeof value === "bigint")
310
+ return ExpressionFactory.bigint(value);
311
+ return ts.factory.createIdentifier(value.toString());
312
+ };
313
+
314
+ //----
315
+ // CHECK OPTIONAL
316
+ //----
317
+ // @todo -> should be elaborated
318
+ const checkOptional: boolean = meta.empty() || meta.isUnionBucket();
319
+
320
+ // NULLABLE
321
+ if (checkOptional || meta.nullable)
322
+ (meta.nullable ? add : create_add(top)(input))(
323
+ meta.nullable,
324
+ ValueFactory.NULL(),
325
+ );
326
+
327
+ // UNDEFINDABLE
328
+ if (checkOptional || !meta.isRequired())
329
+ (meta.isRequired() ? create_add(top)(input) : add)(
330
+ !meta.isRequired(),
331
+ ValueFactory.UNDEFINED(),
332
+ );
333
+
334
+ // FUNCTIONAL
335
+ if (meta.functional === true)
336
+ if (
337
+ OptionPredicator.functional(project.options) ||
338
+ meta.size() !== 1
339
+ )
340
+ add(
341
+ true,
342
+ ts.factory.createStringLiteral("function"),
343
+ ValueFactory.TYPEOF(input),
344
+ );
345
+ else
346
+ binaries.push({
347
+ combined: false,
348
+ expression: config.success,
349
+ });
350
+
351
+ //----
352
+ // VALUES
353
+ //----
354
+ // CONSTANT VALUES
355
+ for (const constant of meta.constants)
356
+ if (AtomicPredicator.constant(meta)(constant.type))
357
+ for (const val of constant.values)
358
+ add(true, getConstantValue(val));
359
+
360
+ // ATOMIC VALUES
361
+ for (const atom of meta.atomics)
362
+ if (AtomicPredicator.atomic(meta)(atom.type) === false)
363
+ continue;
364
+ else if (atom.type === "number")
365
+ binaries.push({
366
+ expression: config.atomist(explore)(
367
+ check_number(project, config.numeric)(atom)(input),
368
+ )(input),
369
+ combined: false,
370
+ });
371
+ else if (atom.type === "bigint")
372
+ binaries.push({
373
+ expression: config.atomist(explore)(
374
+ check_bigint(project)(atom)(input),
375
+ )(input),
376
+ combined: false,
377
+ });
378
+ else if (atom.type === "string")
379
+ binaries.push({
380
+ expression: config.atomist(explore)(
381
+ check_string(project)(atom)(input),
382
+ )(input),
383
+ combined: false,
384
+ });
385
+ else
386
+ add(
387
+ true,
388
+ ts.factory.createStringLiteral(atom.type),
389
+ ValueFactory.TYPEOF(input),
390
+ );
391
+
392
+ // TEMPLATE LITERAL VALUES
393
+ if (meta.templates.length)
394
+ if (AtomicPredicator.template(meta))
395
+ binaries.push({
396
+ expression: config.atomist(explore)(
397
+ check_template(meta.templates)(input),
398
+ )(input),
399
+ combined: false,
400
+ });
401
+
402
+ // NATIVE CLASSES
403
+ for (const native of meta.natives)
404
+ binaries.push({
405
+ expression: check_native(native)(input),
406
+ combined: false,
407
+ });
408
+
409
+ //----
410
+ // INSTANCES
411
+ //----
412
+ interface IInstance {
413
+ pre: ts.Expression;
414
+ body: ts.Expression | null;
415
+ expected: string;
416
+ }
417
+ const instances: IInstance[] = [];
418
+ const prepare =
419
+ (pre: ts.Expression, expected: string) =>
420
+ (body: ts.Expression | null) =>
421
+ instances.push({
422
+ pre,
423
+ expected,
424
+ body,
425
+ });
426
+
427
+ // SETS
428
+ if (meta.sets.length) {
429
+ const install = prepare(
430
+ check_native("Set")(input),
431
+ meta.sets
432
+ .map((elem) => `Set<${elem.getName()}>`)
433
+ .join(" | "),
434
+ );
435
+ if (meta.sets.some((elem) => elem.any)) install(null);
436
+ else
437
+ install(
438
+ explore_sets(project)(config)(importer)(
439
+ input,
440
+ meta.sets,
441
+ {
442
+ ...explore,
443
+ from: "array",
444
+ },
445
+ ),
446
+ );
447
+ }
448
+
449
+ // MAPS
450
+ if (meta.maps.length) {
451
+ const install = prepare(
452
+ check_native("Map")(input),
453
+ meta.maps
454
+ .map(({ key, value }) => `Map<${key}, ${value}>`)
455
+ .join(" | "),
456
+ );
457
+ if (meta.maps.some((elem) => elem.key.any && elem.value.any))
458
+ install(null);
459
+ else
460
+ install(
461
+ explore_maps(project)(config)(importer)(
462
+ input,
463
+ meta.maps,
464
+ {
465
+ ...explore,
466
+ from: "array",
467
+ },
468
+ ),
469
+ );
470
+ }
471
+
472
+ // ARRAYS AND TUPLES
473
+ if (meta.tuples.length + meta.arrays.length > 0) {
474
+ const install = prepare(
475
+ config.atomist(explore)({
476
+ expected: [
477
+ ...meta.tuples.map((t) => t.type.name),
478
+ ...meta.arrays.map((a) => a.getName()),
479
+ ].join(" | "),
480
+ expression: ExpressionFactory.isArray(input),
481
+ conditions: [],
482
+ })(input),
483
+ [...meta.tuples, ...meta.arrays]
484
+ .map((elem) => elem.type.name)
485
+ .join(" | "),
486
+ );
487
+ if (meta.arrays.length === 0)
488
+ if (meta.tuples.length === 1)
489
+ install(
490
+ decode_tuple(project)(config)(importer)(
491
+ input,
492
+ meta.tuples[0]!,
493
+ {
494
+ ...explore,
495
+ from: "array",
496
+ },
497
+ ),
498
+ );
499
+ // TUPLE ONLY
500
+ else
501
+ install(
502
+ explore_tuples(project)(config)(importer)(
503
+ input,
504
+ meta.tuples,
505
+ {
506
+ ...explore,
507
+ from: "array",
508
+ },
509
+ ),
510
+ );
511
+ else if (meta.arrays.some((elem) => elem.type.value.any))
512
+ install(null);
513
+ else if (meta.tuples.length === 0)
514
+ if (meta.arrays.length === 1)
515
+ // ARRAY ONLY
516
+ install(
517
+ decode_array(project)(config)(importer)(
518
+ input,
519
+ meta.arrays[0]!,
520
+ {
521
+ ...explore,
522
+ from: "array",
523
+ },
524
+ ),
525
+ );
526
+ else
527
+ install(
528
+ explore_arrays(project)(config)(importer)(
529
+ input,
530
+ meta.arrays,
531
+ {
532
+ ...explore,
533
+ from: "array",
534
+ },
535
+ ),
536
+ );
537
+ else
538
+ install(
539
+ explore_arrays_and_tuples(project)(config)(importer)(
540
+ input,
541
+ [...meta.tuples, ...meta.arrays],
542
+ explore,
543
+ ),
544
+ );
545
+ }
546
+
547
+ // OBJECT
548
+ if (meta.objects.length > 0)
549
+ prepare(
550
+ ExpressionFactory.isObject({
551
+ checkNull: true,
552
+ checkArray: meta.objects.some((obj) =>
553
+ obj.properties.every(
554
+ (prop) =>
555
+ !prop.key.isSoleLiteral() ||
556
+ !prop.value.isRequired(),
557
+ ),
558
+ ),
559
+ })(input),
560
+ meta.objects.map((obj) => obj.name).join(" | "),
561
+ )(
562
+ explore_objects(config)(importer)(input, meta, {
563
+ ...explore,
564
+ from: "object",
565
+ }),
566
+ );
567
+
568
+ if (instances.length) {
569
+ const transformer =
570
+ (
571
+ merger: (
572
+ x: ts.Expression,
573
+ y: ts.Expression,
574
+ ) => ts.Expression,
575
+ ) =>
576
+ (ins: IInstance) =>
577
+ ins.body
578
+ ? {
579
+ expression: merger(ins.pre, ins.body),
580
+ combined: true,
581
+ }
582
+ : {
583
+ expression: ins.pre,
584
+ combined: false,
585
+ };
586
+ if (instances.length === 1)
587
+ binaries.push(
588
+ transformer((pre, body) =>
589
+ config.combiner(explore)("and")(
590
+ input,
591
+ [pre, body].map((expression) => ({
592
+ expression,
593
+ combined: expression !== pre,
594
+ })),
595
+ meta.getName(),
596
+ ),
597
+ )(instances[0]!),
598
+ );
599
+ else
600
+ binaries.push({
601
+ expression: config.combiner(explore)("or")(
602
+ input,
603
+ instances.map(
604
+ transformer(ts.factory.createLogicalAnd),
605
+ ),
606
+ meta.getName(),
607
+ ),
608
+ combined: true,
609
+ });
610
+ }
611
+
612
+ //----
613
+ // COMBINE CONDITIONS
614
+ //----
615
+ return top.length && binaries.length
616
+ ? config.combiner(explore)("and")(
617
+ input,
618
+ [
619
+ ...top,
620
+ {
621
+ expression: config.combiner(explore)("or")(
622
+ input,
623
+ binaries,
624
+ meta.getName(),
625
+ ),
626
+ combined: true,
627
+ },
628
+ ],
629
+ meta.getName(),
630
+ )
631
+ : binaries.length
632
+ ? config.combiner(explore)("or")(
633
+ input,
634
+ binaries,
635
+ meta.getName(),
636
+ )
637
+ : config.success;
638
+ };
639
+
640
+ export const decode_object =
641
+ (config: IConfig) => (importer: FunctionImporter) => {
642
+ const func = FeatureProgrammer.decode_object(config)(importer);
643
+ return (
644
+ input: ts.Expression,
645
+ obj: MetadataObject,
646
+ explore: IExplore,
647
+ ) => {
648
+ obj.validated = true;
649
+ return func(input, obj, explore);
650
+ };
651
+ };
652
+
653
+ const decode_array =
654
+ (project: IProject) =>
655
+ (config: IConfig) =>
656
+ (importer: FunctionImporter) =>
657
+ (input: ts.Expression, array: MetadataArray, explore: IExplore) => {
658
+ if (array.type.recursive === false)
659
+ return decode_array_inline(project)(config)(importer)(
660
+ input,
661
+ array,
662
+ explore,
663
+ );
664
+
665
+ explore = {
666
+ ...explore,
667
+ source: "function",
668
+ from: "array",
669
+ };
670
+ return ts.factory.createLogicalOr(
671
+ ts.factory.createCallExpression(
672
+ ts.factory.createIdentifier(
673
+ importer.useLocal(
674
+ `${config.prefix}a${array.type.index}`,
675
+ ),
676
+ ),
677
+ undefined,
678
+ FeatureProgrammer.argumentsArray(config)({
679
+ ...explore,
680
+ source: "function",
681
+ from: "array",
682
+ })(input),
683
+ ),
684
+ config.joiner.failure(input, array.type.name, explore),
685
+ );
686
+ };
687
+
688
+ const decode_array_inline =
689
+ (project: IProject) =>
690
+ (config: IConfig) =>
691
+ (importer: FunctionImporter) =>
692
+ (
693
+ input: ts.Expression,
694
+ array: MetadataArray,
695
+ explore: IExplore,
696
+ ): ts.Expression => {
697
+ const length = check_array_length(project)(array)(input);
698
+ const main = FeatureProgrammer.decode_array({
699
+ prefix: config.prefix,
700
+ trace: config.trace,
701
+ path: config.path,
702
+ decoder: () => decode(project)(config)(importer),
703
+ })(importer)(config.joiner.array)(input, array, explore);
704
+ return length.expression === null && length.conditions.length === 0
705
+ ? main
706
+ : ts.factory.createLogicalAnd(
707
+ config.atomist(explore)(length)(input),
708
+ main,
709
+ );
710
+ };
711
+
712
+ const decode_tuple =
713
+ (project: IProject) =>
714
+ (config: IConfig) =>
715
+ (importer: FunctionImporter) =>
716
+ (
717
+ input: ts.Expression,
718
+ tuple: MetadataTuple,
719
+ explore: IExplore,
720
+ ): ts.Expression => {
721
+ if (tuple.type.recursive === false)
722
+ return decode_tuple_inline(project)(config)(importer)(
723
+ input,
724
+ tuple.type,
725
+ explore,
726
+ );
727
+ explore = {
728
+ ...explore,
729
+ source: "function",
730
+ from: "array",
731
+ };
732
+ return ts.factory.createLogicalOr(
733
+ ts.factory.createCallExpression(
734
+ ts.factory.createIdentifier(
735
+ importer.useLocal(
736
+ `${config.prefix}t${tuple.type.index}`,
737
+ ),
738
+ ),
739
+ undefined,
740
+ FeatureProgrammer.argumentsArray(config)({
741
+ ...explore,
742
+ source: "function",
743
+ })(input),
744
+ ),
745
+ config.joiner.failure(input, tuple.type.name, explore),
746
+ );
747
+ };
748
+
749
+ const decode_tuple_inline =
750
+ (project: IProject) =>
751
+ (config: IConfig) =>
752
+ (importer: FunctionImporter) =>
753
+ (
754
+ input: ts.Expression,
755
+ tuple: MetadataTupleType,
756
+ explore: IExplore,
757
+ ): ts.Expression => {
758
+ const binaries: ts.Expression[] = tuple.elements
759
+ .filter((meta) => meta.rest === null)
760
+ .map((meta, index) =>
761
+ decode(project)(config)(importer)(
762
+ ts.factory.createElementAccessExpression(input, index),
763
+ meta,
764
+ {
765
+ ...explore,
766
+ from: "array",
767
+ postfix: explore.postfix.length
768
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
769
+ : `"[${index}]"`,
770
+ },
771
+ ),
772
+ );
773
+ const rest: ts.Expression | null =
774
+ tuple.elements.length && tuple.elements.at(-1)!.rest !== null
775
+ ? decode(project)(config)(importer)(
776
+ ts.factory.createCallExpression(
777
+ IdentifierFactory.access(input)("slice"),
778
+ undefined,
779
+ [
780
+ ts.factory.createNumericLiteral(
781
+ tuple.elements.length - 1,
782
+ ),
783
+ ],
784
+ ),
785
+ wrap_metadata_rest_tuple(
786
+ tuple.elements.at(-1)!.rest!,
787
+ ),
788
+ {
789
+ ...explore,
790
+ start: tuple.elements.length - 1,
791
+ },
792
+ )
793
+ : null;
794
+
795
+ const arrayLength = ts.factory.createPropertyAccessExpression(
796
+ input,
797
+ "length",
798
+ );
799
+ return config.combiner(explore)("and")(
800
+ input,
801
+ [
802
+ ...(rest === null
803
+ ? tuple.elements.every((t) => t.optional === false)
804
+ ? [
805
+ {
806
+ combined: false,
807
+ expression:
808
+ ts.factory.createStrictEquality(
809
+ arrayLength,
810
+ ts.factory.createNumericLiteral(
811
+ tuple.elements.length,
812
+ ),
813
+ ),
814
+ },
815
+ ]
816
+ : [
817
+ {
818
+ combined: false,
819
+ expression: ts.factory.createLogicalAnd(
820
+ ts.factory.createLessThanEquals(
821
+ ts.factory.createNumericLiteral(
822
+ tuple.elements.filter(
823
+ (t) =>
824
+ t.optional === false,
825
+ ).length,
826
+ ),
827
+ arrayLength,
828
+ ),
829
+ ts.factory.createGreaterThanEquals(
830
+ ts.factory.createNumericLiteral(
831
+ tuple.elements.length,
832
+ ),
833
+ arrayLength,
834
+ ),
835
+ ),
836
+ },
837
+ ]
838
+ : []),
839
+ ...(config.joiner.tuple
840
+ ? [
841
+ {
842
+ expression: config.joiner.tuple(binaries),
843
+ combined: true,
844
+ },
845
+ ]
846
+ : binaries.map((expression) => ({
847
+ expression,
848
+ combined: true,
849
+ }))),
850
+ ...(rest !== null
851
+ ? [
852
+ {
853
+ expression: rest,
854
+ combined: true,
855
+ },
856
+ ]
857
+ : []),
858
+ ],
859
+ `[${tuple.elements.map((t) => t.getName()).join(", ")}]`,
860
+ );
861
+ };
862
+
863
+ /* -----------------------------------------------------------
864
+ UNION TYPE EXPLORERS
865
+ ----------------------------------------------------------- */
866
+ const explore_sets =
867
+ (project: IProject) =>
868
+ (config: IConfig) =>
869
+ (importer: FunctionImporter) =>
870
+ (
871
+ input: ts.Expression,
872
+ sets: Metadata[],
873
+ explore: IExplore,
874
+ ): ts.Expression =>
875
+ ts.factory.createCallExpression(
876
+ UnionExplorer.set({
877
+ checker: decode(project)(config)(importer),
878
+ decoder: decode_array(project)(config)(importer),
879
+ empty: config.success,
880
+ success: config.success,
881
+ failure: (input, expected, explore) =>
882
+ ts.factory.createReturnStatement(
883
+ config.joiner.failure(input, expected, explore),
884
+ ),
885
+ })([])(input, sets, explore),
886
+ undefined,
887
+ undefined,
888
+ );
889
+
890
+ const explore_maps =
891
+ (project: IProject) =>
892
+ (config: IConfig) =>
893
+ (importer: FunctionImporter) =>
894
+ (
895
+ input: ts.Expression,
896
+ maps: Metadata.Entry[],
897
+ explore: IExplore,
898
+ ): ts.Expression =>
899
+ ts.factory.createCallExpression(
900
+ UnionExplorer.map({
901
+ checker: (input, entry, explore) => {
902
+ const func = decode(project)(config)(importer);
903
+ return ts.factory.createLogicalAnd(
904
+ func(
905
+ ts.factory.createElementAccessExpression(
906
+ input,
907
+ 0,
908
+ ),
909
+ entry[0],
910
+ {
911
+ ...explore,
912
+ postfix: `${explore.postfix}[0]`,
913
+ },
914
+ ),
915
+ func(
916
+ ts.factory.createElementAccessExpression(
917
+ input,
918
+ 1,
919
+ ),
920
+ entry[1],
921
+ {
922
+ ...explore,
923
+ postfix: `${explore.postfix}[1]`,
924
+ },
925
+ ),
926
+ );
927
+ },
928
+ decoder: decode_array(project)(config)(importer),
929
+ empty: config.success,
930
+ success: config.success,
931
+ failure: (input, expected, explore) =>
932
+ ts.factory.createReturnStatement(
933
+ config.joiner.failure(input, expected, explore),
934
+ ),
935
+ })([])(input, maps, explore),
936
+ undefined,
937
+ undefined,
938
+ );
939
+
940
+ const explore_tuples =
941
+ (project: IProject) =>
942
+ (config: IConfig) =>
943
+ (importer: FunctionImporter) =>
944
+ (
945
+ input: ts.Expression,
946
+ tuples: MetadataTuple[],
947
+ explore: IExplore,
948
+ ): ts.Expression =>
949
+ explore_array_like_union_types(config)(importer)(
950
+ UnionExplorer.tuple({
951
+ checker: decode_tuple(project)(config)(importer),
952
+ decoder: decode_tuple(project)(config)(importer),
953
+ empty: config.success,
954
+ success: config.success,
955
+ failure: (input, expected, explore) =>
956
+ ts.factory.createReturnStatement(
957
+ config.joiner.failure(input, expected, explore),
958
+ ),
959
+ }),
960
+ )(input, tuples, explore);
961
+
962
+ const explore_arrays =
963
+ (project: IProject) =>
964
+ (config: IConfig) =>
965
+ (importer: FunctionImporter) =>
966
+ (
967
+ input: ts.Expression,
968
+ arrays: MetadataArray[],
969
+ explore: IExplore,
970
+ ): ts.Expression =>
971
+ explore_array_like_union_types(config)(importer)(
972
+ UnionExplorer.array({
973
+ checker: decode(project)(config)(importer),
974
+ decoder: decode_array(project)(config)(importer),
975
+ empty: config.success,
976
+ success: config.success,
977
+ failure: (input, expected, explore) =>
978
+ ts.factory.createReturnStatement(
979
+ config.joiner.failure(input, expected, explore),
980
+ ),
981
+ }),
982
+ )(input, arrays, explore);
983
+
984
+ const explore_arrays_and_tuples =
985
+ (project: IProject) =>
986
+ (config: IConfig) =>
987
+ (importer: FunctionImporter) =>
988
+ (
989
+ input: ts.Expression,
990
+ elements: Array<MetadataArray | MetadataTuple>,
991
+ explore: IExplore,
992
+ ): ts.Expression =>
993
+ explore_array_like_union_types(config)(importer)(
994
+ UnionExplorer.array_or_tuple({
995
+ checker: (front, target, explore, array) =>
996
+ target instanceof MetadataTuple
997
+ ? decode_tuple(project)(config)(importer)(
998
+ front,
999
+ target,
1000
+ explore,
1001
+ )
1002
+ : config.atomist(explore)({
1003
+ expected: elements
1004
+ .map((elem) =>
1005
+ elem instanceof MetadataArray
1006
+ ? elem.getName()
1007
+ : elem.type.name,
1008
+ )
1009
+ .join(" | "),
1010
+ expression: decode(project)(config)(importer)(
1011
+ front,
1012
+ target,
1013
+ explore,
1014
+ ),
1015
+ conditions: [],
1016
+ })(array),
1017
+ decoder: (input, target, explore) =>
1018
+ target instanceof MetadataTuple
1019
+ ? decode_tuple(project)(config)(importer)(
1020
+ input,
1021
+ target,
1022
+ explore,
1023
+ )
1024
+ : decode_array(project)(config)(importer)(
1025
+ input,
1026
+ target,
1027
+ explore,
1028
+ ),
1029
+ empty: config.success,
1030
+ success: config.success,
1031
+ failure: (input, expected, explore) =>
1032
+ ts.factory.createReturnStatement(
1033
+ config.joiner.failure(input, expected, explore),
1034
+ ),
1035
+ }),
1036
+ )(input, elements, explore);
1037
+
1038
+ const explore_array_like_union_types =
1039
+ (config: IConfig) =>
1040
+ (importer: FunctionImporter) =>
1041
+ <T extends MetadataArray | MetadataTuple>(
1042
+ factory: (
1043
+ parameters: ts.ParameterDeclaration[],
1044
+ ) => (
1045
+ input: ts.Expression,
1046
+ elements: T[],
1047
+ explore: IExplore,
1048
+ ) => ts.ArrowFunction,
1049
+ ) =>
1050
+ (
1051
+ input: ts.Expression,
1052
+ elements: T[],
1053
+ explore: IExplore,
1054
+ ): ts.Expression => {
1055
+ const arrow =
1056
+ (parameters: ts.ParameterDeclaration[]) =>
1057
+ (explore: IExplore) =>
1058
+ (input: ts.Expression): ts.ArrowFunction =>
1059
+ factory(parameters)(input, elements, explore);
1060
+ if (elements.every((e) => e.type.recursive === false))
1061
+ ts.factory.createCallExpression(
1062
+ arrow([])(explore)(input),
1063
+ undefined,
1064
+ [],
1065
+ );
1066
+ explore = {
1067
+ ...explore,
1068
+ source: "function",
1069
+ from: "array",
1070
+ };
1071
+ return ts.factory.createLogicalOr(
1072
+ ts.factory.createCallExpression(
1073
+ ts.factory.createIdentifier(
1074
+ importer.emplaceUnion(
1075
+ config.prefix,
1076
+ elements.map((e) => e.type.name).join(" | "),
1077
+ () =>
1078
+ arrow(
1079
+ FeatureProgrammer.parameterDeclarations(
1080
+ config,
1081
+ )(TypeFactory.keyword("any"))(
1082
+ ts.factory.createIdentifier("input"),
1083
+ ),
1084
+ )({
1085
+ ...explore,
1086
+ postfix: "",
1087
+ })(ts.factory.createIdentifier("input")),
1088
+ ),
1089
+ ),
1090
+ undefined,
1091
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
1092
+ ),
1093
+ config.joiner.failure(
1094
+ input,
1095
+ elements.map((e) => e.type.name).join(" | "),
1096
+ explore,
1097
+ ),
1098
+ );
1099
+ };
1100
+
1101
+ const explore_objects =
1102
+ (config: IConfig) =>
1103
+ (importer: FunctionImporter) =>
1104
+ (input: ts.Expression, meta: Metadata, explore: IExplore) =>
1105
+ meta.objects.length === 1
1106
+ ? decode_object(config)(importer)(
1107
+ input,
1108
+ meta.objects[0]!,
1109
+ explore,
1110
+ )
1111
+ : ts.factory.createCallExpression(
1112
+ ts.factory.createIdentifier(
1113
+ importer.useLocal(
1114
+ `${config.prefix}u${meta.union_index!}`,
1115
+ ),
1116
+ ),
1117
+ undefined,
1118
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
1119
+ );
1120
+ }
1121
+
1122
+ const create_add =
1123
+ (binaries: CheckerProgrammer.IBinary[]) =>
1124
+ (defaultInput: ts.Expression) =>
1125
+ (
1126
+ exact: boolean,
1127
+ left: ts.Expression,
1128
+ right: ts.Expression = defaultInput,
1129
+ ) => {
1130
+ const factory = exact
1131
+ ? ts.factory.createStrictEquality
1132
+ : ts.factory.createStrictInequality;
1133
+ binaries.push({
1134
+ expression: factory(left, right),
1135
+ combined: false,
1136
+ });
1137
+ };