typia 5.0.0-dev.20230820-2 → 5.0.0-dev.20230821

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 (217) hide show
  1. package/README.md +1 -1
  2. package/lib/factories/MetadataTagFactory.js.map +1 -1
  3. package/lib/programmers/RandomProgrammer.js +15 -12
  4. package/lib/programmers/RandomProgrammer.js.map +1 -1
  5. package/lib/programmers/helpers/RandomRanger.js +2 -2
  6. package/lib/programmers/helpers/RandomRanger.js.map +1 -1
  7. package/lib/programmers/internal/application_number.js +6 -1
  8. package/lib/programmers/internal/application_number.js.map +1 -1
  9. package/lib/programmers/internal/check_bigint.js +8 -11
  10. package/lib/programmers/internal/check_bigint.js.map +1 -1
  11. package/lib/programmers/internal/check_number.js +12 -2
  12. package/lib/programmers/internal/check_number.js.map +1 -1
  13. package/package.json +1 -1
  14. package/src/factories/ExpressionFactory.ts +115 -115
  15. package/src/factories/MetadataCollection.ts +269 -269
  16. package/src/factories/MetadataFactory.ts +34 -34
  17. package/src/factories/MetadataTagFactory.ts +364 -364
  18. package/src/factories/ProtobufFactory.ts +251 -251
  19. package/src/factories/StatementFactory.ts +72 -72
  20. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  21. package/src/factories/internal/metadata/emend_metadata_atomics.ts +33 -33
  22. package/src/factories/internal/metadata/emplace_metadata_alias.ts +40 -40
  23. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  24. package/src/factories/internal/metadata/emplace_metadata_object.ts +136 -136
  25. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  26. package/src/factories/internal/metadata/explore_metadata.ts +38 -38
  27. package/src/factories/internal/metadata/iterate_metadata.ts +81 -81
  28. package/src/factories/internal/metadata/iterate_metadata_alias.ts +30 -30
  29. package/src/factories/internal/metadata/iterate_metadata_array.ts +24 -24
  30. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  31. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  32. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  33. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  34. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +84 -84
  35. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  36. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  37. package/src/factories/internal/metadata/iterate_metadata_object.ts +43 -43
  38. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  39. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  40. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  41. package/src/factories/internal/metadata/iterate_metadata_tag.ts +31 -31
  42. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  43. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  44. package/src/factories/internal/metadata/iterate_metadata_union.ts +24 -24
  45. package/src/functional/$ProtobufReader.ts +188 -188
  46. package/src/functional/$ProtobufSizer.ts +144 -144
  47. package/src/functional/$ProtobufWriter.ts +152 -152
  48. package/src/functional/$number.ts +13 -13
  49. package/src/functional/$strlen.ts +7 -7
  50. package/src/functional/$varint.ts +130 -130
  51. package/src/functional/$zigzag.ts +39 -39
  52. package/src/functional/IProtobufWriter.ts +18 -18
  53. package/src/functional/Namespace.ts +149 -149
  54. package/src/json.ts +648 -648
  55. package/src/misc.ts +651 -651
  56. package/src/module.ts +708 -708
  57. package/src/programmers/CheckerProgrammer.ts +1173 -1173
  58. package/src/programmers/FeatureProgrammer.ts +497 -497
  59. package/src/programmers/IsProgrammer.ts +239 -239
  60. package/src/programmers/IsPruneProgrammer.ts +73 -73
  61. package/src/programmers/RandomProgrammer.ts +578 -584
  62. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  63. package/src/programmers/helpers/CloneJoiner.ts +107 -107
  64. package/src/programmers/helpers/FunctionImporeter.ts +89 -89
  65. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  66. package/src/programmers/helpers/ProtobufUtil.ts +24 -24
  67. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  68. package/src/programmers/helpers/PruneJoiner.ts +143 -143
  69. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  70. package/src/programmers/helpers/RandomRanger.ts +208 -216
  71. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  72. package/src/programmers/helpers/UnionExplorer.ts +310 -310
  73. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  74. package/src/programmers/helpers/disable_function_importer_declare.ts +27 -27
  75. package/src/programmers/internal/application_alias.ts +66 -66
  76. package/src/programmers/internal/application_array.ts +30 -30
  77. package/src/programmers/internal/application_constant.ts +26 -26
  78. package/src/programmers/internal/application_default.ts +17 -17
  79. package/src/programmers/internal/application_default_string.ts +33 -33
  80. package/src/programmers/internal/application_native.ts +39 -39
  81. package/src/programmers/internal/application_number.ts +81 -77
  82. package/src/programmers/internal/application_object.ts +165 -165
  83. package/src/programmers/internal/application_resolved.ts +55 -55
  84. package/src/programmers/internal/application_schema.ts +157 -157
  85. package/src/programmers/internal/application_string.ts +44 -44
  86. package/src/programmers/internal/application_templates.ts +25 -25
  87. package/src/programmers/internal/application_tuple.ts +57 -57
  88. package/src/programmers/internal/check_array.ts +30 -30
  89. package/src/programmers/internal/check_array_length.ts +35 -35
  90. package/src/programmers/internal/check_bigint.ts +110 -92
  91. package/src/programmers/internal/check_custom.ts +31 -31
  92. package/src/programmers/internal/check_number.ts +258 -230
  93. package/src/programmers/internal/check_string.ts +25 -25
  94. package/src/programmers/internal/check_string_tags.ts +67 -67
  95. package/src/programmers/internal/check_template.ts +56 -56
  96. package/src/programmers/internal/check_union_array_like.ts +329 -329
  97. package/src/programmers/internal/decode_union_object.ts +84 -84
  98. package/src/programmers/internal/feature_object_entries.ts +63 -63
  99. package/src/programmers/internal/get_comment_tags.ts +23 -23
  100. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  101. package/src/programmers/internal/prune_object_properties.ts +60 -60
  102. package/src/programmers/internal/random_custom.ts +29 -29
  103. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  104. package/src/programmers/internal/template_to_pattern.ts +15 -15
  105. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  106. package/src/programmers/json/JsonApplicationProgrammer.ts +47 -47
  107. package/src/programmers/json/JsonAssertParseProgrammer.ts +66 -66
  108. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +66 -66
  109. package/src/programmers/json/JsonIsParseProgrammer.ts +72 -72
  110. package/src/programmers/json/JsonIsStringifyProgrammer.ts +76 -76
  111. package/src/programmers/json/JsonStringifyProgrammer.ts +987 -987
  112. package/src/programmers/json/JsonValidateParseProgrammer.ts +70 -70
  113. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +84 -84
  114. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  115. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +68 -68
  116. package/src/programmers/misc/MiscCloneProgrammer.ts +587 -587
  117. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  118. package/src/programmers/misc/MiscLiteralsProgrammer.ts +64 -64
  119. package/src/programmers/misc/MiscPruneProgrammer.ts +542 -542
  120. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  121. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +78 -78
  122. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +69 -69
  123. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +66 -66
  124. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +38 -38
  125. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +920 -920
  126. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +75 -75
  127. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +76 -76
  128. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +178 -178
  129. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +83 -83
  130. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +86 -86
  131. package/src/protobuf.ts +239 -239
  132. package/src/schemas/json/IJsonApplication.ts +8 -8
  133. package/src/schemas/json/IJsonComponents.ts +33 -33
  134. package/src/schemas/json/IJsonSchema.ts +133 -133
  135. package/src/schemas/metadata/ICommentTag.ts +4 -4
  136. package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
  137. package/src/schemas/metadata/IMetadata.ts +28 -28
  138. package/src/schemas/metadata/IMetadataAlias.ts +14 -14
  139. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  140. package/src/schemas/metadata/IMetadataArray.ts +10 -10
  141. package/src/schemas/metadata/IMetadataCollection.ts +11 -11
  142. package/src/schemas/metadata/IMetadataConstant.ts +16 -16
  143. package/src/schemas/metadata/IMetadataDictionary.ts +14 -14
  144. package/src/schemas/metadata/IMetadataEntry.ts +6 -6
  145. package/src/schemas/metadata/IMetadataObject.ts +18 -18
  146. package/src/schemas/metadata/IMetadataProperty.ts +11 -11
  147. package/src/schemas/metadata/IMetadataResolved.ts +6 -6
  148. package/src/schemas/metadata/IMetadataTag.ts +112 -112
  149. package/src/schemas/metadata/IMetadataTuple.ts +10 -10
  150. package/src/schemas/metadata/Metadata.ts +637 -637
  151. package/src/schemas/metadata/MetadataAlias.ts +66 -66
  152. package/src/schemas/metadata/MetadataArray.ts +55 -55
  153. package/src/schemas/metadata/MetadataConstant.ts +3 -3
  154. package/src/schemas/metadata/MetadataObject.ts +139 -139
  155. package/src/schemas/metadata/MetadataProperty.ts +64 -64
  156. package/src/schemas/metadata/MetadataResolved.ts +51 -51
  157. package/src/schemas/metadata/MetadataTuple.ts +58 -58
  158. package/src/transformers/CallExpressionTransformer.ts +240 -240
  159. package/src/transformers/features/AssertTransformer.ts +11 -11
  160. package/src/transformers/features/CreateAssertTransformer.ts +13 -13
  161. package/src/transformers/features/CreateIsTransformer.ts +11 -11
  162. package/src/transformers/features/CreateRandomTransformer.ts +39 -39
  163. package/src/transformers/features/CreateValidateTransformer.ts +13 -13
  164. package/src/transformers/features/IsTransformer.ts +11 -11
  165. package/src/transformers/features/RandomTransformer.ts +42 -42
  166. package/src/transformers/features/ValidateTransformer.ts +11 -11
  167. package/src/transformers/features/json/JsonApplicationTransformer.ts +111 -111
  168. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  169. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  170. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +10 -10
  171. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  172. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  173. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +10 -10
  174. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  175. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +10 -10
  176. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  177. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  178. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  179. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  180. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  181. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  182. package/src/transformers/features/misc/MetadataTransformer.ts +53 -53
  183. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  184. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  185. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  186. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +10 -10
  187. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +10 -10
  188. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  189. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  190. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  191. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  192. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +10 -10
  193. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +10 -10
  194. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  195. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  196. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +28 -28
  197. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  198. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  199. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  200. package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +12 -12
  201. package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +12 -12
  202. package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +9 -9
  203. package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +9 -9
  204. package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +12 -12
  205. package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +12 -12
  206. package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +12 -12
  207. package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +12 -12
  208. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
  209. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
  210. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +10 -10
  211. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +10 -10
  212. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +10 -10
  213. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +10 -10
  214. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +31 -31
  215. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +12 -12
  216. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +12 -12
  217. package/src/utils/NameEncoder.ts +32 -32
@@ -1,251 +1,251 @@
1
- import ts from "typescript";
2
-
3
- import { Metadata } from "../schemas/metadata/Metadata";
4
- import { MetadataObject } from "../schemas/metadata/MetadataObject";
5
-
6
- import { Escaper } from "../utils/Escaper";
7
-
8
- import { MetadataCollection } from "./MetadataCollection";
9
- import { MetadataFactory } from "./MetadataFactory";
10
-
11
- export namespace ProtobufFactory {
12
- export const metadata =
13
- (method: string) =>
14
- (checker: ts.TypeChecker) =>
15
- (collection: MetadataCollection) =>
16
- (type: ts.Type) => {
17
- // COMPOSE METADATA WITH INDIVIDUAL VALIDATIONS
18
- const top: Metadata = MetadataFactory.analyze(checker)({
19
- resolve: false,
20
- constant: true,
21
- absorb: true,
22
- validate: validate(method),
23
- })(collection)(type);
24
-
25
- // TOP LEVEL TYPE MUST BE A SINGLE OBJECT
26
- const onlyObject: boolean =
27
- top.size() === 1 &&
28
- top.objects.length === 1 &&
29
- top.objects[0]!.properties.every((p) =>
30
- p.key.isSoleLiteral(),
31
- ) &&
32
- top.isRequired() === true &&
33
- top.nullable === false;
34
- if (onlyObject === false)
35
- throw new Error(
36
- `${prefix(
37
- method,
38
- )} target type must be a sole and static object.`,
39
- );
40
-
41
- return top;
42
- };
43
-
44
- const validate = (method: string) => (meta: Metadata) => {
45
- //----
46
- // NOT SUPPORTED TYPES
47
- //----
48
- // PROHIBIT ANY TYPE
49
- if (meta.any) throw notSupportedError({ method })("any type");
50
- // PROHIBIT FUNCTIONAL TYPE
51
- else if (meta.functional)
52
- throw notSupportedError({ method })("functional type");
53
- // PROHIBIT TUPLE TYPE
54
- else if (meta.tuples.length)
55
- throw notSupportedError({ method })("tuple type");
56
- // PROHIBIT SET TYPE
57
- else if (meta.sets.length)
58
- throw notSupportedError({ method })("Set type");
59
- // NATIVE TYPE, BUT NOT Uint8Array
60
- else if (meta.natives.length) {
61
- const banned = meta.natives
62
- .map((n) => [n, BANNED_NATIVE_TYPES.get(n)] as const)
63
- .filter(([_n, b]) => b !== undefined)[0];
64
- if (banned !== undefined)
65
- throw notSupportedError({ method })(
66
- banned[1] === null
67
- ? banned[0]
68
- : `${banned[0]}. Use ${banned[1]} instead.`,
69
- );
70
- }
71
- //----
72
- // ARRRAY CASES
73
- //----
74
- // DO NOT ALLOW MULTI-DIMENTIONAL ARRAY
75
- else if (
76
- meta.arrays.length &&
77
- meta.arrays.some((array) => !!array.value.arrays.length)
78
- )
79
- throw notSupportedError({ method })("two dimenstional array type");
80
- // CHILD OF ARRAY TYPE MUST BE REQUIRED
81
- else if (
82
- meta.arrays.length &&
83
- meta.arrays.some(
84
- (array) =>
85
- array.value.isRequired() === false ||
86
- array.value.nullable === true,
87
- )
88
- )
89
- throw notSupportedError({ method })("optional type in array");
90
- // UNION IN ARRAY
91
- else if (
92
- meta.arrays.length &&
93
- meta.arrays.some((a) => a.value.size() > 1)
94
- )
95
- throw notSupportedError({ method })("union type in array");
96
- // UNION WITH ARRAY
97
- else if (meta.size() > 1 && meta.arrays.length)
98
- throw notSupportedError({ method })("union type with array type");
99
- //----
100
- // OBJECT CASES
101
- //----
102
- // EMPTY PROPERTY
103
- else if (
104
- meta.objects.length &&
105
- meta.objects.some((obj) => obj.properties.length === 0)
106
- )
107
- throw notSupportedError({ method })("empty object type");
108
- // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
109
- else if (
110
- meta.objects.length &&
111
- meta.objects.some(
112
- (obj) =>
113
- obj.properties.filter((p) => !p.key.isSoleLiteral())
114
- .length > 1,
115
- )
116
- )
117
- throw notSupportedError({ method })(
118
- "object type with multiple dynamic key typed properties. Keep only one.",
119
- );
120
- // STATIC AND DYNAMIC PROPERTIES ARE COMPATIBLE
121
- else if (
122
- meta.objects.length &&
123
- meta.objects.some(
124
- (obj) =>
125
- obj.properties.some((p) => p.key.isSoleLiteral()) &&
126
- obj.properties.some((p) => !p.key.isSoleLiteral()),
127
- )
128
- )
129
- throw notSupportedError({ method })(
130
- "object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
131
- );
132
- // STATIC PROPERTY, BUT INVALID KEY NAME
133
- else if (
134
- meta.objects.length &&
135
- meta.objects.some((obj) =>
136
- obj.properties.some(
137
- (p) =>
138
- p.key.isSoleLiteral() &&
139
- !Escaper.variable(p.key.getSoleLiteral()!),
140
- ),
141
- )
142
- )
143
- throw notSupportedError({ method })(
144
- `object type with invalid static key name.`,
145
- );
146
- // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
147
- else if (
148
- meta.objects.length &&
149
- isDynamicObject(meta.objects[0]!) &&
150
- meta.objects[0]!.properties.some((p) => !!p.value.arrays.length)
151
- )
152
- throw notSupportedError({ method })(
153
- "dynamic object with array value type",
154
- );
155
- // UNION WITH DYNAMIC OBJECT
156
- else if (
157
- meta.size() > 1 &&
158
- meta.objects.length &&
159
- isDynamicObject(meta.objects[0]!)
160
- )
161
- throw notSupportedError({ method })(
162
- "union type with dynamic object type",
163
- );
164
- // UNION IN DYNAMIC PROPERTY VALUE
165
- else if (
166
- meta.objects.length &&
167
- meta.objects.some(
168
- (obj) =>
169
- isDynamicObject(obj) &&
170
- obj.properties.some((p) => p.value.isBinaryUnion()),
171
- )
172
- )
173
- throw notSupportedError({ method })(
174
- "union type in dynamic property",
175
- );
176
- //----
177
- // MAP CASES
178
- //----
179
- // MAP TYPE, BUT PROPERTY KEY TYPE IS NOT STRING
180
- else if (
181
- meta.maps.length &&
182
- meta.maps.some(
183
- (m) =>
184
- m.key.isBinaryUnion() ||
185
- (m.key.atomics.find((v) => v === "string") === undefined &&
186
- m.key.constants.find((c) => c.type === "string") ===
187
- undefined),
188
- )
189
- )
190
- throw notSupportedError({ method })("non-string key typed map");
191
- // MAP TYPE, BUT PROPERTY KEY TYPE IS OPTIONAL
192
- else if (
193
- meta.maps.length &&
194
- meta.maps.some(
195
- (m) => m.key.isRequired() === false || m.key.nullable,
196
- )
197
- )
198
- throw notSupportedError({ method })("optional key typed map");
199
- // MAP TYPE, BUT VALUE TYPE IS ARRAY
200
- else if (
201
- meta.maps.length &&
202
- meta.maps.some((m) => !!m.value.arrays.length)
203
- )
204
- throw notSupportedError({ method })(
205
- "map type with array value type",
206
- );
207
- // UNION WITH MAP
208
- else if (meta.size() > 1 && meta.maps.length)
209
- throw notSupportedError({ method })("union type with map type");
210
- // UNION IN MAP
211
- else if (
212
- meta.maps.length &&
213
- meta.maps.some((m) => m.value.isBinaryUnion())
214
- )
215
- throw notSupportedError({ method })("union type in map");
216
- };
217
- }
218
-
219
- const prefix = (method: string) => `Error on typia.protobuf.${method}():`;
220
-
221
- const notSupportedError = (p: { method: string }) => (title: string) =>
222
- new Error(
223
- `${prefix(p.method)}: protocol buffer does not support ${title}.`,
224
- );
225
-
226
- const isDynamicObject = (obj: MetadataObject): boolean =>
227
- obj.properties[0]!.key.isSoleLiteral() === false;
228
-
229
- const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
230
- ["Date", "string"],
231
- ["Boolean", "boolean"],
232
- ["BigInt", "bigint"],
233
- ["Number", "number"],
234
- ["String", "string"],
235
- ...[
236
- "Buffer",
237
- "Uint8ClampedArray",
238
- "Uint16Array",
239
- "Uint32Array",
240
- "BigUint64Array",
241
- "Int8Array",
242
- "Int16Array",
243
- "Int32Array",
244
- "BigInt64Array",
245
- "Float32Array",
246
- "Float64Array",
247
- "DataView",
248
- "ArrayBuffer",
249
- "SharedArrayBuffer",
250
- ].map((name) => [name, "Uint8Array"] as const),
251
- ]);
1
+ import ts from "typescript";
2
+
3
+ import { Metadata } from "../schemas/metadata/Metadata";
4
+ import { MetadataObject } from "../schemas/metadata/MetadataObject";
5
+
6
+ import { Escaper } from "../utils/Escaper";
7
+
8
+ import { MetadataCollection } from "./MetadataCollection";
9
+ import { MetadataFactory } from "./MetadataFactory";
10
+
11
+ export namespace ProtobufFactory {
12
+ export const metadata =
13
+ (method: string) =>
14
+ (checker: ts.TypeChecker) =>
15
+ (collection: MetadataCollection) =>
16
+ (type: ts.Type) => {
17
+ // COMPOSE METADATA WITH INDIVIDUAL VALIDATIONS
18
+ const top: Metadata = MetadataFactory.analyze(checker)({
19
+ resolve: false,
20
+ constant: true,
21
+ absorb: true,
22
+ validate: validate(method),
23
+ })(collection)(type);
24
+
25
+ // TOP LEVEL TYPE MUST BE A SINGLE OBJECT
26
+ const onlyObject: boolean =
27
+ top.size() === 1 &&
28
+ top.objects.length === 1 &&
29
+ top.objects[0]!.properties.every((p) =>
30
+ p.key.isSoleLiteral(),
31
+ ) &&
32
+ top.isRequired() === true &&
33
+ top.nullable === false;
34
+ if (onlyObject === false)
35
+ throw new Error(
36
+ `${prefix(
37
+ method,
38
+ )} target type must be a sole and static object.`,
39
+ );
40
+
41
+ return top;
42
+ };
43
+
44
+ const validate = (method: string) => (meta: Metadata) => {
45
+ //----
46
+ // NOT SUPPORTED TYPES
47
+ //----
48
+ // PROHIBIT ANY TYPE
49
+ if (meta.any) throw notSupportedError({ method })("any type");
50
+ // PROHIBIT FUNCTIONAL TYPE
51
+ else if (meta.functional)
52
+ throw notSupportedError({ method })("functional type");
53
+ // PROHIBIT TUPLE TYPE
54
+ else if (meta.tuples.length)
55
+ throw notSupportedError({ method })("tuple type");
56
+ // PROHIBIT SET TYPE
57
+ else if (meta.sets.length)
58
+ throw notSupportedError({ method })("Set type");
59
+ // NATIVE TYPE, BUT NOT Uint8Array
60
+ else if (meta.natives.length) {
61
+ const banned = meta.natives
62
+ .map((n) => [n, BANNED_NATIVE_TYPES.get(n)] as const)
63
+ .filter(([_n, b]) => b !== undefined)[0];
64
+ if (banned !== undefined)
65
+ throw notSupportedError({ method })(
66
+ banned[1] === null
67
+ ? banned[0]
68
+ : `${banned[0]}. Use ${banned[1]} instead.`,
69
+ );
70
+ }
71
+ //----
72
+ // ARRRAY CASES
73
+ //----
74
+ // DO NOT ALLOW MULTI-DIMENTIONAL ARRAY
75
+ else if (
76
+ meta.arrays.length &&
77
+ meta.arrays.some((array) => !!array.value.arrays.length)
78
+ )
79
+ throw notSupportedError({ method })("two dimenstional array type");
80
+ // CHILD OF ARRAY TYPE MUST BE REQUIRED
81
+ else if (
82
+ meta.arrays.length &&
83
+ meta.arrays.some(
84
+ (array) =>
85
+ array.value.isRequired() === false ||
86
+ array.value.nullable === true,
87
+ )
88
+ )
89
+ throw notSupportedError({ method })("optional type in array");
90
+ // UNION IN ARRAY
91
+ else if (
92
+ meta.arrays.length &&
93
+ meta.arrays.some((a) => a.value.size() > 1)
94
+ )
95
+ throw notSupportedError({ method })("union type in array");
96
+ // UNION WITH ARRAY
97
+ else if (meta.size() > 1 && meta.arrays.length)
98
+ throw notSupportedError({ method })("union type with array type");
99
+ //----
100
+ // OBJECT CASES
101
+ //----
102
+ // EMPTY PROPERTY
103
+ else if (
104
+ meta.objects.length &&
105
+ meta.objects.some((obj) => obj.properties.length === 0)
106
+ )
107
+ throw notSupportedError({ method })("empty object type");
108
+ // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
109
+ else if (
110
+ meta.objects.length &&
111
+ meta.objects.some(
112
+ (obj) =>
113
+ obj.properties.filter((p) => !p.key.isSoleLiteral())
114
+ .length > 1,
115
+ )
116
+ )
117
+ throw notSupportedError({ method })(
118
+ "object type with multiple dynamic key typed properties. Keep only one.",
119
+ );
120
+ // STATIC AND DYNAMIC PROPERTIES ARE COMPATIBLE
121
+ else if (
122
+ meta.objects.length &&
123
+ meta.objects.some(
124
+ (obj) =>
125
+ obj.properties.some((p) => p.key.isSoleLiteral()) &&
126
+ obj.properties.some((p) => !p.key.isSoleLiteral()),
127
+ )
128
+ )
129
+ throw notSupportedError({ method })(
130
+ "object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
131
+ );
132
+ // STATIC PROPERTY, BUT INVALID KEY NAME
133
+ else if (
134
+ meta.objects.length &&
135
+ meta.objects.some((obj) =>
136
+ obj.properties.some(
137
+ (p) =>
138
+ p.key.isSoleLiteral() &&
139
+ !Escaper.variable(p.key.getSoleLiteral()!),
140
+ ),
141
+ )
142
+ )
143
+ throw notSupportedError({ method })(
144
+ `object type with invalid static key name.`,
145
+ );
146
+ // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
147
+ else if (
148
+ meta.objects.length &&
149
+ isDynamicObject(meta.objects[0]!) &&
150
+ meta.objects[0]!.properties.some((p) => !!p.value.arrays.length)
151
+ )
152
+ throw notSupportedError({ method })(
153
+ "dynamic object with array value type",
154
+ );
155
+ // UNION WITH DYNAMIC OBJECT
156
+ else if (
157
+ meta.size() > 1 &&
158
+ meta.objects.length &&
159
+ isDynamicObject(meta.objects[0]!)
160
+ )
161
+ throw notSupportedError({ method })(
162
+ "union type with dynamic object type",
163
+ );
164
+ // UNION IN DYNAMIC PROPERTY VALUE
165
+ else if (
166
+ meta.objects.length &&
167
+ meta.objects.some(
168
+ (obj) =>
169
+ isDynamicObject(obj) &&
170
+ obj.properties.some((p) => p.value.isBinaryUnion()),
171
+ )
172
+ )
173
+ throw notSupportedError({ method })(
174
+ "union type in dynamic property",
175
+ );
176
+ //----
177
+ // MAP CASES
178
+ //----
179
+ // MAP TYPE, BUT PROPERTY KEY TYPE IS NOT STRING
180
+ else if (
181
+ meta.maps.length &&
182
+ meta.maps.some(
183
+ (m) =>
184
+ m.key.isBinaryUnion() ||
185
+ (m.key.atomics.find((v) => v === "string") === undefined &&
186
+ m.key.constants.find((c) => c.type === "string") ===
187
+ undefined),
188
+ )
189
+ )
190
+ throw notSupportedError({ method })("non-string key typed map");
191
+ // MAP TYPE, BUT PROPERTY KEY TYPE IS OPTIONAL
192
+ else if (
193
+ meta.maps.length &&
194
+ meta.maps.some(
195
+ (m) => m.key.isRequired() === false || m.key.nullable,
196
+ )
197
+ )
198
+ throw notSupportedError({ method })("optional key typed map");
199
+ // MAP TYPE, BUT VALUE TYPE IS ARRAY
200
+ else if (
201
+ meta.maps.length &&
202
+ meta.maps.some((m) => !!m.value.arrays.length)
203
+ )
204
+ throw notSupportedError({ method })(
205
+ "map type with array value type",
206
+ );
207
+ // UNION WITH MAP
208
+ else if (meta.size() > 1 && meta.maps.length)
209
+ throw notSupportedError({ method })("union type with map type");
210
+ // UNION IN MAP
211
+ else if (
212
+ meta.maps.length &&
213
+ meta.maps.some((m) => m.value.isBinaryUnion())
214
+ )
215
+ throw notSupportedError({ method })("union type in map");
216
+ };
217
+ }
218
+
219
+ const prefix = (method: string) => `Error on typia.protobuf.${method}():`;
220
+
221
+ const notSupportedError = (p: { method: string }) => (title: string) =>
222
+ new Error(
223
+ `${prefix(p.method)}: protocol buffer does not support ${title}.`,
224
+ );
225
+
226
+ const isDynamicObject = (obj: MetadataObject): boolean =>
227
+ obj.properties[0]!.key.isSoleLiteral() === false;
228
+
229
+ const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
230
+ ["Date", "string"],
231
+ ["Boolean", "boolean"],
232
+ ["BigInt", "bigint"],
233
+ ["Number", "number"],
234
+ ["String", "string"],
235
+ ...[
236
+ "Buffer",
237
+ "Uint8ClampedArray",
238
+ "Uint16Array",
239
+ "Uint32Array",
240
+ "BigUint64Array",
241
+ "Int8Array",
242
+ "Int16Array",
243
+ "Int32Array",
244
+ "BigInt64Array",
245
+ "Float32Array",
246
+ "Float64Array",
247
+ "DataView",
248
+ "ArrayBuffer",
249
+ "SharedArrayBuffer",
250
+ ].map((name) => [name, "Uint8Array"] as const),
251
+ ]);
@@ -1,72 +1,72 @@
1
- import ts from "typescript";
2
-
3
- export namespace StatementFactory {
4
- export const mut = (name: string, initializer?: ts.Expression) =>
5
- ts.factory.createVariableStatement(
6
- undefined,
7
- ts.factory.createVariableDeclarationList(
8
- [
9
- ts.factory.createVariableDeclaration(
10
- name,
11
- undefined,
12
- undefined,
13
- initializer,
14
- ),
15
- ],
16
- ts.NodeFlags.Let,
17
- ),
18
- );
19
-
20
- export const constant = (name: string, initializer?: ts.Expression) =>
21
- ts.factory.createVariableStatement(
22
- undefined,
23
- ts.factory.createVariableDeclarationList(
24
- [
25
- ts.factory.createVariableDeclaration(
26
- name,
27
- undefined,
28
- undefined,
29
- initializer,
30
- ),
31
- ],
32
- ts.NodeFlags.Const,
33
- ),
34
- );
35
-
36
- export const entry = (key: string) => (value: string) =>
37
- ts.factory.createVariableDeclarationList(
38
- [
39
- ts.factory.createVariableDeclaration(
40
- ts.factory.createArrayBindingPattern([
41
- ts.factory.createBindingElement(
42
- undefined,
43
- undefined,
44
- ts.factory.createIdentifier(key),
45
- undefined,
46
- ),
47
- ts.factory.createBindingElement(
48
- undefined,
49
- undefined,
50
- ts.factory.createIdentifier(value),
51
- undefined,
52
- ),
53
- ]),
54
- undefined,
55
- undefined,
56
- undefined,
57
- ),
58
- ],
59
- ts.NodeFlags.Const,
60
- );
61
-
62
- export const transpile = (script: string) =>
63
- ts.factory.createExpressionStatement(
64
- ts.factory.createIdentifier(ts.transpile(script)),
65
- );
66
-
67
- export const block = (expression: ts.Expression) =>
68
- ts.factory.createBlock(
69
- [ts.factory.createExpressionStatement(expression)],
70
- true,
71
- );
72
- }
1
+ import ts from "typescript";
2
+
3
+ export namespace StatementFactory {
4
+ export const mut = (name: string, initializer?: ts.Expression) =>
5
+ ts.factory.createVariableStatement(
6
+ undefined,
7
+ ts.factory.createVariableDeclarationList(
8
+ [
9
+ ts.factory.createVariableDeclaration(
10
+ name,
11
+ undefined,
12
+ undefined,
13
+ initializer,
14
+ ),
15
+ ],
16
+ ts.NodeFlags.Let,
17
+ ),
18
+ );
19
+
20
+ export const constant = (name: string, initializer?: ts.Expression) =>
21
+ ts.factory.createVariableStatement(
22
+ undefined,
23
+ ts.factory.createVariableDeclarationList(
24
+ [
25
+ ts.factory.createVariableDeclaration(
26
+ name,
27
+ undefined,
28
+ undefined,
29
+ initializer,
30
+ ),
31
+ ],
32
+ ts.NodeFlags.Const,
33
+ ),
34
+ );
35
+
36
+ export const entry = (key: string) => (value: string) =>
37
+ ts.factory.createVariableDeclarationList(
38
+ [
39
+ ts.factory.createVariableDeclaration(
40
+ ts.factory.createArrayBindingPattern([
41
+ ts.factory.createBindingElement(
42
+ undefined,
43
+ undefined,
44
+ ts.factory.createIdentifier(key),
45
+ undefined,
46
+ ),
47
+ ts.factory.createBindingElement(
48
+ undefined,
49
+ undefined,
50
+ ts.factory.createIdentifier(value),
51
+ undefined,
52
+ ),
53
+ ]),
54
+ undefined,
55
+ undefined,
56
+ undefined,
57
+ ),
58
+ ],
59
+ ts.NodeFlags.Const,
60
+ );
61
+
62
+ export const transpile = (script: string) =>
63
+ ts.factory.createExpressionStatement(
64
+ ts.factory.createIdentifier(ts.transpile(script)),
65
+ );
66
+
67
+ export const block = (expression: ts.Expression) =>
68
+ ts.factory.createBlock(
69
+ [ts.factory.createExpressionStatement(expression)],
70
+ true,
71
+ );
72
+ }