typia 5.0.0-dev.20230811 → 5.0.0-dev.20230814

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 (190) hide show
  1. package/lib/factories/MetadataCollection.js +1 -0
  2. package/lib/factories/MetadataCollection.js.map +1 -1
  3. package/lib/factories/MetadataFactory.js +27 -0
  4. package/lib/factories/MetadataFactory.js.map +1 -1
  5. package/lib/factories/ProtobufFactory.d.ts +0 -2
  6. package/lib/factories/ProtobufFactory.js +45 -116
  7. package/lib/factories/ProtobufFactory.js.map +1 -1
  8. package/lib/factories/StatementFactory.d.ts +1 -0
  9. package/lib/factories/StatementFactory.js +5 -0
  10. package/lib/factories/StatementFactory.js.map +1 -1
  11. package/lib/factories/internal/metadata/explore_metadata.js +3 -7
  12. package/lib/factories/internal/metadata/explore_metadata.js.map +1 -1
  13. package/lib/factories/internal/metadata/iterate_metadata_intersection.js +1 -0
  14. package/lib/factories/internal/metadata/iterate_metadata_intersection.js.map +1 -1
  15. package/lib/functional/$strlen.d.ts +1 -0
  16. package/lib/functional/$strlen.js +13 -0
  17. package/lib/functional/$strlen.js.map +1 -0
  18. package/lib/functional/Namespace.js +19 -3
  19. package/lib/functional/Namespace.js.map +1 -1
  20. package/lib/json.js +4 -4
  21. package/lib/json.js.map +1 -1
  22. package/lib/misc.js +8 -8
  23. package/lib/misc.js.map +1 -1
  24. package/lib/programmers/CheckerProgrammer.js +0 -5
  25. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  26. package/lib/programmers/FeatureProgrammer.d.ts +3 -3
  27. package/lib/programmers/FeatureProgrammer.js.map +1 -1
  28. package/lib/programmers/helpers/ProtobufUtil.d.ts +5 -0
  29. package/lib/programmers/helpers/ProtobufUtil.js +82 -0
  30. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -0
  31. package/lib/programmers/helpers/ProtobufWire.d.ts +25 -0
  32. package/lib/{schemas/protobuf/IProtocolMap.js → programmers/helpers/ProtobufWire.js} +1 -1
  33. package/lib/programmers/helpers/ProtobufWire.js.map +1 -0
  34. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +253 -5
  35. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  36. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +19 -33
  37. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  38. package/lib/programmers/protobuf/ProtobufSizeProgrammer.d.ts +5 -0
  39. package/lib/programmers/protobuf/ProtobufSizeProgrammer.js +409 -0
  40. package/lib/programmers/protobuf/ProtobufSizeProgrammer.js.map +1 -0
  41. package/lib/protobuf.js +9 -1
  42. package/lib/protobuf.js.map +1 -1
  43. package/lib/schemas/metadata/Metadata.js +11 -0
  44. package/lib/schemas/metadata/Metadata.js.map +1 -1
  45. package/lib/transformers/CallExpressionTransformer.js +26 -22
  46. package/lib/transformers/CallExpressionTransformer.js.map +1 -1
  47. package/lib/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.js +1 -1
  48. package/lib/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.js.map +1 -1
  49. package/lib/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.js +1 -1
  50. package/lib/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.js.map +1 -1
  51. package/lib/transformers/features/protobuf/CreateProtobufDecodeTransformer.js +1 -3
  52. package/lib/transformers/features/protobuf/CreateProtobufDecodeTransformer.js.map +1 -1
  53. package/lib/transformers/features/protobuf/CreateProtobufEncodeTransformer.js +1 -3
  54. package/lib/transformers/features/protobuf/CreateProtobufEncodeTransformer.js.map +1 -1
  55. package/lib/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.js +1 -1
  56. package/lib/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.js.map +1 -1
  57. package/lib/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.js +1 -1
  58. package/lib/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.js.map +1 -1
  59. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.d.ts +3 -0
  60. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js +10 -0
  61. package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js.map +1 -0
  62. package/lib/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.js +1 -1
  63. package/lib/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.js.map +1 -1
  64. package/lib/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.js +1 -1
  65. package/lib/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.js.map +1 -1
  66. package/lib/transformers/features/protobuf/ProtobufAssertDecodeTransformer.js +1 -1
  67. package/lib/transformers/features/protobuf/ProtobufAssertDecodeTransformer.js.map +1 -1
  68. package/lib/transformers/features/protobuf/ProtobufAssertEncodeTransformer.js +1 -1
  69. package/lib/transformers/features/protobuf/ProtobufAssertEncodeTransformer.js.map +1 -1
  70. package/lib/transformers/features/protobuf/ProtobufDecodeTransformer.js +1 -1
  71. package/lib/transformers/features/protobuf/ProtobufDecodeTransformer.js.map +1 -1
  72. package/lib/transformers/features/protobuf/ProtobufEncodeTransformer.js +1 -1
  73. package/lib/transformers/features/protobuf/ProtobufEncodeTransformer.js.map +1 -1
  74. package/lib/transformers/features/protobuf/ProtobufIsDecodeTransformer.js +1 -1
  75. package/lib/transformers/features/protobuf/ProtobufIsDecodeTransformer.js.map +1 -1
  76. package/lib/transformers/features/protobuf/ProtobufIsEncodeTransformer.js +1 -1
  77. package/lib/transformers/features/protobuf/ProtobufIsEncodeTransformer.js.map +1 -1
  78. package/lib/transformers/features/protobuf/ProtobufMessageTransformer.d.ts +1 -1
  79. package/lib/transformers/features/protobuf/ProtobufMessageTransformer.js +7 -7
  80. package/lib/transformers/features/protobuf/ProtobufMessageTransformer.js.map +1 -1
  81. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.d.ts +3 -0
  82. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js +10 -0
  83. package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js.map +1 -0
  84. package/lib/transformers/features/protobuf/ProtobufValidateDecodeTransformer.js +1 -1
  85. package/lib/transformers/features/protobuf/ProtobufValidateDecodeTransformer.js.map +1 -1
  86. package/lib/transformers/features/protobuf/ProtobufValidateEncodeTransformer.js +1 -1
  87. package/lib/transformers/features/protobuf/ProtobufValidateEncodeTransformer.js.map +1 -1
  88. package/package.json +1 -1
  89. package/src/factories/MetadataCollection.ts +5 -0
  90. package/src/factories/MetadataFactory.ts +4 -0
  91. package/src/factories/ProtobufFactory.ts +86 -99
  92. package/src/factories/StatementFactory.ts +16 -0
  93. package/src/factories/internal/metadata/explore_metadata.ts +4 -6
  94. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +1 -0
  95. package/src/functional/$strlen.ts +7 -0
  96. package/src/functional/Namespace.ts +32 -17
  97. package/src/json.ts +4 -4
  98. package/src/misc.ts +8 -8
  99. package/src/programmers/CheckerProgrammer.ts +0 -9
  100. package/src/programmers/FeatureProgrammer.ts +5 -3
  101. package/src/programmers/helpers/ProtobufUtil.ts +24 -0
  102. package/src/programmers/helpers/ProtobufWire.ts +28 -0
  103. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +446 -5
  104. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +6 -4
  105. package/src/programmers/protobuf/ProtobufSizeProgrammer.ts +716 -0
  106. package/src/protobuf.ts +29 -0
  107. package/src/schemas/metadata/Metadata.ts +26 -0
  108. package/src/transformers/CallExpressionTransformer.ts +32 -28
  109. package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +3 -1
  110. package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +3 -1
  111. package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +3 -4
  112. package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +3 -4
  113. package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +3 -1
  114. package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +3 -1
  115. package/src/transformers/features/protobuf/CreateProtobufSizeTransformer.ts +9 -0
  116. package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +3 -1
  117. package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +3 -1
  118. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +1 -1
  119. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +1 -1
  120. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +1 -1
  121. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +1 -1
  122. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +1 -1
  123. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +1 -1
  124. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +3 -3
  125. package/src/transformers/features/protobuf/ProtobufSizeTransformer.ts +9 -0
  126. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +3 -1
  127. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +3 -1
  128. package/lib/factories/internal/protocols/ProtocolMetadataUtil.d.ts +0 -16
  129. package/lib/factories/internal/protocols/ProtocolMetadataUtil.js +0 -173
  130. package/lib/factories/internal/protocols/ProtocolMetadataUtil.js.map +0 -1
  131. package/lib/factories/internal/protocols/emplace_protocol_object.d.ts +0 -3
  132. package/lib/factories/internal/protocols/emplace_protocol_object.js +0 -47
  133. package/lib/factories/internal/protocols/emplace_protocol_object.js.map +0 -1
  134. package/lib/factories/internal/protocols/emplace_protocol_property.d.ts +0 -4
  135. package/lib/factories/internal/protocols/emplace_protocol_property.js +0 -20
  136. package/lib/factories/internal/protocols/emplace_protocol_property.js.map +0 -1
  137. package/lib/factories/internal/protocols/iterate_protocol_atomic.d.ts +0 -3
  138. package/lib/factories/internal/protocols/iterate_protocol_atomic.js +0 -69
  139. package/lib/factories/internal/protocols/iterate_protocol_atomic.js.map +0 -1
  140. package/lib/factories/internal/protocols/iterate_protocol_constant.d.ts +0 -2
  141. package/lib/factories/internal/protocols/iterate_protocol_constant.js +0 -30
  142. package/lib/factories/internal/protocols/iterate_protocol_constant.js.map +0 -1
  143. package/lib/factories/internal/protocols/iterate_protocol_main.d.ts +0 -3
  144. package/lib/factories/internal/protocols/iterate_protocol_main.js +0 -17
  145. package/lib/factories/internal/protocols/iterate_protocol_main.js.map +0 -1
  146. package/lib/factories/internal/protocols/iterate_protocol_map.d.ts +0 -4
  147. package/lib/factories/internal/protocols/iterate_protocol_map.js +0 -75
  148. package/lib/factories/internal/protocols/iterate_protocol_map.js.map +0 -1
  149. package/lib/factories/internal/protocols/iterate_protocol_metadata.d.ts +0 -5
  150. package/lib/factories/internal/protocols/iterate_protocol_metadata.js +0 -190
  151. package/lib/factories/internal/protocols/iterate_protocol_metadata.js.map +0 -1
  152. package/lib/factories/internal/protocols/iterate_protocol_native.d.ts +0 -2
  153. package/lib/factories/internal/protocols/iterate_protocol_native.js +0 -33
  154. package/lib/factories/internal/protocols/iterate_protocol_native.js.map +0 -1
  155. package/lib/factories/internal/protocols/iterate_protocol_never.d.ts +0 -1
  156. package/lib/factories/internal/protocols/iterate_protocol_never.js +0 -6
  157. package/lib/factories/internal/protocols/iterate_protocol_never.js.map +0 -1
  158. package/lib/factories/internal/protocols/iterate_protocol_object.d.ts +0 -4
  159. package/lib/factories/internal/protocols/iterate_protocol_object.js +0 -157
  160. package/lib/factories/internal/protocols/iterate_protocol_object.js.map +0 -1
  161. package/lib/factories/internal/protocols/iterate_protocol_repeated.d.ts +0 -5
  162. package/lib/factories/internal/protocols/iterate_protocol_repeated.js +0 -25
  163. package/lib/factories/internal/protocols/iterate_protocol_repeated.js.map +0 -1
  164. package/lib/factories/internal/protocols/iterate_protocol_tuple.d.ts +0 -3
  165. package/lib/factories/internal/protocols/iterate_protocol_tuple.js +0 -46
  166. package/lib/factories/internal/protocols/iterate_protocol_tuple.js.map +0 -1
  167. package/lib/schemas/protobuf/IProtocolMap.d.ts +0 -5
  168. package/lib/schemas/protobuf/IProtocolMap.js.map +0 -1
  169. package/lib/schemas/protobuf/IProtocolMessage.d.ts +0 -5
  170. package/lib/schemas/protobuf/IProtocolMessage.js +0 -3
  171. package/lib/schemas/protobuf/IProtocolMessage.js.map +0 -1
  172. package/lib/schemas/protobuf/IProtocolProperty.d.ts +0 -12
  173. package/lib/schemas/protobuf/IProtocolProperty.js +0 -3
  174. package/lib/schemas/protobuf/IProtocolProperty.js.map +0 -1
  175. package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +0 -180
  176. package/src/factories/internal/protocols/emplace_protocol_object.ts +0 -25
  177. package/src/factories/internal/protocols/emplace_protocol_property.ts +0 -12
  178. package/src/factories/internal/protocols/iterate_protocol_atomic.ts +0 -34
  179. package/src/factories/internal/protocols/iterate_protocol_constant.ts +0 -27
  180. package/src/factories/internal/protocols/iterate_protocol_main.ts +0 -19
  181. package/src/factories/internal/protocols/iterate_protocol_map.ts +0 -58
  182. package/src/factories/internal/protocols/iterate_protocol_metadata.ts +0 -96
  183. package/src/factories/internal/protocols/iterate_protocol_native.ts +0 -34
  184. package/src/factories/internal/protocols/iterate_protocol_never.ts +0 -1
  185. package/src/factories/internal/protocols/iterate_protocol_object.ts +0 -110
  186. package/src/factories/internal/protocols/iterate_protocol_repeated.ts +0 -34
  187. package/src/factories/internal/protocols/iterate_protocol_tuple.ts +0 -29
  188. package/src/schemas/protobuf/IProtocolMap.ts +0 -5
  189. package/src/schemas/protobuf/IProtocolMessage.ts +0 -6
  190. package/src/schemas/protobuf/IProtocolProperty.ts +0 -13
@@ -2,14 +2,11 @@ import ts from "typescript";
2
2
 
3
3
  import { Metadata } from "../schemas/metadata/Metadata";
4
4
  import { MetadataObject } from "../schemas/metadata/MetadataObject";
5
- import { IProtocolMessage } from "../schemas/protobuf/IProtocolMessage";
6
5
 
7
6
  import { Escaper } from "../utils/Escaper";
8
7
 
9
8
  import { MetadataCollection } from "./MetadataCollection";
10
9
  import { MetadataFactory } from "./MetadataFactory";
11
- import { emplace_protocol_object } from "./internal/protocols/emplace_protocol_object";
12
- import { iterate_protocol_main } from "./internal/protocols/iterate_protocol_main";
13
10
 
14
11
  export namespace ProtobufFactory {
15
12
  export const metadata =
@@ -44,37 +41,70 @@ export namespace ProtobufFactory {
44
41
  return top;
45
42
  };
46
43
 
47
- export const analyze =
48
- (collection: MetadataCollection) =>
49
- (dict: Map<string, IProtocolMessage>) =>
50
- (meta: Metadata) => {
51
- // EMPLACE OBJECTS
52
- for (const obj of collection.objects())
53
- emplace_protocol_object(dict)(obj);
54
-
55
- // WHEN NOT OBJECT, WRAP IT INTO A FAKE MAIN OBJECT
56
- iterate_protocol_main(dict)(meta);
57
- };
58
-
59
44
  const validate = (method: string) => (meta: Metadata) => {
60
45
  //----
61
- // NOT ALLOWED TYPES
46
+ // NOT SUPPORTED TYPES
62
47
  //----
48
+ // PROHIBIT ANY TYPE
63
49
  if (meta.any) throw notSupportedError({ method })("any type");
50
+ // PROHIBIT FUNCTIONAL TYPE
64
51
  else if (meta.functional)
65
52
  throw notSupportedError({ method })("functional type");
53
+ // PROHIBIT TUPLE TYPE
66
54
  else if (meta.tuples.length)
67
55
  throw notSupportedError({ method })("tuple type");
56
+ // PROHIBIT SET TYPE
68
57
  else if (meta.sets.length)
69
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
70
75
  else if (
71
76
  meta.arrays.length &&
72
77
  meta.arrays.some((array) => !!array.value.arrays.length)
73
78
  )
74
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");
75
99
  //----
76
- // SPECIAL CASES
100
+ // OBJECT CASES
77
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");
78
108
  // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
79
109
  else if (
80
110
  meta.objects.length &&
@@ -92,7 +122,6 @@ export namespace ProtobufFactory {
92
122
  meta.objects.length &&
93
123
  meta.objects.some(
94
124
  (obj) =>
95
- obj.properties.length &&
96
125
  obj.properties.some((p) => p.key.isSoleLiteral()) &&
97
126
  obj.properties.some((p) => !p.key.isSoleLiteral()),
98
127
  )
@@ -114,29 +143,7 @@ export namespace ProtobufFactory {
114
143
  throw notSupportedError({ method })(
115
144
  `object type with invalid static key name.`,
116
145
  );
117
- // NATIVE TYPE, BUT NOT Uint8Array
118
- else if (meta.natives.length) {
119
- const banned = meta.natives
120
- .map((n) => [n, BANNED_NATIVE_TYPES.get(n)] as const)
121
- .filter(([_n, b]) => b !== undefined)[0];
122
- if (banned !== undefined)
123
- throw notSupportedError({ method })(
124
- banned[1] === null
125
- ? banned[0]
126
- : `${banned[0]}. Use ${banned[1]} instead.`,
127
- );
128
- }
129
- // MAP TYPE, BUT PROPERTY KEY TYPE IS NOT ATOMIC
130
- else if (meta.maps.length && meta.maps.some((m) => !isAtomicKey(m.key)))
131
- throw notSupportedError({ method })("");
132
- // MAP TYPE, BUT VALUE TYPE IS ARRAY
133
- else if (
134
- meta.maps.length &&
135
- meta.maps.some((m) => !!m.value.arrays.length)
136
- )
137
- throw notSupportedError({ method })(
138
- "map type with array value type",
139
- );
146
+ // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
140
147
  else if (
141
148
  meta.objects.length &&
142
149
  isDynamicObject(meta.objects[0]!) &&
@@ -145,18 +152,6 @@ export namespace ProtobufFactory {
145
152
  throw notSupportedError({ method })(
146
153
  "dynamic object with array value type",
147
154
  );
148
- //----
149
- // UNION TYPES
150
- //----
151
- // UNION IN ARRAY
152
- else if (
153
- meta.arrays.length &&
154
- meta.arrays.some((a) => a.value.size() > 1)
155
- )
156
- throw notSupportedError({ method })("union type in array");
157
- // UNION WITH ARRAY
158
- else if (meta.size() > 1 && meta.arrays.length)
159
- throw notSupportedError({ method })("union type with array type");
160
155
  // UNION WITH DYNAMIC OBJECT
161
156
  else if (
162
157
  meta.size() > 1 &&
@@ -172,17 +167,51 @@ export namespace ProtobufFactory {
172
167
  meta.objects.some(
173
168
  (obj) =>
174
169
  isDynamicObject(obj) &&
175
- obj.properties.some((p) => isUnion(p.value)),
170
+ obj.properties.some((p) => p.value.isBinaryUnion()),
176
171
  )
177
172
  )
178
173
  throw notSupportedError({ method })(
179
174
  "union type in dynamic property",
180
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
+ );
181
207
  // UNION WITH MAP
182
208
  else if (meta.size() > 1 && meta.maps.length)
183
209
  throw notSupportedError({ method })("union type with map type");
184
210
  // UNION IN MAP
185
- else if (meta.maps.length && meta.maps.some((m) => isUnion(m.value)))
211
+ else if (
212
+ meta.maps.length &&
213
+ meta.maps.some((m) => m.value.isBinaryUnion())
214
+ )
186
215
  throw notSupportedError({ method })("union type in map");
187
216
  };
188
217
  }
@@ -194,50 +223,8 @@ const notSupportedError = (p: { method: string }) => (title: string) =>
194
223
  `${prefix(p.method)}: protocol buffer does not support ${title}.`,
195
224
  );
196
225
 
197
- const isAtomicKey = (key: Metadata) => {
198
- if (
199
- key.required &&
200
- key.nullable === false &&
201
- key.functional === false &&
202
- key.resolved === null &&
203
- key.size() ===
204
- key.atomics.length +
205
- key.constants
206
- .map((c) => c.values.length)
207
- .reduce((a, b) => a + b, 0) +
208
- key.templates.length
209
- ) {
210
- const set: Set<string> = new Set();
211
- for (const atomic of key.atomics) set.add(atomic);
212
- for (const constant of key.constants) set.add(constant.type);
213
- if (key.templates.length) set.add("string");
214
-
215
- return set.size === 1;
216
- }
217
- return false;
218
- };
219
-
220
- const isDynamicObject = (obj: MetadataObject): boolean => {
221
- return (
222
- obj.properties.length > 0 &&
223
- obj.properties[0]!.key.isSoleLiteral() === false
224
- );
225
- };
226
-
227
- const isUnion = (meta: Metadata): boolean => {
228
- const count: number =
229
- new Set([
230
- ...meta.atomics,
231
- ...meta.constants.map((c) => c.type),
232
- ...(meta.templates.length ? ["string"] : []),
233
- ]).size +
234
- meta.arrays.length +
235
- meta.tuples.length +
236
- meta.natives.length +
237
- meta.objects.length +
238
- meta.maps.length;
239
- return count > 1;
240
- };
226
+ const isDynamicObject = (obj: MetadataObject): boolean =>
227
+ obj.properties[0]!.key.isSoleLiteral() === false;
241
228
 
242
229
  const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
243
230
  ["Date", "string"],
@@ -1,6 +1,22 @@
1
1
  import ts from "typescript";
2
2
 
3
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
+
4
20
  export const constant = (name: string, initializer?: ts.Expression) =>
5
21
  ts.factory.createVariableStatement(
6
22
  undefined,
@@ -18,11 +18,9 @@ export const explore_metadata =
18
18
  ): Metadata => {
19
19
  // CONSTRUCT METADATA
20
20
  const meta: Metadata = Metadata.initialize(parentResolved);
21
- const out = (meta: Metadata) => {
22
- if (options.validate) options.validate(meta);
23
- return meta;
24
- };
25
- if (type === null) return out(meta);
21
+ collection.entire_.add(meta);
22
+
23
+ if (type === null) return meta;
26
24
 
27
25
  // ITERATE TYPESCRIPT TYPES
28
26
  iterate_metadata(checker)(options)(collection)(
@@ -36,5 +34,5 @@ export const explore_metadata =
36
34
  emend_metadata_atomics(meta.resolved.original);
37
35
  emend_metadata_atomics(meta.resolved.returns);
38
36
  }
39
- return out(meta);
37
+ return meta;
40
38
  };
@@ -74,6 +74,7 @@ export const iterate_metadata_intersection =
74
74
  throw new Error(message(children));
75
75
  });
76
76
  Object.assign(meta, Metadata.merge(meta, least));
77
+ collection.entire_.add(least);
77
78
  return true;
78
79
  };
79
80
 
@@ -0,0 +1,7 @@
1
+ export const $strlen = (s: string): number => {
2
+ let b: number;
3
+ let i: number;
4
+ let c: number;
5
+ for (b = i = 0; (c = s.charCodeAt(i++)); b += c >> 11 ? 3 : c >> 7 ? 2 : 1);
6
+ return b;
7
+ };
@@ -1,3 +1,5 @@
1
+ import { Writer } from "protobufjs";
2
+
1
3
  import { RandomGenerator } from "../utils/RandomGenerator";
2
4
 
3
5
  import { IValidation } from "../IValidation";
@@ -19,6 +21,7 @@ import { $number } from "./$number";
19
21
  import { $report } from "./$report";
20
22
  import { $rest } from "./$rest";
21
23
  import { $string } from "./$string";
24
+ import { $strlen } from "./$strlen";
22
25
  import { $tail } from "./$tail";
23
26
 
24
27
  /**
@@ -91,25 +94,37 @@ export namespace Namespace {
91
94
  },
92
95
  });
93
96
 
94
- export const stringify = (method: string) => ({
95
- ...is(),
96
- number: $number,
97
- string: $string,
98
- tail: $tail,
99
- rest: $rest,
100
- throws: $throws(method),
101
- });
97
+ export namespace json {
98
+ export const stringify = (method: string) => ({
99
+ ...is(),
100
+ number: $number,
101
+ string: $string,
102
+ tail: $tail,
103
+ rest: $rest,
104
+ throws: $throws(method),
105
+ });
106
+ }
102
107
 
103
- export const clone = (method: string) => ({
104
- ...is(),
105
- throws: $throws(method),
106
- any: $any,
107
- });
108
+ export namespace protobuf {
109
+ export const encode = (method: string) => ({
110
+ Writer: Writer,
111
+ strl: $strlen,
112
+ throws: $throws(method),
113
+ });
114
+ }
108
115
 
109
- export const prune = (method: string) => ({
110
- ...is(),
111
- throws: $throws(method),
112
- });
116
+ export namespace misc {
117
+ export const clone = (method: string) => ({
118
+ ...is(),
119
+ throws: $throws(method),
120
+ any: $any,
121
+ });
122
+
123
+ export const prune = (method: string) => ({
124
+ ...is(),
125
+ throws: $throws(method),
126
+ });
127
+ }
113
128
 
114
129
  export const random = () => ({
115
130
  generator: RandomGenerator,
package/src/json.ts CHANGED
@@ -254,7 +254,7 @@ export function stringify<T>(input: T): string;
254
254
  export function stringify(): never {
255
255
  halt("stringify");
256
256
  }
257
- Object.assign(stringify, Namespace.stringify("stringify"));
257
+ Object.assign(stringify, Namespace.json.stringify("stringify"));
258
258
 
259
259
  /**
260
260
  * 5x faster `JSON.stringify()` function with type assertion.
@@ -309,7 +309,7 @@ export function assertStringify(): string {
309
309
  halt("assertStringify");
310
310
  }
311
311
  Object.assign(assertStringify, Namespace.assert("assertStringify"));
312
- Object.assign(assertStringify, Namespace.stringify("assertStringify"));
312
+ Object.assign(assertStringify, Namespace.json.stringify("assertStringify"));
313
313
 
314
314
  /**
315
315
  * 7x faster `JSON.stringify()` function with type checking.
@@ -365,7 +365,7 @@ export function isStringify(): string | null {
365
365
  }
366
366
 
367
367
  Object.assign(isStringify, Namespace.is());
368
- Object.assign(isStringify, Namespace.stringify("isStringify"));
368
+ Object.assign(isStringify, Namespace.json.stringify("isStringify"));
369
369
 
370
370
  /**
371
371
  * 5x faster `JSON.stringify()` function with detailed type validation.
@@ -422,7 +422,7 @@ export function validateStringify(): IValidation<string> {
422
422
  halt("validateStringify");
423
423
  }
424
424
  Object.assign(validateStringify, Namespace.validate());
425
- Object.assign(validateStringify, Namespace.stringify("validateStringify"));
425
+ Object.assign(validateStringify, Namespace.json.stringify("validateStringify"));
426
426
 
427
427
  /* -----------------------------------------------------------
428
428
  FACTORY FUNCTIONS
package/src/misc.ts CHANGED
@@ -84,7 +84,7 @@ export function clone<T>(input: T): Primitive<T>;
84
84
  export function clone(): never {
85
85
  halt("clone");
86
86
  }
87
- Object.assign(clone, Namespace.clone("clone"));
87
+ Object.assign(clone, Namespace.misc.clone("clone"));
88
88
 
89
89
  /**
90
90
  * Clone a data with type assertion.
@@ -131,7 +131,7 @@ export function assertClone(): never {
131
131
  halt("assertClone");
132
132
  }
133
133
  Object.assign(assertClone, Namespace.assert("assertClone"));
134
- Object.assign(assertClone, Namespace.clone("assertClone"));
134
+ Object.assign(assertClone, Namespace.misc.clone("assertClone"));
135
135
 
136
136
  /**
137
137
  * Clone a data with type checking.
@@ -178,7 +178,7 @@ export function isClone(): never {
178
178
  halt("isClone");
179
179
  }
180
180
  Object.assign(isClone, Namespace.is());
181
- Object.assign(isClone, Namespace.clone("isClone"));
181
+ Object.assign(isClone, Namespace.misc.clone("isClone"));
182
182
 
183
183
  /**
184
184
  * Clone a data with detailed type validation.
@@ -223,7 +223,7 @@ export function validateClone(): never {
223
223
  halt("validateClone");
224
224
  }
225
225
  Object.assign(validateClone, Namespace.validate());
226
- Object.assign(validateClone, Namespace.clone("validateClone"));
226
+ Object.assign(validateClone, Namespace.misc.clone("validateClone"));
227
227
 
228
228
  /* -----------------------------------------------------------
229
229
  PRUNE
@@ -257,7 +257,7 @@ export function prune<T extends object>(input: T): void;
257
257
  export function prune(): never {
258
258
  halt("prune");
259
259
  }
260
- Object.assign(prune, Namespace.prune("prune"));
260
+ Object.assign(prune, Namespace.misc.prune("prune"));
261
261
 
262
262
  /**
263
263
  * Prune, erase superfluous properties, with type assertion.
@@ -302,7 +302,7 @@ export function assertPrune(): unknown {
302
302
  halt("assertPrune");
303
303
  }
304
304
  Object.assign(assertPrune, Namespace.assert("assertPrune"));
305
- Object.assign(assertPrune, Namespace.prune("assertPrune"));
305
+ Object.assign(assertPrune, Namespace.misc.prune("assertPrune"));
306
306
 
307
307
  /**
308
308
  * Prune, erase superfluous properties, with type checking.
@@ -349,7 +349,7 @@ export function isPrune(): never {
349
349
  halt("isPrune");
350
350
  }
351
351
  Object.assign(isPrune, Namespace.is());
352
- Object.assign(isPrune, Namespace.prune("isPrune"));
352
+ Object.assign(isPrune, Namespace.misc.prune("isPrune"));
353
353
 
354
354
  /**
355
355
  * Prune, erase superfluous properties, with type validation.
@@ -397,7 +397,7 @@ export function validatePrune<T>(input: unknown): IValidation<T>;
397
397
  export function validatePrune<T>(): IValidation<T> {
398
398
  halt("validatePrune");
399
399
  }
400
- Object.assign(validatePrune, Namespace.prune("validatePrune"));
400
+ Object.assign(validatePrune, Namespace.misc.prune("validatePrune"));
401
401
  Object.assign(validatePrune, Namespace.validate());
402
402
 
403
403
  /* -----------------------------------------------------------
@@ -218,15 +218,6 @@ export namespace CheckerProgrammer {
218
218
  resolve: false,
219
219
  constant: true,
220
220
  absorb: true,
221
- validate: (meta) => {
222
- if (
223
- meta.arrays.length > 1 &&
224
- meta.arrays.some((a) => a.recursive)
225
- )
226
- throw new Error(
227
- `Repeated union array types are not supported yet.`,
228
- );
229
- },
230
221
  })(collection)(type);
231
222
  return [collection, meta];
232
223
  },
@@ -59,7 +59,7 @@ export namespace FeatureProgrammer {
59
59
  /**
60
60
  * Object configurator.
61
61
  */
62
- objector: IConfig.IObjector;
62
+ objector: IConfig.IObjector<Output>;
63
63
 
64
64
  /**
65
65
  * Generator of functions for object types.
@@ -72,7 +72,9 @@ export namespace FeatureProgrammer {
72
72
  output: (type: ts.Type, name?: string) => ts.TypeNode;
73
73
  }
74
74
 
75
- export interface IObjector {
75
+ export interface IObjector<
76
+ Output extends ts.ConciseBody = ts.ConciseBody,
77
+ > {
76
78
  /**
77
79
  * Type checker when union object type comes.
78
80
  */
@@ -88,7 +90,7 @@ export namespace FeatureProgrammer {
88
90
  */
89
91
  joiner(
90
92
  input: ts.Expression,
91
- entries: IExpressionEntry[],
93
+ entries: IExpressionEntry<Output>[],
92
94
  parent: MetadataObject,
93
95
  ): ts.ConciseBody;
94
96
 
@@ -0,0 +1,24 @@
1
+ import { Metadata } from "../../schemas/metadata/Metadata";
2
+
3
+ import { Atomic } from "../../typings/Atomic";
4
+
5
+ export namespace ProtobufUtil {
6
+ export const atomics = (meta: Metadata) => {
7
+ const set: Set<Atomic.Literal> = new Set();
8
+
9
+ for (const atomic of meta.atomics) set.add(atomic);
10
+ for (const constant of meta.constants) set.add(constant.type);
11
+ if (meta.templates.length) set.add("string");
12
+
13
+ return [...set].sort(
14
+ (x, y) => ATOMIC_ORDER.get(x)! - ATOMIC_ORDER.get(y)!,
15
+ );
16
+ };
17
+ }
18
+
19
+ const ATOMIC_ORDER = new Map<Atomic.Literal, number>([
20
+ ["boolean", 0],
21
+ ["bigint", 1],
22
+ ["number", 2],
23
+ ["string", 3],
24
+ ]);
@@ -0,0 +1,28 @@
1
+ export const enum ProtobufWire {
2
+ /**
3
+ * integers
4
+ * bool
5
+ * enum
6
+ */
7
+ VARINT = 0,
8
+
9
+ /**
10
+ * fixed64
11
+ * sfixed64
12
+ * double
13
+ */
14
+ I64 = 1,
15
+
16
+ /**
17
+ * string
18
+ * bytes
19
+ * mebedded messages
20
+ * packed repeated fields
21
+ */
22
+ LEN = 2,
23
+
24
+ /**
25
+ * fixed, sfixed32, float
26
+ */
27
+ I32 = 5,
28
+ }