typia 7.2.1 → 7.3.0-dev.20241213

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 (447) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +148 -148
  3. package/lib/executable/typia.js +0 -0
  4. package/lib/programmers/llm/LlmApplicationProgrammer.js +0 -1
  5. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  6. package/package.json +3 -3
  7. package/src/IRandomGenerator.ts +49 -49
  8. package/src/IReadableURLSearchParams.ts +9 -9
  9. package/src/IValidation.ts +21 -21
  10. package/src/executable/TypiaGenerateWizard.ts +83 -83
  11. package/src/executable/TypiaPatchWizard.ts +45 -45
  12. package/src/executable/TypiaSetupWizard.ts +179 -179
  13. package/src/executable/setup/ArgumentParser.ts +42 -42
  14. package/src/executable/setup/FileRetriever.ts +19 -19
  15. package/src/executable/setup/PackageManager.ts +87 -87
  16. package/src/factories/ExpressionFactory.ts +216 -216
  17. package/src/factories/IdentifierFactory.ts +89 -89
  18. package/src/factories/JsonMetadataFactory.ts +76 -76
  19. package/src/factories/LiteralFactory.ts +52 -52
  20. package/src/factories/MetadataCollection.ts +278 -278
  21. package/src/factories/MetadataCommentTagFactory.ts +650 -650
  22. package/src/factories/MetadataFactory.ts +404 -404
  23. package/src/factories/MetadataTypeTagFactory.ts +411 -411
  24. package/src/factories/MetadataTypeTagSchemaFactory.ts +82 -82
  25. package/src/factories/NumericRangeFactory.ts +72 -72
  26. package/src/factories/ProtobufFactory.ts +875 -875
  27. package/src/factories/StatementFactory.ts +90 -90
  28. package/src/factories/TemplateFactory.ts +64 -64
  29. package/src/factories/TypeFactory.ts +140 -140
  30. package/src/factories/internal/metadata/IMetadataIteratorProps.ts +17 -17
  31. package/src/factories/internal/metadata/MetadataHelper.ts +21 -21
  32. package/src/factories/internal/metadata/emplace_metadata_alias.ts +33 -33
  33. package/src/factories/internal/metadata/emplace_metadata_array_type.ts +39 -39
  34. package/src/factories/internal/metadata/emplace_metadata_object.ts +208 -208
  35. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +57 -57
  36. package/src/factories/internal/metadata/explore_metadata.ts +31 -31
  37. package/src/factories/internal/metadata/iterate_metadata.ts +54 -54
  38. package/src/factories/internal/metadata/iterate_metadata_alias.ts +33 -33
  39. package/src/factories/internal/metadata/iterate_metadata_array.ts +63 -63
  40. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +62 -62
  41. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +28 -28
  42. package/src/factories/internal/metadata/iterate_metadata_collection.ts +146 -146
  43. package/src/factories/internal/metadata/iterate_metadata_comment_tags.ts +32 -32
  44. package/src/factories/internal/metadata/iterate_metadata_constant.ts +76 -76
  45. package/src/factories/internal/metadata/iterate_metadata_escape.ts +49 -49
  46. package/src/factories/internal/metadata/iterate_metadata_function.ts +91 -91
  47. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +213 -213
  48. package/src/factories/internal/metadata/iterate_metadata_map.ts +57 -57
  49. package/src/factories/internal/metadata/iterate_metadata_native.ts +255 -255
  50. package/src/factories/internal/metadata/iterate_metadata_object.ts +35 -35
  51. package/src/factories/internal/metadata/iterate_metadata_set.ts +57 -57
  52. package/src/factories/internal/metadata/iterate_metadata_sort.ts +87 -87
  53. package/src/factories/internal/metadata/iterate_metadata_template.ts +41 -41
  54. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +26 -26
  55. package/src/factories/internal/metadata/iterate_metadata_union.ts +19 -19
  56. package/src/functional.ts +750 -750
  57. package/src/http.ts +1047 -1047
  58. package/src/internal/_IProtobufWriter.ts +18 -18
  59. package/src/internal/_ProtobufReader.ts +194 -194
  60. package/src/internal/_ProtobufSizer.ts +145 -145
  61. package/src/internal/_ProtobufWriter.ts +145 -145
  62. package/src/internal/_accessExpressionAsString.ts +46 -46
  63. package/src/internal/_assertGuard.ts +13 -13
  64. package/src/internal/_functionalTypeGuardErrorFactory.ts +4 -4
  65. package/src/internal/_httpFormDataReadArray.ts +4 -4
  66. package/src/internal/_httpFormDataReadBigint.ts +18 -18
  67. package/src/internal/_httpFormDataReadBlob.ts +10 -10
  68. package/src/internal/_httpFormDataReadBoolean.ts +16 -16
  69. package/src/internal/_httpFormDataReadFile.ts +10 -10
  70. package/src/internal/_httpFormDataReadNumber.ts +15 -15
  71. package/src/internal/_httpFormDataReadString.ts +10 -10
  72. package/src/internal/_httpHeaderReadBigint.ts +10 -10
  73. package/src/internal/_httpHeaderReadBoolean.ts +8 -8
  74. package/src/internal/_httpHeaderReadNumber.ts +7 -7
  75. package/src/internal/_httpParameterReadBigint.ts +10 -10
  76. package/src/internal/_httpParameterReadBoolean.ts +8 -8
  77. package/src/internal/_httpParameterReadNumber.ts +7 -7
  78. package/src/internal/_httpParameterReadString.ts +2 -2
  79. package/src/internal/_httpQueryParseURLSearchParams.ts +12 -12
  80. package/src/internal/_httpQueryReadArray.ts +4 -4
  81. package/src/internal/_httpQueryReadBigint.ts +12 -12
  82. package/src/internal/_httpQueryReadBoolean.ts +14 -14
  83. package/src/internal/_httpQueryReadNumber.ts +9 -9
  84. package/src/internal/_httpQueryReadString.ts +4 -4
  85. package/src/internal/_isBetween.ts +2 -2
  86. package/src/internal/_isBigintString.ts +8 -8
  87. package/src/internal/_isFormatByte.ts +7 -7
  88. package/src/internal/_isFormatDate.ts +3 -3
  89. package/src/internal/_isFormatDateTime.ts +4 -4
  90. package/src/internal/_isFormatDuration.ts +4 -4
  91. package/src/internal/_isFormatEmail.ts +4 -4
  92. package/src/internal/_isFormatHostname.ts +4 -4
  93. package/src/internal/_isFormatIdnEmail.ts +4 -4
  94. package/src/internal/_isFormatIdnHostname.ts +4 -4
  95. package/src/internal/_isFormatIpv4.ts +4 -4
  96. package/src/internal/_isFormatIpv6.ts +4 -4
  97. package/src/internal/_isFormatIri.ts +3 -3
  98. package/src/internal/_isFormatIriReference.ts +4 -4
  99. package/src/internal/_isFormatJsonPointer.ts +3 -3
  100. package/src/internal/_isFormatPassword.ts +1 -1
  101. package/src/internal/_isFormatRegex.ts +8 -8
  102. package/src/internal/_isFormatRelativeJsonPointer.ts +4 -4
  103. package/src/internal/_isFormatTime.ts +4 -4
  104. package/src/internal/_isFormatUri.ts +6 -6
  105. package/src/internal/_isFormatUriReference.ts +5 -5
  106. package/src/internal/_isFormatUriTemplate.ts +4 -4
  107. package/src/internal/_isFormatUrl.ts +4 -4
  108. package/src/internal/_isFormatUuid.ts +3 -3
  109. package/src/internal/_isTypeFloat.ts +5 -5
  110. package/src/internal/_isTypeInt32.ts +5 -5
  111. package/src/internal/_isTypeInt64.ts +5 -5
  112. package/src/internal/_isTypeUint32.ts +5 -5
  113. package/src/internal/_isTypeUint64.ts +5 -5
  114. package/src/internal/_isUniqueItems.ts +159 -159
  115. package/src/internal/_jsonStringifyNumber.ts +12 -12
  116. package/src/internal/_jsonStringifyRest.ts +3 -3
  117. package/src/internal/_jsonStringifyString.ts +42 -42
  118. package/src/internal/_jsonStringifyTail.ts +2 -2
  119. package/src/internal/_llmApplicationFinalize.ts +20 -20
  120. package/src/internal/_miscCloneAny.ts +46 -46
  121. package/src/internal/_notationAny.ts +37 -37
  122. package/src/internal/_notationCamel.ts +13 -13
  123. package/src/internal/_notationPascal.ts +8 -8
  124. package/src/internal/_notationSnake.ts +43 -43
  125. package/src/internal/_randomArray.ts +21 -21
  126. package/src/internal/_randomBigint.ts +6 -6
  127. package/src/internal/_randomBoolean.ts +1 -1
  128. package/src/internal/_randomFormatByte.ts +3 -3
  129. package/src/internal/_randomFormatDate.ts +18 -18
  130. package/src/internal/_randomFormatDatetime.ts +16 -16
  131. package/src/internal/_randomFormatDuration.ts +27 -27
  132. package/src/internal/_randomFormatEmail.ts +11 -11
  133. package/src/internal/_randomFormatHostname.ts +6 -6
  134. package/src/internal/_randomFormatIdnEmail.ts +3 -3
  135. package/src/internal/_randomFormatIdnHostname.ts +3 -3
  136. package/src/internal/_randomFormatIpv4.ts +11 -11
  137. package/src/internal/_randomFormatIpv6.ts +11 -11
  138. package/src/internal/_randomFormatIri.ts +3 -3
  139. package/src/internal/_randomFormatIriReference.ts +3 -3
  140. package/src/internal/_randomFormatJsonPointer.ts +7 -7
  141. package/src/internal/_randomFormatPassword.ts +8 -8
  142. package/src/internal/_randomFormatRegex.ts +4 -4
  143. package/src/internal/_randomFormatRelativeJsonPointer.ts +8 -8
  144. package/src/internal/_randomFormatTime.ts +14 -14
  145. package/src/internal/_randomFormatUri.ts +3 -3
  146. package/src/internal/_randomFormatUriReference.ts +3 -3
  147. package/src/internal/_randomFormatUriTemplate.ts +3 -3
  148. package/src/internal/_randomFormatUrl.ts +11 -11
  149. package/src/internal/_randomFormatUuid.ts +6 -6
  150. package/src/internal/_randomInteger.ts +47 -47
  151. package/src/internal/_randomNumber.ts +74 -74
  152. package/src/internal/_randomPattern.ts +10 -10
  153. package/src/internal/_randomPick.ts +9 -9
  154. package/src/internal/_randomString.ts +24 -24
  155. package/src/internal/_throwTypeGuardError.ts +5 -5
  156. package/src/internal/_validateReport.ts +13 -13
  157. package/src/internal/private/__notationCapitalize.ts +2 -2
  158. package/src/internal/private/__notationUnsnake.ts +24 -24
  159. package/src/json.ts +752 -752
  160. package/src/llm.ts +481 -481
  161. package/src/misc.ts +658 -658
  162. package/src/module.ts +937 -937
  163. package/src/notations.ts +827 -827
  164. package/src/programmers/AssertProgrammer.ts +454 -454
  165. package/src/programmers/CheckerProgrammer.ts +1617 -1617
  166. package/src/programmers/FeatureProgrammer.ts +622 -622
  167. package/src/programmers/ImportProgrammer.ts +185 -185
  168. package/src/programmers/IsProgrammer.ts +273 -273
  169. package/src/programmers/RandomProgrammer.ts +1190 -1190
  170. package/src/programmers/TypiaProgrammer.ts +174 -174
  171. package/src/programmers/ValidateProgrammer.ts +439 -439
  172. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +153 -153
  173. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +125 -125
  174. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +115 -115
  175. package/src/programmers/functional/FunctionalIsFunctionProgrammer.ts +72 -72
  176. package/src/programmers/functional/FunctionalIsParametersProgrammer.ts +113 -113
  177. package/src/programmers/functional/FunctionalIsReturnProgrammer.ts +116 -116
  178. package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +119 -119
  179. package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +274 -274
  180. package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +135 -135
  181. package/src/programmers/functional/internal/FunctionalGeneralProgrammer.ts +34 -34
  182. package/src/programmers/helpers/AtomicPredicator.ts +35 -35
  183. package/src/programmers/helpers/CloneJoiner.ts +143 -143
  184. package/src/programmers/helpers/FunctionProgrammer.ts +67 -67
  185. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  186. package/src/programmers/helpers/NotationJoiner.ts +144 -144
  187. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  188. package/src/programmers/helpers/ProtobufUtil.ts +228 -228
  189. package/src/programmers/helpers/PruneJoiner.ts +148 -148
  190. package/src/programmers/helpers/RandomJoiner.ts +168 -168
  191. package/src/programmers/helpers/StringifyJoinder.ts +115 -115
  192. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  193. package/src/programmers/helpers/UnionExplorer.ts +372 -372
  194. package/src/programmers/helpers/UnionPredicator.ts +79 -79
  195. package/src/programmers/helpers/disable_function_programmer_declare.ts +32 -32
  196. package/src/programmers/http/HttpAssertFormDataProgrammer.ts +99 -99
  197. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +99 -99
  198. package/src/programmers/http/HttpAssertQueryProgrammer.ts +105 -105
  199. package/src/programmers/http/HttpFormDataProgrammer.ts +308 -308
  200. package/src/programmers/http/HttpHeadersProgrammer.ts +400 -400
  201. package/src/programmers/http/HttpIsFormDataProgrammer.ts +108 -108
  202. package/src/programmers/http/HttpIsHeadersProgrammer.ts +108 -108
  203. package/src/programmers/http/HttpIsQueryProgrammer.ts +114 -114
  204. package/src/programmers/http/HttpParameterProgrammer.ts +115 -115
  205. package/src/programmers/http/HttpQueryProgrammer.ts +336 -336
  206. package/src/programmers/http/HttpValidateFormDataProgrammer.ts +92 -92
  207. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +92 -92
  208. package/src/programmers/http/HttpValidateQueryProgrammer.ts +98 -98
  209. package/src/programmers/internal/check_array_length.ts +47 -47
  210. package/src/programmers/internal/check_bigint.ts +50 -50
  211. package/src/programmers/internal/check_dynamic_key.ts +201 -201
  212. package/src/programmers/internal/check_dynamic_properties.ts +208 -208
  213. package/src/programmers/internal/check_everything.ts +23 -23
  214. package/src/programmers/internal/check_native.ts +27 -27
  215. package/src/programmers/internal/check_number.ts +112 -112
  216. package/src/programmers/internal/check_object.ts +75 -75
  217. package/src/programmers/internal/check_string.ts +50 -50
  218. package/src/programmers/internal/check_template.ts +48 -48
  219. package/src/programmers/internal/check_union_array_like.ts +335 -335
  220. package/src/programmers/internal/decode_union_object.ts +116 -116
  221. package/src/programmers/internal/feature_object_entries.ts +61 -61
  222. package/src/programmers/internal/json_schema_alias.ts +47 -47
  223. package/src/programmers/internal/json_schema_array.ts +45 -45
  224. package/src/programmers/internal/json_schema_bigint.ts +15 -15
  225. package/src/programmers/internal/json_schema_boolean.ts +15 -15
  226. package/src/programmers/internal/json_schema_constant.ts +26 -26
  227. package/src/programmers/internal/json_schema_description.ts +12 -12
  228. package/src/programmers/internal/json_schema_discriminator.ts +35 -35
  229. package/src/programmers/internal/json_schema_escaped.ts +82 -82
  230. package/src/programmers/internal/json_schema_native.ts +33 -33
  231. package/src/programmers/internal/json_schema_number.ts +15 -15
  232. package/src/programmers/internal/json_schema_object.ts +158 -158
  233. package/src/programmers/internal/json_schema_plugin.ts +18 -18
  234. package/src/programmers/internal/json_schema_station.ts +182 -182
  235. package/src/programmers/internal/json_schema_string.ts +15 -15
  236. package/src/programmers/internal/json_schema_template.ts +55 -55
  237. package/src/programmers/internal/json_schema_title.ts +20 -20
  238. package/src/programmers/internal/json_schema_tuple.ts +35 -35
  239. package/src/programmers/internal/metadata_to_pattern.ts +42 -42
  240. package/src/programmers/internal/postfix_of_tuple.ts +5 -5
  241. package/src/programmers/internal/prune_object_properties.ts +71 -71
  242. package/src/programmers/internal/stringify_dynamic_properties.ts +162 -162
  243. package/src/programmers/internal/stringify_regular_properties.ts +81 -81
  244. package/src/programmers/internal/template_to_pattern.ts +23 -23
  245. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +23 -23
  246. package/src/programmers/json/JsonApplicationProgrammer.ts +279 -279
  247. package/src/programmers/json/JsonAssertParseProgrammer.ts +113 -113
  248. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +115 -115
  249. package/src/programmers/json/JsonIsParseProgrammer.ts +114 -114
  250. package/src/programmers/json/JsonIsStringifyProgrammer.ts +108 -108
  251. package/src/programmers/json/JsonSchemasProgrammer.ts +91 -91
  252. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  253. package/src/programmers/json/JsonValidateParseProgrammer.ts +105 -105
  254. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +124 -124
  255. package/src/programmers/llm/LlmApplicationOfValidateProgrammer.ts +81 -81
  256. package/src/programmers/llm/LlmApplicationProgrammer.ts +277 -278
  257. package/src/programmers/llm/LlmModelPredicator.ts +127 -127
  258. package/src/programmers/llm/LlmParametersProgrammer.ts +90 -90
  259. package/src/programmers/llm/LlmSchemaProgrammer.ts +143 -143
  260. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +95 -95
  261. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +116 -116
  262. package/src/programmers/misc/MiscCloneProgrammer.ts +1032 -1032
  263. package/src/programmers/misc/MiscIsCloneProgrammer.ts +99 -99
  264. package/src/programmers/misc/MiscIsPruneProgrammer.ts +97 -97
  265. package/src/programmers/misc/MiscLiteralsProgrammer.ts +80 -80
  266. package/src/programmers/misc/MiscPruneProgrammer.ts +728 -728
  267. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +111 -111
  268. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +113 -113
  269. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +101 -101
  270. package/src/programmers/notations/NotationGeneralProgrammer.ts +984 -984
  271. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +105 -105
  272. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +119 -119
  273. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +98 -98
  274. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +102 -102
  275. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +654 -654
  276. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +945 -945
  277. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +109 -109
  278. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +98 -98
  279. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +179 -179
  280. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +92 -92
  281. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +119 -119
  282. package/src/protobuf.ts +868 -868
  283. package/src/reflect.ts +57 -57
  284. package/src/schemas/json/IJsonApplication.ts +73 -73
  285. package/src/schemas/json/IJsonSchemaCollection.ts +29 -29
  286. package/src/schemas/json/__IJsonApplication.ts +63 -63
  287. package/src/schemas/llm/ILlmApplicationOfValidate.ts +55 -55
  288. package/src/schemas/llm/ILlmFunctionOfValidate.ts +39 -39
  289. package/src/schemas/metadata/IMetadata.ts +35 -35
  290. package/src/schemas/metadata/IMetadataAlias.ts +6 -6
  291. package/src/schemas/metadata/IMetadataAliasType.ts +12 -12
  292. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  293. package/src/schemas/metadata/IMetadataArray.ts +6 -6
  294. package/src/schemas/metadata/IMetadataComponents.ts +11 -11
  295. package/src/schemas/metadata/IMetadataConstantValue.ts +11 -11
  296. package/src/schemas/metadata/IMetadataDictionary.ts +11 -11
  297. package/src/schemas/metadata/IMetadataMap.ts +8 -8
  298. package/src/schemas/metadata/IMetadataNative.ts +6 -6
  299. package/src/schemas/metadata/IMetadataObject.ts +6 -6
  300. package/src/schemas/metadata/IMetadataObjectType.ts +13 -13
  301. package/src/schemas/metadata/IMetadataSet.ts +7 -7
  302. package/src/schemas/metadata/IMetadataTemplate.ts +7 -7
  303. package/src/schemas/metadata/IMetadataTuple.ts +6 -6
  304. package/src/schemas/metadata/IMetadataTypeTag.ts +16 -16
  305. package/src/schemas/metadata/Metadata.ts +669 -669
  306. package/src/schemas/metadata/MetadataAlias.ts +46 -46
  307. package/src/schemas/metadata/MetadataAliasType.ts +63 -63
  308. package/src/schemas/metadata/MetadataApplication.ts +44 -44
  309. package/src/schemas/metadata/MetadataArray.ts +49 -49
  310. package/src/schemas/metadata/MetadataAtomic.ts +87 -87
  311. package/src/schemas/metadata/MetadataComponents.ts +98 -98
  312. package/src/schemas/metadata/MetadataConstantValue.ts +62 -62
  313. package/src/schemas/metadata/MetadataMap.ts +48 -48
  314. package/src/schemas/metadata/MetadataNative.ts +44 -44
  315. package/src/schemas/metadata/MetadataObject.ts +48 -48
  316. package/src/schemas/metadata/MetadataObjectType.ts +149 -149
  317. package/src/schemas/metadata/MetadataParameter.ts +54 -54
  318. package/src/schemas/metadata/MetadataProperty.ts +59 -59
  319. package/src/schemas/metadata/MetadataSet.ts +45 -45
  320. package/src/schemas/metadata/MetadataTemplate.ts +80 -80
  321. package/src/schemas/metadata/MetadataTuple.ts +32 -32
  322. package/src/schemas/protobuf/IProtobufProperty.ts +6 -6
  323. package/src/schemas/protobuf/IProtobufPropertyType.ts +37 -37
  324. package/src/schemas/protobuf/IProtobufSchema.ts +50 -50
  325. package/src/tags/Example.ts +24 -24
  326. package/src/tags/Examples.ts +16 -16
  327. package/src/tags/Format.ts +50 -50
  328. package/src/tags/JsonSchemaPlugin.ts +8 -8
  329. package/src/tags/Sequence.ts +10 -10
  330. package/src/tags/TagBase.ts +82 -82
  331. package/src/tags/Type.ts +32 -32
  332. package/src/tags/UniqueItems.ts +14 -14
  333. package/src/tags/index.ts +21 -21
  334. package/src/transform.ts +35 -35
  335. package/src/transformers/CallExpressionTransformer.ts +547 -547
  336. package/src/transformers/FileTransformer.ts +136 -136
  337. package/src/transformers/IProgrammerProps.ts +11 -11
  338. package/src/transformers/ITransformOptions.ts +62 -62
  339. package/src/transformers/ITransformProps.ts +9 -9
  340. package/src/transformers/ITypiaContext.ts +18 -18
  341. package/src/transformers/ImportTransformer.ts +81 -81
  342. package/src/transformers/NodeTransformer.ts +17 -17
  343. package/src/transformers/TransformerError.ts +60 -60
  344. package/src/transformers/features/AssertTransformer.ts +24 -24
  345. package/src/transformers/features/CreateAssertTransformer.ts +24 -24
  346. package/src/transformers/features/CreateIsTransformer.ts +18 -18
  347. package/src/transformers/features/CreateRandomTransformer.ts +43 -43
  348. package/src/transformers/features/CreateValidateTransformer.ts +18 -18
  349. package/src/transformers/features/IsTransformer.ts +18 -18
  350. package/src/transformers/features/RandomTransformer.ts +41 -41
  351. package/src/transformers/features/ValidateTransformer.ts +18 -18
  352. package/src/transformers/features/functional/FunctionalGenericTransformer.ts +57 -57
  353. package/src/transformers/features/http/CreateHttpAssertFormDataTransformer.ts +13 -13
  354. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +13 -13
  355. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +13 -13
  356. package/src/transformers/features/http/CreateHttpFormDataTransformer.ts +13 -13
  357. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +13 -13
  358. package/src/transformers/features/http/CreateHttpIsFormDataTransformer.ts +13 -13
  359. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +13 -13
  360. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +13 -13
  361. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +13 -13
  362. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +13 -13
  363. package/src/transformers/features/http/CreateHttpValidateFormDataTransformer.ts +13 -13
  364. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +13 -13
  365. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +13 -13
  366. package/src/transformers/features/http/HttpAssertFormDataTransformer.ts +13 -13
  367. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +13 -13
  368. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +13 -13
  369. package/src/transformers/features/http/HttpFormDataTransformer.ts +13 -13
  370. package/src/transformers/features/http/HttpHeadersTransformer.ts +13 -13
  371. package/src/transformers/features/http/HttpIsFormDataTransformer.ts +13 -13
  372. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +13 -13
  373. package/src/transformers/features/http/HttpIsQueryTransformer.ts +13 -13
  374. package/src/transformers/features/http/HttpParameterTransformer.ts +13 -13
  375. package/src/transformers/features/http/HttpQueryTransformer.ts +13 -13
  376. package/src/transformers/features/http/HttpValidateFormDataTransformer.ts +13 -13
  377. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +13 -13
  378. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +13 -13
  379. package/src/transformers/features/json/JsonApplicationTransformer.ts +105 -105
  380. package/src/transformers/features/json/JsonAssertParseTransformer.ts +13 -13
  381. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +13 -13
  382. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +13 -13
  383. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +13 -13
  384. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +13 -13
  385. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +13 -13
  386. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +13 -13
  387. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +13 -13
  388. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +13 -13
  389. package/src/transformers/features/json/JsonIsParseTransformer.ts +13 -13
  390. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +13 -13
  391. package/src/transformers/features/json/JsonSchemasTransformer.ts +143 -143
  392. package/src/transformers/features/json/JsonStringifyTransformer.ts +13 -13
  393. package/src/transformers/features/json/JsonValidateParseTransformer.ts +13 -13
  394. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +13 -13
  395. package/src/transformers/features/llm/LlmApplicationOfValidateTransformer.ts +115 -115
  396. package/src/transformers/features/llm/LlmApplicationTransformer.ts +113 -113
  397. package/src/transformers/features/llm/LlmParametersTransformer.ts +89 -89
  398. package/src/transformers/features/llm/LlmSchemaTransformer.ts +130 -130
  399. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +13 -13
  400. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +13 -13
  401. package/src/transformers/features/misc/MiscCloneTransformer.ts +13 -13
  402. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +13 -13
  403. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +13 -13
  404. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +13 -13
  405. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +13 -13
  406. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +13 -13
  407. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +13 -13
  408. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +13 -13
  409. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +13 -13
  410. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +13 -13
  411. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +13 -13
  412. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +35 -35
  413. package/src/transformers/features/misc/MiscPruneTransformer.ts +13 -13
  414. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +13 -13
  415. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +13 -13
  416. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +20 -20
  417. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +20 -20
  418. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +20 -20
  419. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +20 -20
  420. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +20 -20
  421. package/src/transformers/features/notations/NotationGeneralTransformer.ts +18 -18
  422. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +20 -20
  423. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +20 -20
  424. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +13 -13
  425. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +13 -13
  426. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +13 -13
  427. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +13 -13
  428. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +13 -13
  429. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +13 -13
  430. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +13 -13
  431. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +13 -13
  432. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +13 -13
  433. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +13 -13
  434. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +13 -13
  435. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +13 -13
  436. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +13 -13
  437. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +13 -13
  438. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +35 -35
  439. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +13 -13
  440. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +13 -13
  441. package/src/transformers/features/reflect/ReflectMetadataTransformer.ts +69 -69
  442. package/src/transformers/features/reflect/ReflectNameTransformer.ts +82 -82
  443. package/src/transformers/internal/GenericTransformer.ts +101 -101
  444. package/src/utils/MapUtil.ts +14 -14
  445. package/src/utils/NamingConvention.ts +94 -94
  446. package/src/utils/ProtobufNameEncoder.ts +32 -32
  447. package/src/utils/StringUtil.ts +16 -16
@@ -1,945 +1,945 @@
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 { NumericRangeFactory } from "../../factories/NumericRangeFactory";
7
- import { ProtobufFactory } from "../../factories/ProtobufFactory";
8
- import { StatementFactory } from "../../factories/StatementFactory";
9
- import { TypeFactory } from "../../factories/TypeFactory";
10
-
11
- import { Metadata } from "../../schemas/metadata/Metadata";
12
- import { MetadataMap } from "../../schemas/metadata/MetadataMap";
13
- import { MetadataObjectType } from "../../schemas/metadata/MetadataObjectType";
14
- import { IProtobufProperty } from "../../schemas/protobuf/IProtobufProperty";
15
- import { IProtobufPropertyType } from "../../schemas/protobuf/IProtobufPropertyType";
16
- import { IProtobufSchema } from "../../schemas/protobuf/IProtobufSchema";
17
-
18
- import { IProgrammerProps } from "../../transformers/IProgrammerProps";
19
- import { ITypiaContext } from "../../transformers/ITypiaContext";
20
-
21
- import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
22
-
23
- import { FeatureProgrammer } from "../FeatureProgrammer";
24
- import { IsProgrammer } from "../IsProgrammer";
25
- import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
26
- import { ProtobufUtil } from "../helpers/ProtobufUtil";
27
- import { ProtobufWire } from "../helpers/ProtobufWire";
28
- import { UnionPredicator } from "../helpers/UnionPredicator";
29
- import { decode_union_object } from "../internal/decode_union_object";
30
-
31
- export namespace ProtobufEncodeProgrammer {
32
- export const decompose = (props: {
33
- context: ITypiaContext;
34
- modulo: ts.LeftHandSideExpression;
35
- functor: FunctionProgrammer;
36
- type: ts.Type;
37
- name: string | undefined;
38
- }): FeatureProgrammer.IDecomposed => {
39
- const collection: MetadataCollection = new MetadataCollection();
40
- const metadata: Metadata = ProtobufFactory.metadata({
41
- method: props.modulo.getText(),
42
- checker: props.context.checker,
43
- transformer: props.context.transformer,
44
- collection,
45
- type: props.type,
46
- });
47
-
48
- const callEncoder = (writer: string, factory: ts.NewExpression) =>
49
- StatementFactory.constant({
50
- name: writer,
51
- value: ts.factory.createCallExpression(
52
- ts.factory.createIdentifier("encoder"),
53
- undefined,
54
- [factory, ts.factory.createIdentifier("input")],
55
- ),
56
- });
57
- return {
58
- functions: {
59
- encoder: StatementFactory.constant({
60
- name: props.functor.useLocal("encoder"),
61
- value: write_encoder({
62
- context: props.context,
63
- functor: props.functor,
64
- collection,
65
- metadata,
66
- }),
67
- }),
68
- },
69
- statements: [],
70
- arrow: ts.factory.createArrowFunction(
71
- undefined,
72
- undefined,
73
- [
74
- IdentifierFactory.parameter(
75
- "input",
76
- ts.factory.createTypeReferenceNode(
77
- props.name ??
78
- TypeFactory.getFullName({
79
- checker: props.context.checker,
80
- type: props.type,
81
- }),
82
- ),
83
- ),
84
- ],
85
- ts.factory.createTypeReferenceNode("Uint8Array"),
86
- undefined,
87
- ts.factory.createBlock(
88
- [
89
- callEncoder(
90
- "sizer",
91
- ts.factory.createNewExpression(
92
- props.context.importer.internal("ProtobufSizer"),
93
- undefined,
94
- [],
95
- ),
96
- ),
97
- callEncoder(
98
- "writer",
99
- ts.factory.createNewExpression(
100
- props.context.importer.internal("ProtobufWriter"),
101
- undefined,
102
- [ts.factory.createIdentifier("sizer")],
103
- ),
104
- ),
105
- ts.factory.createReturnStatement(callWriter("buffer")),
106
- ],
107
- true,
108
- ),
109
- ),
110
- };
111
- };
112
-
113
- export const write = (props: IProgrammerProps): ts.CallExpression => {
114
- const functor: FunctionProgrammer = new FunctionProgrammer(
115
- props.modulo.getText(),
116
- );
117
- const result: FeatureProgrammer.IDecomposed = decompose({
118
- ...props,
119
- functor,
120
- });
121
- return FeatureProgrammer.writeDecomposed({
122
- modulo: props.modulo,
123
- functor,
124
- result,
125
- });
126
- };
127
-
128
- const write_encoder = (props: {
129
- context: ITypiaContext;
130
- functor: FunctionProgrammer;
131
- collection: MetadataCollection;
132
- metadata: Metadata;
133
- }): ts.ArrowFunction => {
134
- const functors = props.collection
135
- .objects()
136
- .filter((obj) => ProtobufUtil.isStaticObject(obj))
137
- .map((object) =>
138
- StatementFactory.constant({
139
- name: `${PREFIX}o${object.index}`,
140
- value: write_object_function({
141
- context: props.context,
142
- functor: props.functor,
143
- input: ts.factory.createIdentifier("input"),
144
- object,
145
- explore: {
146
- source: "function",
147
- from: "object",
148
- tracable: false,
149
- postfix: "",
150
- },
151
- }),
152
- }),
153
- );
154
- return ts.factory.createArrowFunction(
155
- undefined,
156
- [
157
- ts.factory.createTypeParameterDeclaration(
158
- undefined,
159
- "Writer",
160
- props.context.importer.type({
161
- file: "typia/lib/internal/_IProtobufWriter.js",
162
- name: "_IProtobufWriter",
163
- }),
164
- ),
165
- ],
166
- [
167
- IdentifierFactory.parameter(
168
- "writer",
169
- ts.factory.createTypeReferenceNode("Writer"),
170
- ),
171
- IdentifierFactory.parameter("input"),
172
- ],
173
- ts.factory.createTypeReferenceNode("Writer"),
174
- undefined,
175
- ts.factory.createBlock(
176
- [
177
- ...props.functor.declareUnions(),
178
- ...functors,
179
- ...IsProgrammer.write_function_statements(props),
180
- ts.factory.createExpressionStatement(
181
- ts.factory.createCallExpression(
182
- ts.factory.createIdentifier(
183
- props.functor.useLocal(
184
- `${PREFIX}o${props.metadata.objects[0]?.type.index ?? 0}`,
185
- ),
186
- ),
187
- [],
188
- [ts.factory.createIdentifier("input")],
189
- ),
190
- ),
191
- ts.factory.createReturnStatement(
192
- ts.factory.createIdentifier("writer"),
193
- ),
194
- ],
195
- true,
196
- ),
197
- );
198
- };
199
-
200
- const write_object_function = (props: {
201
- context: ITypiaContext;
202
- functor: FunctionProgrammer;
203
- input: ts.Expression;
204
- object: MetadataObjectType;
205
- explore: FeatureProgrammer.IExplore;
206
- }): ts.ArrowFunction => {
207
- const body: ts.Statement[] = props.object.properties
208
- .map((p) => {
209
- const block = decode_property({
210
- context: props.context,
211
- functor: props.functor,
212
- explore: props.explore,
213
- metadata: p.value,
214
- protobuf: p.of_protobuf_!,
215
- input: IdentifierFactory.access(props.input, p.key.getSoleLiteral()!),
216
- });
217
- return [
218
- ts.factory.createExpressionStatement(
219
- ts.factory.createIdentifier(
220
- `// property ${JSON.stringify(p.key.getSoleLiteral())}: ${p.value.getName()}`,
221
- ),
222
- ),
223
- ...block.statements,
224
- ];
225
- })
226
- .flat();
227
- return ts.factory.createArrowFunction(
228
- undefined,
229
- undefined,
230
- [IdentifierFactory.parameter("input")],
231
- TypeFactory.keyword("any"),
232
- undefined,
233
- ts.factory.createBlock(body, true),
234
- );
235
- };
236
-
237
- /* -----------------------------------------------------------
238
- DECODER STATION
239
- ----------------------------------------------------------- */
240
- const decode_property = (props: {
241
- context: ITypiaContext;
242
- functor: FunctionProgrammer;
243
- metadata: Metadata;
244
- protobuf: IProtobufProperty;
245
- input: ts.Expression;
246
- explore: FeatureProgrammer.IExplore;
247
- }): ts.Block => {
248
- const union: IUnion[] = [];
249
- for (const schema of props.protobuf.union) {
250
- //----
251
- // ATOMICS
252
- //----
253
- if (schema.type === "bool")
254
- union.push({
255
- is: () =>
256
- ts.factory.createStrictEquality(
257
- ts.factory.createStringLiteral("boolean"),
258
- ts.factory.createTypeOfExpression(props.input),
259
- ),
260
- value: () =>
261
- decode_bool({
262
- input: props.input,
263
- index: schema.index,
264
- }),
265
- });
266
- else if (schema.type === "bigint")
267
- union.push(
268
- decode_bigint({
269
- input: props.input,
270
- type: schema.name,
271
- candidates: props.protobuf.union
272
- .filter((s) => s.type === "bigint")
273
- .map((s) => s.name),
274
- index: schema.index,
275
- }),
276
- );
277
- else if (schema.type === "number")
278
- union.push(
279
- decode_number({
280
- input: props.input,
281
- type: schema.name,
282
- candidates: props.protobuf.union
283
- .filter((s) => s.type === "number")
284
- .map((s) => s.name),
285
- index: schema.index,
286
- }),
287
- );
288
- else if (schema.type === "string")
289
- union.push({
290
- is: () =>
291
- ts.factory.createStrictEquality(
292
- ts.factory.createStringLiteral("string"),
293
- ts.factory.createTypeOfExpression(props.input),
294
- ),
295
- value: () =>
296
- decode_bytes({
297
- method: "string",
298
- index: schema.index,
299
- input: props.input,
300
- }),
301
- });
302
- //----
303
- // INSTANCES
304
- //----
305
- else if (schema.type === "bytes")
306
- union.push({
307
- is: () => ExpressionFactory.isInstanceOf("Uint8Array", props.input),
308
- value: () =>
309
- decode_bytes({
310
- method: "bytes",
311
- index: schema.index,
312
- input: props.input,
313
- }),
314
- });
315
- else if (schema.type === "array")
316
- union.push({
317
- is: () => ExpressionFactory.isArray(props.input),
318
- value: () =>
319
- decode_array({
320
- context: props.context,
321
- functor: props.functor,
322
- input: props.input,
323
- schema,
324
- }),
325
- });
326
- else if (schema.type === "map" && schema.map instanceof MetadataMap) {
327
- union.push({
328
- is: () => ExpressionFactory.isInstanceOf("Map", props.input),
329
- value: () =>
330
- decode_map({
331
- context: props.context,
332
- functor: props.functor,
333
- schema,
334
- input: props.input,
335
- }),
336
- });
337
- }
338
- const objectSchemas: Array<
339
- IProtobufPropertyType.IObject | IProtobufPropertyType.IMap
340
- > = props.protobuf.union
341
- .filter((schema) => schema.type === "object" || schema.type === "map")
342
- .filter(
343
- (schema) =>
344
- schema.type === "object" ||
345
- (schema.type === "map" && schema.map instanceof MetadataObjectType),
346
- );
347
- if (objectSchemas.length !== 0)
348
- union.push({
349
- is: () =>
350
- ExpressionFactory.isObject({
351
- checkNull: true,
352
- checkArray: false,
353
- input: props.input,
354
- }),
355
- value: () =>
356
- explore_objects({
357
- context: props.context,
358
- functor: props.functor,
359
- level: 0,
360
- schemas: objectSchemas,
361
- explore: {
362
- ...props.explore,
363
- from: "object",
364
- },
365
- input: props.input,
366
- }),
367
- });
368
- }
369
-
370
- // RETURNS
371
- const wrapper: (block: ts.Block) => ts.Block =
372
- props.metadata.isRequired() && props.metadata.nullable === false
373
- ? (block) => block
374
- : props.metadata.isRequired() === false &&
375
- props.metadata.nullable === true
376
- ? (block) =>
377
- ts.factory.createBlock(
378
- [
379
- ts.factory.createIfStatement(
380
- ts.factory.createLogicalAnd(
381
- ts.factory.createStrictInequality(
382
- ts.factory.createIdentifier("undefined"),
383
- props.input,
384
- ),
385
- ts.factory.createStrictInequality(
386
- ts.factory.createNull(),
387
- props.input,
388
- ),
389
- ),
390
- block,
391
- ),
392
- ],
393
- true,
394
- )
395
- : props.metadata.isRequired() === false
396
- ? (block) =>
397
- ts.factory.createBlock(
398
- [
399
- ts.factory.createIfStatement(
400
- ts.factory.createStrictInequality(
401
- ts.factory.createIdentifier("undefined"),
402
- props.input,
403
- ),
404
- block,
405
- ),
406
- ],
407
- true,
408
- )
409
- : (block) =>
410
- ts.factory.createBlock(
411
- [
412
- ts.factory.createIfStatement(
413
- ts.factory.createStrictInequality(
414
- ts.factory.createNull(),
415
- props.input,
416
- ),
417
- block,
418
- ),
419
- ],
420
- true,
421
- );
422
- if (union.length === 1) return wrapper(union[0]!.value());
423
- return wrapper(
424
- ts.factory.createBlock(
425
- [
426
- union
427
- .map((u, i) =>
428
- ts.factory.createIfStatement(
429
- u.is(),
430
- u.value(),
431
- i === union.length - 1
432
- ? create_throw_error({
433
- context: props.context,
434
- functor: props.functor,
435
- input: props.input,
436
- expected: props.metadata.getName(),
437
- })
438
- : undefined,
439
- ),
440
- )
441
- .reverse()
442
- .reduce((a, b) =>
443
- ts.factory.createIfStatement(b.expression, b.thenStatement, a),
444
- ),
445
- ],
446
- true,
447
- ),
448
- );
449
- };
450
-
451
- /* -----------------------------------------------------------
452
- ATOMIC DECODERS
453
- ----------------------------------------------------------- */
454
- const decode_bool = (props: {
455
- input: ts.Expression;
456
- index: number | null;
457
- }): ts.Block =>
458
- ts.factory.createBlock(
459
- [
460
- ...(props.index !== null
461
- ? [
462
- decode_tag({
463
- wire: ProtobufWire.VARIANT,
464
- index: props.index,
465
- }),
466
- ]
467
- : []),
468
- callWriter("bool", [props.input]),
469
- ].map((exp) => ts.factory.createExpressionStatement(exp)),
470
- true,
471
- );
472
-
473
- const decode_bigint = (props: {
474
- candidates: ProtobufAtomic.BigNumeric[];
475
- type: ProtobufAtomic.BigNumeric;
476
- input: ts.Expression;
477
- index: number | null;
478
- }): IUnion => ({
479
- is: () =>
480
- props.candidates.length === 1
481
- ? ts.factory.createStrictEquality(
482
- ts.factory.createStringLiteral("bigint"),
483
- ts.factory.createTypeOfExpression(props.input),
484
- )
485
- : ts.factory.createLogicalAnd(
486
- ts.factory.createStrictEquality(
487
- ts.factory.createStringLiteral("bigint"),
488
- ts.factory.createTypeOfExpression(props.input),
489
- ),
490
- NumericRangeFactory.bigint(props.type, props.input),
491
- ),
492
- value: () =>
493
- ts.factory.createBlock(
494
- [
495
- ...(props.index !== null
496
- ? [
497
- decode_tag({
498
- wire: ProtobufWire.VARIANT,
499
- index: props.index,
500
- }),
501
- ]
502
- : []),
503
- callWriter(props.type, [props.input]),
504
- ].map((exp) => ts.factory.createExpressionStatement(exp)),
505
- true,
506
- ),
507
- });
508
-
509
- const decode_number = (props: {
510
- candidates: ProtobufAtomic.Numeric[];
511
- type: ProtobufAtomic.Numeric;
512
- input: ts.Expression;
513
- index: number | null;
514
- }): IUnion => ({
515
- is: () =>
516
- props.candidates.length === 1
517
- ? ts.factory.createStrictEquality(
518
- ts.factory.createStringLiteral("number"),
519
- ts.factory.createTypeOfExpression(props.input),
520
- )
521
- : ts.factory.createLogicalAnd(
522
- ts.factory.createStrictEquality(
523
- ts.factory.createStringLiteral("number"),
524
- ts.factory.createTypeOfExpression(props.input),
525
- ),
526
- NumericRangeFactory.number(props.type, props.input),
527
- ),
528
- value: () =>
529
- ts.factory.createBlock(
530
- [
531
- ...(props.index !== null
532
- ? [
533
- decode_tag({
534
- wire: get_numeric_wire(props.type),
535
- index: props.index,
536
- }),
537
- ]
538
- : []),
539
- callWriter(props.type, [props.input]),
540
- ].map((exp) => ts.factory.createExpressionStatement(exp)),
541
- true,
542
- ),
543
- });
544
-
545
- const decode_container_value = (props: {
546
- context: ITypiaContext;
547
- functor: FunctionProgrammer;
548
- schema: IProtobufPropertyType.IArray["value"];
549
- index: number;
550
- kind: "array" | "map";
551
- input: ts.Expression;
552
- }): ts.Block => {
553
- if (props.schema.type === "bool")
554
- return decode_bool({
555
- input: props.input,
556
- index: props.kind === "array" ? null : props.index,
557
- });
558
- else if (props.schema.type === "bigint")
559
- return decode_bigint({
560
- input: props.input,
561
- type: props.schema.name,
562
- candidates: [props.schema.name],
563
- index: props.kind === "array" ? null : props.index,
564
- }).value();
565
- else if (props.schema.type === "number")
566
- return decode_number({
567
- input: props.input,
568
- type: props.schema.name,
569
- candidates: [props.schema.name],
570
- index: props.kind === "array" ? null : props.index,
571
- }).value();
572
- else if (props.schema.type === "string" || props.schema.type === "bytes")
573
- return decode_bytes({
574
- method: props.schema.type,
575
- input: props.input,
576
- index: props.index,
577
- });
578
- return decode_object({
579
- context: props.context,
580
- functor: props.functor,
581
- schema: props.schema,
582
- input: props.input,
583
- index: props.index,
584
- });
585
- };
586
-
587
- /* -----------------------------------------------------------
588
- INSTANCE DECODERS
589
- ----------------------------------------------------------- */
590
- const decode_bytes = (props: {
591
- method: "bytes" | "string";
592
- index: number;
593
- input: ts.Expression;
594
- }): ts.Block =>
595
- ts.factory.createBlock(
596
- [
597
- decode_tag({
598
- wire: ProtobufWire.LEN,
599
- index: props.index,
600
- }),
601
- callWriter(props.method, [props.input]),
602
- ].map((expr) => ts.factory.createExpressionStatement(expr)),
603
- true,
604
- );
605
-
606
- const decode_array = (props: {
607
- context: ITypiaContext;
608
- functor: FunctionProgrammer;
609
- schema: IProtobufPropertyType.IArray;
610
- input: ts.Expression;
611
- }): ts.Block => {
612
- const value: IProtobufPropertyType.IArray["value"] = props.schema.value;
613
- const wire: ProtobufWire = (() => {
614
- if (
615
- value.type === "object" ||
616
- value.type === "bytes" ||
617
- value.type === "string"
618
- )
619
- return ProtobufWire.LEN;
620
- else if (value.type === "number" && value.name === "float")
621
- return ProtobufWire.I32;
622
- return ProtobufWire.VARIANT;
623
- })();
624
- const forLoop = () =>
625
- ts.factory.createForOfStatement(
626
- undefined,
627
- ts.factory.createVariableDeclarationList(
628
- [ts.factory.createVariableDeclaration("elem")],
629
- ts.NodeFlags.Const,
630
- ),
631
- props.input,
632
- decode_container_value({
633
- kind: "array",
634
- context: props.context,
635
- functor: props.functor,
636
- input: ts.factory.createIdentifier("elem"),
637
- index: props.schema.index,
638
- schema: props.schema.value,
639
- }),
640
- );
641
- const length = (block: ts.Block) =>
642
- ts.factory.createBlock(
643
- [
644
- ts.factory.createIfStatement(
645
- ts.factory.createStrictInequality(
646
- ExpressionFactory.number(0),
647
- IdentifierFactory.access(props.input, "length"),
648
- ),
649
- block,
650
- ),
651
- ],
652
- true,
653
- );
654
- if (wire === ProtobufWire.LEN)
655
- return length(ts.factory.createBlock([forLoop()], true));
656
- return length(
657
- ts.factory.createBlock(
658
- [
659
- ts.factory.createExpressionStatement(
660
- decode_tag({
661
- wire: ProtobufWire.LEN,
662
- index: props.schema.index,
663
- }),
664
- ),
665
- ts.factory.createExpressionStatement(callWriter("fork")),
666
- forLoop(),
667
- ts.factory.createExpressionStatement(callWriter("ldelim")),
668
- ],
669
- true,
670
- ),
671
- );
672
- };
673
-
674
- const decode_object = (props: {
675
- context: ITypiaContext;
676
- functor: FunctionProgrammer;
677
- schema: IProtobufSchema.IObject;
678
- index: number;
679
- input: ts.Expression;
680
- }): ts.Block =>
681
- ts.factory.createBlock(
682
- [
683
- decode_tag({
684
- wire: ProtobufWire.LEN,
685
- index: props.index,
686
- }),
687
- callWriter("fork"),
688
- ts.factory.createCallExpression(
689
- ts.factory.createIdentifier(
690
- props.functor.useLocal(`${PREFIX}o${props.schema.object.index}`),
691
- ),
692
- [],
693
- [props.input],
694
- ),
695
- callWriter("ldelim"),
696
- ].map(ts.factory.createExpressionStatement),
697
- true,
698
- );
699
-
700
- const decode_map = (props: {
701
- context: ITypiaContext;
702
- functor: FunctionProgrammer;
703
- schema: IProtobufPropertyType.IMap;
704
- input: ts.Expression;
705
- }): ts.Block => {
706
- const each: ts.Statement[] = [
707
- ts.factory.createExpressionStatement(
708
- decode_tag({
709
- wire: ProtobufWire.LEN,
710
- index: props.schema.index,
711
- }),
712
- ),
713
- ts.factory.createExpressionStatement(callWriter("fork")),
714
- ...decode_container_value({
715
- kind: "map",
716
- context: props.context,
717
- functor: props.functor,
718
- index: 1,
719
- input: ts.factory.createIdentifier("key"),
720
- schema: props.schema.key,
721
- }).statements,
722
- ...decode_container_value({
723
- kind: "map",
724
- context: props.context,
725
- functor: props.functor,
726
- index: 2,
727
- input: ts.factory.createIdentifier("value"),
728
- schema: props.schema.value,
729
- }).statements,
730
- ts.factory.createExpressionStatement(callWriter("ldelim")),
731
- ];
732
- return ts.factory.createBlock(
733
- [
734
- ts.factory.createForOfStatement(
735
- undefined,
736
- StatementFactory.entry({
737
- key: "key",
738
- value: "value",
739
- }),
740
- props.input,
741
- ts.factory.createBlock(each),
742
- ),
743
- ],
744
- true,
745
- );
746
- };
747
-
748
- const explore_objects = (props: {
749
- context: ITypiaContext;
750
- functor: FunctionProgrammer;
751
- level: number;
752
- input: ts.Expression;
753
- schemas: Array<IProtobufPropertyType.IObject | IProtobufPropertyType.IMap>;
754
- explore: FeatureProgrammer.IExplore;
755
- }): ts.Block => {
756
- const out = (
757
- schema: IProtobufPropertyType.IObject | IProtobufPropertyType.IMap,
758
- ) =>
759
- schema.type === "object"
760
- ? decode_object({
761
- context: props.context,
762
- functor: props.functor,
763
- schema,
764
- index: schema.index,
765
- input: props.input,
766
- })
767
- : decode_map({
768
- context: props.context,
769
- functor: props.functor,
770
- schema,
771
- input: ts.factory.createCallExpression(
772
- IdentifierFactory.access(
773
- ts.factory.createIdentifier("Object"),
774
- "entries",
775
- ),
776
- undefined,
777
- [props.input],
778
- ),
779
- });
780
- if (props.schemas.length === 1) return out(props.schemas[0]!);
781
-
782
- const objects: MetadataObjectType[] = props.schemas.map((s) =>
783
- s.type === "map" ? (s.map as MetadataObjectType) : s.object,
784
- );
785
- const expected: string = `(${objects.map((t) => t.name).join(" | ")})`;
786
- const indexes: WeakMap<
787
- MetadataObjectType,
788
- IProtobufPropertyType.IObject | IProtobufPropertyType.IMap
789
- > = new WeakMap(objects.map((o, i) => [o, props.schemas[i]!]));
790
- const specifications: UnionPredicator.ISpecialized[] =
791
- UnionPredicator.object(objects);
792
-
793
- if (specifications.length === 0) {
794
- const condition: ts.Expression = decode_union_object({
795
- checker: (v) =>
796
- IsProgrammer.decode_object({
797
- context: props.context,
798
- functor: props.functor,
799
- object: v.object,
800
- input: v.input,
801
- explore: v.explore,
802
- }),
803
- decoder: (v) => ExpressionFactory.selfCall(out(indexes.get(v.object)!)),
804
- success: (expr) => expr,
805
- escaper: (v) =>
806
- create_throw_error({
807
- context: props.context,
808
- functor: props.functor,
809
- expected: v.expected,
810
- input: v.input,
811
- }),
812
- input: props.input,
813
- explore: props.explore,
814
- objects,
815
- });
816
- return StatementFactory.block(condition);
817
- }
818
- const remained: MetadataObjectType[] = objects.filter(
819
- (o) => specifications.find((s) => s.object === o) === undefined,
820
- );
821
-
822
- // DO SPECIALIZE
823
- const condition: ts.IfStatement = specifications
824
- .filter((spec) => spec.property.key.getSoleLiteral() !== null)
825
- .map((spec, i, array) => {
826
- const key: string = spec.property.key.getSoleLiteral()!;
827
- const accessor: ts.Expression = IdentifierFactory.access(
828
- props.input,
829
- key,
830
- );
831
- const pred: ts.Expression = spec.neighbour
832
- ? IsProgrammer.decode({
833
- context: props.context,
834
- functor: props.functor,
835
- input: accessor,
836
- metadata: spec.property.value,
837
- explore: {
838
- ...props.explore,
839
- tracable: false,
840
- postfix: IdentifierFactory.postfix(key),
841
- },
842
- })
843
- : ExpressionFactory.isRequired(accessor);
844
- const schema = indexes.get(spec.object)!;
845
- return ts.factory.createIfStatement(
846
- pred,
847
- ts.factory.createExpressionStatement(
848
- ExpressionFactory.selfCall(out(schema)),
849
- ),
850
- i === array.length - 1
851
- ? remained.length
852
- ? ts.factory.createExpressionStatement(
853
- ExpressionFactory.selfCall(
854
- explore_objects({
855
- context: props.context,
856
- functor: props.functor,
857
- level: props.level + 1,
858
- input: props.input,
859
- schemas: remained.map((r) => indexes.get(r)!),
860
- explore: props.explore,
861
- }),
862
- ),
863
- )
864
- : create_throw_error({
865
- context: props.context,
866
- functor: props.functor,
867
- input: props.input,
868
- expected,
869
- })
870
- : undefined,
871
- );
872
- })
873
- .reverse()
874
- .reduce((a, b) =>
875
- ts.factory.createIfStatement(b.expression, b.thenStatement, a),
876
- );
877
-
878
- // RETURNS WITH CONDITIONS
879
- return ts.factory.createBlock([condition], true);
880
- };
881
-
882
- /* -----------------------------------------------------------
883
- BACKGROUND FUNCTIONS
884
- ----------------------------------------------------------- */
885
- const PREFIX = "_pe";
886
-
887
- const decode_tag = (props: {
888
- wire: ProtobufWire;
889
- index: number;
890
- }): ts.CallExpression =>
891
- callWriter("uint32", [
892
- ExpressionFactory.number((props.index << 3) | props.wire),
893
- ]);
894
-
895
- const get_numeric_wire = (type: ProtobufAtomic.Numeric) =>
896
- type === "double"
897
- ? ProtobufWire.I64
898
- : type === "float"
899
- ? ProtobufWire.I32
900
- : ProtobufWire.VARIANT;
901
-
902
- const create_throw_error = (props: {
903
- context: ITypiaContext;
904
- functor: FunctionProgrammer;
905
- expected: string;
906
- input: ts.Expression;
907
- }) =>
908
- ts.factory.createExpressionStatement(
909
- ts.factory.createCallExpression(
910
- props.context.importer.internal("throwTypeGuardError"),
911
- [],
912
- [
913
- ts.factory.createObjectLiteralExpression(
914
- [
915
- ts.factory.createPropertyAssignment(
916
- "method",
917
- ts.factory.createStringLiteral(props.functor.method),
918
- ),
919
- ts.factory.createPropertyAssignment(
920
- "expected",
921
- ts.factory.createStringLiteral(props.expected),
922
- ),
923
- ts.factory.createPropertyAssignment("value", props.input),
924
- ],
925
- true,
926
- ),
927
- ],
928
- ),
929
- );
930
- }
931
-
932
- const callWriter = (
933
- method: string,
934
- args?: ts.Expression[],
935
- ): ts.CallExpression =>
936
- ts.factory.createCallExpression(
937
- IdentifierFactory.access(ts.factory.createIdentifier("writer"), method),
938
- undefined,
939
- args,
940
- );
941
-
942
- interface IUnion {
943
- is: () => ts.Expression;
944
- value: () => ts.Block;
945
- }
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 { NumericRangeFactory } from "../../factories/NumericRangeFactory";
7
+ import { ProtobufFactory } from "../../factories/ProtobufFactory";
8
+ import { StatementFactory } from "../../factories/StatementFactory";
9
+ import { TypeFactory } from "../../factories/TypeFactory";
10
+
11
+ import { Metadata } from "../../schemas/metadata/Metadata";
12
+ import { MetadataMap } from "../../schemas/metadata/MetadataMap";
13
+ import { MetadataObjectType } from "../../schemas/metadata/MetadataObjectType";
14
+ import { IProtobufProperty } from "../../schemas/protobuf/IProtobufProperty";
15
+ import { IProtobufPropertyType } from "../../schemas/protobuf/IProtobufPropertyType";
16
+ import { IProtobufSchema } from "../../schemas/protobuf/IProtobufSchema";
17
+
18
+ import { IProgrammerProps } from "../../transformers/IProgrammerProps";
19
+ import { ITypiaContext } from "../../transformers/ITypiaContext";
20
+
21
+ import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
22
+
23
+ import { FeatureProgrammer } from "../FeatureProgrammer";
24
+ import { IsProgrammer } from "../IsProgrammer";
25
+ import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
26
+ import { ProtobufUtil } from "../helpers/ProtobufUtil";
27
+ import { ProtobufWire } from "../helpers/ProtobufWire";
28
+ import { UnionPredicator } from "../helpers/UnionPredicator";
29
+ import { decode_union_object } from "../internal/decode_union_object";
30
+
31
+ export namespace ProtobufEncodeProgrammer {
32
+ export const decompose = (props: {
33
+ context: ITypiaContext;
34
+ modulo: ts.LeftHandSideExpression;
35
+ functor: FunctionProgrammer;
36
+ type: ts.Type;
37
+ name: string | undefined;
38
+ }): FeatureProgrammer.IDecomposed => {
39
+ const collection: MetadataCollection = new MetadataCollection();
40
+ const metadata: Metadata = ProtobufFactory.metadata({
41
+ method: props.modulo.getText(),
42
+ checker: props.context.checker,
43
+ transformer: props.context.transformer,
44
+ collection,
45
+ type: props.type,
46
+ });
47
+
48
+ const callEncoder = (writer: string, factory: ts.NewExpression) =>
49
+ StatementFactory.constant({
50
+ name: writer,
51
+ value: ts.factory.createCallExpression(
52
+ ts.factory.createIdentifier("encoder"),
53
+ undefined,
54
+ [factory, ts.factory.createIdentifier("input")],
55
+ ),
56
+ });
57
+ return {
58
+ functions: {
59
+ encoder: StatementFactory.constant({
60
+ name: props.functor.useLocal("encoder"),
61
+ value: write_encoder({
62
+ context: props.context,
63
+ functor: props.functor,
64
+ collection,
65
+ metadata,
66
+ }),
67
+ }),
68
+ },
69
+ statements: [],
70
+ arrow: ts.factory.createArrowFunction(
71
+ undefined,
72
+ undefined,
73
+ [
74
+ IdentifierFactory.parameter(
75
+ "input",
76
+ ts.factory.createTypeReferenceNode(
77
+ props.name ??
78
+ TypeFactory.getFullName({
79
+ checker: props.context.checker,
80
+ type: props.type,
81
+ }),
82
+ ),
83
+ ),
84
+ ],
85
+ ts.factory.createTypeReferenceNode("Uint8Array"),
86
+ undefined,
87
+ ts.factory.createBlock(
88
+ [
89
+ callEncoder(
90
+ "sizer",
91
+ ts.factory.createNewExpression(
92
+ props.context.importer.internal("ProtobufSizer"),
93
+ undefined,
94
+ [],
95
+ ),
96
+ ),
97
+ callEncoder(
98
+ "writer",
99
+ ts.factory.createNewExpression(
100
+ props.context.importer.internal("ProtobufWriter"),
101
+ undefined,
102
+ [ts.factory.createIdentifier("sizer")],
103
+ ),
104
+ ),
105
+ ts.factory.createReturnStatement(callWriter("buffer")),
106
+ ],
107
+ true,
108
+ ),
109
+ ),
110
+ };
111
+ };
112
+
113
+ export const write = (props: IProgrammerProps): ts.CallExpression => {
114
+ const functor: FunctionProgrammer = new FunctionProgrammer(
115
+ props.modulo.getText(),
116
+ );
117
+ const result: FeatureProgrammer.IDecomposed = decompose({
118
+ ...props,
119
+ functor,
120
+ });
121
+ return FeatureProgrammer.writeDecomposed({
122
+ modulo: props.modulo,
123
+ functor,
124
+ result,
125
+ });
126
+ };
127
+
128
+ const write_encoder = (props: {
129
+ context: ITypiaContext;
130
+ functor: FunctionProgrammer;
131
+ collection: MetadataCollection;
132
+ metadata: Metadata;
133
+ }): ts.ArrowFunction => {
134
+ const functors = props.collection
135
+ .objects()
136
+ .filter((obj) => ProtobufUtil.isStaticObject(obj))
137
+ .map((object) =>
138
+ StatementFactory.constant({
139
+ name: `${PREFIX}o${object.index}`,
140
+ value: write_object_function({
141
+ context: props.context,
142
+ functor: props.functor,
143
+ input: ts.factory.createIdentifier("input"),
144
+ object,
145
+ explore: {
146
+ source: "function",
147
+ from: "object",
148
+ tracable: false,
149
+ postfix: "",
150
+ },
151
+ }),
152
+ }),
153
+ );
154
+ return ts.factory.createArrowFunction(
155
+ undefined,
156
+ [
157
+ ts.factory.createTypeParameterDeclaration(
158
+ undefined,
159
+ "Writer",
160
+ props.context.importer.type({
161
+ file: "typia/lib/internal/_IProtobufWriter.js",
162
+ name: "_IProtobufWriter",
163
+ }),
164
+ ),
165
+ ],
166
+ [
167
+ IdentifierFactory.parameter(
168
+ "writer",
169
+ ts.factory.createTypeReferenceNode("Writer"),
170
+ ),
171
+ IdentifierFactory.parameter("input"),
172
+ ],
173
+ ts.factory.createTypeReferenceNode("Writer"),
174
+ undefined,
175
+ ts.factory.createBlock(
176
+ [
177
+ ...props.functor.declareUnions(),
178
+ ...functors,
179
+ ...IsProgrammer.write_function_statements(props),
180
+ ts.factory.createExpressionStatement(
181
+ ts.factory.createCallExpression(
182
+ ts.factory.createIdentifier(
183
+ props.functor.useLocal(
184
+ `${PREFIX}o${props.metadata.objects[0]?.type.index ?? 0}`,
185
+ ),
186
+ ),
187
+ [],
188
+ [ts.factory.createIdentifier("input")],
189
+ ),
190
+ ),
191
+ ts.factory.createReturnStatement(
192
+ ts.factory.createIdentifier("writer"),
193
+ ),
194
+ ],
195
+ true,
196
+ ),
197
+ );
198
+ };
199
+
200
+ const write_object_function = (props: {
201
+ context: ITypiaContext;
202
+ functor: FunctionProgrammer;
203
+ input: ts.Expression;
204
+ object: MetadataObjectType;
205
+ explore: FeatureProgrammer.IExplore;
206
+ }): ts.ArrowFunction => {
207
+ const body: ts.Statement[] = props.object.properties
208
+ .map((p) => {
209
+ const block = decode_property({
210
+ context: props.context,
211
+ functor: props.functor,
212
+ explore: props.explore,
213
+ metadata: p.value,
214
+ protobuf: p.of_protobuf_!,
215
+ input: IdentifierFactory.access(props.input, p.key.getSoleLiteral()!),
216
+ });
217
+ return [
218
+ ts.factory.createExpressionStatement(
219
+ ts.factory.createIdentifier(
220
+ `// property ${JSON.stringify(p.key.getSoleLiteral())}: ${p.value.getName()}`,
221
+ ),
222
+ ),
223
+ ...block.statements,
224
+ ];
225
+ })
226
+ .flat();
227
+ return ts.factory.createArrowFunction(
228
+ undefined,
229
+ undefined,
230
+ [IdentifierFactory.parameter("input")],
231
+ TypeFactory.keyword("any"),
232
+ undefined,
233
+ ts.factory.createBlock(body, true),
234
+ );
235
+ };
236
+
237
+ /* -----------------------------------------------------------
238
+ DECODER STATION
239
+ ----------------------------------------------------------- */
240
+ const decode_property = (props: {
241
+ context: ITypiaContext;
242
+ functor: FunctionProgrammer;
243
+ metadata: Metadata;
244
+ protobuf: IProtobufProperty;
245
+ input: ts.Expression;
246
+ explore: FeatureProgrammer.IExplore;
247
+ }): ts.Block => {
248
+ const union: IUnion[] = [];
249
+ for (const schema of props.protobuf.union) {
250
+ //----
251
+ // ATOMICS
252
+ //----
253
+ if (schema.type === "bool")
254
+ union.push({
255
+ is: () =>
256
+ ts.factory.createStrictEquality(
257
+ ts.factory.createStringLiteral("boolean"),
258
+ ts.factory.createTypeOfExpression(props.input),
259
+ ),
260
+ value: () =>
261
+ decode_bool({
262
+ input: props.input,
263
+ index: schema.index,
264
+ }),
265
+ });
266
+ else if (schema.type === "bigint")
267
+ union.push(
268
+ decode_bigint({
269
+ input: props.input,
270
+ type: schema.name,
271
+ candidates: props.protobuf.union
272
+ .filter((s) => s.type === "bigint")
273
+ .map((s) => s.name),
274
+ index: schema.index,
275
+ }),
276
+ );
277
+ else if (schema.type === "number")
278
+ union.push(
279
+ decode_number({
280
+ input: props.input,
281
+ type: schema.name,
282
+ candidates: props.protobuf.union
283
+ .filter((s) => s.type === "number")
284
+ .map((s) => s.name),
285
+ index: schema.index,
286
+ }),
287
+ );
288
+ else if (schema.type === "string")
289
+ union.push({
290
+ is: () =>
291
+ ts.factory.createStrictEquality(
292
+ ts.factory.createStringLiteral("string"),
293
+ ts.factory.createTypeOfExpression(props.input),
294
+ ),
295
+ value: () =>
296
+ decode_bytes({
297
+ method: "string",
298
+ index: schema.index,
299
+ input: props.input,
300
+ }),
301
+ });
302
+ //----
303
+ // INSTANCES
304
+ //----
305
+ else if (schema.type === "bytes")
306
+ union.push({
307
+ is: () => ExpressionFactory.isInstanceOf("Uint8Array", props.input),
308
+ value: () =>
309
+ decode_bytes({
310
+ method: "bytes",
311
+ index: schema.index,
312
+ input: props.input,
313
+ }),
314
+ });
315
+ else if (schema.type === "array")
316
+ union.push({
317
+ is: () => ExpressionFactory.isArray(props.input),
318
+ value: () =>
319
+ decode_array({
320
+ context: props.context,
321
+ functor: props.functor,
322
+ input: props.input,
323
+ schema,
324
+ }),
325
+ });
326
+ else if (schema.type === "map" && schema.map instanceof MetadataMap) {
327
+ union.push({
328
+ is: () => ExpressionFactory.isInstanceOf("Map", props.input),
329
+ value: () =>
330
+ decode_map({
331
+ context: props.context,
332
+ functor: props.functor,
333
+ schema,
334
+ input: props.input,
335
+ }),
336
+ });
337
+ }
338
+ const objectSchemas: Array<
339
+ IProtobufPropertyType.IObject | IProtobufPropertyType.IMap
340
+ > = props.protobuf.union
341
+ .filter((schema) => schema.type === "object" || schema.type === "map")
342
+ .filter(
343
+ (schema) =>
344
+ schema.type === "object" ||
345
+ (schema.type === "map" && schema.map instanceof MetadataObjectType),
346
+ );
347
+ if (objectSchemas.length !== 0)
348
+ union.push({
349
+ is: () =>
350
+ ExpressionFactory.isObject({
351
+ checkNull: true,
352
+ checkArray: false,
353
+ input: props.input,
354
+ }),
355
+ value: () =>
356
+ explore_objects({
357
+ context: props.context,
358
+ functor: props.functor,
359
+ level: 0,
360
+ schemas: objectSchemas,
361
+ explore: {
362
+ ...props.explore,
363
+ from: "object",
364
+ },
365
+ input: props.input,
366
+ }),
367
+ });
368
+ }
369
+
370
+ // RETURNS
371
+ const wrapper: (block: ts.Block) => ts.Block =
372
+ props.metadata.isRequired() && props.metadata.nullable === false
373
+ ? (block) => block
374
+ : props.metadata.isRequired() === false &&
375
+ props.metadata.nullable === true
376
+ ? (block) =>
377
+ ts.factory.createBlock(
378
+ [
379
+ ts.factory.createIfStatement(
380
+ ts.factory.createLogicalAnd(
381
+ ts.factory.createStrictInequality(
382
+ ts.factory.createIdentifier("undefined"),
383
+ props.input,
384
+ ),
385
+ ts.factory.createStrictInequality(
386
+ ts.factory.createNull(),
387
+ props.input,
388
+ ),
389
+ ),
390
+ block,
391
+ ),
392
+ ],
393
+ true,
394
+ )
395
+ : props.metadata.isRequired() === false
396
+ ? (block) =>
397
+ ts.factory.createBlock(
398
+ [
399
+ ts.factory.createIfStatement(
400
+ ts.factory.createStrictInequality(
401
+ ts.factory.createIdentifier("undefined"),
402
+ props.input,
403
+ ),
404
+ block,
405
+ ),
406
+ ],
407
+ true,
408
+ )
409
+ : (block) =>
410
+ ts.factory.createBlock(
411
+ [
412
+ ts.factory.createIfStatement(
413
+ ts.factory.createStrictInequality(
414
+ ts.factory.createNull(),
415
+ props.input,
416
+ ),
417
+ block,
418
+ ),
419
+ ],
420
+ true,
421
+ );
422
+ if (union.length === 1) return wrapper(union[0]!.value());
423
+ return wrapper(
424
+ ts.factory.createBlock(
425
+ [
426
+ union
427
+ .map((u, i) =>
428
+ ts.factory.createIfStatement(
429
+ u.is(),
430
+ u.value(),
431
+ i === union.length - 1
432
+ ? create_throw_error({
433
+ context: props.context,
434
+ functor: props.functor,
435
+ input: props.input,
436
+ expected: props.metadata.getName(),
437
+ })
438
+ : undefined,
439
+ ),
440
+ )
441
+ .reverse()
442
+ .reduce((a, b) =>
443
+ ts.factory.createIfStatement(b.expression, b.thenStatement, a),
444
+ ),
445
+ ],
446
+ true,
447
+ ),
448
+ );
449
+ };
450
+
451
+ /* -----------------------------------------------------------
452
+ ATOMIC DECODERS
453
+ ----------------------------------------------------------- */
454
+ const decode_bool = (props: {
455
+ input: ts.Expression;
456
+ index: number | null;
457
+ }): ts.Block =>
458
+ ts.factory.createBlock(
459
+ [
460
+ ...(props.index !== null
461
+ ? [
462
+ decode_tag({
463
+ wire: ProtobufWire.VARIANT,
464
+ index: props.index,
465
+ }),
466
+ ]
467
+ : []),
468
+ callWriter("bool", [props.input]),
469
+ ].map((exp) => ts.factory.createExpressionStatement(exp)),
470
+ true,
471
+ );
472
+
473
+ const decode_bigint = (props: {
474
+ candidates: ProtobufAtomic.BigNumeric[];
475
+ type: ProtobufAtomic.BigNumeric;
476
+ input: ts.Expression;
477
+ index: number | null;
478
+ }): IUnion => ({
479
+ is: () =>
480
+ props.candidates.length === 1
481
+ ? ts.factory.createStrictEquality(
482
+ ts.factory.createStringLiteral("bigint"),
483
+ ts.factory.createTypeOfExpression(props.input),
484
+ )
485
+ : ts.factory.createLogicalAnd(
486
+ ts.factory.createStrictEquality(
487
+ ts.factory.createStringLiteral("bigint"),
488
+ ts.factory.createTypeOfExpression(props.input),
489
+ ),
490
+ NumericRangeFactory.bigint(props.type, props.input),
491
+ ),
492
+ value: () =>
493
+ ts.factory.createBlock(
494
+ [
495
+ ...(props.index !== null
496
+ ? [
497
+ decode_tag({
498
+ wire: ProtobufWire.VARIANT,
499
+ index: props.index,
500
+ }),
501
+ ]
502
+ : []),
503
+ callWriter(props.type, [props.input]),
504
+ ].map((exp) => ts.factory.createExpressionStatement(exp)),
505
+ true,
506
+ ),
507
+ });
508
+
509
+ const decode_number = (props: {
510
+ candidates: ProtobufAtomic.Numeric[];
511
+ type: ProtobufAtomic.Numeric;
512
+ input: ts.Expression;
513
+ index: number | null;
514
+ }): IUnion => ({
515
+ is: () =>
516
+ props.candidates.length === 1
517
+ ? ts.factory.createStrictEquality(
518
+ ts.factory.createStringLiteral("number"),
519
+ ts.factory.createTypeOfExpression(props.input),
520
+ )
521
+ : ts.factory.createLogicalAnd(
522
+ ts.factory.createStrictEquality(
523
+ ts.factory.createStringLiteral("number"),
524
+ ts.factory.createTypeOfExpression(props.input),
525
+ ),
526
+ NumericRangeFactory.number(props.type, props.input),
527
+ ),
528
+ value: () =>
529
+ ts.factory.createBlock(
530
+ [
531
+ ...(props.index !== null
532
+ ? [
533
+ decode_tag({
534
+ wire: get_numeric_wire(props.type),
535
+ index: props.index,
536
+ }),
537
+ ]
538
+ : []),
539
+ callWriter(props.type, [props.input]),
540
+ ].map((exp) => ts.factory.createExpressionStatement(exp)),
541
+ true,
542
+ ),
543
+ });
544
+
545
+ const decode_container_value = (props: {
546
+ context: ITypiaContext;
547
+ functor: FunctionProgrammer;
548
+ schema: IProtobufPropertyType.IArray["value"];
549
+ index: number;
550
+ kind: "array" | "map";
551
+ input: ts.Expression;
552
+ }): ts.Block => {
553
+ if (props.schema.type === "bool")
554
+ return decode_bool({
555
+ input: props.input,
556
+ index: props.kind === "array" ? null : props.index,
557
+ });
558
+ else if (props.schema.type === "bigint")
559
+ return decode_bigint({
560
+ input: props.input,
561
+ type: props.schema.name,
562
+ candidates: [props.schema.name],
563
+ index: props.kind === "array" ? null : props.index,
564
+ }).value();
565
+ else if (props.schema.type === "number")
566
+ return decode_number({
567
+ input: props.input,
568
+ type: props.schema.name,
569
+ candidates: [props.schema.name],
570
+ index: props.kind === "array" ? null : props.index,
571
+ }).value();
572
+ else if (props.schema.type === "string" || props.schema.type === "bytes")
573
+ return decode_bytes({
574
+ method: props.schema.type,
575
+ input: props.input,
576
+ index: props.index,
577
+ });
578
+ return decode_object({
579
+ context: props.context,
580
+ functor: props.functor,
581
+ schema: props.schema,
582
+ input: props.input,
583
+ index: props.index,
584
+ });
585
+ };
586
+
587
+ /* -----------------------------------------------------------
588
+ INSTANCE DECODERS
589
+ ----------------------------------------------------------- */
590
+ const decode_bytes = (props: {
591
+ method: "bytes" | "string";
592
+ index: number;
593
+ input: ts.Expression;
594
+ }): ts.Block =>
595
+ ts.factory.createBlock(
596
+ [
597
+ decode_tag({
598
+ wire: ProtobufWire.LEN,
599
+ index: props.index,
600
+ }),
601
+ callWriter(props.method, [props.input]),
602
+ ].map((expr) => ts.factory.createExpressionStatement(expr)),
603
+ true,
604
+ );
605
+
606
+ const decode_array = (props: {
607
+ context: ITypiaContext;
608
+ functor: FunctionProgrammer;
609
+ schema: IProtobufPropertyType.IArray;
610
+ input: ts.Expression;
611
+ }): ts.Block => {
612
+ const value: IProtobufPropertyType.IArray["value"] = props.schema.value;
613
+ const wire: ProtobufWire = (() => {
614
+ if (
615
+ value.type === "object" ||
616
+ value.type === "bytes" ||
617
+ value.type === "string"
618
+ )
619
+ return ProtobufWire.LEN;
620
+ else if (value.type === "number" && value.name === "float")
621
+ return ProtobufWire.I32;
622
+ return ProtobufWire.VARIANT;
623
+ })();
624
+ const forLoop = () =>
625
+ ts.factory.createForOfStatement(
626
+ undefined,
627
+ ts.factory.createVariableDeclarationList(
628
+ [ts.factory.createVariableDeclaration("elem")],
629
+ ts.NodeFlags.Const,
630
+ ),
631
+ props.input,
632
+ decode_container_value({
633
+ kind: "array",
634
+ context: props.context,
635
+ functor: props.functor,
636
+ input: ts.factory.createIdentifier("elem"),
637
+ index: props.schema.index,
638
+ schema: props.schema.value,
639
+ }),
640
+ );
641
+ const length = (block: ts.Block) =>
642
+ ts.factory.createBlock(
643
+ [
644
+ ts.factory.createIfStatement(
645
+ ts.factory.createStrictInequality(
646
+ ExpressionFactory.number(0),
647
+ IdentifierFactory.access(props.input, "length"),
648
+ ),
649
+ block,
650
+ ),
651
+ ],
652
+ true,
653
+ );
654
+ if (wire === ProtobufWire.LEN)
655
+ return length(ts.factory.createBlock([forLoop()], true));
656
+ return length(
657
+ ts.factory.createBlock(
658
+ [
659
+ ts.factory.createExpressionStatement(
660
+ decode_tag({
661
+ wire: ProtobufWire.LEN,
662
+ index: props.schema.index,
663
+ }),
664
+ ),
665
+ ts.factory.createExpressionStatement(callWriter("fork")),
666
+ forLoop(),
667
+ ts.factory.createExpressionStatement(callWriter("ldelim")),
668
+ ],
669
+ true,
670
+ ),
671
+ );
672
+ };
673
+
674
+ const decode_object = (props: {
675
+ context: ITypiaContext;
676
+ functor: FunctionProgrammer;
677
+ schema: IProtobufSchema.IObject;
678
+ index: number;
679
+ input: ts.Expression;
680
+ }): ts.Block =>
681
+ ts.factory.createBlock(
682
+ [
683
+ decode_tag({
684
+ wire: ProtobufWire.LEN,
685
+ index: props.index,
686
+ }),
687
+ callWriter("fork"),
688
+ ts.factory.createCallExpression(
689
+ ts.factory.createIdentifier(
690
+ props.functor.useLocal(`${PREFIX}o${props.schema.object.index}`),
691
+ ),
692
+ [],
693
+ [props.input],
694
+ ),
695
+ callWriter("ldelim"),
696
+ ].map(ts.factory.createExpressionStatement),
697
+ true,
698
+ );
699
+
700
+ const decode_map = (props: {
701
+ context: ITypiaContext;
702
+ functor: FunctionProgrammer;
703
+ schema: IProtobufPropertyType.IMap;
704
+ input: ts.Expression;
705
+ }): ts.Block => {
706
+ const each: ts.Statement[] = [
707
+ ts.factory.createExpressionStatement(
708
+ decode_tag({
709
+ wire: ProtobufWire.LEN,
710
+ index: props.schema.index,
711
+ }),
712
+ ),
713
+ ts.factory.createExpressionStatement(callWriter("fork")),
714
+ ...decode_container_value({
715
+ kind: "map",
716
+ context: props.context,
717
+ functor: props.functor,
718
+ index: 1,
719
+ input: ts.factory.createIdentifier("key"),
720
+ schema: props.schema.key,
721
+ }).statements,
722
+ ...decode_container_value({
723
+ kind: "map",
724
+ context: props.context,
725
+ functor: props.functor,
726
+ index: 2,
727
+ input: ts.factory.createIdentifier("value"),
728
+ schema: props.schema.value,
729
+ }).statements,
730
+ ts.factory.createExpressionStatement(callWriter("ldelim")),
731
+ ];
732
+ return ts.factory.createBlock(
733
+ [
734
+ ts.factory.createForOfStatement(
735
+ undefined,
736
+ StatementFactory.entry({
737
+ key: "key",
738
+ value: "value",
739
+ }),
740
+ props.input,
741
+ ts.factory.createBlock(each),
742
+ ),
743
+ ],
744
+ true,
745
+ );
746
+ };
747
+
748
+ const explore_objects = (props: {
749
+ context: ITypiaContext;
750
+ functor: FunctionProgrammer;
751
+ level: number;
752
+ input: ts.Expression;
753
+ schemas: Array<IProtobufPropertyType.IObject | IProtobufPropertyType.IMap>;
754
+ explore: FeatureProgrammer.IExplore;
755
+ }): ts.Block => {
756
+ const out = (
757
+ schema: IProtobufPropertyType.IObject | IProtobufPropertyType.IMap,
758
+ ) =>
759
+ schema.type === "object"
760
+ ? decode_object({
761
+ context: props.context,
762
+ functor: props.functor,
763
+ schema,
764
+ index: schema.index,
765
+ input: props.input,
766
+ })
767
+ : decode_map({
768
+ context: props.context,
769
+ functor: props.functor,
770
+ schema,
771
+ input: ts.factory.createCallExpression(
772
+ IdentifierFactory.access(
773
+ ts.factory.createIdentifier("Object"),
774
+ "entries",
775
+ ),
776
+ undefined,
777
+ [props.input],
778
+ ),
779
+ });
780
+ if (props.schemas.length === 1) return out(props.schemas[0]!);
781
+
782
+ const objects: MetadataObjectType[] = props.schemas.map((s) =>
783
+ s.type === "map" ? (s.map as MetadataObjectType) : s.object,
784
+ );
785
+ const expected: string = `(${objects.map((t) => t.name).join(" | ")})`;
786
+ const indexes: WeakMap<
787
+ MetadataObjectType,
788
+ IProtobufPropertyType.IObject | IProtobufPropertyType.IMap
789
+ > = new WeakMap(objects.map((o, i) => [o, props.schemas[i]!]));
790
+ const specifications: UnionPredicator.ISpecialized[] =
791
+ UnionPredicator.object(objects);
792
+
793
+ if (specifications.length === 0) {
794
+ const condition: ts.Expression = decode_union_object({
795
+ checker: (v) =>
796
+ IsProgrammer.decode_object({
797
+ context: props.context,
798
+ functor: props.functor,
799
+ object: v.object,
800
+ input: v.input,
801
+ explore: v.explore,
802
+ }),
803
+ decoder: (v) => ExpressionFactory.selfCall(out(indexes.get(v.object)!)),
804
+ success: (expr) => expr,
805
+ escaper: (v) =>
806
+ create_throw_error({
807
+ context: props.context,
808
+ functor: props.functor,
809
+ expected: v.expected,
810
+ input: v.input,
811
+ }),
812
+ input: props.input,
813
+ explore: props.explore,
814
+ objects,
815
+ });
816
+ return StatementFactory.block(condition);
817
+ }
818
+ const remained: MetadataObjectType[] = objects.filter(
819
+ (o) => specifications.find((s) => s.object === o) === undefined,
820
+ );
821
+
822
+ // DO SPECIALIZE
823
+ const condition: ts.IfStatement = specifications
824
+ .filter((spec) => spec.property.key.getSoleLiteral() !== null)
825
+ .map((spec, i, array) => {
826
+ const key: string = spec.property.key.getSoleLiteral()!;
827
+ const accessor: ts.Expression = IdentifierFactory.access(
828
+ props.input,
829
+ key,
830
+ );
831
+ const pred: ts.Expression = spec.neighbour
832
+ ? IsProgrammer.decode({
833
+ context: props.context,
834
+ functor: props.functor,
835
+ input: accessor,
836
+ metadata: spec.property.value,
837
+ explore: {
838
+ ...props.explore,
839
+ tracable: false,
840
+ postfix: IdentifierFactory.postfix(key),
841
+ },
842
+ })
843
+ : ExpressionFactory.isRequired(accessor);
844
+ const schema = indexes.get(spec.object)!;
845
+ return ts.factory.createIfStatement(
846
+ pred,
847
+ ts.factory.createExpressionStatement(
848
+ ExpressionFactory.selfCall(out(schema)),
849
+ ),
850
+ i === array.length - 1
851
+ ? remained.length
852
+ ? ts.factory.createExpressionStatement(
853
+ ExpressionFactory.selfCall(
854
+ explore_objects({
855
+ context: props.context,
856
+ functor: props.functor,
857
+ level: props.level + 1,
858
+ input: props.input,
859
+ schemas: remained.map((r) => indexes.get(r)!),
860
+ explore: props.explore,
861
+ }),
862
+ ),
863
+ )
864
+ : create_throw_error({
865
+ context: props.context,
866
+ functor: props.functor,
867
+ input: props.input,
868
+ expected,
869
+ })
870
+ : undefined,
871
+ );
872
+ })
873
+ .reverse()
874
+ .reduce((a, b) =>
875
+ ts.factory.createIfStatement(b.expression, b.thenStatement, a),
876
+ );
877
+
878
+ // RETURNS WITH CONDITIONS
879
+ return ts.factory.createBlock([condition], true);
880
+ };
881
+
882
+ /* -----------------------------------------------------------
883
+ BACKGROUND FUNCTIONS
884
+ ----------------------------------------------------------- */
885
+ const PREFIX = "_pe";
886
+
887
+ const decode_tag = (props: {
888
+ wire: ProtobufWire;
889
+ index: number;
890
+ }): ts.CallExpression =>
891
+ callWriter("uint32", [
892
+ ExpressionFactory.number((props.index << 3) | props.wire),
893
+ ]);
894
+
895
+ const get_numeric_wire = (type: ProtobufAtomic.Numeric) =>
896
+ type === "double"
897
+ ? ProtobufWire.I64
898
+ : type === "float"
899
+ ? ProtobufWire.I32
900
+ : ProtobufWire.VARIANT;
901
+
902
+ const create_throw_error = (props: {
903
+ context: ITypiaContext;
904
+ functor: FunctionProgrammer;
905
+ expected: string;
906
+ input: ts.Expression;
907
+ }) =>
908
+ ts.factory.createExpressionStatement(
909
+ ts.factory.createCallExpression(
910
+ props.context.importer.internal("throwTypeGuardError"),
911
+ [],
912
+ [
913
+ ts.factory.createObjectLiteralExpression(
914
+ [
915
+ ts.factory.createPropertyAssignment(
916
+ "method",
917
+ ts.factory.createStringLiteral(props.functor.method),
918
+ ),
919
+ ts.factory.createPropertyAssignment(
920
+ "expected",
921
+ ts.factory.createStringLiteral(props.expected),
922
+ ),
923
+ ts.factory.createPropertyAssignment("value", props.input),
924
+ ],
925
+ true,
926
+ ),
927
+ ],
928
+ ),
929
+ );
930
+ }
931
+
932
+ const callWriter = (
933
+ method: string,
934
+ args?: ts.Expression[],
935
+ ): ts.CallExpression =>
936
+ ts.factory.createCallExpression(
937
+ IdentifierFactory.access(ts.factory.createIdentifier("writer"), method),
938
+ undefined,
939
+ args,
940
+ );
941
+
942
+ interface IUnion {
943
+ is: () => ts.Expression;
944
+ value: () => ts.Block;
945
+ }