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