typia 5.0.0-dev.20230810 → 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 +55 -111
  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 +23 -37
  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 +2 -1
  89. package/src/factories/MetadataCollection.ts +5 -0
  90. package/src/factories/MetadataFactory.ts +4 -0
  91. package/src/factories/ProtobufFactory.ts +106 -90
  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 +12 -8
  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,12 +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";
5
+
6
+ import { Escaper } from "../utils/Escaper";
6
7
 
7
8
  import { MetadataCollection } from "./MetadataCollection";
8
9
  import { MetadataFactory } from "./MetadataFactory";
9
- import { emplace_protocol_object } from "./internal/protocols/emplace_protocol_object";
10
- import { iterate_protocol_main } from "./internal/protocols/iterate_protocol_main";
11
10
 
12
11
  export namespace ProtobufFactory {
13
12
  export const metadata =
@@ -42,37 +41,70 @@ export namespace ProtobufFactory {
42
41
  return top;
43
42
  };
44
43
 
45
- export const analyze =
46
- (collection: MetadataCollection) =>
47
- (dict: Map<string, IProtocolMessage>) =>
48
- (meta: Metadata) => {
49
- // EMPLACE OBJECTS
50
- for (const obj of collection.objects())
51
- emplace_protocol_object(dict)(obj);
52
-
53
- // WHEN NOT OBJECT, WRAP IT INTO A FAKE MAIN OBJECT
54
- iterate_protocol_main(dict)(meta);
55
- };
56
-
57
44
  const validate = (method: string) => (meta: Metadata) => {
58
45
  //----
59
- // NOT ALLOWED TYPES
46
+ // NOT SUPPORTED TYPES
60
47
  //----
48
+ // PROHIBIT ANY TYPE
61
49
  if (meta.any) throw notSupportedError({ method })("any type");
50
+ // PROHIBIT FUNCTIONAL TYPE
62
51
  else if (meta.functional)
63
52
  throw notSupportedError({ method })("functional type");
53
+ // PROHIBIT TUPLE TYPE
64
54
  else if (meta.tuples.length)
65
55
  throw notSupportedError({ method })("tuple type");
56
+ // PROHIBIT SET TYPE
66
57
  else if (meta.sets.length)
67
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
68
75
  else if (
69
76
  meta.arrays.length &&
70
77
  meta.arrays.some((array) => !!array.value.arrays.length)
71
78
  )
72
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");
73
99
  //----
74
- // SPECIAL CASES
100
+ // OBJECT CASES
75
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");
76
108
  // MULTIPLE DYNAMIC KEY TYPED PROPERTIES
77
109
  else if (
78
110
  meta.objects.length &&
@@ -90,7 +122,6 @@ export namespace ProtobufFactory {
90
122
  meta.objects.length &&
91
123
  meta.objects.some(
92
124
  (obj) =>
93
- obj.properties.length &&
94
125
  obj.properties.some((p) => p.key.isSoleLiteral()) &&
95
126
  obj.properties.some((p) => !p.key.isSoleLiteral()),
96
127
  )
@@ -98,36 +129,29 @@ export namespace ProtobufFactory {
98
129
  throw notSupportedError({ method })(
99
130
  "object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
100
131
  );
101
- // NATIVE TYPE, BUT NOT Uint8Array
102
- else if (meta.natives.length) {
103
- const banned = meta.natives
104
- .map((n) => [n, BANNED_NATIVE_TYPES.get(n)] as const)
105
- .filter(([_n, b]) => b !== undefined)[0];
106
- if (banned !== undefined)
107
- throw notSupportedError({ method })(
108
- banned[1] === null
109
- ? banned[0]
110
- : `${banned[0]}. Use ${banned[1]} instead.`,
111
- );
112
- }
113
- // MAP TYPE, BUT PROPERTY KEY TYPE IS NOT ATOMIC
132
+ // STATIC PROPERTY, BUT INVALID KEY NAME
114
133
  else if (
115
- meta.maps.length &&
116
- meta.maps.some((m) => !is_atomic_key(m.key))
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
+ )
117
142
  )
118
- throw notSupportedError({ method })("");
119
- //----
120
- // UNION TYPES
121
- //----
122
- // UNION IN ARRAY
143
+ throw notSupportedError({ method })(
144
+ `object type with invalid static key name.`,
145
+ );
146
+ // DYNAMIC OBJECT, BUT PROPERTY VALUE TYPE IS ARRAY
123
147
  else if (
124
- meta.arrays.length &&
125
- meta.arrays.some((a) => a.value.size() > 1)
148
+ meta.objects.length &&
149
+ isDynamicObject(meta.objects[0]!) &&
150
+ meta.objects[0]!.properties.some((p) => !!p.value.arrays.length)
126
151
  )
127
- throw notSupportedError({ method })("union type in array");
128
- // UNION WITH ARRAY
129
- else if (meta.size() > 1 && meta.arrays.length)
130
- throw notSupportedError({ method })("union type with array type");
152
+ throw notSupportedError({ method })(
153
+ "dynamic object with array value type",
154
+ );
131
155
  // UNION WITH DYNAMIC OBJECT
132
156
  else if (
133
157
  meta.size() > 1 &&
@@ -143,42 +167,53 @@ export namespace ProtobufFactory {
143
167
  meta.objects.some(
144
168
  (obj) =>
145
169
  isDynamicObject(obj) &&
146
- obj.properties.some((p) => isUnion(p.value)),
170
+ obj.properties.some((p) => p.value.isBinaryUnion()),
147
171
  )
148
172
  )
149
173
  throw notSupportedError({ method })(
150
174
  "union type in dynamic property",
151
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
+ );
152
207
  // UNION WITH MAP
153
208
  else if (meta.size() > 1 && meta.maps.length)
154
209
  throw notSupportedError({ method })("union type with map type");
155
210
  // UNION IN MAP
156
- 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
+ )
157
215
  throw notSupportedError({ method })("union type in map");
158
216
  };
159
-
160
- const is_atomic_key = (key: Metadata) => {
161
- if (
162
- key.required &&
163
- key.nullable === false &&
164
- key.functional === false &&
165
- key.resolved === null &&
166
- key.size() ===
167
- key.atomics.length +
168
- key.constants
169
- .map((c) => c.values.length)
170
- .reduce((a, b) => a + b, 0) +
171
- key.templates.length
172
- ) {
173
- const set: Set<string> = new Set();
174
- for (const atomic of key.atomics) set.add(atomic);
175
- for (const constant of key.constants) set.add(constant.type);
176
- if (key.templates.length) set.add("string");
177
-
178
- return set.size === 1;
179
- }
180
- return false;
181
- };
182
217
  }
183
218
 
184
219
  const prefix = (method: string) => `Error on typia.protobuf.${method}():`;
@@ -188,27 +223,8 @@ const notSupportedError = (p: { method: string }) => (title: string) =>
188
223
  `${prefix(p.method)}: protocol buffer does not support ${title}.`,
189
224
  );
190
225
 
191
- const isDynamicObject = (obj: MetadataObject): boolean => {
192
- return (
193
- obj.properties.length > 0 &&
194
- obj.properties[0]!.key.isSoleLiteral() === false
195
- );
196
- };
197
-
198
- const isUnion = (meta: Metadata): boolean => {
199
- const count: number =
200
- new Set([
201
- ...meta.atomics,
202
- ...meta.constants.map((c) => c.type),
203
- ...(meta.templates.length ? ["string"] : []),
204
- ]).size +
205
- meta.arrays.length +
206
- meta.tuples.length +
207
- meta.natives.length +
208
- meta.objects.length +
209
- meta.maps.length;
210
- return count > 1;
211
- };
226
+ const isDynamicObject = (obj: MetadataObject): boolean =>
227
+ obj.properties[0]!.key.isSoleLiteral() === false;
212
228
 
213
229
  const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
214
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
+ }