typia 9.7.1 → 10.0.0-dev.20251107

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 (424) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +153 -153
  3. package/lib/AssertionGuard.d.mts +27 -25
  4. package/lib/AssertionGuard.d.ts +27 -25
  5. package/lib/CamelCase.d.mts +1 -1
  6. package/lib/CamelCase.d.ts +1 -1
  7. package/lib/IRandomGenerator.d.mts +44 -42
  8. package/lib/IRandomGenerator.d.ts +44 -42
  9. package/lib/IReadableURLSearchParams.d.mts +2 -2
  10. package/lib/IReadableURLSearchParams.d.ts +2 -2
  11. package/lib/PascalCase.d.mts +1 -1
  12. package/lib/PascalCase.d.ts +1 -1
  13. package/lib/Primitive.d.mts +20 -22
  14. package/lib/Primitive.d.ts +20 -22
  15. package/lib/Resolved.d.mts +16 -18
  16. package/lib/Resolved.d.ts +16 -18
  17. package/lib/SnakeCase.d.mts +3 -2
  18. package/lib/SnakeCase.d.ts +3 -2
  19. package/lib/TypeGuardError.d.mts +88 -61
  20. package/lib/TypeGuardError.d.ts +88 -61
  21. package/lib/TypeGuardError.js +40 -29
  22. package/lib/TypeGuardError.js.map +1 -1
  23. package/lib/TypeGuardError.mjs +70 -48
  24. package/lib/factories/MetadataCollection.js +4 -12
  25. package/lib/factories/MetadataCollection.js.map +1 -1
  26. package/lib/factories/MetadataCollection.mjs +4 -12
  27. package/lib/factories/MetadataCommentTagFactory.js +5 -15
  28. package/lib/factories/MetadataCommentTagFactory.js.map +1 -1
  29. package/lib/factories/MetadataCommentTagFactory.mjs +5 -15
  30. package/lib/factories/MetadataFactory.js +1 -3
  31. package/lib/factories/MetadataFactory.js.map +1 -1
  32. package/lib/factories/MetadataFactory.mjs +1 -3
  33. package/lib/factories/ProtobufFactory.js +2 -4
  34. package/lib/factories/ProtobufFactory.js.map +1 -1
  35. package/lib/factories/ProtobufFactory.mjs +2 -4
  36. package/lib/functional.d.mts +196 -195
  37. package/lib/functional.d.ts +196 -195
  38. package/lib/functional.js +18 -54
  39. package/lib/functional.js.map +1 -1
  40. package/lib/functional.mjs +18 -54
  41. package/lib/http.d.mts +303 -319
  42. package/lib/http.d.ts +303 -319
  43. package/lib/http.js +26 -78
  44. package/lib/http.js.map +1 -1
  45. package/lib/http.mjs +26 -78
  46. package/lib/internal/_ProtobufReader.d.mts +3 -9
  47. package/lib/internal/_ProtobufReader.d.ts +3 -9
  48. package/lib/internal/_ProtobufReader.js.map +1 -1
  49. package/lib/internal/_ProtobufReader.mjs +3 -9
  50. package/lib/internal/_ProtobufSizer.d.mts +4 -12
  51. package/lib/internal/_ProtobufSizer.d.ts +4 -12
  52. package/lib/internal/_ProtobufSizer.js.map +1 -1
  53. package/lib/internal/_ProtobufSizer.mjs +4 -12
  54. package/lib/internal/_ProtobufWriter.d.mts +5 -15
  55. package/lib/internal/_ProtobufWriter.d.ts +5 -15
  56. package/lib/internal/_ProtobufWriter.js.map +1 -1
  57. package/lib/internal/_ProtobufWriter.mjs +5 -15
  58. package/lib/internal/_jsonStringifyString.d.mts +4 -4
  59. package/lib/internal/_jsonStringifyString.d.ts +4 -4
  60. package/lib/internal/_jsonStringifyString.js +4 -4
  61. package/lib/internal/_jsonStringifyString.mjs +4 -4
  62. package/lib/json.d.mts +174 -195
  63. package/lib/json.d.ts +174 -195
  64. package/lib/json.js +16 -48
  65. package/lib/json.js.map +1 -1
  66. package/lib/json.mjs +16 -48
  67. package/lib/llm.d.mts +275 -192
  68. package/lib/llm.d.ts +275 -192
  69. package/lib/llm.js +4 -12
  70. package/lib/llm.js.map +1 -1
  71. package/lib/llm.mjs +4 -12
  72. package/lib/misc.d.mts +149 -172
  73. package/lib/misc.d.ts +149 -172
  74. package/lib/misc.js +17 -51
  75. package/lib/misc.js.map +1 -1
  76. package/lib/misc.mjs +17 -51
  77. package/lib/module.d.mts +263 -275
  78. package/lib/module.d.ts +263 -275
  79. package/lib/module.js +18 -54
  80. package/lib/module.js.map +1 -1
  81. package/lib/module.mjs +18 -54
  82. package/lib/notations.d.mts +153 -174
  83. package/lib/notations.d.ts +153 -174
  84. package/lib/notations.js +24 -72
  85. package/lib/notations.js.map +1 -1
  86. package/lib/notations.mjs +24 -72
  87. package/lib/programmers/FeatureProgrammer.d.mts +19 -41
  88. package/lib/programmers/FeatureProgrammer.d.ts +19 -41
  89. package/lib/programmers/FeatureProgrammer.js.map +1 -1
  90. package/lib/programmers/ImportProgrammer.js +3 -9
  91. package/lib/programmers/ImportProgrammer.js.map +1 -1
  92. package/lib/programmers/ImportProgrammer.mjs +3 -9
  93. package/lib/programmers/RandomProgrammer.js +6 -0
  94. package/lib/programmers/RandomProgrammer.js.map +1 -1
  95. package/lib/programmers/RandomProgrammer.mjs +6 -0
  96. package/lib/programmers/helpers/ProtobufWire.d.mts +13 -13
  97. package/lib/programmers/helpers/ProtobufWire.d.ts +13 -13
  98. package/lib/programmers/internal/check_array_length.js +2 -6
  99. package/lib/programmers/internal/check_array_length.js.map +1 -1
  100. package/lib/programmers/internal/check_array_length.mjs +2 -6
  101. package/lib/programmers/internal/check_bigint.js +2 -6
  102. package/lib/programmers/internal/check_bigint.js.map +1 -1
  103. package/lib/programmers/internal/check_bigint.mjs +2 -6
  104. package/lib/programmers/internal/check_dynamic_key.js +2 -6
  105. package/lib/programmers/internal/check_dynamic_key.js.map +1 -1
  106. package/lib/programmers/internal/check_dynamic_key.mjs +2 -6
  107. package/lib/programmers/internal/check_dynamic_properties.js +3 -9
  108. package/lib/programmers/internal/check_dynamic_properties.js.map +1 -1
  109. package/lib/programmers/internal/check_dynamic_properties.mjs +3 -9
  110. package/lib/programmers/internal/check_everything.js +1 -3
  111. package/lib/programmers/internal/check_everything.js.map +1 -1
  112. package/lib/programmers/internal/check_everything.mjs +1 -3
  113. package/lib/programmers/internal/check_native.js +2 -6
  114. package/lib/programmers/internal/check_native.js.map +1 -1
  115. package/lib/programmers/internal/check_native.mjs +2 -6
  116. package/lib/programmers/internal/check_number.js +2 -6
  117. package/lib/programmers/internal/check_number.js.map +1 -1
  118. package/lib/programmers/internal/check_number.mjs +2 -6
  119. package/lib/programmers/internal/check_object.js +2 -6
  120. package/lib/programmers/internal/check_object.js.map +1 -1
  121. package/lib/programmers/internal/check_object.mjs +2 -6
  122. package/lib/programmers/internal/check_string.js +2 -6
  123. package/lib/programmers/internal/check_string.js.map +1 -1
  124. package/lib/programmers/internal/check_string.mjs +2 -6
  125. package/lib/programmers/internal/check_template.js +1 -3
  126. package/lib/programmers/internal/check_template.js.map +1 -1
  127. package/lib/programmers/internal/check_template.mjs +1 -3
  128. package/lib/programmers/internal/check_union_array_like.js +1 -3
  129. package/lib/programmers/internal/check_union_array_like.js.map +1 -1
  130. package/lib/programmers/internal/check_union_array_like.mjs +1 -3
  131. package/lib/programmers/internal/decode_union_object.js +2 -6
  132. package/lib/programmers/internal/decode_union_object.js.map +1 -1
  133. package/lib/programmers/internal/decode_union_object.mjs +2 -6
  134. package/lib/programmers/internal/feature_object_entries.js +1 -3
  135. package/lib/programmers/internal/feature_object_entries.js.map +1 -1
  136. package/lib/programmers/internal/feature_object_entries.mjs +1 -3
  137. package/lib/programmers/internal/json_schema_escaped.js +2 -6
  138. package/lib/programmers/internal/json_schema_escaped.js.map +1 -1
  139. package/lib/programmers/internal/json_schema_escaped.mjs +2 -6
  140. package/lib/programmers/internal/json_schema_object.js +3 -9
  141. package/lib/programmers/internal/json_schema_object.js.map +1 -1
  142. package/lib/programmers/internal/json_schema_object.mjs +3 -9
  143. package/lib/programmers/internal/json_schema_station.d.mts +2 -2
  144. package/lib/programmers/internal/json_schema_station.d.ts +2 -2
  145. package/lib/programmers/internal/metadata_to_pattern.js +1 -3
  146. package/lib/programmers/internal/metadata_to_pattern.js.map +1 -1
  147. package/lib/programmers/internal/metadata_to_pattern.mjs +1 -3
  148. package/lib/programmers/internal/postfix_of_tuple.js +1 -3
  149. package/lib/programmers/internal/postfix_of_tuple.js.map +1 -1
  150. package/lib/programmers/internal/postfix_of_tuple.mjs +1 -3
  151. package/lib/programmers/internal/prune_object_properties.js +1 -3
  152. package/lib/programmers/internal/prune_object_properties.js.map +1 -1
  153. package/lib/programmers/internal/prune_object_properties.mjs +1 -3
  154. package/lib/programmers/internal/stringify_dynamic_properties.js +2 -6
  155. package/lib/programmers/internal/stringify_dynamic_properties.js.map +1 -1
  156. package/lib/programmers/internal/stringify_dynamic_properties.mjs +2 -6
  157. package/lib/programmers/internal/stringify_native.js +1 -3
  158. package/lib/programmers/internal/stringify_native.js.map +1 -1
  159. package/lib/programmers/internal/stringify_native.mjs +1 -3
  160. package/lib/programmers/internal/stringify_regular_properties.js +2 -6
  161. package/lib/programmers/internal/stringify_regular_properties.js.map +1 -1
  162. package/lib/programmers/internal/stringify_regular_properties.mjs +2 -6
  163. package/lib/programmers/internal/template_to_pattern.js +1 -3
  164. package/lib/programmers/internal/template_to_pattern.js.map +1 -1
  165. package/lib/programmers/internal/template_to_pattern.mjs +1 -3
  166. package/lib/programmers/internal/wrap_metadata_rest_tuple.js +1 -3
  167. package/lib/programmers/internal/wrap_metadata_rest_tuple.js.map +1 -1
  168. package/lib/programmers/internal/wrap_metadata_rest_tuple.mjs +1 -3
  169. package/lib/programmers/json/JsonStringifyProgrammer.js +2 -2
  170. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  171. package/lib/programmers/json/JsonStringifyProgrammer.mjs +2 -2
  172. package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
  173. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  174. package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
  175. package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
  176. package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
  177. package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
  178. package/lib/protobuf.d.mts +290 -297
  179. package/lib/protobuf.d.ts +290 -297
  180. package/lib/protobuf.js +17 -51
  181. package/lib/protobuf.js.map +1 -1
  182. package/lib/protobuf.mjs +17 -51
  183. package/lib/reflect.d.mts +2 -4
  184. package/lib/reflect.d.ts +2 -4
  185. package/lib/reflect.js +1 -3
  186. package/lib/reflect.js.map +1 -1
  187. package/lib/reflect.mjs +1 -3
  188. package/lib/schemas/json/IJsonApplication.d.mts +4 -4
  189. package/lib/schemas/json/IJsonApplication.d.ts +4 -4
  190. package/lib/schemas/json/IJsonSchemaCollection.d.mts +73 -56
  191. package/lib/schemas/json/IJsonSchemaCollection.d.ts +73 -56
  192. package/lib/schemas/json/IJsonSchemaUnit.d.mts +83 -70
  193. package/lib/schemas/json/IJsonSchemaUnit.d.ts +83 -70
  194. package/lib/schemas/metadata/Metadata.d.mts +1 -3
  195. package/lib/schemas/metadata/Metadata.d.ts +1 -3
  196. package/lib/schemas/metadata/Metadata.js +9 -27
  197. package/lib/schemas/metadata/Metadata.js.map +1 -1
  198. package/lib/schemas/metadata/Metadata.mjs +9 -27
  199. package/lib/schemas/metadata/MetadataAliasType.d.mts +1 -3
  200. package/lib/schemas/metadata/MetadataAliasType.d.ts +1 -3
  201. package/lib/schemas/metadata/MetadataAliasType.js +3 -9
  202. package/lib/schemas/metadata/MetadataAliasType.js.map +1 -1
  203. package/lib/schemas/metadata/MetadataAliasType.mjs +3 -9
  204. package/lib/schemas/metadata/MetadataApplication.d.mts +1 -3
  205. package/lib/schemas/metadata/MetadataApplication.d.ts +1 -3
  206. package/lib/schemas/metadata/MetadataApplication.js +2 -6
  207. package/lib/schemas/metadata/MetadataApplication.js.map +1 -1
  208. package/lib/schemas/metadata/MetadataApplication.mjs +2 -6
  209. package/lib/schemas/metadata/MetadataArray.d.mts +1 -3
  210. package/lib/schemas/metadata/MetadataArray.d.ts +1 -3
  211. package/lib/schemas/metadata/MetadataArray.js +1 -3
  212. package/lib/schemas/metadata/MetadataArray.js.map +1 -1
  213. package/lib/schemas/metadata/MetadataArray.mjs +1 -3
  214. package/lib/schemas/metadata/MetadataArrayType.d.mts +1 -3
  215. package/lib/schemas/metadata/MetadataArrayType.d.ts +1 -3
  216. package/lib/schemas/metadata/MetadataArrayType.js +3 -9
  217. package/lib/schemas/metadata/MetadataArrayType.js.map +1 -1
  218. package/lib/schemas/metadata/MetadataArrayType.mjs +3 -9
  219. package/lib/schemas/metadata/MetadataAtomic.js +1 -3
  220. package/lib/schemas/metadata/MetadataAtomic.js.map +1 -1
  221. package/lib/schemas/metadata/MetadataAtomic.mjs +1 -3
  222. package/lib/schemas/metadata/MetadataEscaped.d.mts +1 -3
  223. package/lib/schemas/metadata/MetadataEscaped.d.ts +1 -3
  224. package/lib/schemas/metadata/MetadataEscaped.js +3 -9
  225. package/lib/schemas/metadata/MetadataEscaped.js.map +1 -1
  226. package/lib/schemas/metadata/MetadataEscaped.mjs +3 -9
  227. package/lib/schemas/metadata/MetadataFunction.d.mts +1 -3
  228. package/lib/schemas/metadata/MetadataFunction.d.ts +1 -3
  229. package/lib/schemas/metadata/MetadataFunction.js +2 -6
  230. package/lib/schemas/metadata/MetadataFunction.js.map +1 -1
  231. package/lib/schemas/metadata/MetadataFunction.mjs +2 -6
  232. package/lib/schemas/metadata/MetadataObject.d.mts +1 -3
  233. package/lib/schemas/metadata/MetadataObject.d.ts +1 -3
  234. package/lib/schemas/metadata/MetadataObject.js +1 -3
  235. package/lib/schemas/metadata/MetadataObject.js.map +1 -1
  236. package/lib/schemas/metadata/MetadataObject.mjs +1 -3
  237. package/lib/schemas/metadata/MetadataObjectType.d.mts +1 -3
  238. package/lib/schemas/metadata/MetadataObjectType.d.ts +1 -3
  239. package/lib/schemas/metadata/MetadataObjectType.js +5 -15
  240. package/lib/schemas/metadata/MetadataObjectType.js.map +1 -1
  241. package/lib/schemas/metadata/MetadataObjectType.mjs +6 -18
  242. package/lib/schemas/metadata/MetadataParameter.js +1 -3
  243. package/lib/schemas/metadata/MetadataParameter.js.map +1 -1
  244. package/lib/schemas/metadata/MetadataParameter.mjs +1 -3
  245. package/lib/schemas/metadata/MetadataProperty.d.mts +1 -3
  246. package/lib/schemas/metadata/MetadataProperty.d.ts +1 -3
  247. package/lib/schemas/metadata/MetadataProperty.js +3 -9
  248. package/lib/schemas/metadata/MetadataProperty.js.map +1 -1
  249. package/lib/schemas/metadata/MetadataProperty.mjs +3 -9
  250. package/lib/schemas/metadata/MetadataTemplate.js +1 -3
  251. package/lib/schemas/metadata/MetadataTemplate.js.map +1 -1
  252. package/lib/schemas/metadata/MetadataTemplate.mjs +1 -3
  253. package/lib/schemas/metadata/MetadataTuple.d.mts +1 -3
  254. package/lib/schemas/metadata/MetadataTuple.d.ts +1 -3
  255. package/lib/schemas/metadata/MetadataTuple.js +2 -6
  256. package/lib/schemas/metadata/MetadataTuple.js.map +1 -1
  257. package/lib/schemas/metadata/MetadataTuple.mjs +2 -6
  258. package/lib/schemas/metadata/MetadataTupleType.js +2 -6
  259. package/lib/schemas/metadata/MetadataTupleType.js.map +1 -1
  260. package/lib/schemas/metadata/MetadataTupleType.mjs +3 -9
  261. package/lib/tags/Constant.d.mts +16 -16
  262. package/lib/tags/Constant.d.ts +16 -16
  263. package/lib/tags/ContentMediaType.d.mts +7 -7
  264. package/lib/tags/ContentMediaType.d.ts +7 -7
  265. package/lib/tags/Default.d.mts +19 -19
  266. package/lib/tags/Default.d.ts +19 -19
  267. package/lib/tags/Example.d.mts +18 -18
  268. package/lib/tags/Example.d.ts +18 -18
  269. package/lib/tags/Examples.d.mts +23 -23
  270. package/lib/tags/Examples.d.ts +23 -23
  271. package/lib/tags/ExclusiveMaximum.d.mts +8 -5
  272. package/lib/tags/ExclusiveMaximum.d.ts +8 -5
  273. package/lib/tags/ExclusiveMinimum.d.mts +8 -5
  274. package/lib/tags/ExclusiveMinimum.d.ts +8 -5
  275. package/lib/tags/Format.d.mts +12 -8
  276. package/lib/tags/Format.d.ts +12 -8
  277. package/lib/tags/JsonSchemaPlugin.d.mts +20 -18
  278. package/lib/tags/JsonSchemaPlugin.d.ts +20 -18
  279. package/lib/tags/MaxItems.d.mts +9 -9
  280. package/lib/tags/MaxItems.d.ts +9 -9
  281. package/lib/tags/MaxLength.d.mts +6 -5
  282. package/lib/tags/MaxLength.d.ts +6 -5
  283. package/lib/tags/Maximum.d.mts +9 -7
  284. package/lib/tags/Maximum.d.ts +9 -7
  285. package/lib/tags/MinItems.d.mts +9 -9
  286. package/lib/tags/MinItems.d.ts +9 -9
  287. package/lib/tags/MinLength.d.mts +6 -5
  288. package/lib/tags/MinLength.d.ts +6 -5
  289. package/lib/tags/Minimum.d.mts +9 -7
  290. package/lib/tags/Minimum.d.ts +9 -7
  291. package/lib/tags/MultipleOf.d.mts +10 -7
  292. package/lib/tags/MultipleOf.d.ts +10 -7
  293. package/lib/tags/Pattern.d.mts +7 -4
  294. package/lib/tags/Pattern.d.ts +7 -4
  295. package/lib/tags/Sequence.d.mts +19 -17
  296. package/lib/tags/Sequence.d.ts +19 -17
  297. package/lib/tags/TagBase.d.mts +21 -28
  298. package/lib/tags/TagBase.d.ts +21 -28
  299. package/lib/tags/Type.d.mts +12 -11
  300. package/lib/tags/Type.d.ts +12 -11
  301. package/lib/tags/UniqueItems.d.mts +10 -9
  302. package/lib/tags/UniqueItems.d.ts +10 -9
  303. package/lib/tags/internal/FormatCheatSheet.d.mts +1 -3
  304. package/lib/tags/internal/FormatCheatSheet.d.ts +1 -3
  305. package/lib/tags/internal/FormatCheatSheet.js +1 -3
  306. package/lib/tags/internal/FormatCheatSheet.js.map +1 -1
  307. package/lib/tags/internal/FormatCheatSheet.mjs +1 -3
  308. package/lib/transformers/ITransformOptions.d.mts +27 -19
  309. package/lib/transformers/ITransformOptions.d.ts +27 -19
  310. package/lib/transformers/ImportTransformer.js +5 -10
  311. package/lib/transformers/ImportTransformer.js.map +1 -1
  312. package/lib/transformers/ImportTransformer.mjs +5 -10
  313. package/lib/transformers/NoTransformConfigurationError.js +1 -3
  314. package/lib/transformers/NoTransformConfigurationError.js.map +1 -1
  315. package/lib/transformers/NoTransformConfigurationError.mjs +1 -3
  316. package/lib/transformers/features/llm/LlmApplicationTransformer.js +1 -3
  317. package/lib/transformers/features/llm/LlmApplicationTransformer.js.map +1 -1
  318. package/lib/transformers/features/llm/LlmApplicationTransformer.mjs +1 -3
  319. package/lib/typings/Equal.d.mts +6 -6
  320. package/lib/typings/Equal.d.ts +6 -6
  321. package/package.json +121 -120
  322. package/src/AssertionGuard.ts +41 -39
  323. package/src/CamelCase.ts +75 -75
  324. package/src/IRandomGenerator.ts +337 -335
  325. package/src/IReadableURLSearchParams.ts +9 -9
  326. package/src/PascalCase.ts +71 -71
  327. package/src/Primitive.ts +90 -92
  328. package/src/Resolved.ts +72 -74
  329. package/src/SnakeCase.ts +127 -126
  330. package/src/TypeGuardError.ts +216 -179
  331. package/src/factories/MetadataCollection.ts +270 -278
  332. package/src/factories/MetadataCommentTagFactory.ts +632 -648
  333. package/src/factories/MetadataFactory.ts +402 -404
  334. package/src/factories/ProtobufFactory.ts +873 -875
  335. package/src/functional.ts +705 -740
  336. package/src/http.ts +972 -1040
  337. package/src/internal/_ProtobufReader.ts +188 -194
  338. package/src/internal/_ProtobufSizer.ts +137 -145
  339. package/src/internal/_ProtobufWriter.ts +135 -145
  340. package/src/internal/_jsonStringifyString.ts +42 -42
  341. package/src/json.ts +643 -696
  342. package/src/llm.ts +615 -540
  343. package/src/misc.ts +594 -651
  344. package/src/module.ts +889 -937
  345. package/src/notations.ts +751 -820
  346. package/src/programmers/FeatureProgrammer.ts +605 -627
  347. package/src/programmers/ImportProgrammer.ts +179 -185
  348. package/src/programmers/RandomProgrammer.ts +1195 -1190
  349. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  350. package/src/programmers/internal/check_array_length.ts +43 -47
  351. package/src/programmers/internal/check_bigint.ts +46 -50
  352. package/src/programmers/internal/check_dynamic_key.ts +197 -201
  353. package/src/programmers/internal/check_dynamic_properties.ts +231 -237
  354. package/src/programmers/internal/check_everything.ts +21 -23
  355. package/src/programmers/internal/check_native.ts +23 -27
  356. package/src/programmers/internal/check_number.ts +108 -112
  357. package/src/programmers/internal/check_object.ts +72 -78
  358. package/src/programmers/internal/check_string.ts +46 -50
  359. package/src/programmers/internal/check_template.ts +46 -48
  360. package/src/programmers/internal/check_union_array_like.ts +331 -335
  361. package/src/programmers/internal/decode_union_object.ts +110 -116
  362. package/src/programmers/internal/feature_object_entries.ts +59 -61
  363. package/src/programmers/internal/json_schema_escaped.ts +78 -82
  364. package/src/programmers/internal/json_schema_object.ts +150 -158
  365. package/src/programmers/internal/json_schema_station.ts +2 -2
  366. package/src/programmers/internal/metadata_to_pattern.ts +40 -42
  367. package/src/programmers/internal/postfix_of_tuple.ts +3 -5
  368. package/src/programmers/internal/prune_object_properties.ts +69 -71
  369. package/src/programmers/internal/stringify_dynamic_properties.ts +158 -162
  370. package/src/programmers/internal/stringify_native.ts +5 -7
  371. package/src/programmers/internal/stringify_regular_properties.ts +77 -81
  372. package/src/programmers/internal/template_to_pattern.ts +21 -23
  373. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -23
  374. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  375. package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
  376. package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
  377. package/src/protobuf.ts +820 -861
  378. package/src/reflect.ts +46 -50
  379. package/src/schemas/json/IJsonApplication.ts +77 -77
  380. package/src/schemas/json/IJsonSchemaCollection.ts +212 -195
  381. package/src/schemas/json/IJsonSchemaUnit.ts +263 -250
  382. package/src/schemas/metadata/IMetadataTypeTag.ts +14 -16
  383. package/src/schemas/metadata/Metadata.ts +669 -687
  384. package/src/schemas/metadata/MetadataAliasType.ts +57 -63
  385. package/src/schemas/metadata/MetadataApplication.ts +40 -44
  386. package/src/schemas/metadata/MetadataArray.ts +47 -49
  387. package/src/schemas/metadata/MetadataArrayType.ts +51 -57
  388. package/src/schemas/metadata/MetadataAtomic.ts +85 -87
  389. package/src/schemas/metadata/MetadataEscaped.ts +45 -51
  390. package/src/schemas/metadata/MetadataFunction.ts +45 -49
  391. package/src/schemas/metadata/MetadataObject.ts +46 -48
  392. package/src/schemas/metadata/MetadataObjectType.ts +137 -149
  393. package/src/schemas/metadata/MetadataParameter.ts +52 -54
  394. package/src/schemas/metadata/MetadataProperty.ts +53 -59
  395. package/src/schemas/metadata/MetadataTemplate.ts +78 -80
  396. package/src/schemas/metadata/MetadataTuple.ts +28 -32
  397. package/src/schemas/metadata/MetadataTupleType.ts +61 -67
  398. package/src/tags/Constant.ts +47 -47
  399. package/src/tags/ContentMediaType.ts +27 -27
  400. package/src/tags/Default.ts +52 -52
  401. package/src/tags/Example.ts +56 -56
  402. package/src/tags/Examples.ts +56 -56
  403. package/src/tags/ExclusiveMaximum.ts +44 -41
  404. package/src/tags/ExclusiveMinimum.ts +44 -41
  405. package/src/tags/Format.ts +78 -74
  406. package/src/tags/JsonSchemaPlugin.ts +36 -34
  407. package/src/tags/MaxItems.ts +31 -31
  408. package/src/tags/MaxLength.ts +25 -24
  409. package/src/tags/Maximum.ts +39 -37
  410. package/src/tags/MinItems.ts +31 -31
  411. package/src/tags/MinLength.ts +25 -24
  412. package/src/tags/Minimum.ts +39 -37
  413. package/src/tags/MultipleOf.ts +42 -39
  414. package/src/tags/Pattern.ts +49 -46
  415. package/src/tags/Sequence.ts +37 -35
  416. package/src/tags/TagBase.ts +102 -109
  417. package/src/tags/Type.ts +64 -63
  418. package/src/tags/UniqueItems.ts +34 -33
  419. package/src/tags/internal/FormatCheatSheet.ts +71 -73
  420. package/src/transformers/ITransformOptions.ts +70 -62
  421. package/src/transformers/ImportTransformer.ts +253 -258
  422. package/src/transformers/NoTransformConfigurationError.ts +16 -18
  423. package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -226
  424. package/src/typings/Equal.ts +18 -18
@@ -1,875 +1,873 @@
1
- import ts from "typescript";
2
-
3
- import { IMetadataTypeTag } from "../schemas/metadata/IMetadataTypeTag";
4
- import { Metadata } from "../schemas/metadata/Metadata";
5
- import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
6
- import { MetadataProperty } from "../schemas/metadata/MetadataProperty";
7
- import { IProtobufProperty } from "../schemas/protobuf/IProtobufProperty";
8
- import { IProtobufPropertyType } from "../schemas/protobuf/IProtobufPropertyType";
9
- import { IProtobufSchema } from "../schemas/protobuf/IProtobufSchema";
10
-
11
- import { ProtobufUtil } from "../programmers/helpers/ProtobufUtil";
12
-
13
- import { TransformerError } from "../transformers/TransformerError";
14
-
15
- import { ProtobufAtomic } from "../typings/ProtobufAtomic";
16
- import { ValidationPipe } from "../typings/ValidationPipe";
17
-
18
- import { MetadataCollection } from "./MetadataCollection";
19
- import { MetadataFactory } from "./MetadataFactory";
20
-
21
- export namespace ProtobufFactory {
22
- export interface IProps {
23
- method: string;
24
- checker: ts.TypeChecker;
25
- transformer?: ts.TransformationContext;
26
- collection: MetadataCollection;
27
- type: ts.Type;
28
- }
29
-
30
- /* -----------------------------------------------------------
31
- METADATA COMPOSER
32
- ----------------------------------------------------------- */
33
- export const metadata = (props: IProps): Metadata => {
34
- // COMPOSE METADATA WITH INDIVIDUAL VALIDATIONS
35
- const result: ValidationPipe<Metadata, MetadataFactory.IError> =
36
- MetadataFactory.analyze({
37
- ...props,
38
- transformer: props.transformer,
39
- options: {
40
- escape: false,
41
- constant: true,
42
- absorb: true,
43
- validate: validate(),
44
- },
45
- });
46
- if (result.success === false)
47
- throw TransformerError.from({
48
- code: `typia.protobuf.${props.method}`,
49
- errors: result.errors,
50
- });
51
- return result.data;
52
- };
53
-
54
- /**
55
- * @internal
56
- */
57
- export const emplaceObject = (object: MetadataObjectType): void => {
58
- for (const p of object.properties) emplaceProperty(p);
59
- const properties: IProtobufProperty[] = object.properties
60
- .map((p) => p.of_protobuf_)
61
- .filter((p) => p !== undefined);
62
- const unique: Set<number> = new Set(
63
- properties
64
- .filter((p) => p !== undefined)
65
- .filter((p) => p.fixed === true)
66
- .map((p) => p.union.map((u) => u.index))
67
- .flat(),
68
- );
69
- let index: number = 1;
70
- properties.forEach((schema) => {
71
- if (schema.fixed === true)
72
- index = Math.max(
73
- index,
74
- Math.max(...schema.union.map((u) => u.index)) + 1,
75
- );
76
- else {
77
- for (const u of schema.union) {
78
- while (unique.has(index) === true) ++index;
79
- u.index = index;
80
- unique.add(index);
81
- }
82
- ++index;
83
- }
84
- });
85
- };
86
-
87
- const emplaceProperty = (prop: MetadataProperty): void => {
88
- const union: IProtobufPropertyType[] = [];
89
- for (const native of prop.value.natives)
90
- if (native.name === "Uint8Array")
91
- union.push({
92
- type: "bytes",
93
- index: ProtobufUtil.getSequence(native.tags[0] ?? [])!,
94
- });
95
- union.push(...emplaceAtomic(prop.value).values());
96
- for (const array of prop.value.arrays)
97
- union.push({
98
- type: "array",
99
- array: array.type,
100
- value: emplaceSchema(
101
- array.type.value,
102
- ) as IProtobufSchema.IArray["value"],
103
- index: ProtobufUtil.getSequence(array.tags[0] ?? [])!,
104
- });
105
- for (const obj of prop.value.objects)
106
- if (isDynamicObject(obj.type))
107
- union.push({
108
- type: "map",
109
- map: obj.type,
110
- key: emplaceSchema(
111
- obj.type.properties[0]!.key,
112
- ) as IProtobufSchema.IMap["key"],
113
- value: emplaceSchema(
114
- obj.type.properties[0]!.value,
115
- ) as IProtobufSchema.IMap["value"],
116
- index: ProtobufUtil.getSequence(obj.tags[0] ?? [])!,
117
- });
118
- else
119
- union.push({
120
- type: "object",
121
- object: obj.type,
122
- index: ProtobufUtil.getSequence(obj.tags[0] ?? [])!,
123
- });
124
- for (const map of prop.value.maps)
125
- union.push({
126
- type: "map",
127
- map,
128
- key: emplaceSchema(map.key) as IProtobufSchema.IMap["key"],
129
- value: emplaceSchema(map.value) as IProtobufSchema.IMap["value"],
130
- index: ProtobufUtil.getSequence(map.tags[0] ?? [])!,
131
- });
132
- prop.of_protobuf_ = {
133
- union,
134
- fixed: union.every((p) => p.index !== null),
135
- };
136
- };
137
-
138
- const emplaceSchema = (metadata: Metadata): IProtobufSchema => {
139
- for (const native of metadata.natives)
140
- if (native.name === "Uint8Array")
141
- return {
142
- type: "bytes",
143
- };
144
- const atomic = emplaceAtomic(metadata);
145
- if (atomic.size) return atomic.values().next().value!;
146
- for (const array of metadata.arrays)
147
- return {
148
- type: "array",
149
- array: array.type,
150
- value: emplaceSchema(
151
- array.type.value,
152
- ) as IProtobufSchema.IArray["value"],
153
- };
154
- for (const obj of metadata.objects)
155
- if (isDynamicObject(obj.type))
156
- return {
157
- type: "map",
158
- map: obj.type,
159
- key: emplaceSchema(
160
- obj.type.properties[0]!.key,
161
- ) as IProtobufSchema.IMap["key"],
162
- value: emplaceSchema(
163
- obj.type.properties[0]!.value,
164
- ) as IProtobufSchema.IMap["value"],
165
- };
166
- else
167
- return {
168
- type: "object",
169
- object: obj.type,
170
- };
171
- for (const map of metadata.maps)
172
- return {
173
- type: "map",
174
- map,
175
- key: emplaceSchema(map.key) as IProtobufSchema.IMap["key"],
176
- value: emplaceSchema(map.value) as IProtobufSchema.IMap["value"],
177
- };
178
- throw new Error(
179
- "Error on ProtobufFactory.emplaceSchema(): any type detected.",
180
- );
181
- };
182
-
183
- const emplaceAtomic = (
184
- meta: Metadata,
185
- ): Map<ProtobufAtomic, IProtobufPropertyType> => {
186
- const map: Map<ProtobufAtomic, IProtobufPropertyType> = new Map();
187
-
188
- // CONSTANTS
189
- for (const c of meta.constants)
190
- if (c.type === "boolean")
191
- map.set("bool", {
192
- type: "bool",
193
- index: getSequence(c.values[0]?.tags[0] ?? [])!,
194
- });
195
- else if (c.type === "bigint") {
196
- const init: ProtobufAtomic.BigNumeric = getBigintType(
197
- c.values.map((v) => BigInt(v.value)),
198
- );
199
- for (const value of c.values)
200
- emplaceBigint({
201
- map,
202
- tags: value.tags,
203
- init,
204
- });
205
- } else if (c.type === "number") {
206
- const init: ProtobufAtomic.Numeric = getNumberType(
207
- c.values.map((v) => v.value) as number[],
208
- );
209
- for (const value of c.values)
210
- emplaceNumber({
211
- map,
212
- tags: value.tags,
213
- init,
214
- });
215
- } else if (c.type === "string")
216
- map.set("string", {
217
- type: "string",
218
- index: getSequence(c.values[0]?.tags[0] ?? [])!,
219
- });
220
-
221
- // TEMPLATE
222
- if (meta.templates.length)
223
- map.set("string", {
224
- type: "string",
225
- index: getSequence(meta.templates[0]?.tags[0] ?? [])!,
226
- });
227
-
228
- // ATOMICS
229
- for (const atomic of meta.atomics)
230
- if (atomic.type === "boolean")
231
- map.set("bool", {
232
- type: "bool",
233
- index: getSequence(atomic.tags[0] ?? [])!,
234
- });
235
- else if (atomic.type === "bigint")
236
- emplaceBigint({
237
- map,
238
- tags: atomic.tags,
239
- init: "int64",
240
- });
241
- else if (atomic.type === "number")
242
- emplaceNumber({
243
- map,
244
- tags: atomic.tags,
245
- init: "double",
246
- });
247
- else if (atomic.type === "string")
248
- map.set("string", {
249
- type: "string",
250
- index: getSequence(atomic.tags[0] ?? [])!,
251
- });
252
-
253
- // SORTING FOR VALIDATION REASON
254
- return new Map(
255
- Array.from(map).sort((x, y) => ProtobufUtil.compare(x[0], y[0])),
256
- );
257
- };
258
-
259
- const emplaceBigint = (next: {
260
- map: Map<ProtobufAtomic, IProtobufPropertyType>;
261
- tags: IMetadataTypeTag[][];
262
- init: ProtobufAtomic.BigNumeric;
263
- }): void => {
264
- if (next.tags.length === 0) {
265
- next.map.set(next.init, {
266
- type: "bigint",
267
- name: next.init,
268
- index: null!,
269
- });
270
- return;
271
- }
272
- for (const row of next.tags) {
273
- const value: ProtobufAtomic.BigNumeric =
274
- row.find(
275
- (tag) =>
276
- tag.kind === "type" &&
277
- (tag.value === "int64" || tag.value === "uint64"),
278
- )?.value ?? next.init;
279
- next.map.set(next.init, {
280
- type: "bigint",
281
- name: value,
282
- index: ProtobufUtil.getSequence(row)!,
283
- });
284
- }
285
- };
286
-
287
- const emplaceNumber = (next: {
288
- map: Map<ProtobufAtomic, IProtobufPropertyType>;
289
- tags: IMetadataTypeTag[][];
290
- init: ProtobufAtomic.Numeric;
291
- }): void => {
292
- if (next.tags.length === 0) {
293
- next.map.set(next.init, {
294
- type: "number",
295
- name: next.init,
296
- index: null!,
297
- });
298
- return;
299
- }
300
- for (const row of next.tags) {
301
- const value: ProtobufAtomic.Numeric =
302
- row.find(
303
- (tag) =>
304
- tag.kind === "type" &&
305
- (tag.value === "int32" ||
306
- tag.value === "uint32" ||
307
- tag.value === "int64" ||
308
- tag.value === "uint64" ||
309
- tag.value === "float" ||
310
- tag.value === "double"),
311
- )?.value ?? next.init;
312
- next.map.set(value, {
313
- type: "number",
314
- name: value,
315
- index: ProtobufUtil.getSequence(row)!,
316
- });
317
- }
318
- };
319
-
320
- const getBigintType = (values: bigint[]): ProtobufAtomic.BigNumeric =>
321
- values.some((v) => v < 0) ? "int64" : "uint64";
322
-
323
- const getNumberType = (values: number[]): ProtobufAtomic.Numeric =>
324
- values.every((v) => Math.floor(v) === v)
325
- ? values.every((v) => -2147483648 <= v && v <= 2147483647)
326
- ? "int32"
327
- : "int64"
328
- : "double";
329
-
330
- const getSequence = (tags: IMetadataTypeTag[]): number | null => {
331
- const sequence = tags.find(
332
- (t) =>
333
- t.kind === "sequence" &&
334
- typeof (t.schema as any)?.["x-protobuf-sequence"] === "number",
335
- );
336
- if (sequence === undefined) return null;
337
- const value: number = Number(
338
- (sequence.schema as any)["x-protobuf-sequence"],
339
- );
340
- return Number.isNaN(value) ? null : value;
341
- };
342
-
343
- /* -----------------------------------------------------------
344
- VALIDATORS
345
- ----------------------------------------------------------- */
346
- const validate = () => {
347
- const visited: WeakSet<MetadataObjectType> = new WeakSet();
348
- return (meta: Metadata, explore: MetadataFactory.IExplore): string[] => {
349
- const errors: string[] = [];
350
- const insert = (msg: string) => errors.push(msg);
351
-
352
- if (explore.top === true) {
353
- const onlyObject: boolean =
354
- meta.size() === 1 &&
355
- meta.objects.length === 1 &&
356
- meta.objects[0]!.type.properties.every((p) =>
357
- p.key.isSoleLiteral(),
358
- ) &&
359
- meta.isRequired() === true &&
360
- meta.nullable === false;
361
- if (onlyObject === false)
362
- insert("target type must be a sole and static object type");
363
- }
364
- for (const obj of meta.objects) {
365
- if (visited.has(obj.type)) continue;
366
- visited.add(obj.type);
367
- validateObject({
368
- object: obj.type,
369
- errors,
370
- });
371
- try {
372
- emplaceObject(obj.type);
373
- } catch {}
374
- }
375
-
376
- //----
377
- // NOT SUPPORTED TYPES
378
- //----
379
- const noSupport = (msg: string) => insert(`does not support ${msg}`);
380
-
381
- // PROHIBIT ANY TYPE
382
- if (meta.any) noSupport("any type");
383
- // PROHIBIT FUNCTIONAL TYPE
384
- if (meta.functions.length) noSupport("functional type");
385
- // PROHIBIT TUPLE TYPE
386
- if (meta.tuples.length) noSupport("tuple type");
387
- // PROHIBIT SET TYPE
388
- if (meta.sets.length) noSupport("Set type");
389
- // NATIVE TYPE, BUT NOT Uint8Array
390
- if (meta.natives.length)
391
- for (const native of meta.natives) {
392
- if (native.name === "Uint8Array") continue;
393
-
394
- const instead = BANNED_NATIVE_TYPES.get(native.name);
395
- if (instead === undefined) noSupport(`${native.name} type`);
396
- else noSupport(`${native.name} type. Use ${instead} type instead.`);
397
- }
398
- //----
399
- // ATOMIC CASES
400
- //----
401
- if (meta.atomics.length) {
402
- const numbers = ProtobufUtil.getNumbers(meta);
403
- const bigints = ProtobufUtil.getBigints(meta);
404
-
405
- for (const type of ["int64", "uint64"])
406
- if (numbers.has(type) && bigints.has(type))
407
- insert(
408
- `tags.Type<"${type}"> cannot be used in both number and bigint types. Recommend to remove from number type`,
409
- );
410
- }
411
- //----
412
- // ARRAY CASES
413
- //----
414
- // DO NOT ALLOW MULTI-DIMENSIONAL ARRAY
415
- if (
416
- meta.arrays.length &&
417
- meta.arrays.some((array) => !!array.type.value.arrays.length)
418
- )
419
- noSupport("over two dimensional array type");
420
- // CHILD OF ARRAY TYPE MUST BE REQUIRED
421
- if (
422
- meta.arrays.length &&
423
- meta.arrays.some(
424
- (array) =>
425
- array.type.value.isRequired() === false ||
426
- array.type.value.nullable === true,
427
- )
428
- )
429
- noSupport("optional type in array");
430
- // UNION IN ARRAY
431
- if (
432
- meta.arrays.length &&
433
- meta.arrays.some(
434
- (a) =>
435
- a.type.value.size() > 1 &&
436
- a.type.value.constants.length !== 1 &&
437
- a.type.value.constants[0]?.values.length !== a.type.value.size(),
438
- )
439
- )
440
- noSupport("union type in array");
441
- // DO DYNAMIC OBJECT IN ARRAY
442
- if (
443
- meta.arrays.length &&
444
- meta.arrays.some(
445
- (a) =>
446
- a.type.value.maps.length ||
447
- (a.type.value.objects.length &&
448
- a.type.value.objects.some(
449
- (o) => ProtobufUtil.isStaticObject(o.type) === false,
450
- )),
451
- )
452
- )
453
- noSupport("dynamic object in array");
454
- // UNION WITH ARRAY
455
- if (meta.size() > 1 && meta.arrays.length)
456
- noSupport("union type with array type");
457
- //----
458
- // OBJECT CASES
459
- //----
460
- // EMPTY PROPERTY
461
- if (
462
- meta.objects.length &&
463
- meta.objects.some((obj) => obj.type.properties.length === 0)
464
- )
465
- noSupport("empty object type");
466
- // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
467
- if (
468
- meta.objects.length &&
469
- meta.objects.some(
470
- (obj) =>
471
- obj.type.properties.filter((p) => !p.key.isSoleLiteral()).length >
472
- 1,
473
- )
474
- )
475
- noSupport(
476
- "object type with multiple dynamic key typed properties. Keep only one.",
477
- );
478
- // STATIC AND DYNAMIC PROPERTIES ARE COMPATIBLE
479
- if (
480
- meta.objects.length &&
481
- meta.objects.some(
482
- (obj) =>
483
- obj.type.properties.some((p) => p.key.isSoleLiteral()) &&
484
- obj.type.properties.some((p) => !p.key.isSoleLiteral()),
485
- )
486
- )
487
- noSupport(
488
- "object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
489
- );
490
- // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
491
- if (
492
- meta.objects.length &&
493
- isDynamicObject(meta.objects[0]!.type) &&
494
- meta.objects[0]!.type.properties.some((p) => !!p.value.arrays.length)
495
- )
496
- noSupport("dynamic object with array value type");
497
- // UNION WITH DYNAMIC OBJECTTa
498
- if (
499
- meta.size() > 1 &&
500
- meta.objects.length &&
501
- isDynamicObject(meta.objects[0]!.type)
502
- )
503
- noSupport("union type with dynamic object type");
504
- // UNION IN DYNAMIC PROPERTY VALUE
505
- if (
506
- meta.objects.length &&
507
- meta.objects.some(
508
- (obj) =>
509
- isDynamicObject(obj.type) &&
510
- obj.type.properties.some((p) => ProtobufUtil.isUnion(p.value)),
511
- )
512
- )
513
- noSupport("union type in dynamic property");
514
- //----
515
- // MAP CASES
516
- //----
517
- // KEY TYPE IS UNION
518
- if (
519
- meta.maps.length &&
520
- meta.maps.some((m) => ProtobufUtil.isUnion(m.key))
521
- )
522
- noSupport("union key typed map");
523
- // KEY TYPE IS NOT ATOMIC
524
- if (
525
- meta.maps.length &&
526
- meta.maps.some((m) => ProtobufUtil.getAtomics(m.key).size !== 1)
527
- )
528
- noSupport("non-atomic key typed map");
529
- // MAP TYPE, BUT PROPERTY KEY TYPE IS OPTIONAL
530
- if (
531
- meta.maps.length &&
532
- meta.maps.some((m) => m.key.isRequired() === false || m.key.nullable)
533
- )
534
- noSupport("optional key typed map");
535
- // MAP TYPE, BUT VALUE TYPE IS ARRAY
536
- if (meta.maps.length && meta.maps.some((m) => !!m.value.arrays.length))
537
- noSupport("map type with array value type");
538
- // UNION WITH MAP
539
- if (meta.size() > 1 && meta.maps.length)
540
- noSupport("union type with map type");
541
- // UNION IN MAP
542
- if (
543
- meta.maps.length &&
544
- meta.maps.some((m) => ProtobufUtil.isUnion(m.value))
545
- )
546
- noSupport("union type in map value type");
547
- return errors;
548
- };
549
- };
550
-
551
- /* -----------------------------------------------------------
552
- SEQUENE VALIDATOR
553
- ----------------------------------------------------------- */
554
- const validateObject = (next: {
555
- object: MetadataObjectType;
556
- errors: string[];
557
- }): void => {
558
- for (const property of next.object.properties)
559
- validateProperty({
560
- metadata: property.value,
561
- errors: next.errors,
562
- });
563
-
564
- const entire: Map<number, string> = new Map();
565
- const visitProperty = (p: MetadataProperty) => {
566
- const local: Set<number> = new Set();
567
- const tagger = (matrix: IMetadataTypeTag[][]): void => {
568
- matrix.forEach((tags) => {
569
- const value: number | null = ProtobufUtil.getSequence(tags);
570
- if (value !== null) local.add(value);
571
- });
572
- };
573
- for (const c of p.value.constants)
574
- for (const v of c.values) tagger(v.tags);
575
- for (const a of p.value.atomics) tagger(a.tags);
576
- for (const t of p.value.templates) tagger(t.tags);
577
- for (const o of p.value.objects) tagger(o.tags);
578
- for (const a of p.value.arrays) tagger(a.tags);
579
- for (const s of local)
580
- if (entire.has(s))
581
- next.errors.push(
582
- `The Sequence<${s}> tag is duplicated in two properties (${JSON.stringify(entire.get(s))} and ${JSON.stringify(p.key.getSoleLiteral())})`,
583
- );
584
- else entire.set(s, p.key.getSoleLiteral()!);
585
- };
586
- for (const p of next.object.properties) visitProperty(p);
587
- };
588
-
589
- const validateProperty = (next: {
590
- metadata: Metadata;
591
- errors: string[];
592
- }): void => {
593
- let expected: number = 0;
594
- const sequences: Set<number> = new Set();
595
- const add = (value: number): boolean => {
596
- if (sequences.has(value)) return false;
597
- sequences.add(value);
598
- ++expected;
599
- return true;
600
- };
601
-
602
- for (const validator of [
603
- validateBooleanSequence,
604
- validateNumericSequences({
605
- type: "bigint",
606
- default: "int64",
607
- categories: BIGINT_TYPES,
608
- }),
609
- validateNumericSequences({
610
- type: "number",
611
- default: "double",
612
- categories: NUMBER_TYPES,
613
- }),
614
- validateStringSequence,
615
- ])
616
- validator({ metadata: next.metadata, errors: next.errors, add });
617
- for (const array of next.metadata.arrays)
618
- validateInstanceSequence({
619
- type: "array",
620
- tags: array.tags,
621
- errors: next.errors,
622
- add,
623
- });
624
- for (const object of next.metadata.objects)
625
- validateInstanceSequence({
626
- type: "object",
627
- tags: object.tags,
628
- errors: next.errors,
629
- add,
630
- });
631
- for (const map of next.metadata.maps)
632
- validateInstanceSequence({
633
- type: "map",
634
- tags: map.tags,
635
- errors: next.errors,
636
- add,
637
- });
638
- for (const native of next.metadata.natives)
639
- if (native.name === "Uint8Array")
640
- validateInstanceSequence({
641
- type: "Uint8Array",
642
- tags: native.tags,
643
- errors: next.errors,
644
- add,
645
- });
646
- };
647
-
648
- const validateBooleanSequence = (next: {
649
- metadata: Metadata;
650
- errors: string[];
651
- add: (value: number) => boolean;
652
- }): void => {
653
- // PREPARE EMPLACER
654
- const unique: Set<number> = new Set();
655
- let expected: number = 0;
656
- let actual: number = 0;
657
- const emplace = (matrix: IMetadataTypeTag[][]): void => {
658
- for (const tags of matrix)
659
- for (const tag of tags) {
660
- const sequence = ProtobufUtil.getSequence([tag]);
661
- if (sequence !== null) {
662
- unique.add(sequence);
663
- ++actual;
664
- }
665
- ++expected;
666
- }
667
- };
668
-
669
- // GATHER SEQUENCE TAGS
670
- for (const atomic of next.metadata.atomics)
671
- if (atomic.type === "boolean") emplace(atomic.tags);
672
- for (const constant of next.metadata.constants)
673
- if (constant.type === "boolean")
674
- for (const value of constant.values) emplace(value.tags);
675
-
676
- // PREDICATE
677
- if (unique.size && actual !== expected)
678
- next.errors.push(
679
- `The sequence tag must be declared in every union type members`,
680
- );
681
- else if (unique.size > 1)
682
- next.errors.push(
683
- `The sequence tag value must be the same in boolean type (including literal types)`,
684
- );
685
- else if (unique.size === 1) {
686
- const value: number = unique.values().next().value!;
687
- if (next.add(value) === false)
688
- next.errors.push(
689
- `The sequence tag value ${value} in boolean type is duplicated with other types`,
690
- );
691
- }
692
- };
693
-
694
- const validateNumericSequences =
695
- (config: {
696
- type: "number" | "bigint";
697
- default: string;
698
- categories: Set<string>;
699
- }) =>
700
- (next: {
701
- metadata: Metadata;
702
- errors: string[];
703
- add: (value: number) => boolean;
704
- }): void => {
705
- // FIND TYPE CATEGORIES
706
- const categories: Set<string> = new Set();
707
- const getType = (tags: IMetadataTypeTag[]): string => {
708
- const found: IMetadataTypeTag | undefined = tags.find(
709
- (t) => t.kind === "type" && config.categories.has(t.value),
710
- );
711
- return found?.value ?? config.default;
712
- };
713
- const exploreCategory = (matrix: IMetadataTypeTag[][]): void => {
714
- for (const tags of matrix) categories.add(getType(tags));
715
- };
716
- for (const atomic of next.metadata.atomics)
717
- if (atomic.type === config.type) exploreCategory(atomic.tags);
718
- for (const constant of next.metadata.constants)
719
- if (constant.type === config.type)
720
- for (const value of constant.values) exploreCategory(value.tags);
721
-
722
- // ITERATE TYPE CATEGORIES
723
- for (const category of categories) {
724
- const unique: Set<number> = new Set();
725
- let expected: number = 0;
726
- let actual: number = 0;
727
- const emplace = (tags: IMetadataTypeTag[]): void => {
728
- const sequence: number | null = ProtobufUtil.getSequence(tags);
729
- if (sequence !== null) {
730
- unique.add(sequence);
731
- ++actual;
732
- }
733
- ++expected;
734
- };
735
-
736
- for (const atomic of next.metadata.atomics)
737
- if (atomic.type === config.type)
738
- for (const tags of atomic.tags)
739
- if (getType(tags) === category) emplace(tags);
740
- for (const constant of next.metadata.constants)
741
- if (constant.type === config.type)
742
- for (const value of constant.values)
743
- for (const tags of value.tags)
744
- if (getType(tags) === category) emplace(tags);
745
-
746
- if (unique.size && actual !== expected) {
747
- next.errors.push(
748
- `The sequence tag must be declared in every union type members`,
749
- );
750
- } else if (unique.size > 1)
751
- next.errors.push(
752
- `The sequence tag value must be the same in ${config.type} type (including literal types)`,
753
- );
754
- else if (unique.size === 1) {
755
- const value: number = unique.values().next().value!;
756
- if (next.add(value) === false)
757
- next.errors.push(
758
- `The sequence tag value ${value} in ${config.type} type is duplicated with other types`,
759
- );
760
- }
761
- }
762
- };
763
-
764
- const validateStringSequence = (next: {
765
- metadata: Metadata;
766
- errors: string[];
767
- add: (value: number) => boolean;
768
- }): void => {
769
- const unique: Set<number> = new Set();
770
- let expected: number = 0;
771
- let actual: number = 0;
772
- const emplace = (matrix: IMetadataTypeTag[][]): void => {
773
- for (const tags of matrix)
774
- for (const tag of tags) {
775
- const sequence = ProtobufUtil.getSequence([tag]);
776
- if (sequence !== null) {
777
- unique.add(sequence);
778
- ++actual;
779
- }
780
- ++expected;
781
- }
782
- };
783
- for (const atomic of next.metadata.atomics)
784
- if (atomic.type === "string") emplace(atomic.tags);
785
- for (const constant of next.metadata.constants)
786
- if (constant.type === "string")
787
- for (const value of constant.values) emplace(value.tags);
788
- for (const template of next.metadata.templates) emplace(template.tags);
789
-
790
- if (unique.size && actual !== expected)
791
- next.errors.push(
792
- `The sequence tag must be declared in every union type members`,
793
- );
794
- else if (unique.size > 1)
795
- next.errors.push(
796
- `The sequence tag value must be the same in string types including literal and template types`,
797
- );
798
- else if (unique.size === 1) {
799
- const value: number = unique.values().next().value!;
800
- if (next.add(value) === false)
801
- next.errors.push(
802
- `The sequence tag value ${value} in string type is duplicated with other types`,
803
- );
804
- }
805
- };
806
-
807
- const validateInstanceSequence = (next: {
808
- type: "array" | "object" | "map" | "Uint8Array";
809
- tags: IMetadataTypeTag[][];
810
- errors: string[];
811
- add: (value: number) => boolean;
812
- }): void => {
813
- const unique: Set<number> = new Set();
814
- let count: number = 0;
815
- for (const tags of next.tags) {
816
- const value: number | null = ProtobufUtil.getSequence(tags);
817
- if (value === null) continue;
818
- unique.add(value);
819
- ++count;
820
- }
821
- if (unique.size && count !== next.tags.length)
822
- next.errors.push(
823
- `The sequence tag must be declared in every union type members`,
824
- );
825
- else if (unique.size > 1)
826
- next.errors.push(
827
- `The sequence tag value must be the same in ${next.type === "array" ? "an array" : "object"} type.`,
828
- );
829
- else if (unique.size === 1) {
830
- const value: number = unique.values().next().value!;
831
- if (next.add(value) === false)
832
- next.errors.push(
833
- `The sequence tag value ${value} in ${next.type} type is duplicated with other types`,
834
- );
835
- }
836
- };
837
- }
838
-
839
- const isDynamicObject = (obj: MetadataObjectType): boolean =>
840
- obj.properties[0]!.key.isSoleLiteral() === false;
841
-
842
- const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
843
- ["Date", "string"],
844
- ["Boolean", "boolean"],
845
- ["BigInt", "bigint"],
846
- ["Number", "number"],
847
- ["String", "string"],
848
- ...[
849
- "Buffer",
850
- "Uint8ClampedArray",
851
- "Uint16Array",
852
- "Uint32Array",
853
- "BigUint64Array",
854
- "Int8Array",
855
- "Int16Array",
856
- "Int32Array",
857
- "BigInt64Array",
858
- "Float32Array",
859
- "Float64Array",
860
- "DataView",
861
- "ArrayBuffer",
862
- "SharedArrayBuffer",
863
- ].map((name) => [name, "Uint8Array"] as const),
864
- ["WeakSet", "Array"],
865
- ["WeakMap", "Map"],
866
- ]);
867
- const NUMBER_TYPES: Set<string> = new Set([
868
- "int32",
869
- "uint32",
870
- "int64",
871
- "uint64",
872
- "float",
873
- "double",
874
- ]);
875
- const BIGINT_TYPES = new Set(["int64", "uint64"]);
1
+ import ts from "typescript";
2
+
3
+ import { IMetadataTypeTag } from "../schemas/metadata/IMetadataTypeTag";
4
+ import { Metadata } from "../schemas/metadata/Metadata";
5
+ import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
6
+ import { MetadataProperty } from "../schemas/metadata/MetadataProperty";
7
+ import { IProtobufProperty } from "../schemas/protobuf/IProtobufProperty";
8
+ import { IProtobufPropertyType } from "../schemas/protobuf/IProtobufPropertyType";
9
+ import { IProtobufSchema } from "../schemas/protobuf/IProtobufSchema";
10
+
11
+ import { ProtobufUtil } from "../programmers/helpers/ProtobufUtil";
12
+
13
+ import { TransformerError } from "../transformers/TransformerError";
14
+
15
+ import { ProtobufAtomic } from "../typings/ProtobufAtomic";
16
+ import { ValidationPipe } from "../typings/ValidationPipe";
17
+
18
+ import { MetadataCollection } from "./MetadataCollection";
19
+ import { MetadataFactory } from "./MetadataFactory";
20
+
21
+ export namespace ProtobufFactory {
22
+ export interface IProps {
23
+ method: string;
24
+ checker: ts.TypeChecker;
25
+ transformer?: ts.TransformationContext;
26
+ collection: MetadataCollection;
27
+ type: ts.Type;
28
+ }
29
+
30
+ /* -----------------------------------------------------------
31
+ METADATA COMPOSER
32
+ ----------------------------------------------------------- */
33
+ export const metadata = (props: IProps): Metadata => {
34
+ // COMPOSE METADATA WITH INDIVIDUAL VALIDATIONS
35
+ const result: ValidationPipe<Metadata, MetadataFactory.IError> =
36
+ MetadataFactory.analyze({
37
+ ...props,
38
+ transformer: props.transformer,
39
+ options: {
40
+ escape: false,
41
+ constant: true,
42
+ absorb: true,
43
+ validate: validate(),
44
+ },
45
+ });
46
+ if (result.success === false)
47
+ throw TransformerError.from({
48
+ code: `typia.protobuf.${props.method}`,
49
+ errors: result.errors,
50
+ });
51
+ return result.data;
52
+ };
53
+
54
+ /** @internal */
55
+ export const emplaceObject = (object: MetadataObjectType): void => {
56
+ for (const p of object.properties) emplaceProperty(p);
57
+ const properties: IProtobufProperty[] = object.properties
58
+ .map((p) => p.of_protobuf_)
59
+ .filter((p) => p !== undefined);
60
+ const unique: Set<number> = new Set(
61
+ properties
62
+ .filter((p) => p !== undefined)
63
+ .filter((p) => p.fixed === true)
64
+ .map((p) => p.union.map((u) => u.index))
65
+ .flat(),
66
+ );
67
+ let index: number = 1;
68
+ properties.forEach((schema) => {
69
+ if (schema.fixed === true)
70
+ index = Math.max(
71
+ index,
72
+ Math.max(...schema.union.map((u) => u.index)) + 1,
73
+ );
74
+ else {
75
+ for (const u of schema.union) {
76
+ while (unique.has(index) === true) ++index;
77
+ u.index = index;
78
+ unique.add(index);
79
+ }
80
+ ++index;
81
+ }
82
+ });
83
+ };
84
+
85
+ const emplaceProperty = (prop: MetadataProperty): void => {
86
+ const union: IProtobufPropertyType[] = [];
87
+ for (const native of prop.value.natives)
88
+ if (native.name === "Uint8Array")
89
+ union.push({
90
+ type: "bytes",
91
+ index: ProtobufUtil.getSequence(native.tags[0] ?? [])!,
92
+ });
93
+ union.push(...emplaceAtomic(prop.value).values());
94
+ for (const array of prop.value.arrays)
95
+ union.push({
96
+ type: "array",
97
+ array: array.type,
98
+ value: emplaceSchema(
99
+ array.type.value,
100
+ ) as IProtobufSchema.IArray["value"],
101
+ index: ProtobufUtil.getSequence(array.tags[0] ?? [])!,
102
+ });
103
+ for (const obj of prop.value.objects)
104
+ if (isDynamicObject(obj.type))
105
+ union.push({
106
+ type: "map",
107
+ map: obj.type,
108
+ key: emplaceSchema(
109
+ obj.type.properties[0]!.key,
110
+ ) as IProtobufSchema.IMap["key"],
111
+ value: emplaceSchema(
112
+ obj.type.properties[0]!.value,
113
+ ) as IProtobufSchema.IMap["value"],
114
+ index: ProtobufUtil.getSequence(obj.tags[0] ?? [])!,
115
+ });
116
+ else
117
+ union.push({
118
+ type: "object",
119
+ object: obj.type,
120
+ index: ProtobufUtil.getSequence(obj.tags[0] ?? [])!,
121
+ });
122
+ for (const map of prop.value.maps)
123
+ union.push({
124
+ type: "map",
125
+ map,
126
+ key: emplaceSchema(map.key) as IProtobufSchema.IMap["key"],
127
+ value: emplaceSchema(map.value) as IProtobufSchema.IMap["value"],
128
+ index: ProtobufUtil.getSequence(map.tags[0] ?? [])!,
129
+ });
130
+ prop.of_protobuf_ = {
131
+ union,
132
+ fixed: union.every((p) => p.index !== null),
133
+ };
134
+ };
135
+
136
+ const emplaceSchema = (metadata: Metadata): IProtobufSchema => {
137
+ for (const native of metadata.natives)
138
+ if (native.name === "Uint8Array")
139
+ return {
140
+ type: "bytes",
141
+ };
142
+ const atomic = emplaceAtomic(metadata);
143
+ if (atomic.size) return atomic.values().next().value!;
144
+ for (const array of metadata.arrays)
145
+ return {
146
+ type: "array",
147
+ array: array.type,
148
+ value: emplaceSchema(
149
+ array.type.value,
150
+ ) as IProtobufSchema.IArray["value"],
151
+ };
152
+ for (const obj of metadata.objects)
153
+ if (isDynamicObject(obj.type))
154
+ return {
155
+ type: "map",
156
+ map: obj.type,
157
+ key: emplaceSchema(
158
+ obj.type.properties[0]!.key,
159
+ ) as IProtobufSchema.IMap["key"],
160
+ value: emplaceSchema(
161
+ obj.type.properties[0]!.value,
162
+ ) as IProtobufSchema.IMap["value"],
163
+ };
164
+ else
165
+ return {
166
+ type: "object",
167
+ object: obj.type,
168
+ };
169
+ for (const map of metadata.maps)
170
+ return {
171
+ type: "map",
172
+ map,
173
+ key: emplaceSchema(map.key) as IProtobufSchema.IMap["key"],
174
+ value: emplaceSchema(map.value) as IProtobufSchema.IMap["value"],
175
+ };
176
+ throw new Error(
177
+ "Error on ProtobufFactory.emplaceSchema(): any type detected.",
178
+ );
179
+ };
180
+
181
+ const emplaceAtomic = (
182
+ meta: Metadata,
183
+ ): Map<ProtobufAtomic, IProtobufPropertyType> => {
184
+ const map: Map<ProtobufAtomic, IProtobufPropertyType> = new Map();
185
+
186
+ // CONSTANTS
187
+ for (const c of meta.constants)
188
+ if (c.type === "boolean")
189
+ map.set("bool", {
190
+ type: "bool",
191
+ index: getSequence(c.values[0]?.tags[0] ?? [])!,
192
+ });
193
+ else if (c.type === "bigint") {
194
+ const init: ProtobufAtomic.BigNumeric = getBigintType(
195
+ c.values.map((v) => BigInt(v.value)),
196
+ );
197
+ for (const value of c.values)
198
+ emplaceBigint({
199
+ map,
200
+ tags: value.tags,
201
+ init,
202
+ });
203
+ } else if (c.type === "number") {
204
+ const init: ProtobufAtomic.Numeric = getNumberType(
205
+ c.values.map((v) => v.value) as number[],
206
+ );
207
+ for (const value of c.values)
208
+ emplaceNumber({
209
+ map,
210
+ tags: value.tags,
211
+ init,
212
+ });
213
+ } else if (c.type === "string")
214
+ map.set("string", {
215
+ type: "string",
216
+ index: getSequence(c.values[0]?.tags[0] ?? [])!,
217
+ });
218
+
219
+ // TEMPLATE
220
+ if (meta.templates.length)
221
+ map.set("string", {
222
+ type: "string",
223
+ index: getSequence(meta.templates[0]?.tags[0] ?? [])!,
224
+ });
225
+
226
+ // ATOMICS
227
+ for (const atomic of meta.atomics)
228
+ if (atomic.type === "boolean")
229
+ map.set("bool", {
230
+ type: "bool",
231
+ index: getSequence(atomic.tags[0] ?? [])!,
232
+ });
233
+ else if (atomic.type === "bigint")
234
+ emplaceBigint({
235
+ map,
236
+ tags: atomic.tags,
237
+ init: "int64",
238
+ });
239
+ else if (atomic.type === "number")
240
+ emplaceNumber({
241
+ map,
242
+ tags: atomic.tags,
243
+ init: "double",
244
+ });
245
+ else if (atomic.type === "string")
246
+ map.set("string", {
247
+ type: "string",
248
+ index: getSequence(atomic.tags[0] ?? [])!,
249
+ });
250
+
251
+ // SORTING FOR VALIDATION REASON
252
+ return new Map(
253
+ Array.from(map).sort((x, y) => ProtobufUtil.compare(x[0], y[0])),
254
+ );
255
+ };
256
+
257
+ const emplaceBigint = (next: {
258
+ map: Map<ProtobufAtomic, IProtobufPropertyType>;
259
+ tags: IMetadataTypeTag[][];
260
+ init: ProtobufAtomic.BigNumeric;
261
+ }): void => {
262
+ if (next.tags.length === 0) {
263
+ next.map.set(next.init, {
264
+ type: "bigint",
265
+ name: next.init,
266
+ index: null!,
267
+ });
268
+ return;
269
+ }
270
+ for (const row of next.tags) {
271
+ const value: ProtobufAtomic.BigNumeric =
272
+ row.find(
273
+ (tag) =>
274
+ tag.kind === "type" &&
275
+ (tag.value === "int64" || tag.value === "uint64"),
276
+ )?.value ?? next.init;
277
+ next.map.set(next.init, {
278
+ type: "bigint",
279
+ name: value,
280
+ index: ProtobufUtil.getSequence(row)!,
281
+ });
282
+ }
283
+ };
284
+
285
+ const emplaceNumber = (next: {
286
+ map: Map<ProtobufAtomic, IProtobufPropertyType>;
287
+ tags: IMetadataTypeTag[][];
288
+ init: ProtobufAtomic.Numeric;
289
+ }): void => {
290
+ if (next.tags.length === 0) {
291
+ next.map.set(next.init, {
292
+ type: "number",
293
+ name: next.init,
294
+ index: null!,
295
+ });
296
+ return;
297
+ }
298
+ for (const row of next.tags) {
299
+ const value: ProtobufAtomic.Numeric =
300
+ row.find(
301
+ (tag) =>
302
+ tag.kind === "type" &&
303
+ (tag.value === "int32" ||
304
+ tag.value === "uint32" ||
305
+ tag.value === "int64" ||
306
+ tag.value === "uint64" ||
307
+ tag.value === "float" ||
308
+ tag.value === "double"),
309
+ )?.value ?? next.init;
310
+ next.map.set(value, {
311
+ type: "number",
312
+ name: value,
313
+ index: ProtobufUtil.getSequence(row)!,
314
+ });
315
+ }
316
+ };
317
+
318
+ const getBigintType = (values: bigint[]): ProtobufAtomic.BigNumeric =>
319
+ values.some((v) => v < 0) ? "int64" : "uint64";
320
+
321
+ const getNumberType = (values: number[]): ProtobufAtomic.Numeric =>
322
+ values.every((v) => Math.floor(v) === v)
323
+ ? values.every((v) => -2147483648 <= v && v <= 2147483647)
324
+ ? "int32"
325
+ : "int64"
326
+ : "double";
327
+
328
+ const getSequence = (tags: IMetadataTypeTag[]): number | null => {
329
+ const sequence = tags.find(
330
+ (t) =>
331
+ t.kind === "sequence" &&
332
+ typeof (t.schema as any)?.["x-protobuf-sequence"] === "number",
333
+ );
334
+ if (sequence === undefined) return null;
335
+ const value: number = Number(
336
+ (sequence.schema as any)["x-protobuf-sequence"],
337
+ );
338
+ return Number.isNaN(value) ? null : value;
339
+ };
340
+
341
+ /* -----------------------------------------------------------
342
+ VALIDATORS
343
+ ----------------------------------------------------------- */
344
+ const validate = () => {
345
+ const visited: WeakSet<MetadataObjectType> = new WeakSet();
346
+ return (meta: Metadata, explore: MetadataFactory.IExplore): string[] => {
347
+ const errors: string[] = [];
348
+ const insert = (msg: string) => errors.push(msg);
349
+
350
+ if (explore.top === true) {
351
+ const onlyObject: boolean =
352
+ meta.size() === 1 &&
353
+ meta.objects.length === 1 &&
354
+ meta.objects[0]!.type.properties.every((p) =>
355
+ p.key.isSoleLiteral(),
356
+ ) &&
357
+ meta.isRequired() === true &&
358
+ meta.nullable === false;
359
+ if (onlyObject === false)
360
+ insert("target type must be a sole and static object type");
361
+ }
362
+ for (const obj of meta.objects) {
363
+ if (visited.has(obj.type)) continue;
364
+ visited.add(obj.type);
365
+ validateObject({
366
+ object: obj.type,
367
+ errors,
368
+ });
369
+ try {
370
+ emplaceObject(obj.type);
371
+ } catch {}
372
+ }
373
+
374
+ //----
375
+ // NOT SUPPORTED TYPES
376
+ //----
377
+ const noSupport = (msg: string) => insert(`does not support ${msg}`);
378
+
379
+ // PROHIBIT ANY TYPE
380
+ if (meta.any) noSupport("any type");
381
+ // PROHIBIT FUNCTIONAL TYPE
382
+ if (meta.functions.length) noSupport("functional type");
383
+ // PROHIBIT TUPLE TYPE
384
+ if (meta.tuples.length) noSupport("tuple type");
385
+ // PROHIBIT SET TYPE
386
+ if (meta.sets.length) noSupport("Set type");
387
+ // NATIVE TYPE, BUT NOT Uint8Array
388
+ if (meta.natives.length)
389
+ for (const native of meta.natives) {
390
+ if (native.name === "Uint8Array") continue;
391
+
392
+ const instead = BANNED_NATIVE_TYPES.get(native.name);
393
+ if (instead === undefined) noSupport(`${native.name} type`);
394
+ else noSupport(`${native.name} type. Use ${instead} type instead.`);
395
+ }
396
+ //----
397
+ // ATOMIC CASES
398
+ //----
399
+ if (meta.atomics.length) {
400
+ const numbers = ProtobufUtil.getNumbers(meta);
401
+ const bigints = ProtobufUtil.getBigints(meta);
402
+
403
+ for (const type of ["int64", "uint64"])
404
+ if (numbers.has(type) && bigints.has(type))
405
+ insert(
406
+ `tags.Type<"${type}"> cannot be used in both number and bigint types. Recommend to remove from number type`,
407
+ );
408
+ }
409
+ //----
410
+ // ARRAY CASES
411
+ //----
412
+ // DO NOT ALLOW MULTI-DIMENSIONAL ARRAY
413
+ if (
414
+ meta.arrays.length &&
415
+ meta.arrays.some((array) => !!array.type.value.arrays.length)
416
+ )
417
+ noSupport("over two dimensional array type");
418
+ // CHILD OF ARRAY TYPE MUST BE REQUIRED
419
+ if (
420
+ meta.arrays.length &&
421
+ meta.arrays.some(
422
+ (array) =>
423
+ array.type.value.isRequired() === false ||
424
+ array.type.value.nullable === true,
425
+ )
426
+ )
427
+ noSupport("optional type in array");
428
+ // UNION IN ARRAY
429
+ if (
430
+ meta.arrays.length &&
431
+ meta.arrays.some(
432
+ (a) =>
433
+ a.type.value.size() > 1 &&
434
+ a.type.value.constants.length !== 1 &&
435
+ a.type.value.constants[0]?.values.length !== a.type.value.size(),
436
+ )
437
+ )
438
+ noSupport("union type in array");
439
+ // DO DYNAMIC OBJECT IN ARRAY
440
+ if (
441
+ meta.arrays.length &&
442
+ meta.arrays.some(
443
+ (a) =>
444
+ a.type.value.maps.length ||
445
+ (a.type.value.objects.length &&
446
+ a.type.value.objects.some(
447
+ (o) => ProtobufUtil.isStaticObject(o.type) === false,
448
+ )),
449
+ )
450
+ )
451
+ noSupport("dynamic object in array");
452
+ // UNION WITH ARRAY
453
+ if (meta.size() > 1 && meta.arrays.length)
454
+ noSupport("union type with array type");
455
+ //----
456
+ // OBJECT CASES
457
+ //----
458
+ // EMPTY PROPERTY
459
+ if (
460
+ meta.objects.length &&
461
+ meta.objects.some((obj) => obj.type.properties.length === 0)
462
+ )
463
+ noSupport("empty object type");
464
+ // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
465
+ if (
466
+ meta.objects.length &&
467
+ meta.objects.some(
468
+ (obj) =>
469
+ obj.type.properties.filter((p) => !p.key.isSoleLiteral()).length >
470
+ 1,
471
+ )
472
+ )
473
+ noSupport(
474
+ "object type with multiple dynamic key typed properties. Keep only one.",
475
+ );
476
+ // STATIC AND DYNAMIC PROPERTIES ARE COMPATIBLE
477
+ if (
478
+ meta.objects.length &&
479
+ meta.objects.some(
480
+ (obj) =>
481
+ obj.type.properties.some((p) => p.key.isSoleLiteral()) &&
482
+ obj.type.properties.some((p) => !p.key.isSoleLiteral()),
483
+ )
484
+ )
485
+ noSupport(
486
+ "object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
487
+ );
488
+ // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
489
+ if (
490
+ meta.objects.length &&
491
+ isDynamicObject(meta.objects[0]!.type) &&
492
+ meta.objects[0]!.type.properties.some((p) => !!p.value.arrays.length)
493
+ )
494
+ noSupport("dynamic object with array value type");
495
+ // UNION WITH DYNAMIC OBJECTTa
496
+ if (
497
+ meta.size() > 1 &&
498
+ meta.objects.length &&
499
+ isDynamicObject(meta.objects[0]!.type)
500
+ )
501
+ noSupport("union type with dynamic object type");
502
+ // UNION IN DYNAMIC PROPERTY VALUE
503
+ if (
504
+ meta.objects.length &&
505
+ meta.objects.some(
506
+ (obj) =>
507
+ isDynamicObject(obj.type) &&
508
+ obj.type.properties.some((p) => ProtobufUtil.isUnion(p.value)),
509
+ )
510
+ )
511
+ noSupport("union type in dynamic property");
512
+ //----
513
+ // MAP CASES
514
+ //----
515
+ // KEY TYPE IS UNION
516
+ if (
517
+ meta.maps.length &&
518
+ meta.maps.some((m) => ProtobufUtil.isUnion(m.key))
519
+ )
520
+ noSupport("union key typed map");
521
+ // KEY TYPE IS NOT ATOMIC
522
+ if (
523
+ meta.maps.length &&
524
+ meta.maps.some((m) => ProtobufUtil.getAtomics(m.key).size !== 1)
525
+ )
526
+ noSupport("non-atomic key typed map");
527
+ // MAP TYPE, BUT PROPERTY KEY TYPE IS OPTIONAL
528
+ if (
529
+ meta.maps.length &&
530
+ meta.maps.some((m) => m.key.isRequired() === false || m.key.nullable)
531
+ )
532
+ noSupport("optional key typed map");
533
+ // MAP TYPE, BUT VALUE TYPE IS ARRAY
534
+ if (meta.maps.length && meta.maps.some((m) => !!m.value.arrays.length))
535
+ noSupport("map type with array value type");
536
+ // UNION WITH MAP
537
+ if (meta.size() > 1 && meta.maps.length)
538
+ noSupport("union type with map type");
539
+ // UNION IN MAP
540
+ if (
541
+ meta.maps.length &&
542
+ meta.maps.some((m) => ProtobufUtil.isUnion(m.value))
543
+ )
544
+ noSupport("union type in map value type");
545
+ return errors;
546
+ };
547
+ };
548
+
549
+ /* -----------------------------------------------------------
550
+ SEQUENCE VALIDATOR
551
+ ----------------------------------------------------------- */
552
+ const validateObject = (next: {
553
+ object: MetadataObjectType;
554
+ errors: string[];
555
+ }): void => {
556
+ for (const property of next.object.properties)
557
+ validateProperty({
558
+ metadata: property.value,
559
+ errors: next.errors,
560
+ });
561
+
562
+ const entire: Map<number, string> = new Map();
563
+ const visitProperty = (p: MetadataProperty) => {
564
+ const local: Set<number> = new Set();
565
+ const tagger = (matrix: IMetadataTypeTag[][]): void => {
566
+ matrix.forEach((tags) => {
567
+ const value: number | null = ProtobufUtil.getSequence(tags);
568
+ if (value !== null) local.add(value);
569
+ });
570
+ };
571
+ for (const c of p.value.constants)
572
+ for (const v of c.values) tagger(v.tags);
573
+ for (const a of p.value.atomics) tagger(a.tags);
574
+ for (const t of p.value.templates) tagger(t.tags);
575
+ for (const o of p.value.objects) tagger(o.tags);
576
+ for (const a of p.value.arrays) tagger(a.tags);
577
+ for (const s of local)
578
+ if (entire.has(s))
579
+ next.errors.push(
580
+ `The Sequence<${s}> tag is duplicated in two properties (${JSON.stringify(entire.get(s))} and ${JSON.stringify(p.key.getSoleLiteral())})`,
581
+ );
582
+ else entire.set(s, p.key.getSoleLiteral()!);
583
+ };
584
+ for (const p of next.object.properties) visitProperty(p);
585
+ };
586
+
587
+ const validateProperty = (next: {
588
+ metadata: Metadata;
589
+ errors: string[];
590
+ }): void => {
591
+ let expected: number = 0;
592
+ const sequences: Set<number> = new Set();
593
+ const add = (value: number): boolean => {
594
+ if (sequences.has(value)) return false;
595
+ sequences.add(value);
596
+ ++expected;
597
+ return true;
598
+ };
599
+
600
+ for (const validator of [
601
+ validateBooleanSequence,
602
+ validateNumericSequences({
603
+ type: "bigint",
604
+ default: "int64",
605
+ categories: BIGINT_TYPES,
606
+ }),
607
+ validateNumericSequences({
608
+ type: "number",
609
+ default: "double",
610
+ categories: NUMBER_TYPES,
611
+ }),
612
+ validateStringSequence,
613
+ ])
614
+ validator({ metadata: next.metadata, errors: next.errors, add });
615
+ for (const array of next.metadata.arrays)
616
+ validateInstanceSequence({
617
+ type: "array",
618
+ tags: array.tags,
619
+ errors: next.errors,
620
+ add,
621
+ });
622
+ for (const object of next.metadata.objects)
623
+ validateInstanceSequence({
624
+ type: "object",
625
+ tags: object.tags,
626
+ errors: next.errors,
627
+ add,
628
+ });
629
+ for (const map of next.metadata.maps)
630
+ validateInstanceSequence({
631
+ type: "map",
632
+ tags: map.tags,
633
+ errors: next.errors,
634
+ add,
635
+ });
636
+ for (const native of next.metadata.natives)
637
+ if (native.name === "Uint8Array")
638
+ validateInstanceSequence({
639
+ type: "Uint8Array",
640
+ tags: native.tags,
641
+ errors: next.errors,
642
+ add,
643
+ });
644
+ };
645
+
646
+ const validateBooleanSequence = (next: {
647
+ metadata: Metadata;
648
+ errors: string[];
649
+ add: (value: number) => boolean;
650
+ }): void => {
651
+ // PREPARE EMPLACER
652
+ const unique: Set<number> = new Set();
653
+ let expected: number = 0;
654
+ let actual: number = 0;
655
+ const emplace = (matrix: IMetadataTypeTag[][]): void => {
656
+ for (const tags of matrix)
657
+ for (const tag of tags) {
658
+ const sequence = ProtobufUtil.getSequence([tag]);
659
+ if (sequence !== null) {
660
+ unique.add(sequence);
661
+ ++actual;
662
+ }
663
+ ++expected;
664
+ }
665
+ };
666
+
667
+ // GATHER SEQUENCE TAGS
668
+ for (const atomic of next.metadata.atomics)
669
+ if (atomic.type === "boolean") emplace(atomic.tags);
670
+ for (const constant of next.metadata.constants)
671
+ if (constant.type === "boolean")
672
+ for (const value of constant.values) emplace(value.tags);
673
+
674
+ // PREDICATE
675
+ if (unique.size && actual !== expected)
676
+ next.errors.push(
677
+ `The sequence tag must be declared in every union type members`,
678
+ );
679
+ else if (unique.size > 1)
680
+ next.errors.push(
681
+ `The sequence tag value must be the same in boolean type (including literal types)`,
682
+ );
683
+ else if (unique.size === 1) {
684
+ const value: number = unique.values().next().value!;
685
+ if (next.add(value) === false)
686
+ next.errors.push(
687
+ `The sequence tag value ${value} in boolean type is duplicated with other types`,
688
+ );
689
+ }
690
+ };
691
+
692
+ const validateNumericSequences =
693
+ (config: {
694
+ type: "number" | "bigint";
695
+ default: string;
696
+ categories: Set<string>;
697
+ }) =>
698
+ (next: {
699
+ metadata: Metadata;
700
+ errors: string[];
701
+ add: (value: number) => boolean;
702
+ }): void => {
703
+ // FIND TYPE CATEGORIES
704
+ const categories: Set<string> = new Set();
705
+ const getType = (tags: IMetadataTypeTag[]): string => {
706
+ const found: IMetadataTypeTag | undefined = tags.find(
707
+ (t) => t.kind === "type" && config.categories.has(t.value),
708
+ );
709
+ return found?.value ?? config.default;
710
+ };
711
+ const exploreCategory = (matrix: IMetadataTypeTag[][]): void => {
712
+ for (const tags of matrix) categories.add(getType(tags));
713
+ };
714
+ for (const atomic of next.metadata.atomics)
715
+ if (atomic.type === config.type) exploreCategory(atomic.tags);
716
+ for (const constant of next.metadata.constants)
717
+ if (constant.type === config.type)
718
+ for (const value of constant.values) exploreCategory(value.tags);
719
+
720
+ // ITERATE TYPE CATEGORIES
721
+ for (const category of categories) {
722
+ const unique: Set<number> = new Set();
723
+ let expected: number = 0;
724
+ let actual: number = 0;
725
+ const emplace = (tags: IMetadataTypeTag[]): void => {
726
+ const sequence: number | null = ProtobufUtil.getSequence(tags);
727
+ if (sequence !== null) {
728
+ unique.add(sequence);
729
+ ++actual;
730
+ }
731
+ ++expected;
732
+ };
733
+
734
+ for (const atomic of next.metadata.atomics)
735
+ if (atomic.type === config.type)
736
+ for (const tags of atomic.tags)
737
+ if (getType(tags) === category) emplace(tags);
738
+ for (const constant of next.metadata.constants)
739
+ if (constant.type === config.type)
740
+ for (const value of constant.values)
741
+ for (const tags of value.tags)
742
+ if (getType(tags) === category) emplace(tags);
743
+
744
+ if (unique.size && actual !== expected) {
745
+ next.errors.push(
746
+ `The sequence tag must be declared in every union type members`,
747
+ );
748
+ } else if (unique.size > 1)
749
+ next.errors.push(
750
+ `The sequence tag value must be the same in ${config.type} type (including literal types)`,
751
+ );
752
+ else if (unique.size === 1) {
753
+ const value: number = unique.values().next().value!;
754
+ if (next.add(value) === false)
755
+ next.errors.push(
756
+ `The sequence tag value ${value} in ${config.type} type is duplicated with other types`,
757
+ );
758
+ }
759
+ }
760
+ };
761
+
762
+ const validateStringSequence = (next: {
763
+ metadata: Metadata;
764
+ errors: string[];
765
+ add: (value: number) => boolean;
766
+ }): void => {
767
+ const unique: Set<number> = new Set();
768
+ let expected: number = 0;
769
+ let actual: number = 0;
770
+ const emplace = (matrix: IMetadataTypeTag[][]): void => {
771
+ for (const tags of matrix)
772
+ for (const tag of tags) {
773
+ const sequence = ProtobufUtil.getSequence([tag]);
774
+ if (sequence !== null) {
775
+ unique.add(sequence);
776
+ ++actual;
777
+ }
778
+ ++expected;
779
+ }
780
+ };
781
+ for (const atomic of next.metadata.atomics)
782
+ if (atomic.type === "string") emplace(atomic.tags);
783
+ for (const constant of next.metadata.constants)
784
+ if (constant.type === "string")
785
+ for (const value of constant.values) emplace(value.tags);
786
+ for (const template of next.metadata.templates) emplace(template.tags);
787
+
788
+ if (unique.size && actual !== expected)
789
+ next.errors.push(
790
+ `The sequence tag must be declared in every union type members`,
791
+ );
792
+ else if (unique.size > 1)
793
+ next.errors.push(
794
+ `The sequence tag value must be the same in string types including literal and template types`,
795
+ );
796
+ else if (unique.size === 1) {
797
+ const value: number = unique.values().next().value!;
798
+ if (next.add(value) === false)
799
+ next.errors.push(
800
+ `The sequence tag value ${value} in string type is duplicated with other types`,
801
+ );
802
+ }
803
+ };
804
+
805
+ const validateInstanceSequence = (next: {
806
+ type: "array" | "object" | "map" | "Uint8Array";
807
+ tags: IMetadataTypeTag[][];
808
+ errors: string[];
809
+ add: (value: number) => boolean;
810
+ }): void => {
811
+ const unique: Set<number> = new Set();
812
+ let count: number = 0;
813
+ for (const tags of next.tags) {
814
+ const value: number | null = ProtobufUtil.getSequence(tags);
815
+ if (value === null) continue;
816
+ unique.add(value);
817
+ ++count;
818
+ }
819
+ if (unique.size && count !== next.tags.length)
820
+ next.errors.push(
821
+ `The sequence tag must be declared in every union type members`,
822
+ );
823
+ else if (unique.size > 1)
824
+ next.errors.push(
825
+ `The sequence tag value must be the same in ${next.type === "array" ? "an array" : "object"} type.`,
826
+ );
827
+ else if (unique.size === 1) {
828
+ const value: number = unique.values().next().value!;
829
+ if (next.add(value) === false)
830
+ next.errors.push(
831
+ `The sequence tag value ${value} in ${next.type} type is duplicated with other types`,
832
+ );
833
+ }
834
+ };
835
+ }
836
+
837
+ const isDynamicObject = (obj: MetadataObjectType): boolean =>
838
+ obj.properties[0]!.key.isSoleLiteral() === false;
839
+
840
+ const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
841
+ ["Date", "string"],
842
+ ["Boolean", "boolean"],
843
+ ["BigInt", "bigint"],
844
+ ["Number", "number"],
845
+ ["String", "string"],
846
+ ...[
847
+ "Buffer",
848
+ "Uint8ClampedArray",
849
+ "Uint16Array",
850
+ "Uint32Array",
851
+ "BigUint64Array",
852
+ "Int8Array",
853
+ "Int16Array",
854
+ "Int32Array",
855
+ "BigInt64Array",
856
+ "Float32Array",
857
+ "Float64Array",
858
+ "DataView",
859
+ "ArrayBuffer",
860
+ "SharedArrayBuffer",
861
+ ].map((name) => [name, "Uint8Array"] as const),
862
+ ["WeakSet", "Array"],
863
+ ["WeakMap", "Map"],
864
+ ]);
865
+ const NUMBER_TYPES: Set<string> = new Set([
866
+ "int32",
867
+ "uint32",
868
+ "int64",
869
+ "uint64",
870
+ "float",
871
+ "double",
872
+ ]);
873
+ const BIGINT_TYPES = new Set(["int64", "uint64"]);