typia 7.3.0-dev.20241213 → 7.3.0

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 (445) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +148 -148
  3. package/lib/executable/typia.js +0 -0
  4. package/package.json +1 -1
  5. package/src/IRandomGenerator.ts +49 -49
  6. package/src/IReadableURLSearchParams.ts +9 -9
  7. package/src/IValidation.ts +21 -21
  8. package/src/executable/TypiaGenerateWizard.ts +83 -83
  9. package/src/executable/TypiaPatchWizard.ts +45 -45
  10. package/src/executable/TypiaSetupWizard.ts +179 -179
  11. package/src/executable/setup/ArgumentParser.ts +42 -42
  12. package/src/executable/setup/FileRetriever.ts +19 -19
  13. package/src/executable/setup/PackageManager.ts +87 -87
  14. package/src/factories/ExpressionFactory.ts +216 -216
  15. package/src/factories/IdentifierFactory.ts +89 -89
  16. package/src/factories/JsonMetadataFactory.ts +76 -76
  17. package/src/factories/LiteralFactory.ts +52 -52
  18. package/src/factories/MetadataCollection.ts +278 -278
  19. package/src/factories/MetadataCommentTagFactory.ts +650 -650
  20. package/src/factories/MetadataFactory.ts +404 -404
  21. package/src/factories/MetadataTypeTagFactory.ts +411 -411
  22. package/src/factories/MetadataTypeTagSchemaFactory.ts +82 -82
  23. package/src/factories/NumericRangeFactory.ts +72 -72
  24. package/src/factories/ProtobufFactory.ts +875 -875
  25. package/src/factories/StatementFactory.ts +90 -90
  26. package/src/factories/TemplateFactory.ts +64 -64
  27. package/src/factories/TypeFactory.ts +140 -140
  28. package/src/factories/internal/metadata/IMetadataIteratorProps.ts +17 -17
  29. package/src/factories/internal/metadata/MetadataHelper.ts +21 -21
  30. package/src/factories/internal/metadata/emplace_metadata_alias.ts +33 -33
  31. package/src/factories/internal/metadata/emplace_metadata_array_type.ts +39 -39
  32. package/src/factories/internal/metadata/emplace_metadata_object.ts +208 -208
  33. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +57 -57
  34. package/src/factories/internal/metadata/explore_metadata.ts +31 -31
  35. package/src/factories/internal/metadata/iterate_metadata.ts +54 -54
  36. package/src/factories/internal/metadata/iterate_metadata_alias.ts +33 -33
  37. package/src/factories/internal/metadata/iterate_metadata_array.ts +63 -63
  38. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +62 -62
  39. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +28 -28
  40. package/src/factories/internal/metadata/iterate_metadata_collection.ts +146 -146
  41. package/src/factories/internal/metadata/iterate_metadata_comment_tags.ts +32 -32
  42. package/src/factories/internal/metadata/iterate_metadata_constant.ts +76 -76
  43. package/src/factories/internal/metadata/iterate_metadata_escape.ts +49 -49
  44. package/src/factories/internal/metadata/iterate_metadata_function.ts +91 -91
  45. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +213 -213
  46. package/src/factories/internal/metadata/iterate_metadata_map.ts +57 -57
  47. package/src/factories/internal/metadata/iterate_metadata_native.ts +255 -255
  48. package/src/factories/internal/metadata/iterate_metadata_object.ts +35 -35
  49. package/src/factories/internal/metadata/iterate_metadata_set.ts +57 -57
  50. package/src/factories/internal/metadata/iterate_metadata_sort.ts +87 -87
  51. package/src/factories/internal/metadata/iterate_metadata_template.ts +41 -41
  52. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +26 -26
  53. package/src/factories/internal/metadata/iterate_metadata_union.ts +19 -19
  54. package/src/functional.ts +750 -750
  55. package/src/http.ts +1047 -1047
  56. package/src/internal/_IProtobufWriter.ts +18 -18
  57. package/src/internal/_ProtobufReader.ts +194 -194
  58. package/src/internal/_ProtobufSizer.ts +145 -145
  59. package/src/internal/_ProtobufWriter.ts +145 -145
  60. package/src/internal/_accessExpressionAsString.ts +46 -46
  61. package/src/internal/_assertGuard.ts +13 -13
  62. package/src/internal/_functionalTypeGuardErrorFactory.ts +4 -4
  63. package/src/internal/_httpFormDataReadArray.ts +4 -4
  64. package/src/internal/_httpFormDataReadBigint.ts +18 -18
  65. package/src/internal/_httpFormDataReadBlob.ts +10 -10
  66. package/src/internal/_httpFormDataReadBoolean.ts +16 -16
  67. package/src/internal/_httpFormDataReadFile.ts +10 -10
  68. package/src/internal/_httpFormDataReadNumber.ts +15 -15
  69. package/src/internal/_httpFormDataReadString.ts +10 -10
  70. package/src/internal/_httpHeaderReadBigint.ts +10 -10
  71. package/src/internal/_httpHeaderReadBoolean.ts +8 -8
  72. package/src/internal/_httpHeaderReadNumber.ts +7 -7
  73. package/src/internal/_httpParameterReadBigint.ts +10 -10
  74. package/src/internal/_httpParameterReadBoolean.ts +8 -8
  75. package/src/internal/_httpParameterReadNumber.ts +7 -7
  76. package/src/internal/_httpParameterReadString.ts +2 -2
  77. package/src/internal/_httpQueryParseURLSearchParams.ts +12 -12
  78. package/src/internal/_httpQueryReadArray.ts +4 -4
  79. package/src/internal/_httpQueryReadBigint.ts +12 -12
  80. package/src/internal/_httpQueryReadBoolean.ts +14 -14
  81. package/src/internal/_httpQueryReadNumber.ts +9 -9
  82. package/src/internal/_httpQueryReadString.ts +4 -4
  83. package/src/internal/_isBetween.ts +2 -2
  84. package/src/internal/_isBigintString.ts +8 -8
  85. package/src/internal/_isFormatByte.ts +7 -7
  86. package/src/internal/_isFormatDate.ts +3 -3
  87. package/src/internal/_isFormatDateTime.ts +4 -4
  88. package/src/internal/_isFormatDuration.ts +4 -4
  89. package/src/internal/_isFormatEmail.ts +4 -4
  90. package/src/internal/_isFormatHostname.ts +4 -4
  91. package/src/internal/_isFormatIdnEmail.ts +4 -4
  92. package/src/internal/_isFormatIdnHostname.ts +4 -4
  93. package/src/internal/_isFormatIpv4.ts +4 -4
  94. package/src/internal/_isFormatIpv6.ts +4 -4
  95. package/src/internal/_isFormatIri.ts +3 -3
  96. package/src/internal/_isFormatIriReference.ts +4 -4
  97. package/src/internal/_isFormatJsonPointer.ts +3 -3
  98. package/src/internal/_isFormatPassword.ts +1 -1
  99. package/src/internal/_isFormatRegex.ts +8 -8
  100. package/src/internal/_isFormatRelativeJsonPointer.ts +4 -4
  101. package/src/internal/_isFormatTime.ts +4 -4
  102. package/src/internal/_isFormatUri.ts +6 -6
  103. package/src/internal/_isFormatUriReference.ts +5 -5
  104. package/src/internal/_isFormatUriTemplate.ts +4 -4
  105. package/src/internal/_isFormatUrl.ts +4 -4
  106. package/src/internal/_isFormatUuid.ts +3 -3
  107. package/src/internal/_isTypeFloat.ts +5 -5
  108. package/src/internal/_isTypeInt32.ts +5 -5
  109. package/src/internal/_isTypeInt64.ts +5 -5
  110. package/src/internal/_isTypeUint32.ts +5 -5
  111. package/src/internal/_isTypeUint64.ts +5 -5
  112. package/src/internal/_isUniqueItems.ts +159 -159
  113. package/src/internal/_jsonStringifyNumber.ts +12 -12
  114. package/src/internal/_jsonStringifyRest.ts +3 -3
  115. package/src/internal/_jsonStringifyString.ts +42 -42
  116. package/src/internal/_jsonStringifyTail.ts +2 -2
  117. package/src/internal/_llmApplicationFinalize.ts +20 -20
  118. package/src/internal/_miscCloneAny.ts +46 -46
  119. package/src/internal/_notationAny.ts +37 -37
  120. package/src/internal/_notationCamel.ts +13 -13
  121. package/src/internal/_notationPascal.ts +8 -8
  122. package/src/internal/_notationSnake.ts +43 -43
  123. package/src/internal/_randomArray.ts +21 -21
  124. package/src/internal/_randomBigint.ts +6 -6
  125. package/src/internal/_randomBoolean.ts +1 -1
  126. package/src/internal/_randomFormatByte.ts +3 -3
  127. package/src/internal/_randomFormatDate.ts +18 -18
  128. package/src/internal/_randomFormatDatetime.ts +16 -16
  129. package/src/internal/_randomFormatDuration.ts +27 -27
  130. package/src/internal/_randomFormatEmail.ts +11 -11
  131. package/src/internal/_randomFormatHostname.ts +6 -6
  132. package/src/internal/_randomFormatIdnEmail.ts +3 -3
  133. package/src/internal/_randomFormatIdnHostname.ts +3 -3
  134. package/src/internal/_randomFormatIpv4.ts +11 -11
  135. package/src/internal/_randomFormatIpv6.ts +11 -11
  136. package/src/internal/_randomFormatIri.ts +3 -3
  137. package/src/internal/_randomFormatIriReference.ts +3 -3
  138. package/src/internal/_randomFormatJsonPointer.ts +7 -7
  139. package/src/internal/_randomFormatPassword.ts +8 -8
  140. package/src/internal/_randomFormatRegex.ts +4 -4
  141. package/src/internal/_randomFormatRelativeJsonPointer.ts +8 -8
  142. package/src/internal/_randomFormatTime.ts +14 -14
  143. package/src/internal/_randomFormatUri.ts +3 -3
  144. package/src/internal/_randomFormatUriReference.ts +3 -3
  145. package/src/internal/_randomFormatUriTemplate.ts +3 -3
  146. package/src/internal/_randomFormatUrl.ts +11 -11
  147. package/src/internal/_randomFormatUuid.ts +6 -6
  148. package/src/internal/_randomInteger.ts +47 -47
  149. package/src/internal/_randomNumber.ts +74 -74
  150. package/src/internal/_randomPattern.ts +10 -10
  151. package/src/internal/_randomPick.ts +9 -9
  152. package/src/internal/_randomString.ts +24 -24
  153. package/src/internal/_throwTypeGuardError.ts +5 -5
  154. package/src/internal/_validateReport.ts +13 -13
  155. package/src/internal/private/__notationCapitalize.ts +2 -2
  156. package/src/internal/private/__notationUnsnake.ts +24 -24
  157. package/src/json.ts +752 -752
  158. package/src/llm.ts +481 -481
  159. package/src/misc.ts +658 -658
  160. package/src/module.ts +937 -937
  161. package/src/notations.ts +827 -827
  162. package/src/programmers/AssertProgrammer.ts +454 -454
  163. package/src/programmers/CheckerProgrammer.ts +1617 -1617
  164. package/src/programmers/FeatureProgrammer.ts +622 -622
  165. package/src/programmers/ImportProgrammer.ts +185 -185
  166. package/src/programmers/IsProgrammer.ts +273 -273
  167. package/src/programmers/RandomProgrammer.ts +1190 -1190
  168. package/src/programmers/TypiaProgrammer.ts +174 -174
  169. package/src/programmers/ValidateProgrammer.ts +439 -439
  170. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +153 -153
  171. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +125 -125
  172. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +115 -115
  173. package/src/programmers/functional/FunctionalIsFunctionProgrammer.ts +72 -72
  174. package/src/programmers/functional/FunctionalIsParametersProgrammer.ts +113 -113
  175. package/src/programmers/functional/FunctionalIsReturnProgrammer.ts +116 -116
  176. package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +119 -119
  177. package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +274 -274
  178. package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +135 -135
  179. package/src/programmers/functional/internal/FunctionalGeneralProgrammer.ts +34 -34
  180. package/src/programmers/helpers/AtomicPredicator.ts +35 -35
  181. package/src/programmers/helpers/CloneJoiner.ts +143 -143
  182. package/src/programmers/helpers/FunctionProgrammer.ts +67 -67
  183. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  184. package/src/programmers/helpers/NotationJoiner.ts +144 -144
  185. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  186. package/src/programmers/helpers/ProtobufUtil.ts +228 -228
  187. package/src/programmers/helpers/PruneJoiner.ts +148 -148
  188. package/src/programmers/helpers/RandomJoiner.ts +168 -168
  189. package/src/programmers/helpers/StringifyJoinder.ts +115 -115
  190. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  191. package/src/programmers/helpers/UnionExplorer.ts +372 -372
  192. package/src/programmers/helpers/UnionPredicator.ts +79 -79
  193. package/src/programmers/helpers/disable_function_programmer_declare.ts +32 -32
  194. package/src/programmers/http/HttpAssertFormDataProgrammer.ts +99 -99
  195. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +99 -99
  196. package/src/programmers/http/HttpAssertQueryProgrammer.ts +105 -105
  197. package/src/programmers/http/HttpFormDataProgrammer.ts +308 -308
  198. package/src/programmers/http/HttpHeadersProgrammer.ts +400 -400
  199. package/src/programmers/http/HttpIsFormDataProgrammer.ts +108 -108
  200. package/src/programmers/http/HttpIsHeadersProgrammer.ts +108 -108
  201. package/src/programmers/http/HttpIsQueryProgrammer.ts +114 -114
  202. package/src/programmers/http/HttpParameterProgrammer.ts +115 -115
  203. package/src/programmers/http/HttpQueryProgrammer.ts +336 -336
  204. package/src/programmers/http/HttpValidateFormDataProgrammer.ts +92 -92
  205. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +92 -92
  206. package/src/programmers/http/HttpValidateQueryProgrammer.ts +98 -98
  207. package/src/programmers/internal/check_array_length.ts +47 -47
  208. package/src/programmers/internal/check_bigint.ts +50 -50
  209. package/src/programmers/internal/check_dynamic_key.ts +201 -201
  210. package/src/programmers/internal/check_dynamic_properties.ts +208 -208
  211. package/src/programmers/internal/check_everything.ts +23 -23
  212. package/src/programmers/internal/check_native.ts +27 -27
  213. package/src/programmers/internal/check_number.ts +112 -112
  214. package/src/programmers/internal/check_object.ts +75 -75
  215. package/src/programmers/internal/check_string.ts +50 -50
  216. package/src/programmers/internal/check_template.ts +48 -48
  217. package/src/programmers/internal/check_union_array_like.ts +335 -335
  218. package/src/programmers/internal/decode_union_object.ts +116 -116
  219. package/src/programmers/internal/feature_object_entries.ts +61 -61
  220. package/src/programmers/internal/json_schema_alias.ts +47 -47
  221. package/src/programmers/internal/json_schema_array.ts +45 -45
  222. package/src/programmers/internal/json_schema_bigint.ts +15 -15
  223. package/src/programmers/internal/json_schema_boolean.ts +15 -15
  224. package/src/programmers/internal/json_schema_constant.ts +26 -26
  225. package/src/programmers/internal/json_schema_description.ts +12 -12
  226. package/src/programmers/internal/json_schema_discriminator.ts +35 -35
  227. package/src/programmers/internal/json_schema_escaped.ts +82 -82
  228. package/src/programmers/internal/json_schema_native.ts +33 -33
  229. package/src/programmers/internal/json_schema_number.ts +15 -15
  230. package/src/programmers/internal/json_schema_object.ts +158 -158
  231. package/src/programmers/internal/json_schema_plugin.ts +18 -18
  232. package/src/programmers/internal/json_schema_station.ts +182 -182
  233. package/src/programmers/internal/json_schema_string.ts +15 -15
  234. package/src/programmers/internal/json_schema_template.ts +55 -55
  235. package/src/programmers/internal/json_schema_title.ts +20 -20
  236. package/src/programmers/internal/json_schema_tuple.ts +35 -35
  237. package/src/programmers/internal/metadata_to_pattern.ts +42 -42
  238. package/src/programmers/internal/postfix_of_tuple.ts +5 -5
  239. package/src/programmers/internal/prune_object_properties.ts +71 -71
  240. package/src/programmers/internal/stringify_dynamic_properties.ts +162 -162
  241. package/src/programmers/internal/stringify_regular_properties.ts +81 -81
  242. package/src/programmers/internal/template_to_pattern.ts +23 -23
  243. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +23 -23
  244. package/src/programmers/json/JsonApplicationProgrammer.ts +279 -279
  245. package/src/programmers/json/JsonAssertParseProgrammer.ts +113 -113
  246. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +115 -115
  247. package/src/programmers/json/JsonIsParseProgrammer.ts +114 -114
  248. package/src/programmers/json/JsonIsStringifyProgrammer.ts +108 -108
  249. package/src/programmers/json/JsonSchemasProgrammer.ts +91 -91
  250. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  251. package/src/programmers/json/JsonValidateParseProgrammer.ts +105 -105
  252. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +124 -124
  253. package/src/programmers/llm/LlmApplicationOfValidateProgrammer.ts +81 -81
  254. package/src/programmers/llm/LlmApplicationProgrammer.ts +277 -277
  255. package/src/programmers/llm/LlmModelPredicator.ts +127 -127
  256. package/src/programmers/llm/LlmParametersProgrammer.ts +90 -90
  257. package/src/programmers/llm/LlmSchemaProgrammer.ts +143 -143
  258. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +95 -95
  259. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +116 -116
  260. package/src/programmers/misc/MiscCloneProgrammer.ts +1032 -1032
  261. package/src/programmers/misc/MiscIsCloneProgrammer.ts +99 -99
  262. package/src/programmers/misc/MiscIsPruneProgrammer.ts +97 -97
  263. package/src/programmers/misc/MiscLiteralsProgrammer.ts +80 -80
  264. package/src/programmers/misc/MiscPruneProgrammer.ts +728 -728
  265. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +111 -111
  266. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +113 -113
  267. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +101 -101
  268. package/src/programmers/notations/NotationGeneralProgrammer.ts +984 -984
  269. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +105 -105
  270. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +119 -119
  271. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +98 -98
  272. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +102 -102
  273. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +654 -654
  274. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +945 -945
  275. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +109 -109
  276. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +98 -98
  277. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +179 -179
  278. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +92 -92
  279. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +119 -119
  280. package/src/protobuf.ts +868 -868
  281. package/src/reflect.ts +57 -57
  282. package/src/schemas/json/IJsonApplication.ts +73 -73
  283. package/src/schemas/json/IJsonSchemaCollection.ts +29 -29
  284. package/src/schemas/json/__IJsonApplication.ts +63 -63
  285. package/src/schemas/llm/ILlmApplicationOfValidate.ts +55 -55
  286. package/src/schemas/llm/ILlmFunctionOfValidate.ts +39 -39
  287. package/src/schemas/metadata/IMetadata.ts +35 -35
  288. package/src/schemas/metadata/IMetadataAlias.ts +6 -6
  289. package/src/schemas/metadata/IMetadataAliasType.ts +12 -12
  290. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  291. package/src/schemas/metadata/IMetadataArray.ts +6 -6
  292. package/src/schemas/metadata/IMetadataComponents.ts +11 -11
  293. package/src/schemas/metadata/IMetadataConstantValue.ts +11 -11
  294. package/src/schemas/metadata/IMetadataDictionary.ts +11 -11
  295. package/src/schemas/metadata/IMetadataMap.ts +8 -8
  296. package/src/schemas/metadata/IMetadataNative.ts +6 -6
  297. package/src/schemas/metadata/IMetadataObject.ts +6 -6
  298. package/src/schemas/metadata/IMetadataObjectType.ts +13 -13
  299. package/src/schemas/metadata/IMetadataSet.ts +7 -7
  300. package/src/schemas/metadata/IMetadataTemplate.ts +7 -7
  301. package/src/schemas/metadata/IMetadataTuple.ts +6 -6
  302. package/src/schemas/metadata/IMetadataTypeTag.ts +16 -16
  303. package/src/schemas/metadata/Metadata.ts +669 -669
  304. package/src/schemas/metadata/MetadataAlias.ts +46 -46
  305. package/src/schemas/metadata/MetadataAliasType.ts +63 -63
  306. package/src/schemas/metadata/MetadataApplication.ts +44 -44
  307. package/src/schemas/metadata/MetadataArray.ts +49 -49
  308. package/src/schemas/metadata/MetadataAtomic.ts +87 -87
  309. package/src/schemas/metadata/MetadataComponents.ts +98 -98
  310. package/src/schemas/metadata/MetadataConstantValue.ts +62 -62
  311. package/src/schemas/metadata/MetadataMap.ts +48 -48
  312. package/src/schemas/metadata/MetadataNative.ts +44 -44
  313. package/src/schemas/metadata/MetadataObject.ts +48 -48
  314. package/src/schemas/metadata/MetadataObjectType.ts +149 -149
  315. package/src/schemas/metadata/MetadataParameter.ts +54 -54
  316. package/src/schemas/metadata/MetadataProperty.ts +59 -59
  317. package/src/schemas/metadata/MetadataSet.ts +45 -45
  318. package/src/schemas/metadata/MetadataTemplate.ts +80 -80
  319. package/src/schemas/metadata/MetadataTuple.ts +32 -32
  320. package/src/schemas/protobuf/IProtobufProperty.ts +6 -6
  321. package/src/schemas/protobuf/IProtobufPropertyType.ts +37 -37
  322. package/src/schemas/protobuf/IProtobufSchema.ts +50 -50
  323. package/src/tags/Example.ts +24 -24
  324. package/src/tags/Examples.ts +16 -16
  325. package/src/tags/Format.ts +50 -50
  326. package/src/tags/JsonSchemaPlugin.ts +8 -8
  327. package/src/tags/Sequence.ts +10 -10
  328. package/src/tags/TagBase.ts +82 -82
  329. package/src/tags/Type.ts +32 -32
  330. package/src/tags/UniqueItems.ts +14 -14
  331. package/src/tags/index.ts +21 -21
  332. package/src/transform.ts +35 -35
  333. package/src/transformers/CallExpressionTransformer.ts +547 -547
  334. package/src/transformers/FileTransformer.ts +136 -136
  335. package/src/transformers/IProgrammerProps.ts +11 -11
  336. package/src/transformers/ITransformOptions.ts +62 -62
  337. package/src/transformers/ITransformProps.ts +9 -9
  338. package/src/transformers/ITypiaContext.ts +18 -18
  339. package/src/transformers/ImportTransformer.ts +81 -81
  340. package/src/transformers/NodeTransformer.ts +17 -17
  341. package/src/transformers/TransformerError.ts +60 -60
  342. package/src/transformers/features/AssertTransformer.ts +24 -24
  343. package/src/transformers/features/CreateAssertTransformer.ts +24 -24
  344. package/src/transformers/features/CreateIsTransformer.ts +18 -18
  345. package/src/transformers/features/CreateRandomTransformer.ts +43 -43
  346. package/src/transformers/features/CreateValidateTransformer.ts +18 -18
  347. package/src/transformers/features/IsTransformer.ts +18 -18
  348. package/src/transformers/features/RandomTransformer.ts +41 -41
  349. package/src/transformers/features/ValidateTransformer.ts +18 -18
  350. package/src/transformers/features/functional/FunctionalGenericTransformer.ts +57 -57
  351. package/src/transformers/features/http/CreateHttpAssertFormDataTransformer.ts +13 -13
  352. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +13 -13
  353. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +13 -13
  354. package/src/transformers/features/http/CreateHttpFormDataTransformer.ts +13 -13
  355. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +13 -13
  356. package/src/transformers/features/http/CreateHttpIsFormDataTransformer.ts +13 -13
  357. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +13 -13
  358. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +13 -13
  359. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +13 -13
  360. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +13 -13
  361. package/src/transformers/features/http/CreateHttpValidateFormDataTransformer.ts +13 -13
  362. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +13 -13
  363. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +13 -13
  364. package/src/transformers/features/http/HttpAssertFormDataTransformer.ts +13 -13
  365. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +13 -13
  366. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +13 -13
  367. package/src/transformers/features/http/HttpFormDataTransformer.ts +13 -13
  368. package/src/transformers/features/http/HttpHeadersTransformer.ts +13 -13
  369. package/src/transformers/features/http/HttpIsFormDataTransformer.ts +13 -13
  370. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +13 -13
  371. package/src/transformers/features/http/HttpIsQueryTransformer.ts +13 -13
  372. package/src/transformers/features/http/HttpParameterTransformer.ts +13 -13
  373. package/src/transformers/features/http/HttpQueryTransformer.ts +13 -13
  374. package/src/transformers/features/http/HttpValidateFormDataTransformer.ts +13 -13
  375. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +13 -13
  376. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +13 -13
  377. package/src/transformers/features/json/JsonApplicationTransformer.ts +105 -105
  378. package/src/transformers/features/json/JsonAssertParseTransformer.ts +13 -13
  379. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +13 -13
  380. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +13 -13
  381. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +13 -13
  382. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +13 -13
  383. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +13 -13
  384. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +13 -13
  385. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +13 -13
  386. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +13 -13
  387. package/src/transformers/features/json/JsonIsParseTransformer.ts +13 -13
  388. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +13 -13
  389. package/src/transformers/features/json/JsonSchemasTransformer.ts +143 -143
  390. package/src/transformers/features/json/JsonStringifyTransformer.ts +13 -13
  391. package/src/transformers/features/json/JsonValidateParseTransformer.ts +13 -13
  392. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +13 -13
  393. package/src/transformers/features/llm/LlmApplicationOfValidateTransformer.ts +115 -115
  394. package/src/transformers/features/llm/LlmApplicationTransformer.ts +113 -113
  395. package/src/transformers/features/llm/LlmParametersTransformer.ts +89 -89
  396. package/src/transformers/features/llm/LlmSchemaTransformer.ts +130 -130
  397. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +13 -13
  398. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +13 -13
  399. package/src/transformers/features/misc/MiscCloneTransformer.ts +13 -13
  400. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +13 -13
  401. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +13 -13
  402. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +13 -13
  403. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +13 -13
  404. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +13 -13
  405. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +13 -13
  406. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +13 -13
  407. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +13 -13
  408. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +13 -13
  409. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +13 -13
  410. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +35 -35
  411. package/src/transformers/features/misc/MiscPruneTransformer.ts +13 -13
  412. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +13 -13
  413. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +13 -13
  414. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +20 -20
  415. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +20 -20
  416. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +20 -20
  417. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +20 -20
  418. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +20 -20
  419. package/src/transformers/features/notations/NotationGeneralTransformer.ts +18 -18
  420. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +20 -20
  421. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +20 -20
  422. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +13 -13
  423. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +13 -13
  424. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +13 -13
  425. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +13 -13
  426. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +13 -13
  427. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +13 -13
  428. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +13 -13
  429. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +13 -13
  430. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +13 -13
  431. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +13 -13
  432. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +13 -13
  433. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +13 -13
  434. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +13 -13
  435. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +13 -13
  436. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +35 -35
  437. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +13 -13
  438. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +13 -13
  439. package/src/transformers/features/reflect/ReflectMetadataTransformer.ts +69 -69
  440. package/src/transformers/features/reflect/ReflectNameTransformer.ts +82 -82
  441. package/src/transformers/internal/GenericTransformer.ts +101 -101
  442. package/src/utils/MapUtil.ts +14 -14
  443. package/src/utils/NamingConvention.ts +94 -94
  444. package/src/utils/ProtobufNameEncoder.ts +32 -32
  445. package/src/utils/StringUtil.ts +16 -16
@@ -1,1617 +1,1617 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../factories/MetadataCollection";
6
- import { MetadataFactory } from "../factories/MetadataFactory";
7
- import { StatementFactory } from "../factories/StatementFactory";
8
- import { TypeFactory } from "../factories/TypeFactory";
9
- import { ValueFactory } from "../factories/ValueFactory";
10
-
11
- import { Metadata } from "../schemas/metadata/Metadata";
12
- import { MetadataArray } from "../schemas/metadata/MetadataArray";
13
- import { MetadataConstant } from "../schemas/metadata/MetadataConstant";
14
- import { MetadataMap } from "../schemas/metadata/MetadataMap";
15
- import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
16
- import { MetadataSet } from "../schemas/metadata/MetadataSet";
17
- import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
18
- import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
19
-
20
- import { ITypiaContext } from "../transformers/ITypiaContext";
21
- import { TransformerError } from "../transformers/TransformerError";
22
-
23
- import { FeatureProgrammer } from "./FeatureProgrammer";
24
- import { IsProgrammer } from "./IsProgrammer";
25
- import { AtomicPredicator } from "./helpers/AtomicPredicator";
26
- import { FunctionProgrammer } from "./helpers/FunctionProgrammer";
27
- import { ICheckEntry } from "./helpers/ICheckEntry";
28
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
- import { OptionPredicator } from "./helpers/OptionPredicator";
30
- import { UnionExplorer } from "./helpers/UnionExplorer";
31
- import { check_array_length } from "./internal/check_array_length";
32
- import { check_bigint } from "./internal/check_bigint";
33
- import { check_native } from "./internal/check_native";
34
- import { check_number } from "./internal/check_number";
35
- import { check_string } from "./internal/check_string";
36
- import { check_template } from "./internal/check_template";
37
- import { decode_union_object } from "./internal/decode_union_object";
38
- import { postfix_of_tuple } from "./internal/postfix_of_tuple";
39
- import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
40
-
41
- export namespace CheckerProgrammer {
42
- export interface IConfig {
43
- prefix: string;
44
- path: boolean;
45
- trace: boolean;
46
- equals: boolean;
47
- numeric: boolean;
48
- addition?: () => ts.Statement[];
49
- decoder?: (props: {
50
- metadata: Metadata;
51
- input: ts.Expression;
52
- explore: IExplore;
53
- }) => ts.Expression;
54
- combiner: IConfig.Combiner;
55
- atomist: (props: {
56
- entry: ICheckEntry;
57
- input: ts.Expression;
58
- explore: IExplore;
59
- }) => ts.Expression;
60
- joiner: IConfig.IJoiner;
61
- success: ts.Expression;
62
- }
63
- export namespace IConfig {
64
- export interface Combiner {
65
- (props: {
66
- explore: IExplore;
67
- logic: "and" | "or";
68
- input: ts.Expression;
69
- binaries: IBinary[];
70
- expected: string;
71
- }): ts.Expression;
72
- }
73
- export interface IJoiner {
74
- object(props: {
75
- input: ts.Expression;
76
- entries: IExpressionEntry<ts.Expression>[];
77
- }): ts.Expression;
78
- array(props: {
79
- input: ts.Expression;
80
- arrow: ts.ArrowFunction;
81
- }): ts.Expression;
82
- tuple?: undefined | ((exprs: ts.Expression[]) => ts.Expression);
83
-
84
- failure(props: {
85
- input: ts.Expression;
86
- expected: string;
87
- explore?: undefined | FeatureProgrammer.IExplore;
88
- }): ts.Expression;
89
- is?(expression: ts.Expression): ts.Expression;
90
- required?(exp: ts.Expression): ts.Expression;
91
- full?:
92
- | undefined
93
- | ((props: {
94
- condition: ts.Expression;
95
- input: ts.Expression;
96
- expected: string;
97
- explore: IExplore;
98
- }) => ts.Expression);
99
- }
100
- }
101
- export type IExplore = FeatureProgrammer.IExplore;
102
-
103
- export interface IBinary {
104
- expression: ts.Expression;
105
- combined: boolean;
106
- }
107
-
108
- /* -----------------------------------------------------------
109
- WRITERS
110
- ----------------------------------------------------------- */
111
- export const compose = (props: {
112
- context: ITypiaContext;
113
- config: IConfig;
114
- functor: FunctionProgrammer;
115
- type: ts.Type;
116
- name: string | undefined;
117
- }): FeatureProgrammer.IComposed =>
118
- FeatureProgrammer.compose({
119
- ...props,
120
- config: configure(props),
121
- });
122
-
123
- export const write = (props: {
124
- context: ITypiaContext;
125
- config: IConfig;
126
- functor: FunctionProgrammer;
127
- type: ts.Type;
128
- name?: string;
129
- }): ts.ArrowFunction =>
130
- FeatureProgrammer.write({
131
- config: configure(props),
132
- context: props.context,
133
- functor: props.functor,
134
- type: props.type,
135
- name: props.name,
136
- });
137
-
138
- export const write_object_functions = (props: {
139
- context: ITypiaContext;
140
- config: IConfig;
141
- functor: FunctionProgrammer;
142
- collection: MetadataCollection;
143
- }): ts.VariableStatement[] =>
144
- FeatureProgrammer.write_object_functions({
145
- config: configure(props),
146
- context: props.context,
147
- collection: props.collection,
148
- });
149
-
150
- export const write_union_functions = (props: {
151
- context: ITypiaContext;
152
- config: IConfig;
153
- functor: FunctionProgrammer;
154
- collection: MetadataCollection;
155
- }): ts.VariableStatement[] =>
156
- FeatureProgrammer.write_union_functions({
157
- config: configure({
158
- context: props.context,
159
- config: {
160
- ...props.config,
161
- numeric: false,
162
- },
163
- functor: props.functor,
164
- }),
165
- collection: props.collection,
166
- });
167
-
168
- export const write_array_functions = (props: {
169
- context: ITypiaContext;
170
- config: IConfig;
171
- functor: FunctionProgrammer;
172
- collection: MetadataCollection;
173
- }): ts.VariableStatement[] =>
174
- props.collection
175
- .arrays()
176
- .filter((a) => a.recursive)
177
- .map((type, i) =>
178
- StatementFactory.constant({
179
- name: `${props.config.prefix}a${i}`,
180
- value: ts.factory.createArrowFunction(
181
- undefined,
182
- undefined,
183
- FeatureProgrammer.parameterDeclarations({
184
- config: props.config,
185
- type: TypeFactory.keyword("any"),
186
- input: ts.factory.createIdentifier("input"),
187
- }),
188
- TypeFactory.keyword("any"),
189
- undefined,
190
- decode_array_inline({
191
- ...props,
192
- input: ts.factory.createIdentifier("input"),
193
- array: MetadataArray.create({
194
- type,
195
- tags: [],
196
- }),
197
- explore: {
198
- tracable: props.config.trace,
199
- source: "function",
200
- from: "array",
201
- postfix: "",
202
- },
203
- }),
204
- ),
205
- }),
206
- );
207
-
208
- export const write_tuple_functions = (props: {
209
- context: ITypiaContext;
210
- config: IConfig;
211
- functor: FunctionProgrammer;
212
- collection: MetadataCollection;
213
- }): ts.VariableStatement[] =>
214
- props.collection
215
- .tuples()
216
- .filter((t) => t.recursive)
217
- .map((tuple, i) =>
218
- StatementFactory.constant({
219
- name: `${props.config.prefix}t${i}`,
220
- value: ts.factory.createArrowFunction(
221
- undefined,
222
- undefined,
223
- FeatureProgrammer.parameterDeclarations({
224
- config: props.config,
225
- type: TypeFactory.keyword("any"),
226
- input: ts.factory.createIdentifier("input"),
227
- }),
228
- TypeFactory.keyword("any"),
229
- undefined,
230
- decode_tuple_inline({
231
- config: props.config,
232
- context: props.context,
233
- functor: props.functor,
234
- input: ts.factory.createIdentifier("input"),
235
- tuple,
236
- explore: {
237
- tracable: props.config.trace,
238
- source: "function",
239
- from: "array",
240
- postfix: "",
241
- },
242
- }),
243
- ),
244
- }),
245
- );
246
-
247
- const configure = (props: {
248
- context: ITypiaContext;
249
- config: IConfig;
250
- functor: FunctionProgrammer;
251
- }): FeatureProgrammer.IConfig => ({
252
- types: {
253
- input: () => TypeFactory.keyword("any"),
254
- output: (type, name) =>
255
- ts.factory.createTypePredicateNode(
256
- undefined,
257
- "input",
258
- ts.factory.createTypeReferenceNode(
259
- name ??
260
- TypeFactory.getFullName({ checker: props.context.checker, type }),
261
- ),
262
- ),
263
- },
264
- trace: props.config.trace,
265
- path: props.config.path,
266
- prefix: props.config.prefix,
267
- initializer: (next) => {
268
- const collection: MetadataCollection = new MetadataCollection();
269
- const result = MetadataFactory.analyze({
270
- checker: next.context.checker,
271
- transformer: next.context.transformer,
272
- options: {
273
- escape: false,
274
- constant: true,
275
- absorb: true,
276
- },
277
- collection,
278
- type: next.type,
279
- });
280
- if (result.success === false)
281
- throw TransformerError.from({
282
- code: next.functor.method,
283
- errors: result.errors,
284
- });
285
- return {
286
- collection,
287
- metadata: result.data,
288
- };
289
- },
290
- addition: props.config.addition,
291
- decoder: props.config.decoder
292
- ? (next) => props.config.decoder!(next)
293
- : (next) =>
294
- decode({
295
- context: props.context,
296
- config: props.config,
297
- functor: props.functor,
298
- input: next.input,
299
- metadata: next.metadata,
300
- explore: next.explore,
301
- }),
302
- objector: {
303
- checker: props.config.decoder
304
- ? (next) => props.config.decoder!(next)
305
- : (next) =>
306
- decode({
307
- context: props.context,
308
- config: props.config,
309
- functor: props.functor,
310
- input: next.input,
311
- metadata: next.metadata,
312
- explore: next.explore,
313
- }),
314
- decoder: (next) =>
315
- decode_object({
316
- config: props.config,
317
- functor: props.functor,
318
- input: next.input,
319
- object: next.object,
320
- explore: next.explore,
321
- }),
322
- joiner: props.config.joiner.object,
323
- unionizer: props.config.equals
324
- ? (next) =>
325
- decode_union_object({
326
- checker: (v) =>
327
- decode_object({
328
- config: props.config,
329
- functor: props.functor,
330
- object: v.object,
331
- input: v.input,
332
- explore: v.explore,
333
- }),
334
- decoder: (v) =>
335
- decode_object({
336
- config: props.config,
337
- functor: props.functor,
338
- input: v.input,
339
- object: v.object,
340
- explore: {
341
- ...v.explore,
342
- tracable: true,
343
- },
344
- }),
345
- success: props.config.joiner.is ?? ((expr) => expr),
346
- escaper: (v) =>
347
- ts.factory.createReturnStatement(
348
- props.config.joiner.failure(v),
349
- ),
350
- input: next.input,
351
- objects: next.objects,
352
- explore: next.explore,
353
- })
354
- : (next) =>
355
- props.config.combiner({
356
- logic: "or",
357
- explore: next.explore,
358
- input: next.input,
359
- binaries: next.objects.map((object) => ({
360
- expression: decode_object({
361
- config: props.config,
362
- functor: props.functor,
363
- object,
364
- input: next.input,
365
- explore: next.explore,
366
- }),
367
- combined: true,
368
- })),
369
- expected: `(${next.objects.map((t) => t.name).join(" | ")})`,
370
- }),
371
- failure: (next) =>
372
- ts.factory.createReturnStatement(props.config.joiner.failure(next)),
373
- is: props.config.joiner.is,
374
- required: props.config.joiner.required,
375
- full: props.config.joiner.full
376
- ? (next) => props.config.joiner.full!(next)
377
- : undefined,
378
- type: TypeFactory.keyword("boolean"),
379
- },
380
- generator: {
381
- unions: props.config.numeric
382
- ? (collection) =>
383
- FeatureProgrammer.write_union_functions({
384
- config: configure({
385
- ...props,
386
- config: {
387
- ...props.config,
388
- numeric: false,
389
- },
390
- }),
391
- collection,
392
- })
393
- : undefined,
394
- arrays: (collection) =>
395
- write_array_functions({
396
- ...props,
397
- collection,
398
- }),
399
- tuples: (collection) =>
400
- write_tuple_functions({
401
- ...props,
402
- collection,
403
- }),
404
- },
405
- });
406
-
407
- /* -----------------------------------------------------------
408
- DECODERS
409
- ----------------------------------------------------------- */
410
- export const decode = (props: {
411
- context: ITypiaContext;
412
- config: IConfig;
413
- functor: FunctionProgrammer;
414
- input: ts.Expression;
415
- metadata: Metadata;
416
- explore: IExplore;
417
- }): ts.Expression => {
418
- if (props.metadata.any) return props.config.success;
419
-
420
- const top: IBinary[] = [];
421
- const binaries: IBinary[] = [];
422
- const add = (next: {
423
- exact: boolean;
424
- left: ts.Expression;
425
- right?: ts.Expression;
426
- }) =>
427
- create_add({
428
- binaries,
429
- left: next.left,
430
- right: next.right,
431
- exact: next.exact,
432
- default: props.input,
433
- });
434
- const getConstantValue = (value: number | string | bigint | boolean) => {
435
- if (typeof value === "string")
436
- return ts.factory.createStringLiteral(value);
437
- else if (typeof value === "bigint")
438
- return ExpressionFactory.bigint(value);
439
- return ts.factory.createIdentifier(value.toString());
440
- };
441
-
442
- //----
443
- // CHECK OPTIONAL
444
- //----
445
- // @todo -> should be elaborated
446
- const checkOptional: boolean =
447
- props.metadata.empty() || props.metadata.isUnionBucket();
448
-
449
- // NULLABLE
450
- if (checkOptional || props.metadata.nullable)
451
- if (props.metadata.nullable)
452
- add({
453
- exact: props.metadata.nullable,
454
- left: ValueFactory.NULL(),
455
- });
456
- else
457
- create_add({
458
- binaries: top,
459
- default: props.input,
460
- exact: props.metadata.nullable,
461
- left: ValueFactory.NULL(),
462
- });
463
-
464
- // UNDEFINDABLE
465
- if (checkOptional || !props.metadata.isRequired())
466
- if (props.metadata.isRequired())
467
- create_add({
468
- binaries: top,
469
- default: props.input,
470
- exact: false,
471
- left: ValueFactory.UNDEFINED(),
472
- });
473
- else
474
- add({
475
- exact: true,
476
- left: ValueFactory.UNDEFINED(),
477
- });
478
-
479
- // FUNCTIONAL
480
- if (props.metadata.functions.length)
481
- if (
482
- OptionPredicator.functional(props.context.options) ||
483
- props.metadata.size() !== 1
484
- )
485
- add({
486
- exact: true,
487
- left: ts.factory.createStringLiteral("function"),
488
- right: ValueFactory.TYPEOF(props.input),
489
- });
490
- else
491
- binaries.push({
492
- combined: false,
493
- expression: props.config.success,
494
- });
495
-
496
- //----
497
- // VALUES
498
- //----
499
- // CONSTANT VALUES
500
- const constants: MetadataConstant[] = props.metadata.constants.filter((c) =>
501
- AtomicPredicator.constant({
502
- metadata: props.metadata,
503
- name: c.type,
504
- }),
505
- );
506
- const constantLength: number = constants
507
- .map((c) => c.values.length)
508
- .reduce((a, b) => a + b, 0);
509
- if (constantLength >= 10) {
510
- const values: Array<boolean | number | string | bigint> = constants
511
- .map((c) => c.values.map((v) => v.value))
512
- .flat();
513
- add({
514
- exact: true,
515
- left: ts.factory.createTrue(),
516
- right: ts.factory.createCallExpression(
517
- IdentifierFactory.access(
518
- props.functor.emplaceVariable(
519
- `${props.config.prefix}v${props.functor.increment()}`,
520
- ts.factory.createNewExpression(
521
- ts.factory.createIdentifier("Set"),
522
- undefined,
523
- [
524
- ts.factory.createArrayLiteralExpression(
525
- values.map((v) =>
526
- typeof v === "boolean"
527
- ? v === true
528
- ? ts.factory.createTrue()
529
- : ts.factory.createFalse()
530
- : typeof v === "bigint"
531
- ? ExpressionFactory.bigint(v)
532
- : typeof v === "number"
533
- ? ExpressionFactory.number(v)
534
- : ts.factory.createStringLiteral(v.toString()),
535
- ),
536
- ),
537
- ],
538
- ),
539
- ),
540
- "has",
541
- ),
542
- undefined,
543
- [props.input],
544
- ),
545
- });
546
- } else
547
- for (const c of constants)
548
- if (
549
- AtomicPredicator.constant({
550
- metadata: props.metadata,
551
- name: c.type,
552
- })
553
- )
554
- for (const v of c.values)
555
- add({
556
- exact: true,
557
- left: getConstantValue(v.value),
558
- });
559
-
560
- // ATOMIC VALUES
561
- for (const atom of props.metadata.atomics)
562
- if (
563
- AtomicPredicator.atomic({
564
- metadata: props.metadata,
565
- name: atom.type,
566
- }) === false
567
- )
568
- continue;
569
- else if (atom.type === "number")
570
- binaries.push({
571
- expression: props.config.atomist({
572
- explore: props.explore,
573
- entry: check_number({
574
- context: props.context,
575
- numeric: props.config.numeric,
576
- atomic: atom,
577
- input: props.input,
578
- }),
579
- input: props.input,
580
- }),
581
- combined: false,
582
- });
583
- else if (atom.type === "bigint")
584
- binaries.push({
585
- expression: props.config.atomist({
586
- explore: props.explore,
587
- entry: check_bigint({
588
- context: props.context,
589
- atomic: atom,
590
- input: props.input,
591
- }),
592
- input: props.input,
593
- }),
594
- combined: false,
595
- });
596
- else if (atom.type === "string")
597
- binaries.push({
598
- expression: props.config.atomist({
599
- explore: props.explore,
600
- entry: check_string({
601
- context: props.context,
602
- atomic: atom,
603
- input: props.input,
604
- }),
605
- input: props.input,
606
- }),
607
- combined: false,
608
- });
609
- else
610
- add({
611
- exact: true,
612
- left: ts.factory.createStringLiteral(atom.type),
613
- right: ValueFactory.TYPEOF(props.input),
614
- });
615
-
616
- // TEMPLATE LITERAL VALUES
617
- if (props.metadata.templates.length)
618
- if (AtomicPredicator.template(props.metadata))
619
- binaries.push({
620
- expression: props.config.atomist({
621
- explore: props.explore,
622
- entry: check_template({
623
- templates: props.metadata.templates,
624
- input: props.input,
625
- }),
626
- input: props.input,
627
- }),
628
- combined: false,
629
- });
630
-
631
- // NATIVE CLASSES
632
- for (const native of props.metadata.natives)
633
- binaries.push({
634
- expression: check_native({
635
- name: native.name,
636
- input: props.input,
637
- }),
638
- combined: false,
639
- });
640
-
641
- //----
642
- // INSTANCES
643
- //----
644
- interface IInstance {
645
- head: ts.Expression;
646
- body: ts.Expression | null;
647
- expected: string;
648
- }
649
- const instances: IInstance[] = [];
650
- const prepare = (next: IInstance) => instances.push(next);
651
-
652
- // SETS
653
- if (props.metadata.sets.length) {
654
- const install = (body: ts.Expression | null) =>
655
- prepare({
656
- head: check_native({
657
- name: "Set",
658
- input: props.input,
659
- }),
660
- expected: props.metadata.sets
661
- .map((elem) => `Set<${elem.getName()}>`)
662
- .join(" | "),
663
- body,
664
- });
665
- if (props.metadata.sets.some((elem) => elem.value.any)) install(null);
666
- else
667
- install(
668
- explore_sets({
669
- config: props.config,
670
- context: props.context,
671
- functor: props.functor,
672
- sets: props.metadata.sets,
673
- input: props.input,
674
- explore: {
675
- ...props.explore,
676
- from: "array",
677
- },
678
- }),
679
- );
680
- }
681
-
682
- // MAPS
683
- if (props.metadata.maps.length) {
684
- const install = (body: ts.Expression | null) =>
685
- prepare({
686
- head: check_native({
687
- name: "Map",
688
- input: props.input,
689
- }),
690
- expected: props.metadata.maps
691
- .map(({ key, value }) => `Map<${key}, ${value}>`)
692
- .join(" | "),
693
- body,
694
- });
695
- if (props.metadata.maps.some((elem) => elem.key.any && elem.value.any))
696
- install(null);
697
- else
698
- install(
699
- explore_maps({
700
- config: props.config,
701
- context: props.context,
702
- functor: props.functor,
703
- maps: props.metadata.maps,
704
- input: props.input,
705
- explore: {
706
- ...props.explore,
707
- from: "array",
708
- },
709
- }),
710
- );
711
- }
712
-
713
- // ARRAYS AND TUPLES
714
- if (props.metadata.tuples.length + props.metadata.arrays.length > 0) {
715
- const install = (body: ts.Expression | null) =>
716
- prepare({
717
- head: props.config.atomist({
718
- explore: props.explore,
719
- entry: {
720
- expected: [
721
- ...props.metadata.tuples.map((t) => t.type.name),
722
- ...props.metadata.arrays.map((a) => a.getName()),
723
- ].join(" | "),
724
- expression: ExpressionFactory.isArray(props.input),
725
- conditions: [],
726
- },
727
- input: props.input,
728
- }),
729
- expected: [...props.metadata.tuples, ...props.metadata.arrays]
730
- .map((elem) => elem.type.name)
731
- .join(" | "),
732
- body,
733
- });
734
- if (props.metadata.arrays.length === 0)
735
- if (props.metadata.tuples.length === 1)
736
- install(
737
- decode_tuple({
738
- config: props.config,
739
- context: props.context,
740
- functor: props.functor,
741
- tuple: props.metadata.tuples[0]!,
742
- input: props.input,
743
- explore: {
744
- ...props.explore,
745
- from: "array",
746
- },
747
- }),
748
- );
749
- // TUPLE ONLY
750
- else
751
- install(
752
- explore_tuples({
753
- config: props.config,
754
- context: props.context,
755
- functor: props.functor,
756
- tuples: props.metadata.tuples,
757
- input: props.input,
758
- explore: {
759
- ...props.explore,
760
- from: "array",
761
- },
762
- }),
763
- );
764
- else if (props.metadata.arrays.some((elem) => elem.type.value.any))
765
- install(null);
766
- else if (props.metadata.tuples.length === 0)
767
- if (props.metadata.arrays.length === 1)
768
- // ARRAY ONLY
769
- install(
770
- decode_array({
771
- config: props.config,
772
- context: props.context,
773
- functor: props.functor,
774
- array: props.metadata.arrays[0]!,
775
- input: props.input,
776
- explore: {
777
- ...props.explore,
778
- from: "array",
779
- },
780
- }),
781
- );
782
- else
783
- install(
784
- explore_arrays({
785
- config: props.config,
786
- context: props.context,
787
- functor: props.functor,
788
- arrays: props.metadata.arrays,
789
- input: props.input,
790
- explore: {
791
- ...props.explore,
792
- from: "array",
793
- },
794
- }),
795
- );
796
- else
797
- install(
798
- explore_arrays_and_tuples({
799
- config: props.config,
800
- context: props.context,
801
- functor: props.functor,
802
- definitions: [...props.metadata.tuples, ...props.metadata.arrays],
803
- input: props.input,
804
- explore: props.explore,
805
- }),
806
- );
807
- }
808
-
809
- // OBJECT
810
- if (props.metadata.objects.length > 0)
811
- prepare({
812
- head: ExpressionFactory.isObject({
813
- checkNull: true,
814
- checkArray: props.metadata.objects.some((obj) =>
815
- obj.type.properties.every(
816
- (prop) => !prop.key.isSoleLiteral() || !prop.value.isRequired(),
817
- ),
818
- ),
819
- input: props.input,
820
- }),
821
- expected: props.metadata.objects
822
- .map((obj) => obj.type.name)
823
- .join(" | "),
824
- body: explore_objects({
825
- config: props.config,
826
- functor: props.functor,
827
- metadata: props.metadata,
828
- input: props.input,
829
- explore: {
830
- ...props.explore,
831
- from: "object",
832
- },
833
- }),
834
- });
835
-
836
- if (instances.length) {
837
- const transformer =
838
- (merge: (x: ts.Expression, y: ts.Expression) => ts.Expression) =>
839
- (instance: IInstance) =>
840
- instance.body
841
- ? {
842
- expression: merge(instance.head, instance.body),
843
- combined: true,
844
- }
845
- : {
846
- expression: instance.head,
847
- combined: false,
848
- };
849
- if (instances.length === 1)
850
- binaries.push(
851
- transformer((head, body) =>
852
- props.config.combiner({
853
- explore: props.explore,
854
- logic: "and",
855
- input: props.input,
856
- binaries: [head, body].map((expression) => ({
857
- expression,
858
- combined: expression !== head,
859
- })),
860
- expected: props.metadata.getName(),
861
- }),
862
- )(instances[0]!),
863
- );
864
- else
865
- binaries.push({
866
- expression: props.config.combiner({
867
- explore: props.explore,
868
- logic: "or",
869
- input: props.input,
870
- binaries: instances.map(transformer(ts.factory.createLogicalAnd)),
871
- expected: props.metadata.getName(),
872
- }),
873
- combined: true,
874
- });
875
- }
876
-
877
- // ESCAPED CASE
878
- if (props.metadata.escaped !== null)
879
- binaries.push({
880
- combined: false,
881
- expression:
882
- props.metadata.escaped.original.size() === 1 &&
883
- props.metadata.escaped.original.natives.length === 1
884
- ? check_native({
885
- name: props.metadata.escaped.original.natives[0]!.name,
886
- input: props.input,
887
- })
888
- : ts.factory.createLogicalAnd(
889
- decode({
890
- context: props.context,
891
- config: props.config,
892
- functor: props.functor,
893
- metadata: props.metadata.escaped.original,
894
- input: props.input,
895
- explore: props.explore,
896
- }),
897
- ts.factory.createLogicalAnd(
898
- IsProgrammer.decode_to_json({
899
- checkNull: false,
900
- input: props.input,
901
- }),
902
- decode_escaped({
903
- config: props.config,
904
- context: props.context,
905
- functor: props.functor,
906
- metadata: props.metadata.escaped.returns,
907
- input: props.input,
908
- explore: props.explore,
909
- }),
910
- ),
911
- ),
912
- });
913
-
914
- //----
915
- // COMBINE CONDITIONS
916
- //----
917
- return top.length && binaries.length
918
- ? props.config.combiner({
919
- explore: props.explore,
920
- logic: "and",
921
- input: props.input,
922
- binaries: [
923
- ...top,
924
- {
925
- expression: props.config.combiner({
926
- explore: props.explore,
927
- logic: "or",
928
- input: props.input,
929
- binaries,
930
- expected: props.metadata.getName(),
931
- }),
932
- combined: true,
933
- },
934
- ],
935
- expected: props.metadata.getName(),
936
- })
937
- : binaries.length
938
- ? props.config.combiner({
939
- explore: props.explore,
940
- logic: "or",
941
- input: props.input,
942
- binaries,
943
- expected: props.metadata.getName(),
944
- })
945
- : props.config.success;
946
- };
947
-
948
- export const decode_object = (props: {
949
- config: IConfig;
950
- functor: FunctionProgrammer;
951
- object: MetadataObjectType;
952
- input: ts.Expression;
953
- explore: IExplore;
954
- }) => {
955
- props.object.validated ||= true;
956
- return FeatureProgrammer.decode_object(props);
957
- };
958
-
959
- const decode_array = (props: {
960
- config: IConfig;
961
- context: ITypiaContext;
962
- functor: FunctionProgrammer;
963
- array: MetadataArray;
964
- input: ts.Expression;
965
- explore: IExplore;
966
- }) => {
967
- if (props.array.type.recursive === false) return decode_array_inline(props);
968
-
969
- const arrayExplore: IExplore = {
970
- ...props.explore,
971
- source: "function",
972
- from: "array",
973
- };
974
- return ts.factory.createLogicalOr(
975
- ts.factory.createCallExpression(
976
- ts.factory.createIdentifier(
977
- props.functor.useLocal(
978
- `${props.config.prefix}a${props.array.type.index}`,
979
- ),
980
- ),
981
- undefined,
982
- FeatureProgrammer.argumentsArray({
983
- config: props.config,
984
- explore: {
985
- ...arrayExplore,
986
- source: "function",
987
- from: "array",
988
- },
989
- input: props.input,
990
- }),
991
- ),
992
- props.config.joiner.failure({
993
- input: props.input,
994
- expected: props.array.type.name,
995
- explore: arrayExplore,
996
- }),
997
- );
998
- };
999
-
1000
- const decode_array_inline = (props: {
1001
- config: IConfig;
1002
- context: ITypiaContext;
1003
- functor: FunctionProgrammer;
1004
- array: MetadataArray;
1005
- input: ts.Expression;
1006
- explore: IExplore;
1007
- }): ts.Expression => {
1008
- const length: ICheckEntry = check_array_length({
1009
- context: props.context,
1010
- array: props.array,
1011
- input: props.input,
1012
- });
1013
- const main: ts.Expression = FeatureProgrammer.decode_array({
1014
- config: {
1015
- prefix: props.config.prefix,
1016
- trace: props.config.trace,
1017
- path: props.config.path,
1018
- decoder: (next) =>
1019
- decode({
1020
- ...props,
1021
- ...next,
1022
- }),
1023
- },
1024
- functor: props.functor,
1025
- combiner: props.config.joiner.array,
1026
- array: props.array,
1027
- input: props.input,
1028
- explore: props.explore,
1029
- });
1030
- return length.expression === null && length.conditions.length === 0
1031
- ? main
1032
- : ts.factory.createLogicalAnd(
1033
- props.config.atomist({
1034
- explore: props.explore,
1035
- input: props.input,
1036
- entry: length,
1037
- }),
1038
- main,
1039
- );
1040
- };
1041
-
1042
- const decode_tuple = (props: {
1043
- context: ITypiaContext;
1044
- config: IConfig;
1045
- functor: FunctionProgrammer;
1046
- tuple: MetadataTuple;
1047
- input: ts.Expression;
1048
- explore: IExplore;
1049
- }): ts.Expression => {
1050
- if (props.tuple.type.recursive === false)
1051
- return decode_tuple_inline({
1052
- ...props,
1053
- tuple: props.tuple.type,
1054
- });
1055
- const arrayExplore: IExplore = {
1056
- ...props.explore,
1057
- source: "function",
1058
- from: "array",
1059
- };
1060
- return ts.factory.createLogicalOr(
1061
- ts.factory.createCallExpression(
1062
- ts.factory.createIdentifier(
1063
- props.functor.useLocal(
1064
- `${props.config.prefix}t${props.tuple.type.index}`,
1065
- ),
1066
- ),
1067
- undefined,
1068
- FeatureProgrammer.argumentsArray({
1069
- config: props.config,
1070
- explore: {
1071
- ...arrayExplore,
1072
- source: "function",
1073
- },
1074
- input: props.input,
1075
- }),
1076
- ),
1077
- props.config.joiner.failure({
1078
- input: props.input,
1079
- expected: props.tuple.type.name,
1080
- explore: arrayExplore,
1081
- }),
1082
- );
1083
- };
1084
-
1085
- const decode_tuple_inline = (props: {
1086
- config: IConfig;
1087
- context: ITypiaContext;
1088
- functor: FunctionProgrammer;
1089
- tuple: MetadataTupleType;
1090
- input: ts.Expression;
1091
- explore: IExplore;
1092
- }): ts.Expression => {
1093
- const binaries: ts.Expression[] = props.tuple.elements
1094
- .filter((metadata) => metadata.rest === null)
1095
- .map((metadata, index) =>
1096
- decode({
1097
- context: props.context,
1098
- config: props.config,
1099
- functor: props.functor,
1100
- input: ts.factory.createElementAccessExpression(props.input, index),
1101
- metadata,
1102
- explore: {
1103
- ...props.explore,
1104
- from: "array",
1105
- postfix: props.explore.postfix.length
1106
- ? `${postfix_of_tuple(props.explore.postfix)}[${index}]"`
1107
- : `"[${index}]"`,
1108
- },
1109
- }),
1110
- );
1111
- const rest: ts.Expression | null =
1112
- props.tuple.elements.length && props.tuple.elements.at(-1)!.rest !== null
1113
- ? decode({
1114
- config: props.config,
1115
- context: props.context,
1116
- functor: props.functor,
1117
- input: ts.factory.createCallExpression(
1118
- IdentifierFactory.access(props.input, "slice"),
1119
- undefined,
1120
- [ExpressionFactory.number(props.tuple.elements.length - 1)],
1121
- ),
1122
- metadata: wrap_metadata_rest_tuple(
1123
- props.tuple.elements.at(-1)!.rest!,
1124
- ),
1125
- explore: {
1126
- ...props.explore,
1127
- start: props.tuple.elements.length - 1,
1128
- },
1129
- })
1130
- : null;
1131
- const arrayLength = ts.factory.createPropertyAccessExpression(
1132
- props.input,
1133
- "length",
1134
- );
1135
- return props.config.combiner({
1136
- explore: props.explore,
1137
- logic: "and",
1138
- input: props.input,
1139
- binaries: [
1140
- ...(rest === null
1141
- ? props.tuple.elements.every((t) => t.optional === false)
1142
- ? [
1143
- {
1144
- combined: false,
1145
- expression: ts.factory.createStrictEquality(
1146
- arrayLength,
1147
- ExpressionFactory.number(props.tuple.elements.length),
1148
- ),
1149
- },
1150
- ]
1151
- : [
1152
- {
1153
- combined: false,
1154
- expression: ts.factory.createLogicalAnd(
1155
- ts.factory.createLessThanEquals(
1156
- ExpressionFactory.number(
1157
- props.tuple.elements.filter((t) => t.optional === false)
1158
- .length,
1159
- ),
1160
- arrayLength,
1161
- ),
1162
- ts.factory.createGreaterThanEquals(
1163
- ExpressionFactory.number(props.tuple.elements.length),
1164
- arrayLength,
1165
- ),
1166
- ),
1167
- },
1168
- ]
1169
- : []),
1170
- ...(props.config.joiner.tuple
1171
- ? [
1172
- {
1173
- expression: props.config.joiner.tuple(binaries),
1174
- combined: true,
1175
- },
1176
- ]
1177
- : binaries.map((expression) => ({
1178
- expression,
1179
- combined: true,
1180
- }))),
1181
- ...(rest !== null
1182
- ? [
1183
- {
1184
- expression: rest,
1185
- combined: true,
1186
- },
1187
- ]
1188
- : []),
1189
- ],
1190
- expected: `[${props.tuple.elements.map((t) => t.getName()).join(", ")}]`,
1191
- });
1192
- };
1193
-
1194
- const decode_escaped = (props: {
1195
- config: IConfig;
1196
- context: ITypiaContext;
1197
- functor: FunctionProgrammer;
1198
- metadata: Metadata;
1199
- input: ts.Expression;
1200
- explore: IExplore;
1201
- }): ts.Expression =>
1202
- ts.factory.createCallExpression(
1203
- ts.factory.createParenthesizedExpression(
1204
- ts.factory.createArrowFunction(
1205
- undefined,
1206
- undefined,
1207
- [IdentifierFactory.parameter("input", TypeFactory.keyword("any"))],
1208
- undefined,
1209
- undefined,
1210
- decode({
1211
- ...props,
1212
- input: ts.factory.createIdentifier("input"),
1213
- }),
1214
- ),
1215
- ),
1216
- undefined,
1217
- [
1218
- ts.factory.createCallExpression(
1219
- IdentifierFactory.access(props.input, "toJSON"),
1220
- undefined,
1221
- [],
1222
- ),
1223
- ],
1224
- );
1225
-
1226
- /* -----------------------------------------------------------
1227
- UNION TYPE EXPLORERS
1228
- ----------------------------------------------------------- */
1229
- const explore_sets = (props: {
1230
- context: ITypiaContext;
1231
- config: IConfig;
1232
- functor: FunctionProgrammer;
1233
- sets: MetadataSet[];
1234
- input: ts.Expression;
1235
- explore: IExplore;
1236
- }): ts.Expression =>
1237
- ts.factory.createCallExpression(
1238
- UnionExplorer.set({
1239
- config: {
1240
- checker: (v) =>
1241
- decode({
1242
- context: props.context,
1243
- config: props.config,
1244
- functor: props.functor,
1245
- input: v.input,
1246
- metadata: v.definition,
1247
- explore: v.explore,
1248
- }),
1249
- decoder: (v) =>
1250
- decode_array({
1251
- config: props.config,
1252
- context: props.context,
1253
- functor: props.functor,
1254
- array: v.definition,
1255
- input: v.input,
1256
- explore: v.explore,
1257
- }),
1258
- empty: props.config.success,
1259
- success: props.config.success,
1260
- failure: (v) =>
1261
- ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1262
- },
1263
- parameters: [],
1264
- input: props.input,
1265
- sets: props.sets,
1266
- explore: props.explore,
1267
- }),
1268
- undefined,
1269
- undefined,
1270
- );
1271
-
1272
- const explore_maps = (props: {
1273
- context: ITypiaContext;
1274
- config: IConfig;
1275
- functor: FunctionProgrammer;
1276
- input: ts.Expression;
1277
- maps: MetadataMap[];
1278
- explore: IExplore;
1279
- }): ts.Expression =>
1280
- ts.factory.createCallExpression(
1281
- UnionExplorer.map({
1282
- config: {
1283
- checker: (v) =>
1284
- ts.factory.createLogicalAnd(
1285
- decode({
1286
- config: props.config,
1287
- context: props.context,
1288
- functor: props.functor,
1289
- input: ts.factory.createElementAccessExpression(v.input, 0),
1290
- metadata: v.definition[0],
1291
- explore: {
1292
- ...v.explore,
1293
- postfix: `${v.explore.postfix}[0]`,
1294
- },
1295
- }),
1296
- decode({
1297
- config: props.config,
1298
- context: props.context,
1299
- functor: props.functor,
1300
- input: ts.factory.createElementAccessExpression(v.input, 1),
1301
- metadata: v.definition[1],
1302
- explore: {
1303
- ...v.explore,
1304
- postfix: `${v.explore.postfix}[1]`,
1305
- },
1306
- }),
1307
- ),
1308
- decoder: (v) =>
1309
- decode_array({
1310
- context: props.context,
1311
- config: props.config,
1312
- functor: props.functor,
1313
- array: v.definition,
1314
- input: v.input,
1315
- explore: v.explore,
1316
- }),
1317
- empty: props.config.success,
1318
- success: props.config.success,
1319
- failure: (v) =>
1320
- ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1321
- },
1322
- parameters: [],
1323
- input: props.input,
1324
- maps: props.maps,
1325
- explore: props.explore,
1326
- }),
1327
- undefined,
1328
- undefined,
1329
- );
1330
-
1331
- const explore_tuples = (props: {
1332
- config: IConfig;
1333
- context: ITypiaContext;
1334
- functor: FunctionProgrammer;
1335
- tuples: MetadataTuple[];
1336
- input: ts.Expression;
1337
- explore: IExplore;
1338
- }): ts.Expression =>
1339
- explore_array_like_union_types<MetadataTuple>({
1340
- config: props.config,
1341
- functor: props.functor,
1342
- factory: (next) =>
1343
- UnionExplorer.tuple({
1344
- config: {
1345
- checker: (v) =>
1346
- decode_tuple({
1347
- context: props.context,
1348
- config: props.config,
1349
- functor: props.functor,
1350
- input: v.input,
1351
- tuple: v.definition,
1352
- explore: v.explore,
1353
- }),
1354
- decoder: (v) =>
1355
- decode_tuple({
1356
- context: props.context,
1357
- config: props.config,
1358
- functor: props.functor,
1359
- tuple: v.definition,
1360
- input: v.input,
1361
- explore: v.explore,
1362
- }),
1363
- empty: props.config.success,
1364
- success: props.config.success,
1365
- failure: (v) =>
1366
- ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1367
- },
1368
- parameters: next.parameters,
1369
- tuples: next.definitions,
1370
- input: next.input,
1371
- explore: next.explore,
1372
- }),
1373
- definitions: props.tuples,
1374
- input: props.input,
1375
- explore: props.explore,
1376
- });
1377
-
1378
- const explore_arrays = (props: {
1379
- config: IConfig;
1380
- context: ITypiaContext;
1381
- functor: FunctionProgrammer;
1382
- arrays: MetadataArray[];
1383
- input: ts.Expression;
1384
- explore: IExplore;
1385
- }): ts.Expression =>
1386
- explore_array_like_union_types<MetadataArray>({
1387
- config: props.config,
1388
- functor: props.functor,
1389
- factory: (next) =>
1390
- UnionExplorer.array({
1391
- config: {
1392
- checker: (v) =>
1393
- decode({
1394
- context: props.context,
1395
- config: props.config,
1396
- functor: props.functor,
1397
- metadata: v.definition,
1398
- input: v.input,
1399
- explore: v.explore,
1400
- }),
1401
- decoder: (v) =>
1402
- decode_array({
1403
- context: props.context,
1404
- config: props.config,
1405
- functor: props.functor,
1406
- array: v.definition,
1407
- input: v.input,
1408
- explore: v.explore,
1409
- }),
1410
- empty: props.config.success,
1411
- success: props.config.success,
1412
- failure: (v) =>
1413
- ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1414
- },
1415
- parameters: next.parameters,
1416
- arrays: next.definitions,
1417
- input: next.input,
1418
- explore: next.explore,
1419
- }),
1420
- definitions: props.arrays,
1421
- input: props.input,
1422
- explore: props.explore,
1423
- });
1424
-
1425
- const explore_arrays_and_tuples = (props: {
1426
- config: IConfig;
1427
- context: ITypiaContext;
1428
- functor: FunctionProgrammer;
1429
- definitions: Array<MetadataArray | MetadataTuple>;
1430
- input: ts.Expression;
1431
- explore: IExplore;
1432
- }): ts.Expression =>
1433
- explore_array_like_union_types<MetadataArray | MetadataTuple>({
1434
- config: props.config,
1435
- functor: props.functor,
1436
- factory: (next) =>
1437
- UnionExplorer.array_or_tuple({
1438
- config: {
1439
- checker: (v) =>
1440
- v.definition instanceof MetadataTuple
1441
- ? decode_tuple({
1442
- config: props.config,
1443
- context: props.context,
1444
- functor: props.functor,
1445
- input: v.input,
1446
- tuple: v.definition,
1447
- explore: v.explore,
1448
- })
1449
- : props.config.atomist({
1450
- explore: v.explore,
1451
- entry: {
1452
- expected: props.definitions
1453
- .map((elem) =>
1454
- elem instanceof MetadataArray
1455
- ? elem.getName()
1456
- : elem.type.name,
1457
- )
1458
- .join(" | "),
1459
- expression: decode({
1460
- functor: props.functor,
1461
- context: props.context,
1462
- config: props.config,
1463
- metadata: v.definition,
1464
- input: v.input,
1465
- explore: v.explore,
1466
- }),
1467
- conditions: [],
1468
- },
1469
- input: v.container,
1470
- }),
1471
- decoder: (v) =>
1472
- v.definition instanceof MetadataTuple
1473
- ? decode_tuple({
1474
- context: props.context,
1475
- config: props.config,
1476
- functor: props.functor,
1477
- input: v.input,
1478
- tuple: v.definition,
1479
- explore: v.explore,
1480
- })
1481
- : decode_array({
1482
- context: props.context,
1483
- config: props.config,
1484
- functor: props.functor,
1485
- input: v.input,
1486
- array: v.definition,
1487
- explore: v.explore,
1488
- }),
1489
- empty: props.config.success,
1490
- success: props.config.success,
1491
- failure: (v) =>
1492
- ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1493
- },
1494
- parameters: next.parameters,
1495
- definitions: next.definitions,
1496
- input: next.input,
1497
- explore: next.explore,
1498
- }),
1499
- input: props.input,
1500
- definitions: props.definitions,
1501
- explore: props.explore,
1502
- });
1503
-
1504
- const explore_array_like_union_types = <
1505
- T extends MetadataArray | MetadataTuple,
1506
- >(props: {
1507
- config: IConfig;
1508
- functor: FunctionProgrammer;
1509
- factory: (next: {
1510
- parameters: ts.ParameterDeclaration[];
1511
- definitions: T[];
1512
- input: ts.Expression;
1513
- explore: IExplore;
1514
- }) => ts.ArrowFunction;
1515
- input: ts.Expression;
1516
- definitions: T[];
1517
- explore: IExplore;
1518
- }): ts.Expression => {
1519
- const arrow = (next: {
1520
- parameters: ts.ParameterDeclaration[];
1521
- explore: IExplore;
1522
- input: ts.Expression;
1523
- }): ts.ArrowFunction =>
1524
- props.factory({
1525
- parameters: next.parameters,
1526
- definitions: props.definitions,
1527
- input: next.input,
1528
- explore: next.explore,
1529
- });
1530
- if (props.definitions.every((e) => e.type.recursive === false))
1531
- ts.factory.createCallExpression(
1532
- arrow({
1533
- explore: props.explore,
1534
- input: props.input,
1535
- parameters: [],
1536
- }),
1537
- undefined,
1538
- [],
1539
- );
1540
- const arrayExplore: IExplore = {
1541
- ...props.explore,
1542
- source: "function",
1543
- from: "array",
1544
- };
1545
- return ts.factory.createLogicalOr(
1546
- ts.factory.createCallExpression(
1547
- ts.factory.createIdentifier(
1548
- props.functor.emplaceUnion(
1549
- props.config.prefix,
1550
- props.definitions.map((e) => e.type.name).join(" | "),
1551
- () =>
1552
- arrow({
1553
- parameters: FeatureProgrammer.parameterDeclarations({
1554
- config: props.config,
1555
- type: TypeFactory.keyword("any"),
1556
- input: ts.factory.createIdentifier("input"),
1557
- }),
1558
- explore: {
1559
- ...arrayExplore,
1560
- postfix: "",
1561
- },
1562
- input: ts.factory.createIdentifier("input"),
1563
- }),
1564
- ),
1565
- ),
1566
- undefined,
1567
- FeatureProgrammer.argumentsArray(props),
1568
- ),
1569
- props.config.joiner.failure({
1570
- input: props.input,
1571
- expected: props.definitions.map((e) => e.type.name).join(" | "),
1572
- explore: arrayExplore,
1573
- }),
1574
- );
1575
- };
1576
-
1577
- const explore_objects = (props: {
1578
- config: IConfig;
1579
- functor: FunctionProgrammer;
1580
- input: ts.Expression;
1581
- metadata: Metadata;
1582
- explore: IExplore;
1583
- }) =>
1584
- props.metadata.objects.length === 1
1585
- ? decode_object({
1586
- config: props.config,
1587
- functor: props.functor,
1588
- object: props.metadata.objects[0]!.type,
1589
- input: props.input,
1590
- explore: props.explore,
1591
- })
1592
- : ts.factory.createCallExpression(
1593
- ts.factory.createIdentifier(
1594
- props.functor.useLocal(
1595
- `${props.config.prefix}u${props.metadata.union_index!}`,
1596
- ),
1597
- ),
1598
- undefined,
1599
- FeatureProgrammer.argumentsArray(props),
1600
- );
1601
- }
1602
-
1603
- const create_add = (props: {
1604
- binaries: CheckerProgrammer.IBinary[];
1605
- default: ts.Expression;
1606
- exact: boolean;
1607
- left: ts.Expression;
1608
- right?: ts.Expression;
1609
- }) => {
1610
- const factory = props.exact
1611
- ? ts.factory.createStrictEquality
1612
- : ts.factory.createStrictInequality;
1613
- props.binaries.push({
1614
- expression: factory(props.left, props.right ?? props.default),
1615
- combined: false,
1616
- });
1617
- };
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../factories/MetadataFactory";
7
+ import { StatementFactory } from "../factories/StatementFactory";
8
+ import { TypeFactory } from "../factories/TypeFactory";
9
+ import { ValueFactory } from "../factories/ValueFactory";
10
+
11
+ import { Metadata } from "../schemas/metadata/Metadata";
12
+ import { MetadataArray } from "../schemas/metadata/MetadataArray";
13
+ import { MetadataConstant } from "../schemas/metadata/MetadataConstant";
14
+ import { MetadataMap } from "../schemas/metadata/MetadataMap";
15
+ import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
16
+ import { MetadataSet } from "../schemas/metadata/MetadataSet";
17
+ import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
18
+ import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
19
+
20
+ import { ITypiaContext } from "../transformers/ITypiaContext";
21
+ import { TransformerError } from "../transformers/TransformerError";
22
+
23
+ import { FeatureProgrammer } from "./FeatureProgrammer";
24
+ import { IsProgrammer } from "./IsProgrammer";
25
+ import { AtomicPredicator } from "./helpers/AtomicPredicator";
26
+ import { FunctionProgrammer } from "./helpers/FunctionProgrammer";
27
+ import { ICheckEntry } from "./helpers/ICheckEntry";
28
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
+ import { OptionPredicator } from "./helpers/OptionPredicator";
30
+ import { UnionExplorer } from "./helpers/UnionExplorer";
31
+ import { check_array_length } from "./internal/check_array_length";
32
+ import { check_bigint } from "./internal/check_bigint";
33
+ import { check_native } from "./internal/check_native";
34
+ import { check_number } from "./internal/check_number";
35
+ import { check_string } from "./internal/check_string";
36
+ import { check_template } from "./internal/check_template";
37
+ import { decode_union_object } from "./internal/decode_union_object";
38
+ import { postfix_of_tuple } from "./internal/postfix_of_tuple";
39
+ import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
40
+
41
+ export namespace CheckerProgrammer {
42
+ export interface IConfig {
43
+ prefix: string;
44
+ path: boolean;
45
+ trace: boolean;
46
+ equals: boolean;
47
+ numeric: boolean;
48
+ addition?: () => ts.Statement[];
49
+ decoder?: (props: {
50
+ metadata: Metadata;
51
+ input: ts.Expression;
52
+ explore: IExplore;
53
+ }) => ts.Expression;
54
+ combiner: IConfig.Combiner;
55
+ atomist: (props: {
56
+ entry: ICheckEntry;
57
+ input: ts.Expression;
58
+ explore: IExplore;
59
+ }) => ts.Expression;
60
+ joiner: IConfig.IJoiner;
61
+ success: ts.Expression;
62
+ }
63
+ export namespace IConfig {
64
+ export interface Combiner {
65
+ (props: {
66
+ explore: IExplore;
67
+ logic: "and" | "or";
68
+ input: ts.Expression;
69
+ binaries: IBinary[];
70
+ expected: string;
71
+ }): ts.Expression;
72
+ }
73
+ export interface IJoiner {
74
+ object(props: {
75
+ input: ts.Expression;
76
+ entries: IExpressionEntry<ts.Expression>[];
77
+ }): ts.Expression;
78
+ array(props: {
79
+ input: ts.Expression;
80
+ arrow: ts.ArrowFunction;
81
+ }): ts.Expression;
82
+ tuple?: undefined | ((exprs: ts.Expression[]) => ts.Expression);
83
+
84
+ failure(props: {
85
+ input: ts.Expression;
86
+ expected: string;
87
+ explore?: undefined | FeatureProgrammer.IExplore;
88
+ }): ts.Expression;
89
+ is?(expression: ts.Expression): ts.Expression;
90
+ required?(exp: ts.Expression): ts.Expression;
91
+ full?:
92
+ | undefined
93
+ | ((props: {
94
+ condition: ts.Expression;
95
+ input: ts.Expression;
96
+ expected: string;
97
+ explore: IExplore;
98
+ }) => ts.Expression);
99
+ }
100
+ }
101
+ export type IExplore = FeatureProgrammer.IExplore;
102
+
103
+ export interface IBinary {
104
+ expression: ts.Expression;
105
+ combined: boolean;
106
+ }
107
+
108
+ /* -----------------------------------------------------------
109
+ WRITERS
110
+ ----------------------------------------------------------- */
111
+ export const compose = (props: {
112
+ context: ITypiaContext;
113
+ config: IConfig;
114
+ functor: FunctionProgrammer;
115
+ type: ts.Type;
116
+ name: string | undefined;
117
+ }): FeatureProgrammer.IComposed =>
118
+ FeatureProgrammer.compose({
119
+ ...props,
120
+ config: configure(props),
121
+ });
122
+
123
+ export const write = (props: {
124
+ context: ITypiaContext;
125
+ config: IConfig;
126
+ functor: FunctionProgrammer;
127
+ type: ts.Type;
128
+ name?: string;
129
+ }): ts.ArrowFunction =>
130
+ FeatureProgrammer.write({
131
+ config: configure(props),
132
+ context: props.context,
133
+ functor: props.functor,
134
+ type: props.type,
135
+ name: props.name,
136
+ });
137
+
138
+ export const write_object_functions = (props: {
139
+ context: ITypiaContext;
140
+ config: IConfig;
141
+ functor: FunctionProgrammer;
142
+ collection: MetadataCollection;
143
+ }): ts.VariableStatement[] =>
144
+ FeatureProgrammer.write_object_functions({
145
+ config: configure(props),
146
+ context: props.context,
147
+ collection: props.collection,
148
+ });
149
+
150
+ export const write_union_functions = (props: {
151
+ context: ITypiaContext;
152
+ config: IConfig;
153
+ functor: FunctionProgrammer;
154
+ collection: MetadataCollection;
155
+ }): ts.VariableStatement[] =>
156
+ FeatureProgrammer.write_union_functions({
157
+ config: configure({
158
+ context: props.context,
159
+ config: {
160
+ ...props.config,
161
+ numeric: false,
162
+ },
163
+ functor: props.functor,
164
+ }),
165
+ collection: props.collection,
166
+ });
167
+
168
+ export const write_array_functions = (props: {
169
+ context: ITypiaContext;
170
+ config: IConfig;
171
+ functor: FunctionProgrammer;
172
+ collection: MetadataCollection;
173
+ }): ts.VariableStatement[] =>
174
+ props.collection
175
+ .arrays()
176
+ .filter((a) => a.recursive)
177
+ .map((type, i) =>
178
+ StatementFactory.constant({
179
+ name: `${props.config.prefix}a${i}`,
180
+ value: ts.factory.createArrowFunction(
181
+ undefined,
182
+ undefined,
183
+ FeatureProgrammer.parameterDeclarations({
184
+ config: props.config,
185
+ type: TypeFactory.keyword("any"),
186
+ input: ts.factory.createIdentifier("input"),
187
+ }),
188
+ TypeFactory.keyword("any"),
189
+ undefined,
190
+ decode_array_inline({
191
+ ...props,
192
+ input: ts.factory.createIdentifier("input"),
193
+ array: MetadataArray.create({
194
+ type,
195
+ tags: [],
196
+ }),
197
+ explore: {
198
+ tracable: props.config.trace,
199
+ source: "function",
200
+ from: "array",
201
+ postfix: "",
202
+ },
203
+ }),
204
+ ),
205
+ }),
206
+ );
207
+
208
+ export const write_tuple_functions = (props: {
209
+ context: ITypiaContext;
210
+ config: IConfig;
211
+ functor: FunctionProgrammer;
212
+ collection: MetadataCollection;
213
+ }): ts.VariableStatement[] =>
214
+ props.collection
215
+ .tuples()
216
+ .filter((t) => t.recursive)
217
+ .map((tuple, i) =>
218
+ StatementFactory.constant({
219
+ name: `${props.config.prefix}t${i}`,
220
+ value: ts.factory.createArrowFunction(
221
+ undefined,
222
+ undefined,
223
+ FeatureProgrammer.parameterDeclarations({
224
+ config: props.config,
225
+ type: TypeFactory.keyword("any"),
226
+ input: ts.factory.createIdentifier("input"),
227
+ }),
228
+ TypeFactory.keyword("any"),
229
+ undefined,
230
+ decode_tuple_inline({
231
+ config: props.config,
232
+ context: props.context,
233
+ functor: props.functor,
234
+ input: ts.factory.createIdentifier("input"),
235
+ tuple,
236
+ explore: {
237
+ tracable: props.config.trace,
238
+ source: "function",
239
+ from: "array",
240
+ postfix: "",
241
+ },
242
+ }),
243
+ ),
244
+ }),
245
+ );
246
+
247
+ const configure = (props: {
248
+ context: ITypiaContext;
249
+ config: IConfig;
250
+ functor: FunctionProgrammer;
251
+ }): FeatureProgrammer.IConfig => ({
252
+ types: {
253
+ input: () => TypeFactory.keyword("any"),
254
+ output: (type, name) =>
255
+ ts.factory.createTypePredicateNode(
256
+ undefined,
257
+ "input",
258
+ ts.factory.createTypeReferenceNode(
259
+ name ??
260
+ TypeFactory.getFullName({ checker: props.context.checker, type }),
261
+ ),
262
+ ),
263
+ },
264
+ trace: props.config.trace,
265
+ path: props.config.path,
266
+ prefix: props.config.prefix,
267
+ initializer: (next) => {
268
+ const collection: MetadataCollection = new MetadataCollection();
269
+ const result = MetadataFactory.analyze({
270
+ checker: next.context.checker,
271
+ transformer: next.context.transformer,
272
+ options: {
273
+ escape: false,
274
+ constant: true,
275
+ absorb: true,
276
+ },
277
+ collection,
278
+ type: next.type,
279
+ });
280
+ if (result.success === false)
281
+ throw TransformerError.from({
282
+ code: next.functor.method,
283
+ errors: result.errors,
284
+ });
285
+ return {
286
+ collection,
287
+ metadata: result.data,
288
+ };
289
+ },
290
+ addition: props.config.addition,
291
+ decoder: props.config.decoder
292
+ ? (next) => props.config.decoder!(next)
293
+ : (next) =>
294
+ decode({
295
+ context: props.context,
296
+ config: props.config,
297
+ functor: props.functor,
298
+ input: next.input,
299
+ metadata: next.metadata,
300
+ explore: next.explore,
301
+ }),
302
+ objector: {
303
+ checker: props.config.decoder
304
+ ? (next) => props.config.decoder!(next)
305
+ : (next) =>
306
+ decode({
307
+ context: props.context,
308
+ config: props.config,
309
+ functor: props.functor,
310
+ input: next.input,
311
+ metadata: next.metadata,
312
+ explore: next.explore,
313
+ }),
314
+ decoder: (next) =>
315
+ decode_object({
316
+ config: props.config,
317
+ functor: props.functor,
318
+ input: next.input,
319
+ object: next.object,
320
+ explore: next.explore,
321
+ }),
322
+ joiner: props.config.joiner.object,
323
+ unionizer: props.config.equals
324
+ ? (next) =>
325
+ decode_union_object({
326
+ checker: (v) =>
327
+ decode_object({
328
+ config: props.config,
329
+ functor: props.functor,
330
+ object: v.object,
331
+ input: v.input,
332
+ explore: v.explore,
333
+ }),
334
+ decoder: (v) =>
335
+ decode_object({
336
+ config: props.config,
337
+ functor: props.functor,
338
+ input: v.input,
339
+ object: v.object,
340
+ explore: {
341
+ ...v.explore,
342
+ tracable: true,
343
+ },
344
+ }),
345
+ success: props.config.joiner.is ?? ((expr) => expr),
346
+ escaper: (v) =>
347
+ ts.factory.createReturnStatement(
348
+ props.config.joiner.failure(v),
349
+ ),
350
+ input: next.input,
351
+ objects: next.objects,
352
+ explore: next.explore,
353
+ })
354
+ : (next) =>
355
+ props.config.combiner({
356
+ logic: "or",
357
+ explore: next.explore,
358
+ input: next.input,
359
+ binaries: next.objects.map((object) => ({
360
+ expression: decode_object({
361
+ config: props.config,
362
+ functor: props.functor,
363
+ object,
364
+ input: next.input,
365
+ explore: next.explore,
366
+ }),
367
+ combined: true,
368
+ })),
369
+ expected: `(${next.objects.map((t) => t.name).join(" | ")})`,
370
+ }),
371
+ failure: (next) =>
372
+ ts.factory.createReturnStatement(props.config.joiner.failure(next)),
373
+ is: props.config.joiner.is,
374
+ required: props.config.joiner.required,
375
+ full: props.config.joiner.full
376
+ ? (next) => props.config.joiner.full!(next)
377
+ : undefined,
378
+ type: TypeFactory.keyword("boolean"),
379
+ },
380
+ generator: {
381
+ unions: props.config.numeric
382
+ ? (collection) =>
383
+ FeatureProgrammer.write_union_functions({
384
+ config: configure({
385
+ ...props,
386
+ config: {
387
+ ...props.config,
388
+ numeric: false,
389
+ },
390
+ }),
391
+ collection,
392
+ })
393
+ : undefined,
394
+ arrays: (collection) =>
395
+ write_array_functions({
396
+ ...props,
397
+ collection,
398
+ }),
399
+ tuples: (collection) =>
400
+ write_tuple_functions({
401
+ ...props,
402
+ collection,
403
+ }),
404
+ },
405
+ });
406
+
407
+ /* -----------------------------------------------------------
408
+ DECODERS
409
+ ----------------------------------------------------------- */
410
+ export const decode = (props: {
411
+ context: ITypiaContext;
412
+ config: IConfig;
413
+ functor: FunctionProgrammer;
414
+ input: ts.Expression;
415
+ metadata: Metadata;
416
+ explore: IExplore;
417
+ }): ts.Expression => {
418
+ if (props.metadata.any) return props.config.success;
419
+
420
+ const top: IBinary[] = [];
421
+ const binaries: IBinary[] = [];
422
+ const add = (next: {
423
+ exact: boolean;
424
+ left: ts.Expression;
425
+ right?: ts.Expression;
426
+ }) =>
427
+ create_add({
428
+ binaries,
429
+ left: next.left,
430
+ right: next.right,
431
+ exact: next.exact,
432
+ default: props.input,
433
+ });
434
+ const getConstantValue = (value: number | string | bigint | boolean) => {
435
+ if (typeof value === "string")
436
+ return ts.factory.createStringLiteral(value);
437
+ else if (typeof value === "bigint")
438
+ return ExpressionFactory.bigint(value);
439
+ return ts.factory.createIdentifier(value.toString());
440
+ };
441
+
442
+ //----
443
+ // CHECK OPTIONAL
444
+ //----
445
+ // @todo -> should be elaborated
446
+ const checkOptional: boolean =
447
+ props.metadata.empty() || props.metadata.isUnionBucket();
448
+
449
+ // NULLABLE
450
+ if (checkOptional || props.metadata.nullable)
451
+ if (props.metadata.nullable)
452
+ add({
453
+ exact: props.metadata.nullable,
454
+ left: ValueFactory.NULL(),
455
+ });
456
+ else
457
+ create_add({
458
+ binaries: top,
459
+ default: props.input,
460
+ exact: props.metadata.nullable,
461
+ left: ValueFactory.NULL(),
462
+ });
463
+
464
+ // UNDEFINDABLE
465
+ if (checkOptional || !props.metadata.isRequired())
466
+ if (props.metadata.isRequired())
467
+ create_add({
468
+ binaries: top,
469
+ default: props.input,
470
+ exact: false,
471
+ left: ValueFactory.UNDEFINED(),
472
+ });
473
+ else
474
+ add({
475
+ exact: true,
476
+ left: ValueFactory.UNDEFINED(),
477
+ });
478
+
479
+ // FUNCTIONAL
480
+ if (props.metadata.functions.length)
481
+ if (
482
+ OptionPredicator.functional(props.context.options) ||
483
+ props.metadata.size() !== 1
484
+ )
485
+ add({
486
+ exact: true,
487
+ left: ts.factory.createStringLiteral("function"),
488
+ right: ValueFactory.TYPEOF(props.input),
489
+ });
490
+ else
491
+ binaries.push({
492
+ combined: false,
493
+ expression: props.config.success,
494
+ });
495
+
496
+ //----
497
+ // VALUES
498
+ //----
499
+ // CONSTANT VALUES
500
+ const constants: MetadataConstant[] = props.metadata.constants.filter((c) =>
501
+ AtomicPredicator.constant({
502
+ metadata: props.metadata,
503
+ name: c.type,
504
+ }),
505
+ );
506
+ const constantLength: number = constants
507
+ .map((c) => c.values.length)
508
+ .reduce((a, b) => a + b, 0);
509
+ if (constantLength >= 10) {
510
+ const values: Array<boolean | number | string | bigint> = constants
511
+ .map((c) => c.values.map((v) => v.value))
512
+ .flat();
513
+ add({
514
+ exact: true,
515
+ left: ts.factory.createTrue(),
516
+ right: ts.factory.createCallExpression(
517
+ IdentifierFactory.access(
518
+ props.functor.emplaceVariable(
519
+ `${props.config.prefix}v${props.functor.increment()}`,
520
+ ts.factory.createNewExpression(
521
+ ts.factory.createIdentifier("Set"),
522
+ undefined,
523
+ [
524
+ ts.factory.createArrayLiteralExpression(
525
+ values.map((v) =>
526
+ typeof v === "boolean"
527
+ ? v === true
528
+ ? ts.factory.createTrue()
529
+ : ts.factory.createFalse()
530
+ : typeof v === "bigint"
531
+ ? ExpressionFactory.bigint(v)
532
+ : typeof v === "number"
533
+ ? ExpressionFactory.number(v)
534
+ : ts.factory.createStringLiteral(v.toString()),
535
+ ),
536
+ ),
537
+ ],
538
+ ),
539
+ ),
540
+ "has",
541
+ ),
542
+ undefined,
543
+ [props.input],
544
+ ),
545
+ });
546
+ } else
547
+ for (const c of constants)
548
+ if (
549
+ AtomicPredicator.constant({
550
+ metadata: props.metadata,
551
+ name: c.type,
552
+ })
553
+ )
554
+ for (const v of c.values)
555
+ add({
556
+ exact: true,
557
+ left: getConstantValue(v.value),
558
+ });
559
+
560
+ // ATOMIC VALUES
561
+ for (const atom of props.metadata.atomics)
562
+ if (
563
+ AtomicPredicator.atomic({
564
+ metadata: props.metadata,
565
+ name: atom.type,
566
+ }) === false
567
+ )
568
+ continue;
569
+ else if (atom.type === "number")
570
+ binaries.push({
571
+ expression: props.config.atomist({
572
+ explore: props.explore,
573
+ entry: check_number({
574
+ context: props.context,
575
+ numeric: props.config.numeric,
576
+ atomic: atom,
577
+ input: props.input,
578
+ }),
579
+ input: props.input,
580
+ }),
581
+ combined: false,
582
+ });
583
+ else if (atom.type === "bigint")
584
+ binaries.push({
585
+ expression: props.config.atomist({
586
+ explore: props.explore,
587
+ entry: check_bigint({
588
+ context: props.context,
589
+ atomic: atom,
590
+ input: props.input,
591
+ }),
592
+ input: props.input,
593
+ }),
594
+ combined: false,
595
+ });
596
+ else if (atom.type === "string")
597
+ binaries.push({
598
+ expression: props.config.atomist({
599
+ explore: props.explore,
600
+ entry: check_string({
601
+ context: props.context,
602
+ atomic: atom,
603
+ input: props.input,
604
+ }),
605
+ input: props.input,
606
+ }),
607
+ combined: false,
608
+ });
609
+ else
610
+ add({
611
+ exact: true,
612
+ left: ts.factory.createStringLiteral(atom.type),
613
+ right: ValueFactory.TYPEOF(props.input),
614
+ });
615
+
616
+ // TEMPLATE LITERAL VALUES
617
+ if (props.metadata.templates.length)
618
+ if (AtomicPredicator.template(props.metadata))
619
+ binaries.push({
620
+ expression: props.config.atomist({
621
+ explore: props.explore,
622
+ entry: check_template({
623
+ templates: props.metadata.templates,
624
+ input: props.input,
625
+ }),
626
+ input: props.input,
627
+ }),
628
+ combined: false,
629
+ });
630
+
631
+ // NATIVE CLASSES
632
+ for (const native of props.metadata.natives)
633
+ binaries.push({
634
+ expression: check_native({
635
+ name: native.name,
636
+ input: props.input,
637
+ }),
638
+ combined: false,
639
+ });
640
+
641
+ //----
642
+ // INSTANCES
643
+ //----
644
+ interface IInstance {
645
+ head: ts.Expression;
646
+ body: ts.Expression | null;
647
+ expected: string;
648
+ }
649
+ const instances: IInstance[] = [];
650
+ const prepare = (next: IInstance) => instances.push(next);
651
+
652
+ // SETS
653
+ if (props.metadata.sets.length) {
654
+ const install = (body: ts.Expression | null) =>
655
+ prepare({
656
+ head: check_native({
657
+ name: "Set",
658
+ input: props.input,
659
+ }),
660
+ expected: props.metadata.sets
661
+ .map((elem) => `Set<${elem.getName()}>`)
662
+ .join(" | "),
663
+ body,
664
+ });
665
+ if (props.metadata.sets.some((elem) => elem.value.any)) install(null);
666
+ else
667
+ install(
668
+ explore_sets({
669
+ config: props.config,
670
+ context: props.context,
671
+ functor: props.functor,
672
+ sets: props.metadata.sets,
673
+ input: props.input,
674
+ explore: {
675
+ ...props.explore,
676
+ from: "array",
677
+ },
678
+ }),
679
+ );
680
+ }
681
+
682
+ // MAPS
683
+ if (props.metadata.maps.length) {
684
+ const install = (body: ts.Expression | null) =>
685
+ prepare({
686
+ head: check_native({
687
+ name: "Map",
688
+ input: props.input,
689
+ }),
690
+ expected: props.metadata.maps
691
+ .map(({ key, value }) => `Map<${key}, ${value}>`)
692
+ .join(" | "),
693
+ body,
694
+ });
695
+ if (props.metadata.maps.some((elem) => elem.key.any && elem.value.any))
696
+ install(null);
697
+ else
698
+ install(
699
+ explore_maps({
700
+ config: props.config,
701
+ context: props.context,
702
+ functor: props.functor,
703
+ maps: props.metadata.maps,
704
+ input: props.input,
705
+ explore: {
706
+ ...props.explore,
707
+ from: "array",
708
+ },
709
+ }),
710
+ );
711
+ }
712
+
713
+ // ARRAYS AND TUPLES
714
+ if (props.metadata.tuples.length + props.metadata.arrays.length > 0) {
715
+ const install = (body: ts.Expression | null) =>
716
+ prepare({
717
+ head: props.config.atomist({
718
+ explore: props.explore,
719
+ entry: {
720
+ expected: [
721
+ ...props.metadata.tuples.map((t) => t.type.name),
722
+ ...props.metadata.arrays.map((a) => a.getName()),
723
+ ].join(" | "),
724
+ expression: ExpressionFactory.isArray(props.input),
725
+ conditions: [],
726
+ },
727
+ input: props.input,
728
+ }),
729
+ expected: [...props.metadata.tuples, ...props.metadata.arrays]
730
+ .map((elem) => elem.type.name)
731
+ .join(" | "),
732
+ body,
733
+ });
734
+ if (props.metadata.arrays.length === 0)
735
+ if (props.metadata.tuples.length === 1)
736
+ install(
737
+ decode_tuple({
738
+ config: props.config,
739
+ context: props.context,
740
+ functor: props.functor,
741
+ tuple: props.metadata.tuples[0]!,
742
+ input: props.input,
743
+ explore: {
744
+ ...props.explore,
745
+ from: "array",
746
+ },
747
+ }),
748
+ );
749
+ // TUPLE ONLY
750
+ else
751
+ install(
752
+ explore_tuples({
753
+ config: props.config,
754
+ context: props.context,
755
+ functor: props.functor,
756
+ tuples: props.metadata.tuples,
757
+ input: props.input,
758
+ explore: {
759
+ ...props.explore,
760
+ from: "array",
761
+ },
762
+ }),
763
+ );
764
+ else if (props.metadata.arrays.some((elem) => elem.type.value.any))
765
+ install(null);
766
+ else if (props.metadata.tuples.length === 0)
767
+ if (props.metadata.arrays.length === 1)
768
+ // ARRAY ONLY
769
+ install(
770
+ decode_array({
771
+ config: props.config,
772
+ context: props.context,
773
+ functor: props.functor,
774
+ array: props.metadata.arrays[0]!,
775
+ input: props.input,
776
+ explore: {
777
+ ...props.explore,
778
+ from: "array",
779
+ },
780
+ }),
781
+ );
782
+ else
783
+ install(
784
+ explore_arrays({
785
+ config: props.config,
786
+ context: props.context,
787
+ functor: props.functor,
788
+ arrays: props.metadata.arrays,
789
+ input: props.input,
790
+ explore: {
791
+ ...props.explore,
792
+ from: "array",
793
+ },
794
+ }),
795
+ );
796
+ else
797
+ install(
798
+ explore_arrays_and_tuples({
799
+ config: props.config,
800
+ context: props.context,
801
+ functor: props.functor,
802
+ definitions: [...props.metadata.tuples, ...props.metadata.arrays],
803
+ input: props.input,
804
+ explore: props.explore,
805
+ }),
806
+ );
807
+ }
808
+
809
+ // OBJECT
810
+ if (props.metadata.objects.length > 0)
811
+ prepare({
812
+ head: ExpressionFactory.isObject({
813
+ checkNull: true,
814
+ checkArray: props.metadata.objects.some((obj) =>
815
+ obj.type.properties.every(
816
+ (prop) => !prop.key.isSoleLiteral() || !prop.value.isRequired(),
817
+ ),
818
+ ),
819
+ input: props.input,
820
+ }),
821
+ expected: props.metadata.objects
822
+ .map((obj) => obj.type.name)
823
+ .join(" | "),
824
+ body: explore_objects({
825
+ config: props.config,
826
+ functor: props.functor,
827
+ metadata: props.metadata,
828
+ input: props.input,
829
+ explore: {
830
+ ...props.explore,
831
+ from: "object",
832
+ },
833
+ }),
834
+ });
835
+
836
+ if (instances.length) {
837
+ const transformer =
838
+ (merge: (x: ts.Expression, y: ts.Expression) => ts.Expression) =>
839
+ (instance: IInstance) =>
840
+ instance.body
841
+ ? {
842
+ expression: merge(instance.head, instance.body),
843
+ combined: true,
844
+ }
845
+ : {
846
+ expression: instance.head,
847
+ combined: false,
848
+ };
849
+ if (instances.length === 1)
850
+ binaries.push(
851
+ transformer((head, body) =>
852
+ props.config.combiner({
853
+ explore: props.explore,
854
+ logic: "and",
855
+ input: props.input,
856
+ binaries: [head, body].map((expression) => ({
857
+ expression,
858
+ combined: expression !== head,
859
+ })),
860
+ expected: props.metadata.getName(),
861
+ }),
862
+ )(instances[0]!),
863
+ );
864
+ else
865
+ binaries.push({
866
+ expression: props.config.combiner({
867
+ explore: props.explore,
868
+ logic: "or",
869
+ input: props.input,
870
+ binaries: instances.map(transformer(ts.factory.createLogicalAnd)),
871
+ expected: props.metadata.getName(),
872
+ }),
873
+ combined: true,
874
+ });
875
+ }
876
+
877
+ // ESCAPED CASE
878
+ if (props.metadata.escaped !== null)
879
+ binaries.push({
880
+ combined: false,
881
+ expression:
882
+ props.metadata.escaped.original.size() === 1 &&
883
+ props.metadata.escaped.original.natives.length === 1
884
+ ? check_native({
885
+ name: props.metadata.escaped.original.natives[0]!.name,
886
+ input: props.input,
887
+ })
888
+ : ts.factory.createLogicalAnd(
889
+ decode({
890
+ context: props.context,
891
+ config: props.config,
892
+ functor: props.functor,
893
+ metadata: props.metadata.escaped.original,
894
+ input: props.input,
895
+ explore: props.explore,
896
+ }),
897
+ ts.factory.createLogicalAnd(
898
+ IsProgrammer.decode_to_json({
899
+ checkNull: false,
900
+ input: props.input,
901
+ }),
902
+ decode_escaped({
903
+ config: props.config,
904
+ context: props.context,
905
+ functor: props.functor,
906
+ metadata: props.metadata.escaped.returns,
907
+ input: props.input,
908
+ explore: props.explore,
909
+ }),
910
+ ),
911
+ ),
912
+ });
913
+
914
+ //----
915
+ // COMBINE CONDITIONS
916
+ //----
917
+ return top.length && binaries.length
918
+ ? props.config.combiner({
919
+ explore: props.explore,
920
+ logic: "and",
921
+ input: props.input,
922
+ binaries: [
923
+ ...top,
924
+ {
925
+ expression: props.config.combiner({
926
+ explore: props.explore,
927
+ logic: "or",
928
+ input: props.input,
929
+ binaries,
930
+ expected: props.metadata.getName(),
931
+ }),
932
+ combined: true,
933
+ },
934
+ ],
935
+ expected: props.metadata.getName(),
936
+ })
937
+ : binaries.length
938
+ ? props.config.combiner({
939
+ explore: props.explore,
940
+ logic: "or",
941
+ input: props.input,
942
+ binaries,
943
+ expected: props.metadata.getName(),
944
+ })
945
+ : props.config.success;
946
+ };
947
+
948
+ export const decode_object = (props: {
949
+ config: IConfig;
950
+ functor: FunctionProgrammer;
951
+ object: MetadataObjectType;
952
+ input: ts.Expression;
953
+ explore: IExplore;
954
+ }) => {
955
+ props.object.validated ||= true;
956
+ return FeatureProgrammer.decode_object(props);
957
+ };
958
+
959
+ const decode_array = (props: {
960
+ config: IConfig;
961
+ context: ITypiaContext;
962
+ functor: FunctionProgrammer;
963
+ array: MetadataArray;
964
+ input: ts.Expression;
965
+ explore: IExplore;
966
+ }) => {
967
+ if (props.array.type.recursive === false) return decode_array_inline(props);
968
+
969
+ const arrayExplore: IExplore = {
970
+ ...props.explore,
971
+ source: "function",
972
+ from: "array",
973
+ };
974
+ return ts.factory.createLogicalOr(
975
+ ts.factory.createCallExpression(
976
+ ts.factory.createIdentifier(
977
+ props.functor.useLocal(
978
+ `${props.config.prefix}a${props.array.type.index}`,
979
+ ),
980
+ ),
981
+ undefined,
982
+ FeatureProgrammer.argumentsArray({
983
+ config: props.config,
984
+ explore: {
985
+ ...arrayExplore,
986
+ source: "function",
987
+ from: "array",
988
+ },
989
+ input: props.input,
990
+ }),
991
+ ),
992
+ props.config.joiner.failure({
993
+ input: props.input,
994
+ expected: props.array.type.name,
995
+ explore: arrayExplore,
996
+ }),
997
+ );
998
+ };
999
+
1000
+ const decode_array_inline = (props: {
1001
+ config: IConfig;
1002
+ context: ITypiaContext;
1003
+ functor: FunctionProgrammer;
1004
+ array: MetadataArray;
1005
+ input: ts.Expression;
1006
+ explore: IExplore;
1007
+ }): ts.Expression => {
1008
+ const length: ICheckEntry = check_array_length({
1009
+ context: props.context,
1010
+ array: props.array,
1011
+ input: props.input,
1012
+ });
1013
+ const main: ts.Expression = FeatureProgrammer.decode_array({
1014
+ config: {
1015
+ prefix: props.config.prefix,
1016
+ trace: props.config.trace,
1017
+ path: props.config.path,
1018
+ decoder: (next) =>
1019
+ decode({
1020
+ ...props,
1021
+ ...next,
1022
+ }),
1023
+ },
1024
+ functor: props.functor,
1025
+ combiner: props.config.joiner.array,
1026
+ array: props.array,
1027
+ input: props.input,
1028
+ explore: props.explore,
1029
+ });
1030
+ return length.expression === null && length.conditions.length === 0
1031
+ ? main
1032
+ : ts.factory.createLogicalAnd(
1033
+ props.config.atomist({
1034
+ explore: props.explore,
1035
+ input: props.input,
1036
+ entry: length,
1037
+ }),
1038
+ main,
1039
+ );
1040
+ };
1041
+
1042
+ const decode_tuple = (props: {
1043
+ context: ITypiaContext;
1044
+ config: IConfig;
1045
+ functor: FunctionProgrammer;
1046
+ tuple: MetadataTuple;
1047
+ input: ts.Expression;
1048
+ explore: IExplore;
1049
+ }): ts.Expression => {
1050
+ if (props.tuple.type.recursive === false)
1051
+ return decode_tuple_inline({
1052
+ ...props,
1053
+ tuple: props.tuple.type,
1054
+ });
1055
+ const arrayExplore: IExplore = {
1056
+ ...props.explore,
1057
+ source: "function",
1058
+ from: "array",
1059
+ };
1060
+ return ts.factory.createLogicalOr(
1061
+ ts.factory.createCallExpression(
1062
+ ts.factory.createIdentifier(
1063
+ props.functor.useLocal(
1064
+ `${props.config.prefix}t${props.tuple.type.index}`,
1065
+ ),
1066
+ ),
1067
+ undefined,
1068
+ FeatureProgrammer.argumentsArray({
1069
+ config: props.config,
1070
+ explore: {
1071
+ ...arrayExplore,
1072
+ source: "function",
1073
+ },
1074
+ input: props.input,
1075
+ }),
1076
+ ),
1077
+ props.config.joiner.failure({
1078
+ input: props.input,
1079
+ expected: props.tuple.type.name,
1080
+ explore: arrayExplore,
1081
+ }),
1082
+ );
1083
+ };
1084
+
1085
+ const decode_tuple_inline = (props: {
1086
+ config: IConfig;
1087
+ context: ITypiaContext;
1088
+ functor: FunctionProgrammer;
1089
+ tuple: MetadataTupleType;
1090
+ input: ts.Expression;
1091
+ explore: IExplore;
1092
+ }): ts.Expression => {
1093
+ const binaries: ts.Expression[] = props.tuple.elements
1094
+ .filter((metadata) => metadata.rest === null)
1095
+ .map((metadata, index) =>
1096
+ decode({
1097
+ context: props.context,
1098
+ config: props.config,
1099
+ functor: props.functor,
1100
+ input: ts.factory.createElementAccessExpression(props.input, index),
1101
+ metadata,
1102
+ explore: {
1103
+ ...props.explore,
1104
+ from: "array",
1105
+ postfix: props.explore.postfix.length
1106
+ ? `${postfix_of_tuple(props.explore.postfix)}[${index}]"`
1107
+ : `"[${index}]"`,
1108
+ },
1109
+ }),
1110
+ );
1111
+ const rest: ts.Expression | null =
1112
+ props.tuple.elements.length && props.tuple.elements.at(-1)!.rest !== null
1113
+ ? decode({
1114
+ config: props.config,
1115
+ context: props.context,
1116
+ functor: props.functor,
1117
+ input: ts.factory.createCallExpression(
1118
+ IdentifierFactory.access(props.input, "slice"),
1119
+ undefined,
1120
+ [ExpressionFactory.number(props.tuple.elements.length - 1)],
1121
+ ),
1122
+ metadata: wrap_metadata_rest_tuple(
1123
+ props.tuple.elements.at(-1)!.rest!,
1124
+ ),
1125
+ explore: {
1126
+ ...props.explore,
1127
+ start: props.tuple.elements.length - 1,
1128
+ },
1129
+ })
1130
+ : null;
1131
+ const arrayLength = ts.factory.createPropertyAccessExpression(
1132
+ props.input,
1133
+ "length",
1134
+ );
1135
+ return props.config.combiner({
1136
+ explore: props.explore,
1137
+ logic: "and",
1138
+ input: props.input,
1139
+ binaries: [
1140
+ ...(rest === null
1141
+ ? props.tuple.elements.every((t) => t.optional === false)
1142
+ ? [
1143
+ {
1144
+ combined: false,
1145
+ expression: ts.factory.createStrictEquality(
1146
+ arrayLength,
1147
+ ExpressionFactory.number(props.tuple.elements.length),
1148
+ ),
1149
+ },
1150
+ ]
1151
+ : [
1152
+ {
1153
+ combined: false,
1154
+ expression: ts.factory.createLogicalAnd(
1155
+ ts.factory.createLessThanEquals(
1156
+ ExpressionFactory.number(
1157
+ props.tuple.elements.filter((t) => t.optional === false)
1158
+ .length,
1159
+ ),
1160
+ arrayLength,
1161
+ ),
1162
+ ts.factory.createGreaterThanEquals(
1163
+ ExpressionFactory.number(props.tuple.elements.length),
1164
+ arrayLength,
1165
+ ),
1166
+ ),
1167
+ },
1168
+ ]
1169
+ : []),
1170
+ ...(props.config.joiner.tuple
1171
+ ? [
1172
+ {
1173
+ expression: props.config.joiner.tuple(binaries),
1174
+ combined: true,
1175
+ },
1176
+ ]
1177
+ : binaries.map((expression) => ({
1178
+ expression,
1179
+ combined: true,
1180
+ }))),
1181
+ ...(rest !== null
1182
+ ? [
1183
+ {
1184
+ expression: rest,
1185
+ combined: true,
1186
+ },
1187
+ ]
1188
+ : []),
1189
+ ],
1190
+ expected: `[${props.tuple.elements.map((t) => t.getName()).join(", ")}]`,
1191
+ });
1192
+ };
1193
+
1194
+ const decode_escaped = (props: {
1195
+ config: IConfig;
1196
+ context: ITypiaContext;
1197
+ functor: FunctionProgrammer;
1198
+ metadata: Metadata;
1199
+ input: ts.Expression;
1200
+ explore: IExplore;
1201
+ }): ts.Expression =>
1202
+ ts.factory.createCallExpression(
1203
+ ts.factory.createParenthesizedExpression(
1204
+ ts.factory.createArrowFunction(
1205
+ undefined,
1206
+ undefined,
1207
+ [IdentifierFactory.parameter("input", TypeFactory.keyword("any"))],
1208
+ undefined,
1209
+ undefined,
1210
+ decode({
1211
+ ...props,
1212
+ input: ts.factory.createIdentifier("input"),
1213
+ }),
1214
+ ),
1215
+ ),
1216
+ undefined,
1217
+ [
1218
+ ts.factory.createCallExpression(
1219
+ IdentifierFactory.access(props.input, "toJSON"),
1220
+ undefined,
1221
+ [],
1222
+ ),
1223
+ ],
1224
+ );
1225
+
1226
+ /* -----------------------------------------------------------
1227
+ UNION TYPE EXPLORERS
1228
+ ----------------------------------------------------------- */
1229
+ const explore_sets = (props: {
1230
+ context: ITypiaContext;
1231
+ config: IConfig;
1232
+ functor: FunctionProgrammer;
1233
+ sets: MetadataSet[];
1234
+ input: ts.Expression;
1235
+ explore: IExplore;
1236
+ }): ts.Expression =>
1237
+ ts.factory.createCallExpression(
1238
+ UnionExplorer.set({
1239
+ config: {
1240
+ checker: (v) =>
1241
+ decode({
1242
+ context: props.context,
1243
+ config: props.config,
1244
+ functor: props.functor,
1245
+ input: v.input,
1246
+ metadata: v.definition,
1247
+ explore: v.explore,
1248
+ }),
1249
+ decoder: (v) =>
1250
+ decode_array({
1251
+ config: props.config,
1252
+ context: props.context,
1253
+ functor: props.functor,
1254
+ array: v.definition,
1255
+ input: v.input,
1256
+ explore: v.explore,
1257
+ }),
1258
+ empty: props.config.success,
1259
+ success: props.config.success,
1260
+ failure: (v) =>
1261
+ ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1262
+ },
1263
+ parameters: [],
1264
+ input: props.input,
1265
+ sets: props.sets,
1266
+ explore: props.explore,
1267
+ }),
1268
+ undefined,
1269
+ undefined,
1270
+ );
1271
+
1272
+ const explore_maps = (props: {
1273
+ context: ITypiaContext;
1274
+ config: IConfig;
1275
+ functor: FunctionProgrammer;
1276
+ input: ts.Expression;
1277
+ maps: MetadataMap[];
1278
+ explore: IExplore;
1279
+ }): ts.Expression =>
1280
+ ts.factory.createCallExpression(
1281
+ UnionExplorer.map({
1282
+ config: {
1283
+ checker: (v) =>
1284
+ ts.factory.createLogicalAnd(
1285
+ decode({
1286
+ config: props.config,
1287
+ context: props.context,
1288
+ functor: props.functor,
1289
+ input: ts.factory.createElementAccessExpression(v.input, 0),
1290
+ metadata: v.definition[0],
1291
+ explore: {
1292
+ ...v.explore,
1293
+ postfix: `${v.explore.postfix}[0]`,
1294
+ },
1295
+ }),
1296
+ decode({
1297
+ config: props.config,
1298
+ context: props.context,
1299
+ functor: props.functor,
1300
+ input: ts.factory.createElementAccessExpression(v.input, 1),
1301
+ metadata: v.definition[1],
1302
+ explore: {
1303
+ ...v.explore,
1304
+ postfix: `${v.explore.postfix}[1]`,
1305
+ },
1306
+ }),
1307
+ ),
1308
+ decoder: (v) =>
1309
+ decode_array({
1310
+ context: props.context,
1311
+ config: props.config,
1312
+ functor: props.functor,
1313
+ array: v.definition,
1314
+ input: v.input,
1315
+ explore: v.explore,
1316
+ }),
1317
+ empty: props.config.success,
1318
+ success: props.config.success,
1319
+ failure: (v) =>
1320
+ ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1321
+ },
1322
+ parameters: [],
1323
+ input: props.input,
1324
+ maps: props.maps,
1325
+ explore: props.explore,
1326
+ }),
1327
+ undefined,
1328
+ undefined,
1329
+ );
1330
+
1331
+ const explore_tuples = (props: {
1332
+ config: IConfig;
1333
+ context: ITypiaContext;
1334
+ functor: FunctionProgrammer;
1335
+ tuples: MetadataTuple[];
1336
+ input: ts.Expression;
1337
+ explore: IExplore;
1338
+ }): ts.Expression =>
1339
+ explore_array_like_union_types<MetadataTuple>({
1340
+ config: props.config,
1341
+ functor: props.functor,
1342
+ factory: (next) =>
1343
+ UnionExplorer.tuple({
1344
+ config: {
1345
+ checker: (v) =>
1346
+ decode_tuple({
1347
+ context: props.context,
1348
+ config: props.config,
1349
+ functor: props.functor,
1350
+ input: v.input,
1351
+ tuple: v.definition,
1352
+ explore: v.explore,
1353
+ }),
1354
+ decoder: (v) =>
1355
+ decode_tuple({
1356
+ context: props.context,
1357
+ config: props.config,
1358
+ functor: props.functor,
1359
+ tuple: v.definition,
1360
+ input: v.input,
1361
+ explore: v.explore,
1362
+ }),
1363
+ empty: props.config.success,
1364
+ success: props.config.success,
1365
+ failure: (v) =>
1366
+ ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1367
+ },
1368
+ parameters: next.parameters,
1369
+ tuples: next.definitions,
1370
+ input: next.input,
1371
+ explore: next.explore,
1372
+ }),
1373
+ definitions: props.tuples,
1374
+ input: props.input,
1375
+ explore: props.explore,
1376
+ });
1377
+
1378
+ const explore_arrays = (props: {
1379
+ config: IConfig;
1380
+ context: ITypiaContext;
1381
+ functor: FunctionProgrammer;
1382
+ arrays: MetadataArray[];
1383
+ input: ts.Expression;
1384
+ explore: IExplore;
1385
+ }): ts.Expression =>
1386
+ explore_array_like_union_types<MetadataArray>({
1387
+ config: props.config,
1388
+ functor: props.functor,
1389
+ factory: (next) =>
1390
+ UnionExplorer.array({
1391
+ config: {
1392
+ checker: (v) =>
1393
+ decode({
1394
+ context: props.context,
1395
+ config: props.config,
1396
+ functor: props.functor,
1397
+ metadata: v.definition,
1398
+ input: v.input,
1399
+ explore: v.explore,
1400
+ }),
1401
+ decoder: (v) =>
1402
+ decode_array({
1403
+ context: props.context,
1404
+ config: props.config,
1405
+ functor: props.functor,
1406
+ array: v.definition,
1407
+ input: v.input,
1408
+ explore: v.explore,
1409
+ }),
1410
+ empty: props.config.success,
1411
+ success: props.config.success,
1412
+ failure: (v) =>
1413
+ ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1414
+ },
1415
+ parameters: next.parameters,
1416
+ arrays: next.definitions,
1417
+ input: next.input,
1418
+ explore: next.explore,
1419
+ }),
1420
+ definitions: props.arrays,
1421
+ input: props.input,
1422
+ explore: props.explore,
1423
+ });
1424
+
1425
+ const explore_arrays_and_tuples = (props: {
1426
+ config: IConfig;
1427
+ context: ITypiaContext;
1428
+ functor: FunctionProgrammer;
1429
+ definitions: Array<MetadataArray | MetadataTuple>;
1430
+ input: ts.Expression;
1431
+ explore: IExplore;
1432
+ }): ts.Expression =>
1433
+ explore_array_like_union_types<MetadataArray | MetadataTuple>({
1434
+ config: props.config,
1435
+ functor: props.functor,
1436
+ factory: (next) =>
1437
+ UnionExplorer.array_or_tuple({
1438
+ config: {
1439
+ checker: (v) =>
1440
+ v.definition instanceof MetadataTuple
1441
+ ? decode_tuple({
1442
+ config: props.config,
1443
+ context: props.context,
1444
+ functor: props.functor,
1445
+ input: v.input,
1446
+ tuple: v.definition,
1447
+ explore: v.explore,
1448
+ })
1449
+ : props.config.atomist({
1450
+ explore: v.explore,
1451
+ entry: {
1452
+ expected: props.definitions
1453
+ .map((elem) =>
1454
+ elem instanceof MetadataArray
1455
+ ? elem.getName()
1456
+ : elem.type.name,
1457
+ )
1458
+ .join(" | "),
1459
+ expression: decode({
1460
+ functor: props.functor,
1461
+ context: props.context,
1462
+ config: props.config,
1463
+ metadata: v.definition,
1464
+ input: v.input,
1465
+ explore: v.explore,
1466
+ }),
1467
+ conditions: [],
1468
+ },
1469
+ input: v.container,
1470
+ }),
1471
+ decoder: (v) =>
1472
+ v.definition instanceof MetadataTuple
1473
+ ? decode_tuple({
1474
+ context: props.context,
1475
+ config: props.config,
1476
+ functor: props.functor,
1477
+ input: v.input,
1478
+ tuple: v.definition,
1479
+ explore: v.explore,
1480
+ })
1481
+ : decode_array({
1482
+ context: props.context,
1483
+ config: props.config,
1484
+ functor: props.functor,
1485
+ input: v.input,
1486
+ array: v.definition,
1487
+ explore: v.explore,
1488
+ }),
1489
+ empty: props.config.success,
1490
+ success: props.config.success,
1491
+ failure: (v) =>
1492
+ ts.factory.createReturnStatement(props.config.joiner.failure(v)),
1493
+ },
1494
+ parameters: next.parameters,
1495
+ definitions: next.definitions,
1496
+ input: next.input,
1497
+ explore: next.explore,
1498
+ }),
1499
+ input: props.input,
1500
+ definitions: props.definitions,
1501
+ explore: props.explore,
1502
+ });
1503
+
1504
+ const explore_array_like_union_types = <
1505
+ T extends MetadataArray | MetadataTuple,
1506
+ >(props: {
1507
+ config: IConfig;
1508
+ functor: FunctionProgrammer;
1509
+ factory: (next: {
1510
+ parameters: ts.ParameterDeclaration[];
1511
+ definitions: T[];
1512
+ input: ts.Expression;
1513
+ explore: IExplore;
1514
+ }) => ts.ArrowFunction;
1515
+ input: ts.Expression;
1516
+ definitions: T[];
1517
+ explore: IExplore;
1518
+ }): ts.Expression => {
1519
+ const arrow = (next: {
1520
+ parameters: ts.ParameterDeclaration[];
1521
+ explore: IExplore;
1522
+ input: ts.Expression;
1523
+ }): ts.ArrowFunction =>
1524
+ props.factory({
1525
+ parameters: next.parameters,
1526
+ definitions: props.definitions,
1527
+ input: next.input,
1528
+ explore: next.explore,
1529
+ });
1530
+ if (props.definitions.every((e) => e.type.recursive === false))
1531
+ ts.factory.createCallExpression(
1532
+ arrow({
1533
+ explore: props.explore,
1534
+ input: props.input,
1535
+ parameters: [],
1536
+ }),
1537
+ undefined,
1538
+ [],
1539
+ );
1540
+ const arrayExplore: IExplore = {
1541
+ ...props.explore,
1542
+ source: "function",
1543
+ from: "array",
1544
+ };
1545
+ return ts.factory.createLogicalOr(
1546
+ ts.factory.createCallExpression(
1547
+ ts.factory.createIdentifier(
1548
+ props.functor.emplaceUnion(
1549
+ props.config.prefix,
1550
+ props.definitions.map((e) => e.type.name).join(" | "),
1551
+ () =>
1552
+ arrow({
1553
+ parameters: FeatureProgrammer.parameterDeclarations({
1554
+ config: props.config,
1555
+ type: TypeFactory.keyword("any"),
1556
+ input: ts.factory.createIdentifier("input"),
1557
+ }),
1558
+ explore: {
1559
+ ...arrayExplore,
1560
+ postfix: "",
1561
+ },
1562
+ input: ts.factory.createIdentifier("input"),
1563
+ }),
1564
+ ),
1565
+ ),
1566
+ undefined,
1567
+ FeatureProgrammer.argumentsArray(props),
1568
+ ),
1569
+ props.config.joiner.failure({
1570
+ input: props.input,
1571
+ expected: props.definitions.map((e) => e.type.name).join(" | "),
1572
+ explore: arrayExplore,
1573
+ }),
1574
+ );
1575
+ };
1576
+
1577
+ const explore_objects = (props: {
1578
+ config: IConfig;
1579
+ functor: FunctionProgrammer;
1580
+ input: ts.Expression;
1581
+ metadata: Metadata;
1582
+ explore: IExplore;
1583
+ }) =>
1584
+ props.metadata.objects.length === 1
1585
+ ? decode_object({
1586
+ config: props.config,
1587
+ functor: props.functor,
1588
+ object: props.metadata.objects[0]!.type,
1589
+ input: props.input,
1590
+ explore: props.explore,
1591
+ })
1592
+ : ts.factory.createCallExpression(
1593
+ ts.factory.createIdentifier(
1594
+ props.functor.useLocal(
1595
+ `${props.config.prefix}u${props.metadata.union_index!}`,
1596
+ ),
1597
+ ),
1598
+ undefined,
1599
+ FeatureProgrammer.argumentsArray(props),
1600
+ );
1601
+ }
1602
+
1603
+ const create_add = (props: {
1604
+ binaries: CheckerProgrammer.IBinary[];
1605
+ default: ts.Expression;
1606
+ exact: boolean;
1607
+ left: ts.Expression;
1608
+ right?: ts.Expression;
1609
+ }) => {
1610
+ const factory = props.exact
1611
+ ? ts.factory.createStrictEquality
1612
+ : ts.factory.createStrictInequality;
1613
+ props.binaries.push({
1614
+ expression: factory(props.left, props.right ?? props.default),
1615
+ combined: false,
1616
+ });
1617
+ };