typia 5.3.12-dev.20240121 → 5.3.12

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 (360) hide show
  1. package/package.json +1 -1
  2. package/src/AssertionGuard.ts +1 -1
  3. package/src/CamelCase.ts +114 -114
  4. package/src/IRandomGenerator.ts +35 -35
  5. package/src/IValidation.ts +21 -21
  6. package/src/PascalCase.ts +114 -114
  7. package/src/Primitive.ts +135 -135
  8. package/src/Resolved.ts +116 -116
  9. package/src/SnakeCase.ts +156 -156
  10. package/src/TypeGuardError.ts +36 -36
  11. package/src/executable/TypiaGenerateWizard.ts +83 -83
  12. package/src/executable/TypiaPatchWizard.ts +42 -42
  13. package/src/executable/TypiaSetupWizard.ts +151 -151
  14. package/src/executable/setup/ArgumentParser.ts +43 -43
  15. package/src/executable/setup/CommandExecutor.ts +8 -8
  16. package/src/executable/setup/FileRetriever.ts +22 -22
  17. package/src/executable/setup/PackageManager.ts +71 -71
  18. package/src/executable/setup/PluginConfigurator.ts +69 -69
  19. package/src/executable/typia.ts +55 -55
  20. package/src/factories/CommentFactory.ts +79 -79
  21. package/src/factories/ExpressionFactory.ts +150 -150
  22. package/src/factories/IdentifierFactory.ts +70 -70
  23. package/src/factories/JsonMetadataFactory.ts +53 -53
  24. package/src/factories/LiteralFactory.ts +47 -47
  25. package/src/factories/MetadataCollection.ts +260 -260
  26. package/src/factories/MetadataCommentTagFactory.ts +601 -601
  27. package/src/factories/MetadataFactory.ts +267 -267
  28. package/src/factories/MetadataTypeTagFactory.ts +313 -313
  29. package/src/factories/NumericRangeFactory.ts +72 -72
  30. package/src/factories/ProtobufFactory.ts +259 -259
  31. package/src/factories/StatementFactory.ts +72 -72
  32. package/src/factories/TemplateFactory.ts +56 -56
  33. package/src/factories/TypeFactory.ts +118 -118
  34. package/src/factories/ValueFactory.ts +12 -12
  35. package/src/factories/internal/metadata/MetadataHelper.ts +15 -15
  36. package/src/factories/internal/metadata/emend_metadata_atomics.ts +37 -37
  37. package/src/factories/internal/metadata/emplace_metadata_alias.ts +41 -41
  38. package/src/factories/internal/metadata/emplace_metadata_array_type.ts +38 -38
  39. package/src/factories/internal/metadata/emplace_metadata_object.ts +175 -175
  40. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +57 -57
  41. package/src/factories/internal/metadata/explore_metadata.ts +28 -28
  42. package/src/factories/internal/metadata/iterate_metadata.ts +94 -94
  43. package/src/factories/internal/metadata/iterate_metadata_alias.ts +34 -34
  44. package/src/factories/internal/metadata/iterate_metadata_array.ts +37 -37
  45. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +62 -62
  46. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  47. package/src/factories/internal/metadata/iterate_metadata_collection.ts +131 -131
  48. package/src/factories/internal/metadata/iterate_metadata_comment_tags.ts +26 -26
  49. package/src/factories/internal/metadata/iterate_metadata_constant.ts +48 -48
  50. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +230 -230
  51. package/src/factories/internal/metadata/iterate_metadata_map.ts +50 -50
  52. package/src/factories/internal/metadata/iterate_metadata_native.ts +208 -208
  53. package/src/factories/internal/metadata/iterate_metadata_object.ts +43 -43
  54. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +52 -52
  55. package/src/factories/internal/metadata/iterate_metadata_set.ts +41 -41
  56. package/src/factories/internal/metadata/iterate_metadata_sort.ts +61 -61
  57. package/src/factories/internal/metadata/iterate_metadata_template.ts +44 -44
  58. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +37 -37
  59. package/src/factories/internal/metadata/iterate_metadata_union.ts +27 -27
  60. package/src/functional/$HeadersReader/$HeadersReader.ts +26 -26
  61. package/src/functional/$HeadersReader/index.ts +1 -1
  62. package/src/functional/$ParameterReader/$ParameterReader.ts +29 -29
  63. package/src/functional/$ParameterReader/index.ts +1 -1
  64. package/src/functional/$ProtobufReader.ts +195 -195
  65. package/src/functional/$ProtobufSizer.ts +147 -147
  66. package/src/functional/$ProtobufWriter.ts +150 -150
  67. package/src/functional/$QueryReader/$QueryReader.ts +46 -46
  68. package/src/functional/$QueryReader/index.ts +1 -1
  69. package/src/functional/$any.ts +4 -4
  70. package/src/functional/$clone.ts +4 -4
  71. package/src/functional/$convention.ts +37 -37
  72. package/src/functional/$dictionary.ts +22 -22
  73. package/src/functional/$every.ts +11 -11
  74. package/src/functional/$guard.ts +35 -35
  75. package/src/functional/$is_between.ts +2 -2
  76. package/src/functional/$join.ts +46 -46
  77. package/src/functional/$number.ts +12 -12
  78. package/src/functional/$report.ts +13 -13
  79. package/src/functional/$rest.ts +3 -3
  80. package/src/functional/$stoll.ts +8 -8
  81. package/src/functional/$string.ts +50 -50
  82. package/src/functional/$strlen.ts +7 -7
  83. package/src/functional/$tail.ts +5 -5
  84. package/src/functional/$throws.ts +10 -10
  85. package/src/functional/$varint.ts +130 -130
  86. package/src/functional/$zigzag.ts +39 -39
  87. package/src/functional/IProtobufWriter.ts +18 -18
  88. package/src/functional/Namespace/http.ts +7 -7
  89. package/src/functional/Namespace/index.ts +75 -75
  90. package/src/functional/Namespace/json.ts +15 -15
  91. package/src/functional/Namespace/misc.ts +14 -14
  92. package/src/functional/Namespace/notations.ts +23 -23
  93. package/src/functional/Namespace/protobuf.ts +20 -20
  94. package/src/functional/is.ts +10 -10
  95. package/src/http.ts +1267 -1267
  96. package/src/index.ts +4 -4
  97. package/src/notations.ts +977 -977
  98. package/src/programmers/AssertProgrammer.ts +293 -293
  99. package/src/programmers/CheckerProgrammer.ts +1025 -1025
  100. package/src/programmers/FeatureProgrammer.ts +460 -460
  101. package/src/programmers/IsProgrammer.ts +232 -232
  102. package/src/programmers/RandomProgrammer.ts +825 -825
  103. package/src/programmers/TypiaProgrammer.ts +167 -167
  104. package/src/programmers/ValidateProgrammer.ts +307 -307
  105. package/src/programmers/helpers/AtomicPredicator.ts +25 -25
  106. package/src/programmers/helpers/CloneJoiner.ts +130 -130
  107. package/src/programmers/helpers/FunctionImporeter.ts +87 -87
  108. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  109. package/src/programmers/helpers/ICheckEntry.ts +13 -13
  110. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  111. package/src/programmers/helpers/NotationJoiner.ts +132 -132
  112. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  113. package/src/programmers/helpers/ProtobufUtil.ts +125 -125
  114. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  115. package/src/programmers/helpers/PruneJoiner.ts +141 -141
  116. package/src/programmers/helpers/RandomJoiner.ts +140 -140
  117. package/src/programmers/helpers/RandomRanger.ts +171 -171
  118. package/src/programmers/helpers/StringifyJoinder.ts +105 -105
  119. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  120. package/src/programmers/helpers/UnionExplorer.ts +269 -269
  121. package/src/programmers/helpers/UnionPredicator.ts +77 -77
  122. package/src/programmers/helpers/disable_function_importer_declare.ts +32 -32
  123. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -77
  124. package/src/programmers/http/HttpAssertQueryProgrammer.ts +75 -75
  125. package/src/programmers/http/HttpHeadersProgrammer.ts +314 -314
  126. package/src/programmers/http/HttpIsHeadersProgrammer.ts +84 -84
  127. package/src/programmers/http/HttpIsQueryProgrammer.ts +82 -82
  128. package/src/programmers/http/HttpParameterProgrammer.ts +97 -97
  129. package/src/programmers/http/HttpQueryProgrammer.ts +252 -252
  130. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -77
  131. package/src/programmers/http/HttpValidateQueryProgrammer.ts +75 -75
  132. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  133. package/src/programmers/internal/application_alias.ts +74 -74
  134. package/src/programmers/internal/application_array.ts +59 -59
  135. package/src/programmers/internal/application_boolean.ts +35 -35
  136. package/src/programmers/internal/application_constant.ts +25 -25
  137. package/src/programmers/internal/application_default.ts +17 -17
  138. package/src/programmers/internal/application_default_string.ts +33 -33
  139. package/src/programmers/internal/application_escaped.ts +52 -52
  140. package/src/programmers/internal/application_native.ts +34 -34
  141. package/src/programmers/internal/application_number.ts +97 -97
  142. package/src/programmers/internal/application_object.ts +183 -183
  143. package/src/programmers/internal/application_schema.ts +165 -165
  144. package/src/programmers/internal/application_string.ts +61 -61
  145. package/src/programmers/internal/application_templates.ts +24 -24
  146. package/src/programmers/internal/application_tuple.ts +54 -54
  147. package/src/programmers/internal/check_array_length.ts +45 -45
  148. package/src/programmers/internal/check_bigint.ts +49 -49
  149. package/src/programmers/internal/check_dynamic_key.ts +175 -175
  150. package/src/programmers/internal/check_dynamic_properties.ts +198 -198
  151. package/src/programmers/internal/check_everything.ts +23 -23
  152. package/src/programmers/internal/check_native.ts +21 -21
  153. package/src/programmers/internal/check_number.ts +106 -106
  154. package/src/programmers/internal/check_object.ts +60 -60
  155. package/src/programmers/internal/check_string.ts +48 -48
  156. package/src/programmers/internal/check_template.ts +61 -61
  157. package/src/programmers/internal/check_union_array_like.ts +307 -307
  158. package/src/programmers/internal/decode_union_object.ts +78 -78
  159. package/src/programmers/internal/feature_object_entries.ts +58 -58
  160. package/src/programmers/internal/metadata_to_pattern.ts +33 -33
  161. package/src/programmers/internal/prune_object_properties.ts +60 -60
  162. package/src/programmers/internal/random_custom.ts +37 -37
  163. package/src/programmers/internal/stringify_dynamic_properties.ts +157 -157
  164. package/src/programmers/internal/stringify_native.ts +7 -7
  165. package/src/programmers/internal/stringify_regular_properties.ts +81 -81
  166. package/src/programmers/internal/template_to_pattern.ts +15 -15
  167. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +20 -20
  168. package/src/programmers/json/JsonApplicationProgrammer.ts +49 -49
  169. package/src/programmers/json/JsonAssertParseProgrammer.ts +67 -67
  170. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +61 -61
  171. package/src/programmers/json/JsonIsParseProgrammer.ts +73 -73
  172. package/src/programmers/json/JsonIsStringifyProgrammer.ts +75 -75
  173. package/src/programmers/json/JsonStringifyProgrammer.ts +893 -893
  174. package/src/programmers/json/JsonValidateParseProgrammer.ts +64 -64
  175. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +83 -83
  176. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +63 -63
  177. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +60 -60
  178. package/src/programmers/misc/MiscCloneProgrammer.ts +713 -713
  179. package/src/programmers/misc/MiscIsCloneProgrammer.ts +70 -70
  180. package/src/programmers/misc/MiscIsPruneProgrammer.ts +66 -66
  181. package/src/programmers/misc/MiscLiteralsProgrammer.ts +72 -72
  182. package/src/programmers/misc/MiscPruneProgrammer.ts +523 -523
  183. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +80 -80
  184. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +73 -73
  185. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +64 -64
  186. package/src/programmers/notations/NotationGeneralProgrammer.ts +647 -647
  187. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +71 -71
  188. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +81 -81
  189. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +75 -75
  190. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +61 -61
  191. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +607 -607
  192. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +819 -819
  193. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +82 -82
  194. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +75 -75
  195. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +160 -160
  196. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +75 -75
  197. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +83 -83
  198. package/src/protobuf.ts +970 -970
  199. package/src/reflect.ts +57 -57
  200. package/src/schemas/json/IJsonApplication.ts +9 -9
  201. package/src/schemas/json/IJsonComponents.ts +19 -19
  202. package/src/schemas/json/IJsonSchema.ts +122 -122
  203. package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
  204. package/src/schemas/metadata/IMetadata.ts +34 -34
  205. package/src/schemas/metadata/IMetadataAlias.ts +12 -12
  206. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  207. package/src/schemas/metadata/IMetadataArray.ts +7 -7
  208. package/src/schemas/metadata/IMetadataArrayType.ts +10 -10
  209. package/src/schemas/metadata/IMetadataAtomic.ts +6 -6
  210. package/src/schemas/metadata/IMetadataComponents.ts +11 -11
  211. package/src/schemas/metadata/IMetadataConstant.ts +23 -23
  212. package/src/schemas/metadata/IMetadataDictionary.ts +11 -11
  213. package/src/schemas/metadata/IMetadataEntry.ts +6 -6
  214. package/src/schemas/metadata/IMetadataEscaped.ts +6 -6
  215. package/src/schemas/metadata/IMetadataObject.ts +13 -13
  216. package/src/schemas/metadata/IMetadataProperty.ts +9 -9
  217. package/src/schemas/metadata/IMetadataTuple.ts +7 -7
  218. package/src/schemas/metadata/IMetadataTupleType.ts +10 -10
  219. package/src/schemas/metadata/IMetadataTypeTag.ts +15 -15
  220. package/src/schemas/metadata/Metadata.ts +612 -612
  221. package/src/schemas/metadata/MetadataAlias.ts +61 -61
  222. package/src/schemas/metadata/MetadataApplication.ts +44 -44
  223. package/src/schemas/metadata/MetadataArray.ts +49 -49
  224. package/src/schemas/metadata/MetadataArrayType.ts +57 -57
  225. package/src/schemas/metadata/MetadataAtomic.ts +84 -84
  226. package/src/schemas/metadata/MetadataComponents.ts +98 -98
  227. package/src/schemas/metadata/MetadataConstant.ts +46 -46
  228. package/src/schemas/metadata/MetadataEscaped.ts +51 -51
  229. package/src/schemas/metadata/MetadataObject.ts +154 -154
  230. package/src/schemas/metadata/MetadataProperty.ts +56 -56
  231. package/src/schemas/metadata/MetadataTuple.ts +32 -32
  232. package/src/schemas/metadata/MetadataTupleType.ts +67 -67
  233. package/src/tags/Default.ts +15 -15
  234. package/src/tags/ExclusiveMaximum.ts +13 -13
  235. package/src/tags/ExclusiveMinimum.ts +13 -13
  236. package/src/tags/Format.ts +30 -30
  237. package/src/tags/MaxItems.ts +9 -9
  238. package/src/tags/MaxLength.ts +9 -9
  239. package/src/tags/Maximum.ts +13 -13
  240. package/src/tags/MinItems.ts +9 -9
  241. package/src/tags/MinLength.ts +9 -9
  242. package/src/tags/Minimum.ts +13 -13
  243. package/src/tags/MultipleOf.ts +15 -15
  244. package/src/tags/Pattern.ts +9 -9
  245. package/src/tags/TagBase.ts +68 -68
  246. package/src/tags/Type.ts +27 -27
  247. package/src/tags/index.ts +14 -14
  248. package/src/transform.ts +35 -35
  249. package/src/transformers/CallExpressionTransformer.ts +351 -351
  250. package/src/transformers/FileTransformer.ts +91 -91
  251. package/src/transformers/IProject.ts +15 -15
  252. package/src/transformers/ITransformOptions.ts +62 -62
  253. package/src/transformers/ImportTransformer.ts +66 -66
  254. package/src/transformers/NodeTransformer.ts +13 -13
  255. package/src/transformers/TransformerError.ts +55 -55
  256. package/src/transformers/features/AssertTransformer.ts +16 -16
  257. package/src/transformers/features/CreateAssertTransformer.ts +16 -16
  258. package/src/transformers/features/CreateIsTransformer.ts +10 -10
  259. package/src/transformers/features/CreateRandomTransformer.ts +40 -40
  260. package/src/transformers/features/CreateValidateTransformer.ts +13 -13
  261. package/src/transformers/features/IsTransformer.ts +10 -10
  262. package/src/transformers/features/RandomTransformer.ts +44 -44
  263. package/src/transformers/features/ValidateTransformer.ts +11 -11
  264. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +11 -11
  265. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +9 -9
  266. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -9
  267. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -9
  268. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -9
  269. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -9
  270. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -9
  271. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +12 -12
  272. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +11 -11
  273. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +9 -9
  274. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +9 -9
  275. package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -9
  276. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -9
  277. package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -9
  278. package/src/transformers/features/http/HttpParameterTransformer.ts +9 -9
  279. package/src/transformers/features/http/HttpQueryTransformer.ts +9 -9
  280. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -10
  281. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +9 -9
  282. package/src/transformers/features/json/JsonApplicationTransformer.ts +134 -134
  283. package/src/transformers/features/json/JsonAssertParseTransformer.ts +9 -9
  284. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  285. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +9 -9
  286. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  287. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  288. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +9 -9
  289. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  290. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +11 -11
  291. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  292. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  293. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +9 -9
  294. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  295. package/src/transformers/features/json/JsonValidateParseTransformer.ts +9 -9
  296. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  297. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +9 -9
  298. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +9 -9
  299. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  300. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +9 -9
  301. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +9 -9
  302. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  303. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  304. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  305. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  306. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +11 -11
  307. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +11 -11
  308. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  309. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  310. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +32 -32
  311. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  312. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +9 -9
  313. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +9 -9
  314. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +15 -15
  315. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +15 -15
  316. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +15 -15
  317. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +15 -15
  318. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +15 -15
  319. package/src/transformers/features/notations/NotationGeneralTransformer.ts +11 -11
  320. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +15 -15
  321. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +15 -15
  322. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
  323. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
  324. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +12 -12
  325. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +12 -12
  326. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +9 -9
  327. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +9 -9
  328. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +9 -9
  329. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +9 -9
  330. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +12 -12
  331. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +12 -12
  332. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +9 -9
  333. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +9 -9
  334. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +9 -9
  335. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +9 -9
  336. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +33 -33
  337. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +10 -10
  338. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +10 -10
  339. package/src/transformers/features/reflect/ReflectMetadataTransformer.ts +63 -63
  340. package/src/transformers/internal/GenericTransformer.ts +96 -96
  341. package/src/typings/Atomic.ts +13 -13
  342. package/src/typings/ClassProperties.ts +5 -5
  343. package/src/typings/Customizable.ts +5 -5
  344. package/src/typings/OmitNever.ts +3 -3
  345. package/src/typings/ProtobufAtomic.ts +19 -19
  346. package/src/typings/SpecialFields.ts +3 -3
  347. package/src/typings/ValidationPipe.ts +9 -9
  348. package/src/typings/Writable.ts +11 -11
  349. package/src/utils/ArrayUtil.ts +43 -43
  350. package/src/utils/Escaper.ts +46 -46
  351. package/src/utils/MapUtil.ts +12 -12
  352. package/src/utils/NameEncoder.ts +32 -32
  353. package/src/utils/NamingConvention/NamingConvention.ts +77 -77
  354. package/src/utils/NamingConvention/index.ts +1 -1
  355. package/src/utils/PatternUtil.ts +29 -29
  356. package/src/utils/RandomGenerator/RandomGenerator.ts +74 -74
  357. package/src/utils/RandomGenerator/index.ts +1 -1
  358. package/src/utils/Singleton.ts +16 -16
  359. package/src/utils/StringUtil/StringUtil.ts +2 -2
  360. package/src/utils/StringUtil/index.ts +1 -1
@@ -1,893 +1,893 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { JsonMetadataFactory } from "../../factories/JsonMetadataFactory";
6
- import { MetadataCollection } from "../../factories/MetadataCollection";
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 { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
16
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
17
-
18
- import { IProject } from "../../transformers/IProject";
19
-
20
- import { Atomic } from "../../typings/Atomic";
21
-
22
- import { ArrayUtil } from "../../utils/ArrayUtil";
23
-
24
- import { FeatureProgrammer } from "../FeatureProgrammer";
25
- import { IsProgrammer } from "../IsProgrammer";
26
- import { AtomicPredicator } from "../helpers/AtomicPredicator";
27
- import { FunctionImporter } from "../helpers/FunctionImporeter";
28
- import { IExpressionEntry } from "../helpers/IExpressionEntry";
29
- import { OptionPredicator } from "../helpers/OptionPredicator";
30
- import { StringifyJoiner } from "../helpers/StringifyJoinder";
31
- import { StringifyPredicator } from "../helpers/StringifyPredicator";
32
- import { UnionExplorer } from "../helpers/UnionExplorer";
33
- import { check_native } from "../internal/check_native";
34
- import { decode_union_object } from "../internal/decode_union_object";
35
- import { feature_object_entries } from "../internal/feature_object_entries";
36
- import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
37
-
38
- export namespace JsonStringifyProgrammer {
39
- /* -----------------------------------------------------------
40
- WRITER
41
- ----------------------------------------------------------- */
42
-
43
- export const write =
44
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
- const importer: FunctionImporter = new FunctionImporter(modulo.getText());
46
- const config: FeatureProgrammer.IConfig = configure(project)(importer);
47
-
48
- return FeatureProgrammer.write(project)({
49
- ...config,
50
- addition: (collection) => [
51
- ...IsProgrammer.write_function_statements(project)(importer)(
52
- collection,
53
- ),
54
- ...importer.declare(modulo),
55
- ],
56
- })(importer);
57
- };
58
-
59
- const write_array_functions =
60
- (config: FeatureProgrammer.IConfig) =>
61
- (importer: FunctionImporter) =>
62
- (collection: MetadataCollection): ts.VariableStatement[] =>
63
- collection
64
- .arrays()
65
- .filter((a) => a.recursive)
66
- .map((type, i) =>
67
- StatementFactory.constant(
68
- `${config.prefix}a${i}`,
69
- ts.factory.createArrowFunction(
70
- undefined,
71
- undefined,
72
- FeatureProgrammer.parameterDeclarations(config)(
73
- TypeFactory.keyword("any"),
74
- )(ts.factory.createIdentifier("input")),
75
- TypeFactory.keyword("any"),
76
- undefined,
77
- decode_array_inline(config)(importer)(
78
- ts.factory.createIdentifier("input"),
79
- MetadataArray.create({
80
- type,
81
- tags: [],
82
- }),
83
- {
84
- tracable: config.trace,
85
- source: "function",
86
- from: "array",
87
- postfix: "",
88
- },
89
- ),
90
- ),
91
- ),
92
- );
93
-
94
- const write_tuple_functions =
95
- (project: IProject) =>
96
- (config: FeatureProgrammer.IConfig) =>
97
- (importer: FunctionImporter) =>
98
- (collection: MetadataCollection): ts.VariableStatement[] =>
99
- collection
100
- .tuples()
101
- .filter((t) => t.recursive)
102
- .map((tuple, i) =>
103
- StatementFactory.constant(
104
- `${config.prefix}t${i}`,
105
- ts.factory.createArrowFunction(
106
- undefined,
107
- undefined,
108
- FeatureProgrammer.parameterDeclarations(config)(
109
- TypeFactory.keyword("any"),
110
- )(ts.factory.createIdentifier("input")),
111
- TypeFactory.keyword("any"),
112
- undefined,
113
- decode_tuple_inline(project)(config)(importer)(
114
- ts.factory.createIdentifier("input"),
115
- tuple,
116
- {
117
- tracable: config.trace,
118
- source: "function",
119
- from: "array",
120
- postfix: "",
121
- },
122
- ),
123
- ),
124
- ),
125
- );
126
-
127
- /* -----------------------------------------------------------
128
- DECODERS
129
- ----------------------------------------------------------- */
130
- const decode =
131
- (project: IProject) =>
132
- (config: FeatureProgrammer.IConfig) =>
133
- (importer: FunctionImporter) =>
134
- (
135
- input: ts.Expression,
136
- meta: Metadata,
137
- explore: FeatureProgrammer.IExplore,
138
- ): ts.Expression => {
139
- // ANY TYPE
140
- if (meta.any === true)
141
- return wrap_required(
142
- input,
143
- meta,
144
- explore,
145
- )(
146
- wrap_functional(
147
- input,
148
- meta,
149
- explore,
150
- )(
151
- ts.factory.createCallExpression(
152
- ts.factory.createIdentifier("JSON.stringify"),
153
- undefined,
154
- [input],
155
- ),
156
- ),
157
- );
158
-
159
- // ONLY NULL OR UNDEFINED
160
- const size: number = meta.size();
161
- if (
162
- size === 0 &&
163
- (meta.isRequired() === false || meta.nullable === true)
164
- ) {
165
- if (meta.isRequired() === false && meta.nullable === true)
166
- return explore.from === "array"
167
- ? ts.factory.createStringLiteral("null")
168
- : ts.factory.createConditionalExpression(
169
- ts.factory.createStrictEquality(ts.factory.createNull(), input),
170
- undefined,
171
- ts.factory.createStringLiteral("null"),
172
- undefined,
173
- ts.factory.createIdentifier("undefined"),
174
- );
175
- else if (meta.isRequired() === false)
176
- return explore.from === "array"
177
- ? ts.factory.createStringLiteral("null")
178
- : ts.factory.createIdentifier("undefined");
179
- else return ts.factory.createStringLiteral("null");
180
- }
181
-
182
- //----
183
- // LIST UP UNION TYPES
184
- //----
185
- const unions: IUnion[] = [];
186
-
187
- // toJSON() METHOD
188
- if (meta.escaped !== null)
189
- unions.push({
190
- type: "resolved",
191
- is: () => IsProgrammer.decode_to_json(false)(input),
192
- value: () =>
193
- decode_to_json(project)(config)(importer)(
194
- input,
195
- meta.escaped!.returns,
196
- explore,
197
- ),
198
- });
199
- else if (meta.functional === true)
200
- unions.push({
201
- type: "functional",
202
- is: () => IsProgrammer.decode_functional(input),
203
- value: () => decode_functional(explore),
204
- });
205
-
206
- // TEMPLATES
207
- if (
208
- meta.templates.length ||
209
- ArrayUtil.has(meta.constants, (c) => c.type === "string")
210
- )
211
- if (AtomicPredicator.template(meta)) {
212
- const partial = Metadata.initialize();
213
- partial.atomics.push(
214
- MetadataAtomic.create({ type: "string", tags: [] }),
215
- ),
216
- unions.push({
217
- type: "template literal",
218
- is: () =>
219
- IsProgrammer.decode(project)(importer)(input, partial, explore),
220
- value: () =>
221
- decode_atomic(project)(importer)(input, "string", explore),
222
- });
223
- }
224
-
225
- // CONSTANTS
226
- for (const constant of meta.constants)
227
- if (AtomicPredicator.constant(meta)(constant.type) === false) continue;
228
- else if (constant.type !== "string")
229
- unions.push({
230
- type: "atomic",
231
- is: () =>
232
- IsProgrammer.decode(project)(importer)(
233
- input,
234
- (() => {
235
- const partial = Metadata.initialize();
236
- partial.atomics.push(
237
- MetadataAtomic.create({
238
- type: constant.type,
239
- tags: [],
240
- }),
241
- );
242
- return partial;
243
- })(),
244
- explore,
245
- ),
246
- value: () =>
247
- decode_atomic(project)(importer)(input, constant.type, explore),
248
- });
249
- else if (meta.templates.length === 0)
250
- unions.push({
251
- type: "const string",
252
- is: () =>
253
- IsProgrammer.decode(project)(importer)(
254
- input,
255
- (() => {
256
- const partial = Metadata.initialize();
257
- partial.atomics.push(
258
- MetadataAtomic.create({
259
- type: "string",
260
- tags: [],
261
- }),
262
- );
263
- return partial;
264
- })(),
265
- explore,
266
- ),
267
- value: () =>
268
- decode_constant_string(project)(importer)(
269
- input,
270
- [...constant.values] as string[],
271
- explore,
272
- ),
273
- });
274
-
275
- /// ATOMICS
276
- for (const a of meta.atomics)
277
- if (AtomicPredicator.atomic(meta)(a.type))
278
- unions.push({
279
- type: "atomic",
280
- is: () =>
281
- IsProgrammer.decode(project)(importer)(
282
- input,
283
- (() => {
284
- const partial = Metadata.initialize();
285
- partial.atomics.push(a);
286
- return partial;
287
- })(),
288
- explore,
289
- ),
290
- value: () =>
291
- decode_atomic(project)(importer)(input, a.type, explore),
292
- });
293
-
294
- // TUPLES
295
- for (const tuple of meta.tuples)
296
- unions.push({
297
- type: "tuple",
298
- is: () =>
299
- IsProgrammer.decode(project)(importer)(
300
- input,
301
- (() => {
302
- const partial = Metadata.initialize();
303
- partial.tuples.push(tuple);
304
- return partial;
305
- })(),
306
- explore,
307
- ),
308
- value: () =>
309
- decode_tuple(project)(config)(importer)(input, tuple, explore),
310
- });
311
-
312
- // ARRAYS
313
- if (meta.arrays.length) {
314
- const value: () => ts.Expression =
315
- meta.arrays.length === 1
316
- ? () =>
317
- decode_array(config)(importer)(input, meta.arrays[0]!, {
318
- ...explore,
319
- from: "array",
320
- })
321
- : meta.arrays.some((elem) => elem.type.value.any)
322
- ? () =>
323
- ts.factory.createCallExpression(
324
- ts.factory.createIdentifier("JSON.stringify"),
325
- undefined,
326
- [input],
327
- )
328
- : () =>
329
- explore_arrays(project)(config)(importer)(input, meta.arrays, {
330
- ...explore,
331
- from: "array",
332
- });
333
-
334
- unions.push({
335
- type: "array",
336
- is: () => ExpressionFactory.isArray(input),
337
- value,
338
- });
339
- }
340
-
341
- // BUILT-IN CLASSES
342
- if (meta.natives.length)
343
- for (const native of meta.natives)
344
- unions.push({
345
- type: "object",
346
- is: () => check_native(native)(input),
347
- value: () =>
348
- AtomicPredicator.native(native)
349
- ? decode_atomic(project)(importer)(
350
- input,
351
- native.toLowerCase() as Atomic.Literal,
352
- explore,
353
- )
354
- : ts.factory.createStringLiteral("{}"),
355
- });
356
-
357
- // SETS
358
- if (meta.sets.length)
359
- unions.push({
360
- type: "object",
361
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
362
- value: () => ts.factory.createStringLiteral("{}"),
363
- });
364
-
365
- // MAPS
366
- if (meta.maps.length)
367
- unions.push({
368
- type: "object",
369
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
370
- value: () => ts.factory.createStringLiteral("{}"),
371
- });
372
-
373
- // OBJECTS
374
- if (meta.objects.length)
375
- unions.push({
376
- type: "object",
377
- is: () =>
378
- ExpressionFactory.isObject({
379
- checkNull: true,
380
- checkArray: meta.objects.some((obj) =>
381
- obj.properties.every(
382
- (prop) =>
383
- !prop.key.isSoleLiteral() || !prop.value.isRequired(),
384
- ),
385
- ),
386
- })(input),
387
- value: () =>
388
- meta.isParentResolved() === false &&
389
- meta.objects.length === 1 &&
390
- meta.objects[0]!._Is_simple(explore.from === "top" ? 0 : 1)
391
- ? (() => {
392
- const obj: MetadataObject = meta.objects[0]!;
393
- const entries: IExpressionEntry<ts.Expression>[] =
394
- feature_object_entries({
395
- decoder: () => decode(project)(config)(importer),
396
- trace: false,
397
- path: false,
398
- })(importer)(obj)(
399
- ts.factory.createAsExpression(
400
- input,
401
- TypeFactory.keyword("any"),
402
- ),
403
- );
404
- return StringifyJoiner.object(importer)(
405
- ts.factory.createAsExpression(
406
- input,
407
- TypeFactory.keyword("any"),
408
- ),
409
- entries,
410
- );
411
- })()
412
- : explore_objects(config)(importer)(input, meta, {
413
- ...explore,
414
- from: "object",
415
- }),
416
- });
417
-
418
- //----
419
- // RETURNS
420
- //----
421
- // CHECK NULL AND UNDEFINED
422
- const wrapper = (output: ts.Expression) =>
423
- wrap_required(input, meta, explore)(wrap_nullable(input, meta)(output));
424
-
425
- // DIRECT RETURN
426
- if (unions.length === 0)
427
- return ts.factory.createCallExpression(
428
- ts.factory.createIdentifier("JSON.stringify"),
429
- undefined,
430
- [input],
431
- );
432
- else if (unions.length === 1) return wrapper(unions[0]!.value());
433
-
434
- // RETURN WITH TYPE CHECKING
435
- return wrapper(
436
- ts.factory.createCallExpression(
437
- ts.factory.createArrowFunction(
438
- undefined,
439
- undefined,
440
- [],
441
- undefined,
442
- undefined,
443
- iterate(importer, input, unions, meta.getName()),
444
- ),
445
- undefined,
446
- undefined,
447
- ),
448
- );
449
- };
450
-
451
- const decode_object = (importer: FunctionImporter) =>
452
- FeatureProgrammer.decode_object({
453
- trace: false,
454
- path: false,
455
- prefix: PREFIX,
456
- })(importer);
457
-
458
- const decode_array =
459
- (config: FeatureProgrammer.IConfig) =>
460
- (importer: FunctionImporter) =>
461
- (
462
- input: ts.Expression,
463
- array: MetadataArray,
464
- explore: FeatureProgrammer.IExplore,
465
- ) =>
466
- array.type.recursive
467
- ? ts.factory.createCallExpression(
468
- ts.factory.createIdentifier(
469
- importer.useLocal(`${config.prefix}a${array.type.index}`),
470
- ),
471
- undefined,
472
- FeatureProgrammer.argumentsArray(config)({
473
- ...explore,
474
- source: "function",
475
- from: "array",
476
- })(input),
477
- )
478
- : decode_array_inline(config)(importer)(input, array, explore);
479
-
480
- const decode_array_inline =
481
- (config: FeatureProgrammer.IConfig) =>
482
- (importer: FunctionImporter) =>
483
- (
484
- input: ts.Expression,
485
- array: MetadataArray,
486
- explore: FeatureProgrammer.IExplore,
487
- ) =>
488
- FeatureProgrammer.decode_array(config)(importer)(StringifyJoiner.array)(
489
- input,
490
- array,
491
- explore,
492
- );
493
-
494
- const decode_tuple =
495
- (project: IProject) =>
496
- (config: FeatureProgrammer.IConfig) =>
497
- (importer: FunctionImporter) =>
498
- (
499
- input: ts.Expression,
500
- tuple: MetadataTuple,
501
- explore: FeatureProgrammer.IExplore,
502
- ): ts.Expression =>
503
- tuple.type.recursive
504
- ? ts.factory.createCallExpression(
505
- ts.factory.createIdentifier(
506
- importer.useLocal(`${config.prefix}t${tuple.type.index}`),
507
- ),
508
- undefined,
509
- FeatureProgrammer.argumentsArray(config)({
510
- ...explore,
511
- source: "function",
512
- })(input),
513
- )
514
- : decode_tuple_inline(project)(config)(importer)(
515
- input,
516
- tuple.type,
517
- explore,
518
- );
519
-
520
- const decode_tuple_inline =
521
- (project: IProject) =>
522
- (config: FeatureProgrammer.IConfig) =>
523
- (importer: FunctionImporter) =>
524
- (
525
- input: ts.Expression,
526
- tuple: MetadataTupleType,
527
- explore: FeatureProgrammer.IExplore,
528
- ): ts.Expression => {
529
- const children: ts.Expression[] = tuple.elements
530
- .filter((elem) => elem.rest === null)
531
- .map((elem, index) =>
532
- decode(project)(config)(importer)(
533
- ts.factory.createElementAccessExpression(input, index),
534
- elem,
535
- {
536
- ...explore,
537
- from: "array",
538
- postfix: explore.postfix.length
539
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
540
- : `"[${index}]"`,
541
- },
542
- ),
543
- );
544
- const rest = (() => {
545
- if (tuple.elements.length === 0) return null;
546
- const last = tuple.elements.at(-1)!;
547
- if (last.rest === null) return null;
548
-
549
- const code = decode(project)(config)(importer)(
550
- ts.factory.createCallExpression(
551
- IdentifierFactory.access(input)("slice"),
552
- undefined,
553
- [ExpressionFactory.number(tuple.elements.length - 1)],
554
- ),
555
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
556
- {
557
- ...explore,
558
- start: tuple.elements.length - 1,
559
- },
560
- );
561
- return ts.factory.createCallExpression(
562
- importer.use("rest"),
563
- undefined,
564
- [code],
565
- );
566
- })();
567
- return StringifyJoiner.tuple(children, rest);
568
- };
569
-
570
- const decode_atomic =
571
- (project: IProject) =>
572
- (importer: FunctionImporter) =>
573
- (
574
- input: ts.Expression,
575
- type: string,
576
- explore: FeatureProgrammer.IExplore,
577
- ) => {
578
- if (type === "string")
579
- return ts.factory.createCallExpression(
580
- importer.use("string"),
581
- undefined,
582
- [input],
583
- );
584
- else if (type === "number" && OptionPredicator.numeric(project.options))
585
- input = ts.factory.createCallExpression(
586
- importer.use("number"),
587
- undefined,
588
- [input],
589
- );
590
-
591
- return explore.from !== "top"
592
- ? input
593
- : ts.factory.createCallExpression(
594
- IdentifierFactory.access(input)("toString"),
595
- undefined,
596
- undefined,
597
- );
598
- };
599
-
600
- const decode_constant_string =
601
- (project: IProject) =>
602
- (importer: FunctionImporter) =>
603
- (
604
- input: ts.Expression,
605
- values: string[],
606
- explore: FeatureProgrammer.IExplore,
607
- ): ts.Expression => {
608
- if (values.every((v) => !StringifyPredicator.require_escape(v)))
609
- return [
610
- ts.factory.createStringLiteral('"'),
611
- input,
612
- ts.factory.createStringLiteral('"'),
613
- ].reduce((x, y) => ts.factory.createAdd(x, y));
614
- else return decode_atomic(project)(importer)(input, "string", explore);
615
- };
616
-
617
- const decode_to_json =
618
- (project: IProject) =>
619
- (config: FeatureProgrammer.IConfig) =>
620
- (importer: FunctionImporter) =>
621
- (
622
- input: ts.Expression,
623
- resolved: Metadata,
624
- explore: FeatureProgrammer.IExplore,
625
- ): ts.Expression => {
626
- return decode(project)(config)(importer)(
627
- ts.factory.createCallExpression(
628
- IdentifierFactory.access(input)("toJSON"),
629
- undefined,
630
- [],
631
- ),
632
- resolved,
633
- explore,
634
- );
635
- };
636
-
637
- const decode_functional = (explore: FeatureProgrammer.IExplore) =>
638
- explore.from === "array"
639
- ? ts.factory.createStringLiteral("null")
640
- : ts.factory.createIdentifier("undefined");
641
-
642
- /* -----------------------------------------------------------
643
- EXPLORERS
644
- ----------------------------------------------------------- */
645
- const explore_objects =
646
- (config: FeatureProgrammer.IConfig) =>
647
- (importer: FunctionImporter) =>
648
- (
649
- input: ts.Expression,
650
- meta: Metadata,
651
- explore: FeatureProgrammer.IExplore,
652
- ) =>
653
- meta.objects.length === 1
654
- ? decode_object(importer)(input, meta.objects[0]!, explore)
655
- : ts.factory.createCallExpression(
656
- ts.factory.createIdentifier(
657
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
658
- ),
659
- undefined,
660
- FeatureProgrammer.argumentsArray(config)(explore)(input),
661
- );
662
-
663
- const explore_arrays =
664
- (project: IProject) =>
665
- (config: FeatureProgrammer.IConfig) =>
666
- (importer: FunctionImporter) =>
667
- (
668
- input: ts.Expression,
669
- elements: MetadataArray[],
670
- explore: FeatureProgrammer.IExplore,
671
- ): ts.Expression =>
672
- explore_array_like_union_types(config)(importer)(
673
- UnionExplorer.array({
674
- checker: IsProgrammer.decode(project)(importer),
675
- decoder: decode_array(config)(importer),
676
- empty: ts.factory.createStringLiteral("[]"),
677
- success: ts.factory.createTrue(),
678
- failure: (input, expected) =>
679
- create_throw_error(importer)(expected)(input),
680
- }),
681
- )(input, elements, explore);
682
-
683
- const explore_array_like_union_types =
684
- (config: FeatureProgrammer.IConfig) =>
685
- (importer: FunctionImporter) =>
686
- <T extends MetadataArray | MetadataTuple>(
687
- factory: (
688
- parameters: ts.ParameterDeclaration[],
689
- ) => (
690
- input: ts.Expression,
691
- elements: T[],
692
- explore: FeatureProgrammer.IExplore,
693
- ) => ts.ArrowFunction,
694
- ) =>
695
- (
696
- input: ts.Expression,
697
- elements: T[],
698
- explore: FeatureProgrammer.IExplore,
699
- ): ts.Expression => {
700
- const arrow =
701
- (parameters: ts.ParameterDeclaration[]) =>
702
- (explore: FeatureProgrammer.IExplore) =>
703
- (input: ts.Expression): ts.ArrowFunction =>
704
- factory(parameters)(input, elements, explore);
705
- if (elements.every((e) => e.type.recursive === false))
706
- ts.factory.createCallExpression(
707
- arrow([])(explore)(input),
708
- undefined,
709
- [],
710
- );
711
-
712
- explore = {
713
- ...explore,
714
- source: "function",
715
- from: "array",
716
- };
717
- return ts.factory.createCallExpression(
718
- ts.factory.createIdentifier(
719
- importer.emplaceUnion(
720
- config.prefix,
721
- elements.map((e) => e.type.name).join(" | "),
722
- () =>
723
- arrow(
724
- FeatureProgrammer.parameterDeclarations(config)(
725
- TypeFactory.keyword("any"),
726
- )(ts.factory.createIdentifier("input")),
727
- )({
728
- ...explore,
729
- postfix: "",
730
- })(ts.factory.createIdentifier("input")),
731
- ),
732
- ),
733
- undefined,
734
- FeatureProgrammer.argumentsArray(config)(explore)(input),
735
- );
736
- };
737
-
738
- /* -----------------------------------------------------------
739
- RETURN SCRIPTS
740
- ----------------------------------------------------------- */
741
- const wrap_required = (
742
- input: ts.Expression,
743
- meta: Metadata,
744
- explore: FeatureProgrammer.IExplore,
745
- ): ((expression: ts.Expression) => ts.Expression) => {
746
- if (meta.isRequired() === true && meta.any === false)
747
- return (expression) => expression;
748
- return (expression) =>
749
- ts.factory.createConditionalExpression(
750
- ts.factory.createStrictInequality(
751
- ts.factory.createIdentifier("undefined"),
752
- input,
753
- ),
754
- undefined,
755
- expression,
756
- undefined,
757
- explore.from === "array"
758
- ? ts.factory.createStringLiteral("null")
759
- : ts.factory.createIdentifier("undefined"),
760
- );
761
- };
762
-
763
- const wrap_nullable = (
764
- input: ts.Expression,
765
- meta: Metadata,
766
- ): ((expression: ts.Expression) => ts.Expression) => {
767
- if (meta.nullable === false) return (expression) => expression;
768
- return (expression) =>
769
- ts.factory.createConditionalExpression(
770
- ts.factory.createStrictInequality(ts.factory.createNull(), input),
771
- undefined,
772
- expression,
773
- undefined,
774
- ts.factory.createStringLiteral("null"),
775
- );
776
- };
777
-
778
- const wrap_functional = (
779
- input: ts.Expression,
780
- meta: Metadata,
781
- explore: FeatureProgrammer.IExplore,
782
- ): ((expression: ts.Expression) => ts.Expression) => {
783
- if (meta.functional === false) return (expression) => expression;
784
- return (expression) =>
785
- ts.factory.createConditionalExpression(
786
- ts.factory.createStrictInequality(
787
- ts.factory.createStringLiteral("function"),
788
- ValueFactory.TYPEOF(input),
789
- ),
790
- undefined,
791
- expression,
792
- undefined,
793
- decode_functional(explore),
794
- );
795
- };
796
-
797
- const iterate = (
798
- importer: FunctionImporter,
799
- input: ts.Expression,
800
- unions: IUnion[],
801
- expected: string,
802
- ) =>
803
- ts.factory.createBlock(
804
- [
805
- ...unions.map((u) =>
806
- ts.factory.createIfStatement(
807
- u.is(),
808
- ts.factory.createReturnStatement(u.value()),
809
- ),
810
- ),
811
- create_throw_error(importer)(expected)(input),
812
- ],
813
- true,
814
- );
815
-
816
- /* -----------------------------------------------------------
817
- CONFIGURATIONS
818
- ----------------------------------------------------------- */
819
- const PREFIX = "$s";
820
-
821
- const configure =
822
- (project: IProject) =>
823
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
824
- const config: FeatureProgrammer.IConfig = {
825
- types: {
826
- input: (type, name) =>
827
- ts.factory.createTypeReferenceNode(
828
- name ?? TypeFactory.getFullName(project.checker)(type),
829
- ),
830
- output: () => TypeFactory.keyword("string"),
831
- },
832
- prefix: PREFIX,
833
- trace: false,
834
- path: false,
835
- initializer,
836
- decoder: () => decode(project)(config)(importer),
837
- objector: {
838
- checker: () => (input, meta, explore) =>
839
- IsProgrammer.decode(project)(importer)(input, meta, explore),
840
- decoder: () => decode_object(importer),
841
- joiner: StringifyJoiner.object(importer),
842
- unionizer: decode_union_object(
843
- IsProgrammer.decode_object(project)(importer),
844
- )(decode_object(importer))((exp) => exp)((value, expected) =>
845
- create_throw_error(importer)(expected)(value),
846
- ),
847
- failure: (input, expected) =>
848
- create_throw_error(importer)(expected)(input),
849
- },
850
- generator: {
851
- arrays: () => write_array_functions(config)(importer),
852
- tuples: () => write_tuple_functions(project)(config)(importer),
853
- },
854
- };
855
- return config;
856
- };
857
-
858
- const initializer: FeatureProgrammer.IConfig["initializer"] =
859
- (project) => (importer) => (type) =>
860
- JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
861
- project.checker,
862
- project.context,
863
- )(type);
864
-
865
- const create_throw_error =
866
- (importer: FunctionImporter) =>
867
- (expected: string) =>
868
- (value: ts.Expression) =>
869
- ts.factory.createExpressionStatement(
870
- ts.factory.createCallExpression(
871
- importer.use("throws"),
872
- [],
873
- [
874
- ts.factory.createObjectLiteralExpression(
875
- [
876
- ts.factory.createPropertyAssignment(
877
- "expected",
878
- ts.factory.createStringLiteral(expected),
879
- ),
880
- ts.factory.createPropertyAssignment("value", value),
881
- ],
882
- true,
883
- ),
884
- ],
885
- ),
886
- );
887
- }
888
-
889
- interface IUnion {
890
- type: string;
891
- is: () => ts.Expression;
892
- value: () => ts.Expression;
893
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { JsonMetadataFactory } from "../../factories/JsonMetadataFactory";
6
+ import { MetadataCollection } from "../../factories/MetadataCollection";
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 { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
14
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
16
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
17
+
18
+ import { IProject } from "../../transformers/IProject";
19
+
20
+ import { Atomic } from "../../typings/Atomic";
21
+
22
+ import { ArrayUtil } from "../../utils/ArrayUtil";
23
+
24
+ import { FeatureProgrammer } from "../FeatureProgrammer";
25
+ import { IsProgrammer } from "../IsProgrammer";
26
+ import { AtomicPredicator } from "../helpers/AtomicPredicator";
27
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
28
+ import { IExpressionEntry } from "../helpers/IExpressionEntry";
29
+ import { OptionPredicator } from "../helpers/OptionPredicator";
30
+ import { StringifyJoiner } from "../helpers/StringifyJoinder";
31
+ import { StringifyPredicator } from "../helpers/StringifyPredicator";
32
+ import { UnionExplorer } from "../helpers/UnionExplorer";
33
+ import { check_native } from "../internal/check_native";
34
+ import { decode_union_object } from "../internal/decode_union_object";
35
+ import { feature_object_entries } from "../internal/feature_object_entries";
36
+ import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
37
+
38
+ export namespace JsonStringifyProgrammer {
39
+ /* -----------------------------------------------------------
40
+ WRITER
41
+ ----------------------------------------------------------- */
42
+
43
+ export const write =
44
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
+ const importer: FunctionImporter = new FunctionImporter(modulo.getText());
46
+ const config: FeatureProgrammer.IConfig = configure(project)(importer);
47
+
48
+ return FeatureProgrammer.write(project)({
49
+ ...config,
50
+ addition: (collection) => [
51
+ ...IsProgrammer.write_function_statements(project)(importer)(
52
+ collection,
53
+ ),
54
+ ...importer.declare(modulo),
55
+ ],
56
+ })(importer);
57
+ };
58
+
59
+ const write_array_functions =
60
+ (config: FeatureProgrammer.IConfig) =>
61
+ (importer: FunctionImporter) =>
62
+ (collection: MetadataCollection): ts.VariableStatement[] =>
63
+ collection
64
+ .arrays()
65
+ .filter((a) => a.recursive)
66
+ .map((type, i) =>
67
+ StatementFactory.constant(
68
+ `${config.prefix}a${i}`,
69
+ ts.factory.createArrowFunction(
70
+ undefined,
71
+ undefined,
72
+ FeatureProgrammer.parameterDeclarations(config)(
73
+ TypeFactory.keyword("any"),
74
+ )(ts.factory.createIdentifier("input")),
75
+ TypeFactory.keyword("any"),
76
+ undefined,
77
+ decode_array_inline(config)(importer)(
78
+ ts.factory.createIdentifier("input"),
79
+ MetadataArray.create({
80
+ type,
81
+ tags: [],
82
+ }),
83
+ {
84
+ tracable: config.trace,
85
+ source: "function",
86
+ from: "array",
87
+ postfix: "",
88
+ },
89
+ ),
90
+ ),
91
+ ),
92
+ );
93
+
94
+ const write_tuple_functions =
95
+ (project: IProject) =>
96
+ (config: FeatureProgrammer.IConfig) =>
97
+ (importer: FunctionImporter) =>
98
+ (collection: MetadataCollection): ts.VariableStatement[] =>
99
+ collection
100
+ .tuples()
101
+ .filter((t) => t.recursive)
102
+ .map((tuple, i) =>
103
+ StatementFactory.constant(
104
+ `${config.prefix}t${i}`,
105
+ ts.factory.createArrowFunction(
106
+ undefined,
107
+ undefined,
108
+ FeatureProgrammer.parameterDeclarations(config)(
109
+ TypeFactory.keyword("any"),
110
+ )(ts.factory.createIdentifier("input")),
111
+ TypeFactory.keyword("any"),
112
+ undefined,
113
+ decode_tuple_inline(project)(config)(importer)(
114
+ ts.factory.createIdentifier("input"),
115
+ tuple,
116
+ {
117
+ tracable: config.trace,
118
+ source: "function",
119
+ from: "array",
120
+ postfix: "",
121
+ },
122
+ ),
123
+ ),
124
+ ),
125
+ );
126
+
127
+ /* -----------------------------------------------------------
128
+ DECODERS
129
+ ----------------------------------------------------------- */
130
+ const decode =
131
+ (project: IProject) =>
132
+ (config: FeatureProgrammer.IConfig) =>
133
+ (importer: FunctionImporter) =>
134
+ (
135
+ input: ts.Expression,
136
+ meta: Metadata,
137
+ explore: FeatureProgrammer.IExplore,
138
+ ): ts.Expression => {
139
+ // ANY TYPE
140
+ if (meta.any === true)
141
+ return wrap_required(
142
+ input,
143
+ meta,
144
+ explore,
145
+ )(
146
+ wrap_functional(
147
+ input,
148
+ meta,
149
+ explore,
150
+ )(
151
+ ts.factory.createCallExpression(
152
+ ts.factory.createIdentifier("JSON.stringify"),
153
+ undefined,
154
+ [input],
155
+ ),
156
+ ),
157
+ );
158
+
159
+ // ONLY NULL OR UNDEFINED
160
+ const size: number = meta.size();
161
+ if (
162
+ size === 0 &&
163
+ (meta.isRequired() === false || meta.nullable === true)
164
+ ) {
165
+ if (meta.isRequired() === false && meta.nullable === true)
166
+ return explore.from === "array"
167
+ ? ts.factory.createStringLiteral("null")
168
+ : ts.factory.createConditionalExpression(
169
+ ts.factory.createStrictEquality(ts.factory.createNull(), input),
170
+ undefined,
171
+ ts.factory.createStringLiteral("null"),
172
+ undefined,
173
+ ts.factory.createIdentifier("undefined"),
174
+ );
175
+ else if (meta.isRequired() === false)
176
+ return explore.from === "array"
177
+ ? ts.factory.createStringLiteral("null")
178
+ : ts.factory.createIdentifier("undefined");
179
+ else return ts.factory.createStringLiteral("null");
180
+ }
181
+
182
+ //----
183
+ // LIST UP UNION TYPES
184
+ //----
185
+ const unions: IUnion[] = [];
186
+
187
+ // toJSON() METHOD
188
+ if (meta.escaped !== null)
189
+ unions.push({
190
+ type: "resolved",
191
+ is: () => IsProgrammer.decode_to_json(false)(input),
192
+ value: () =>
193
+ decode_to_json(project)(config)(importer)(
194
+ input,
195
+ meta.escaped!.returns,
196
+ explore,
197
+ ),
198
+ });
199
+ else if (meta.functional === true)
200
+ unions.push({
201
+ type: "functional",
202
+ is: () => IsProgrammer.decode_functional(input),
203
+ value: () => decode_functional(explore),
204
+ });
205
+
206
+ // TEMPLATES
207
+ if (
208
+ meta.templates.length ||
209
+ ArrayUtil.has(meta.constants, (c) => c.type === "string")
210
+ )
211
+ if (AtomicPredicator.template(meta)) {
212
+ const partial = Metadata.initialize();
213
+ partial.atomics.push(
214
+ MetadataAtomic.create({ type: "string", tags: [] }),
215
+ ),
216
+ unions.push({
217
+ type: "template literal",
218
+ is: () =>
219
+ IsProgrammer.decode(project)(importer)(input, partial, explore),
220
+ value: () =>
221
+ decode_atomic(project)(importer)(input, "string", explore),
222
+ });
223
+ }
224
+
225
+ // CONSTANTS
226
+ for (const constant of meta.constants)
227
+ if (AtomicPredicator.constant(meta)(constant.type) === false) continue;
228
+ else if (constant.type !== "string")
229
+ unions.push({
230
+ type: "atomic",
231
+ is: () =>
232
+ IsProgrammer.decode(project)(importer)(
233
+ input,
234
+ (() => {
235
+ const partial = Metadata.initialize();
236
+ partial.atomics.push(
237
+ MetadataAtomic.create({
238
+ type: constant.type,
239
+ tags: [],
240
+ }),
241
+ );
242
+ return partial;
243
+ })(),
244
+ explore,
245
+ ),
246
+ value: () =>
247
+ decode_atomic(project)(importer)(input, constant.type, explore),
248
+ });
249
+ else if (meta.templates.length === 0)
250
+ unions.push({
251
+ type: "const string",
252
+ is: () =>
253
+ IsProgrammer.decode(project)(importer)(
254
+ input,
255
+ (() => {
256
+ const partial = Metadata.initialize();
257
+ partial.atomics.push(
258
+ MetadataAtomic.create({
259
+ type: "string",
260
+ tags: [],
261
+ }),
262
+ );
263
+ return partial;
264
+ })(),
265
+ explore,
266
+ ),
267
+ value: () =>
268
+ decode_constant_string(project)(importer)(
269
+ input,
270
+ [...constant.values] as string[],
271
+ explore,
272
+ ),
273
+ });
274
+
275
+ /// ATOMICS
276
+ for (const a of meta.atomics)
277
+ if (AtomicPredicator.atomic(meta)(a.type))
278
+ unions.push({
279
+ type: "atomic",
280
+ is: () =>
281
+ IsProgrammer.decode(project)(importer)(
282
+ input,
283
+ (() => {
284
+ const partial = Metadata.initialize();
285
+ partial.atomics.push(a);
286
+ return partial;
287
+ })(),
288
+ explore,
289
+ ),
290
+ value: () =>
291
+ decode_atomic(project)(importer)(input, a.type, explore),
292
+ });
293
+
294
+ // TUPLES
295
+ for (const tuple of meta.tuples)
296
+ unions.push({
297
+ type: "tuple",
298
+ is: () =>
299
+ IsProgrammer.decode(project)(importer)(
300
+ input,
301
+ (() => {
302
+ const partial = Metadata.initialize();
303
+ partial.tuples.push(tuple);
304
+ return partial;
305
+ })(),
306
+ explore,
307
+ ),
308
+ value: () =>
309
+ decode_tuple(project)(config)(importer)(input, tuple, explore),
310
+ });
311
+
312
+ // ARRAYS
313
+ if (meta.arrays.length) {
314
+ const value: () => ts.Expression =
315
+ meta.arrays.length === 1
316
+ ? () =>
317
+ decode_array(config)(importer)(input, meta.arrays[0]!, {
318
+ ...explore,
319
+ from: "array",
320
+ })
321
+ : meta.arrays.some((elem) => elem.type.value.any)
322
+ ? () =>
323
+ ts.factory.createCallExpression(
324
+ ts.factory.createIdentifier("JSON.stringify"),
325
+ undefined,
326
+ [input],
327
+ )
328
+ : () =>
329
+ explore_arrays(project)(config)(importer)(input, meta.arrays, {
330
+ ...explore,
331
+ from: "array",
332
+ });
333
+
334
+ unions.push({
335
+ type: "array",
336
+ is: () => ExpressionFactory.isArray(input),
337
+ value,
338
+ });
339
+ }
340
+
341
+ // BUILT-IN CLASSES
342
+ if (meta.natives.length)
343
+ for (const native of meta.natives)
344
+ unions.push({
345
+ type: "object",
346
+ is: () => check_native(native)(input),
347
+ value: () =>
348
+ AtomicPredicator.native(native)
349
+ ? decode_atomic(project)(importer)(
350
+ input,
351
+ native.toLowerCase() as Atomic.Literal,
352
+ explore,
353
+ )
354
+ : ts.factory.createStringLiteral("{}"),
355
+ });
356
+
357
+ // SETS
358
+ if (meta.sets.length)
359
+ unions.push({
360
+ type: "object",
361
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
362
+ value: () => ts.factory.createStringLiteral("{}"),
363
+ });
364
+
365
+ // MAPS
366
+ if (meta.maps.length)
367
+ unions.push({
368
+ type: "object",
369
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
370
+ value: () => ts.factory.createStringLiteral("{}"),
371
+ });
372
+
373
+ // OBJECTS
374
+ if (meta.objects.length)
375
+ unions.push({
376
+ type: "object",
377
+ is: () =>
378
+ ExpressionFactory.isObject({
379
+ checkNull: true,
380
+ checkArray: meta.objects.some((obj) =>
381
+ obj.properties.every(
382
+ (prop) =>
383
+ !prop.key.isSoleLiteral() || !prop.value.isRequired(),
384
+ ),
385
+ ),
386
+ })(input),
387
+ value: () =>
388
+ meta.isParentResolved() === false &&
389
+ meta.objects.length === 1 &&
390
+ meta.objects[0]!._Is_simple(explore.from === "top" ? 0 : 1)
391
+ ? (() => {
392
+ const obj: MetadataObject = meta.objects[0]!;
393
+ const entries: IExpressionEntry<ts.Expression>[] =
394
+ feature_object_entries({
395
+ decoder: () => decode(project)(config)(importer),
396
+ trace: false,
397
+ path: false,
398
+ })(importer)(obj)(
399
+ ts.factory.createAsExpression(
400
+ input,
401
+ TypeFactory.keyword("any"),
402
+ ),
403
+ );
404
+ return StringifyJoiner.object(importer)(
405
+ ts.factory.createAsExpression(
406
+ input,
407
+ TypeFactory.keyword("any"),
408
+ ),
409
+ entries,
410
+ );
411
+ })()
412
+ : explore_objects(config)(importer)(input, meta, {
413
+ ...explore,
414
+ from: "object",
415
+ }),
416
+ });
417
+
418
+ //----
419
+ // RETURNS
420
+ //----
421
+ // CHECK NULL AND UNDEFINED
422
+ const wrapper = (output: ts.Expression) =>
423
+ wrap_required(input, meta, explore)(wrap_nullable(input, meta)(output));
424
+
425
+ // DIRECT RETURN
426
+ if (unions.length === 0)
427
+ return ts.factory.createCallExpression(
428
+ ts.factory.createIdentifier("JSON.stringify"),
429
+ undefined,
430
+ [input],
431
+ );
432
+ else if (unions.length === 1) return wrapper(unions[0]!.value());
433
+
434
+ // RETURN WITH TYPE CHECKING
435
+ return wrapper(
436
+ ts.factory.createCallExpression(
437
+ ts.factory.createArrowFunction(
438
+ undefined,
439
+ undefined,
440
+ [],
441
+ undefined,
442
+ undefined,
443
+ iterate(importer, input, unions, meta.getName()),
444
+ ),
445
+ undefined,
446
+ undefined,
447
+ ),
448
+ );
449
+ };
450
+
451
+ const decode_object = (importer: FunctionImporter) =>
452
+ FeatureProgrammer.decode_object({
453
+ trace: false,
454
+ path: false,
455
+ prefix: PREFIX,
456
+ })(importer);
457
+
458
+ const decode_array =
459
+ (config: FeatureProgrammer.IConfig) =>
460
+ (importer: FunctionImporter) =>
461
+ (
462
+ input: ts.Expression,
463
+ array: MetadataArray,
464
+ explore: FeatureProgrammer.IExplore,
465
+ ) =>
466
+ array.type.recursive
467
+ ? ts.factory.createCallExpression(
468
+ ts.factory.createIdentifier(
469
+ importer.useLocal(`${config.prefix}a${array.type.index}`),
470
+ ),
471
+ undefined,
472
+ FeatureProgrammer.argumentsArray(config)({
473
+ ...explore,
474
+ source: "function",
475
+ from: "array",
476
+ })(input),
477
+ )
478
+ : decode_array_inline(config)(importer)(input, array, explore);
479
+
480
+ const decode_array_inline =
481
+ (config: FeatureProgrammer.IConfig) =>
482
+ (importer: FunctionImporter) =>
483
+ (
484
+ input: ts.Expression,
485
+ array: MetadataArray,
486
+ explore: FeatureProgrammer.IExplore,
487
+ ) =>
488
+ FeatureProgrammer.decode_array(config)(importer)(StringifyJoiner.array)(
489
+ input,
490
+ array,
491
+ explore,
492
+ );
493
+
494
+ const decode_tuple =
495
+ (project: IProject) =>
496
+ (config: FeatureProgrammer.IConfig) =>
497
+ (importer: FunctionImporter) =>
498
+ (
499
+ input: ts.Expression,
500
+ tuple: MetadataTuple,
501
+ explore: FeatureProgrammer.IExplore,
502
+ ): ts.Expression =>
503
+ tuple.type.recursive
504
+ ? ts.factory.createCallExpression(
505
+ ts.factory.createIdentifier(
506
+ importer.useLocal(`${config.prefix}t${tuple.type.index}`),
507
+ ),
508
+ undefined,
509
+ FeatureProgrammer.argumentsArray(config)({
510
+ ...explore,
511
+ source: "function",
512
+ })(input),
513
+ )
514
+ : decode_tuple_inline(project)(config)(importer)(
515
+ input,
516
+ tuple.type,
517
+ explore,
518
+ );
519
+
520
+ const decode_tuple_inline =
521
+ (project: IProject) =>
522
+ (config: FeatureProgrammer.IConfig) =>
523
+ (importer: FunctionImporter) =>
524
+ (
525
+ input: ts.Expression,
526
+ tuple: MetadataTupleType,
527
+ explore: FeatureProgrammer.IExplore,
528
+ ): ts.Expression => {
529
+ const children: ts.Expression[] = tuple.elements
530
+ .filter((elem) => elem.rest === null)
531
+ .map((elem, index) =>
532
+ decode(project)(config)(importer)(
533
+ ts.factory.createElementAccessExpression(input, index),
534
+ elem,
535
+ {
536
+ ...explore,
537
+ from: "array",
538
+ postfix: explore.postfix.length
539
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
540
+ : `"[${index}]"`,
541
+ },
542
+ ),
543
+ );
544
+ const rest = (() => {
545
+ if (tuple.elements.length === 0) return null;
546
+ const last = tuple.elements.at(-1)!;
547
+ if (last.rest === null) return null;
548
+
549
+ const code = decode(project)(config)(importer)(
550
+ ts.factory.createCallExpression(
551
+ IdentifierFactory.access(input)("slice"),
552
+ undefined,
553
+ [ExpressionFactory.number(tuple.elements.length - 1)],
554
+ ),
555
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
556
+ {
557
+ ...explore,
558
+ start: tuple.elements.length - 1,
559
+ },
560
+ );
561
+ return ts.factory.createCallExpression(
562
+ importer.use("rest"),
563
+ undefined,
564
+ [code],
565
+ );
566
+ })();
567
+ return StringifyJoiner.tuple(children, rest);
568
+ };
569
+
570
+ const decode_atomic =
571
+ (project: IProject) =>
572
+ (importer: FunctionImporter) =>
573
+ (
574
+ input: ts.Expression,
575
+ type: string,
576
+ explore: FeatureProgrammer.IExplore,
577
+ ) => {
578
+ if (type === "string")
579
+ return ts.factory.createCallExpression(
580
+ importer.use("string"),
581
+ undefined,
582
+ [input],
583
+ );
584
+ else if (type === "number" && OptionPredicator.numeric(project.options))
585
+ input = ts.factory.createCallExpression(
586
+ importer.use("number"),
587
+ undefined,
588
+ [input],
589
+ );
590
+
591
+ return explore.from !== "top"
592
+ ? input
593
+ : ts.factory.createCallExpression(
594
+ IdentifierFactory.access(input)("toString"),
595
+ undefined,
596
+ undefined,
597
+ );
598
+ };
599
+
600
+ const decode_constant_string =
601
+ (project: IProject) =>
602
+ (importer: FunctionImporter) =>
603
+ (
604
+ input: ts.Expression,
605
+ values: string[],
606
+ explore: FeatureProgrammer.IExplore,
607
+ ): ts.Expression => {
608
+ if (values.every((v) => !StringifyPredicator.require_escape(v)))
609
+ return [
610
+ ts.factory.createStringLiteral('"'),
611
+ input,
612
+ ts.factory.createStringLiteral('"'),
613
+ ].reduce((x, y) => ts.factory.createAdd(x, y));
614
+ else return decode_atomic(project)(importer)(input, "string", explore);
615
+ };
616
+
617
+ const decode_to_json =
618
+ (project: IProject) =>
619
+ (config: FeatureProgrammer.IConfig) =>
620
+ (importer: FunctionImporter) =>
621
+ (
622
+ input: ts.Expression,
623
+ resolved: Metadata,
624
+ explore: FeatureProgrammer.IExplore,
625
+ ): ts.Expression => {
626
+ return decode(project)(config)(importer)(
627
+ ts.factory.createCallExpression(
628
+ IdentifierFactory.access(input)("toJSON"),
629
+ undefined,
630
+ [],
631
+ ),
632
+ resolved,
633
+ explore,
634
+ );
635
+ };
636
+
637
+ const decode_functional = (explore: FeatureProgrammer.IExplore) =>
638
+ explore.from === "array"
639
+ ? ts.factory.createStringLiteral("null")
640
+ : ts.factory.createIdentifier("undefined");
641
+
642
+ /* -----------------------------------------------------------
643
+ EXPLORERS
644
+ ----------------------------------------------------------- */
645
+ const explore_objects =
646
+ (config: FeatureProgrammer.IConfig) =>
647
+ (importer: FunctionImporter) =>
648
+ (
649
+ input: ts.Expression,
650
+ meta: Metadata,
651
+ explore: FeatureProgrammer.IExplore,
652
+ ) =>
653
+ meta.objects.length === 1
654
+ ? decode_object(importer)(input, meta.objects[0]!, explore)
655
+ : ts.factory.createCallExpression(
656
+ ts.factory.createIdentifier(
657
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
658
+ ),
659
+ undefined,
660
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
661
+ );
662
+
663
+ const explore_arrays =
664
+ (project: IProject) =>
665
+ (config: FeatureProgrammer.IConfig) =>
666
+ (importer: FunctionImporter) =>
667
+ (
668
+ input: ts.Expression,
669
+ elements: MetadataArray[],
670
+ explore: FeatureProgrammer.IExplore,
671
+ ): ts.Expression =>
672
+ explore_array_like_union_types(config)(importer)(
673
+ UnionExplorer.array({
674
+ checker: IsProgrammer.decode(project)(importer),
675
+ decoder: decode_array(config)(importer),
676
+ empty: ts.factory.createStringLiteral("[]"),
677
+ success: ts.factory.createTrue(),
678
+ failure: (input, expected) =>
679
+ create_throw_error(importer)(expected)(input),
680
+ }),
681
+ )(input, elements, explore);
682
+
683
+ const explore_array_like_union_types =
684
+ (config: FeatureProgrammer.IConfig) =>
685
+ (importer: FunctionImporter) =>
686
+ <T extends MetadataArray | MetadataTuple>(
687
+ factory: (
688
+ parameters: ts.ParameterDeclaration[],
689
+ ) => (
690
+ input: ts.Expression,
691
+ elements: T[],
692
+ explore: FeatureProgrammer.IExplore,
693
+ ) => ts.ArrowFunction,
694
+ ) =>
695
+ (
696
+ input: ts.Expression,
697
+ elements: T[],
698
+ explore: FeatureProgrammer.IExplore,
699
+ ): ts.Expression => {
700
+ const arrow =
701
+ (parameters: ts.ParameterDeclaration[]) =>
702
+ (explore: FeatureProgrammer.IExplore) =>
703
+ (input: ts.Expression): ts.ArrowFunction =>
704
+ factory(parameters)(input, elements, explore);
705
+ if (elements.every((e) => e.type.recursive === false))
706
+ ts.factory.createCallExpression(
707
+ arrow([])(explore)(input),
708
+ undefined,
709
+ [],
710
+ );
711
+
712
+ explore = {
713
+ ...explore,
714
+ source: "function",
715
+ from: "array",
716
+ };
717
+ return ts.factory.createCallExpression(
718
+ ts.factory.createIdentifier(
719
+ importer.emplaceUnion(
720
+ config.prefix,
721
+ elements.map((e) => e.type.name).join(" | "),
722
+ () =>
723
+ arrow(
724
+ FeatureProgrammer.parameterDeclarations(config)(
725
+ TypeFactory.keyword("any"),
726
+ )(ts.factory.createIdentifier("input")),
727
+ )({
728
+ ...explore,
729
+ postfix: "",
730
+ })(ts.factory.createIdentifier("input")),
731
+ ),
732
+ ),
733
+ undefined,
734
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
735
+ );
736
+ };
737
+
738
+ /* -----------------------------------------------------------
739
+ RETURN SCRIPTS
740
+ ----------------------------------------------------------- */
741
+ const wrap_required = (
742
+ input: ts.Expression,
743
+ meta: Metadata,
744
+ explore: FeatureProgrammer.IExplore,
745
+ ): ((expression: ts.Expression) => ts.Expression) => {
746
+ if (meta.isRequired() === true && meta.any === false)
747
+ return (expression) => expression;
748
+ return (expression) =>
749
+ ts.factory.createConditionalExpression(
750
+ ts.factory.createStrictInequality(
751
+ ts.factory.createIdentifier("undefined"),
752
+ input,
753
+ ),
754
+ undefined,
755
+ expression,
756
+ undefined,
757
+ explore.from === "array"
758
+ ? ts.factory.createStringLiteral("null")
759
+ : ts.factory.createIdentifier("undefined"),
760
+ );
761
+ };
762
+
763
+ const wrap_nullable = (
764
+ input: ts.Expression,
765
+ meta: Metadata,
766
+ ): ((expression: ts.Expression) => ts.Expression) => {
767
+ if (meta.nullable === false) return (expression) => expression;
768
+ return (expression) =>
769
+ ts.factory.createConditionalExpression(
770
+ ts.factory.createStrictInequality(ts.factory.createNull(), input),
771
+ undefined,
772
+ expression,
773
+ undefined,
774
+ ts.factory.createStringLiteral("null"),
775
+ );
776
+ };
777
+
778
+ const wrap_functional = (
779
+ input: ts.Expression,
780
+ meta: Metadata,
781
+ explore: FeatureProgrammer.IExplore,
782
+ ): ((expression: ts.Expression) => ts.Expression) => {
783
+ if (meta.functional === false) return (expression) => expression;
784
+ return (expression) =>
785
+ ts.factory.createConditionalExpression(
786
+ ts.factory.createStrictInequality(
787
+ ts.factory.createStringLiteral("function"),
788
+ ValueFactory.TYPEOF(input),
789
+ ),
790
+ undefined,
791
+ expression,
792
+ undefined,
793
+ decode_functional(explore),
794
+ );
795
+ };
796
+
797
+ const iterate = (
798
+ importer: FunctionImporter,
799
+ input: ts.Expression,
800
+ unions: IUnion[],
801
+ expected: string,
802
+ ) =>
803
+ ts.factory.createBlock(
804
+ [
805
+ ...unions.map((u) =>
806
+ ts.factory.createIfStatement(
807
+ u.is(),
808
+ ts.factory.createReturnStatement(u.value()),
809
+ ),
810
+ ),
811
+ create_throw_error(importer)(expected)(input),
812
+ ],
813
+ true,
814
+ );
815
+
816
+ /* -----------------------------------------------------------
817
+ CONFIGURATIONS
818
+ ----------------------------------------------------------- */
819
+ const PREFIX = "$s";
820
+
821
+ const configure =
822
+ (project: IProject) =>
823
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
824
+ const config: FeatureProgrammer.IConfig = {
825
+ types: {
826
+ input: (type, name) =>
827
+ ts.factory.createTypeReferenceNode(
828
+ name ?? TypeFactory.getFullName(project.checker)(type),
829
+ ),
830
+ output: () => TypeFactory.keyword("string"),
831
+ },
832
+ prefix: PREFIX,
833
+ trace: false,
834
+ path: false,
835
+ initializer,
836
+ decoder: () => decode(project)(config)(importer),
837
+ objector: {
838
+ checker: () => (input, meta, explore) =>
839
+ IsProgrammer.decode(project)(importer)(input, meta, explore),
840
+ decoder: () => decode_object(importer),
841
+ joiner: StringifyJoiner.object(importer),
842
+ unionizer: decode_union_object(
843
+ IsProgrammer.decode_object(project)(importer),
844
+ )(decode_object(importer))((exp) => exp)((value, expected) =>
845
+ create_throw_error(importer)(expected)(value),
846
+ ),
847
+ failure: (input, expected) =>
848
+ create_throw_error(importer)(expected)(input),
849
+ },
850
+ generator: {
851
+ arrays: () => write_array_functions(config)(importer),
852
+ tuples: () => write_tuple_functions(project)(config)(importer),
853
+ },
854
+ };
855
+ return config;
856
+ };
857
+
858
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
859
+ (project) => (importer) => (type) =>
860
+ JsonMetadataFactory.analyze(`typia.json.${importer.method}`)(
861
+ project.checker,
862
+ project.context,
863
+ )(type);
864
+
865
+ const create_throw_error =
866
+ (importer: FunctionImporter) =>
867
+ (expected: string) =>
868
+ (value: ts.Expression) =>
869
+ ts.factory.createExpressionStatement(
870
+ ts.factory.createCallExpression(
871
+ importer.use("throws"),
872
+ [],
873
+ [
874
+ ts.factory.createObjectLiteralExpression(
875
+ [
876
+ ts.factory.createPropertyAssignment(
877
+ "expected",
878
+ ts.factory.createStringLiteral(expected),
879
+ ),
880
+ ts.factory.createPropertyAssignment("value", value),
881
+ ],
882
+ true,
883
+ ),
884
+ ],
885
+ ),
886
+ );
887
+ }
888
+
889
+ interface IUnion {
890
+ type: string;
891
+ is: () => ts.Expression;
892
+ value: () => ts.Expression;
893
+ }