typia 5.0.0-dev.20230805 → 5.0.0-dev.20230810
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.
- package/lib/executable/TypiaSetupWizard.js +39 -28
- package/lib/executable/TypiaSetupWizard.js.map +1 -1
- package/lib/factories/{ProtocolFactory.d.ts → ProtobufFactory.d.ts} +2 -2
- package/lib/factories/ProtobufFactory.js +235 -0
- package/lib/factories/ProtobufFactory.js.map +1 -0
- package/lib/programmers/RandomProgrammer.js +1 -1
- package/lib/programmers/RandomProgrammer.js.map +1 -1
- package/lib/programmers/internal/stringify_dynamic_properties.js +1 -0
- package/lib/programmers/internal/stringify_dynamic_properties.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.d.ts +1 -1
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +160 -60
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
- package/lib/schemas/metadata/Metadata.js +1 -1
- package/lib/schemas/metadata/Metadata.js.map +1 -1
- package/lib/transformers/features/protobuf/ProtobufMessageTransformer.js +1 -1
- package/lib/transformers/features/protobuf/ProtobufMessageTransformer.js.map +1 -1
- package/lib/utils/PatternUtil.d.ts +1 -1
- package/lib/utils/PatternUtil.js +3 -1
- package/lib/utils/PatternUtil.js.map +1 -1
- package/package.json +2 -2
- package/src/executable/TypiaSetupWizard.ts +34 -14
- package/src/factories/ProtobufFactory.ts +235 -0
- package/src/programmers/RandomProgrammer.ts +1 -3
- package/src/programmers/internal/stringify_dynamic_properties.ts +4 -0
- package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +113 -72
- package/src/schemas/metadata/Metadata.ts +2 -1
- package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +1 -1
- package/src/utils/PatternUtil.ts +4 -1
- package/lib/factories/ProtocolFactory.js +0 -113
- package/lib/factories/ProtocolFactory.js.map +0 -1
- package/src/factories/ProtocolFactory.ts +0 -80
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { Metadata } from "../schemas/metadata/Metadata";
|
|
4
|
+
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
5
|
+
import { IProtocolMessage } from "../schemas/protobuf/IProtocolMessage";
|
|
6
|
+
|
|
7
|
+
import { MetadataCollection } from "./MetadataCollection";
|
|
8
|
+
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
|
+
|
|
12
|
+
export namespace ProtobufFactory {
|
|
13
|
+
export const metadata =
|
|
14
|
+
(method: string) =>
|
|
15
|
+
(checker: ts.TypeChecker) =>
|
|
16
|
+
(collection: MetadataCollection) =>
|
|
17
|
+
(type: ts.Type) => {
|
|
18
|
+
// COMPOSE METADATA WITH INDIVIDUAL VALIDATIONS
|
|
19
|
+
const top: Metadata = MetadataFactory.analyze(checker)({
|
|
20
|
+
resolve: false,
|
|
21
|
+
constant: true,
|
|
22
|
+
absorb: true,
|
|
23
|
+
validate: validate(method),
|
|
24
|
+
})(collection)(type);
|
|
25
|
+
|
|
26
|
+
// TOP LEVEL TYPE MUST BE A SINGLE OBJECT
|
|
27
|
+
const onlyObject: boolean =
|
|
28
|
+
top.size() === 1 &&
|
|
29
|
+
top.objects.length === 1 &&
|
|
30
|
+
top.objects[0]!.properties.every((p) =>
|
|
31
|
+
p.key.isSoleLiteral(),
|
|
32
|
+
) &&
|
|
33
|
+
top.isRequired() === true &&
|
|
34
|
+
top.nullable === false;
|
|
35
|
+
if (onlyObject === false)
|
|
36
|
+
throw new Error(
|
|
37
|
+
`${prefix(
|
|
38
|
+
method,
|
|
39
|
+
)} target type must be a sole and static object.`,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return top;
|
|
43
|
+
};
|
|
44
|
+
|
|
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
|
+
const validate = (method: string) => (meta: Metadata) => {
|
|
58
|
+
//----
|
|
59
|
+
// NOT ALLOWED TYPES
|
|
60
|
+
//----
|
|
61
|
+
if (meta.any) throw notSupportedError({ method })("any type");
|
|
62
|
+
else if (meta.functional)
|
|
63
|
+
throw notSupportedError({ method })("functional type");
|
|
64
|
+
else if (meta.tuples.length)
|
|
65
|
+
throw notSupportedError({ method })("tuple type");
|
|
66
|
+
else if (meta.sets.length)
|
|
67
|
+
throw notSupportedError({ method })("Set type");
|
|
68
|
+
else if (
|
|
69
|
+
meta.arrays.length &&
|
|
70
|
+
meta.arrays.some((array) => !!array.value.arrays.length)
|
|
71
|
+
)
|
|
72
|
+
throw notSupportedError({ method })("two dimenstional array type");
|
|
73
|
+
//----
|
|
74
|
+
// SPECIAL CASES
|
|
75
|
+
//----
|
|
76
|
+
// MULTIPLE DYNAMIC KEY TYPED PROPERTIES
|
|
77
|
+
else if (
|
|
78
|
+
meta.objects.length &&
|
|
79
|
+
meta.objects.some(
|
|
80
|
+
(obj) =>
|
|
81
|
+
obj.properties.filter((p) => !p.key.isSoleLiteral())
|
|
82
|
+
.length > 1,
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
throw notSupportedError({ method })(
|
|
86
|
+
"object type with multiple dynamic key typed properties. Keep only one.",
|
|
87
|
+
);
|
|
88
|
+
// STATIC AND DYNAMIC PROPERTIES ARE COMPATIBLE
|
|
89
|
+
else if (
|
|
90
|
+
meta.objects.length &&
|
|
91
|
+
meta.objects.some(
|
|
92
|
+
(obj) =>
|
|
93
|
+
obj.properties.length &&
|
|
94
|
+
obj.properties.some((p) => p.key.isSoleLiteral()) &&
|
|
95
|
+
obj.properties.some((p) => !p.key.isSoleLiteral()),
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
throw notSupportedError({ method })(
|
|
99
|
+
"object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
|
|
100
|
+
);
|
|
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
|
|
114
|
+
else if (
|
|
115
|
+
meta.maps.length &&
|
|
116
|
+
meta.maps.some((m) => !is_atomic_key(m.key))
|
|
117
|
+
)
|
|
118
|
+
throw notSupportedError({ method })("");
|
|
119
|
+
//----
|
|
120
|
+
// UNION TYPES
|
|
121
|
+
//----
|
|
122
|
+
// UNION IN ARRAY
|
|
123
|
+
else if (
|
|
124
|
+
meta.arrays.length &&
|
|
125
|
+
meta.arrays.some((a) => a.value.size() > 1)
|
|
126
|
+
)
|
|
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");
|
|
131
|
+
// UNION WITH DYNAMIC OBJECT
|
|
132
|
+
else if (
|
|
133
|
+
meta.size() > 1 &&
|
|
134
|
+
meta.objects.length &&
|
|
135
|
+
isDynamicObject(meta.objects[0]!)
|
|
136
|
+
)
|
|
137
|
+
throw notSupportedError({ method })(
|
|
138
|
+
"union type with dynamic object type",
|
|
139
|
+
);
|
|
140
|
+
// UNION IN DYNAMIC PROPERTY VALUE
|
|
141
|
+
else if (
|
|
142
|
+
meta.objects.length &&
|
|
143
|
+
meta.objects.some(
|
|
144
|
+
(obj) =>
|
|
145
|
+
isDynamicObject(obj) &&
|
|
146
|
+
obj.properties.some((p) => isUnion(p.value)),
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
throw notSupportedError({ method })(
|
|
150
|
+
"union type in dynamic property",
|
|
151
|
+
);
|
|
152
|
+
// UNION WITH MAP
|
|
153
|
+
else if (meta.size() > 1 && meta.maps.length)
|
|
154
|
+
throw notSupportedError({ method })("union type with map type");
|
|
155
|
+
// UNION IN MAP
|
|
156
|
+
else if (meta.maps.length && meta.maps.some((m) => isUnion(m.value)))
|
|
157
|
+
throw notSupportedError({ method })("union type in map");
|
|
158
|
+
};
|
|
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
|
+
}
|
|
183
|
+
|
|
184
|
+
const prefix = (method: string) => `Error on typia.protobuf.${method}():`;
|
|
185
|
+
|
|
186
|
+
const notSupportedError = (p: { method: string }) => (title: string) =>
|
|
187
|
+
new Error(
|
|
188
|
+
`${prefix(p.method)}: protocol buffer does not support ${title}.`,
|
|
189
|
+
);
|
|
190
|
+
|
|
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
|
+
};
|
|
212
|
+
|
|
213
|
+
const BANNED_NATIVE_TYPES: Map<string, string | null> = new Map([
|
|
214
|
+
["Date", "string"],
|
|
215
|
+
["Boolean", "boolean"],
|
|
216
|
+
["BigInt", "bigint"],
|
|
217
|
+
["Number", "number"],
|
|
218
|
+
["String", "string"],
|
|
219
|
+
...[
|
|
220
|
+
"Buffer",
|
|
221
|
+
"Uint8ClampedArray",
|
|
222
|
+
"Uint16Array",
|
|
223
|
+
"Uint32Array",
|
|
224
|
+
"BigUint64Array",
|
|
225
|
+
"Int8Array",
|
|
226
|
+
"Int16Array",
|
|
227
|
+
"Int32Array",
|
|
228
|
+
"BigInt64Array",
|
|
229
|
+
"Float32Array",
|
|
230
|
+
"Float64Array",
|
|
231
|
+
"DataView",
|
|
232
|
+
"ArrayBuffer",
|
|
233
|
+
"SharedArrayBuffer",
|
|
234
|
+
].map((name) => [name, "Uint8Array"] as const),
|
|
235
|
+
]);
|
|
@@ -219,9 +219,7 @@ export namespace RandomProgrammer {
|
|
|
219
219
|
const expressions: ts.Expression[] = [];
|
|
220
220
|
if (meta.any)
|
|
221
221
|
expressions.push(
|
|
222
|
-
ts.factory.createStringLiteral(
|
|
223
|
-
"fucking any type exists...",
|
|
224
|
-
),
|
|
222
|
+
ts.factory.createStringLiteral("any type used..."),
|
|
225
223
|
);
|
|
226
224
|
|
|
227
225
|
// NULL COALESCING
|
|
@@ -1,87 +1,71 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
|
|
3
3
|
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
4
|
-
import {
|
|
4
|
+
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
5
5
|
|
|
6
|
+
import { IMetadataTag } from "../../schemas/metadata/IMetadataTag";
|
|
6
7
|
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
7
|
-
import {
|
|
8
|
-
import { IProtocolMessage } from "../../schemas/protobuf/IProtocolMessage";
|
|
8
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
9
9
|
|
|
10
10
|
import { IProject } from "../../transformers/IProject";
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import { Atomic } from "../../typings/Atomic";
|
|
13
|
+
|
|
13
14
|
import { MapUtil } from "../../utils/MapUtil";
|
|
14
15
|
import { NameEncoder } from "../../utils/NameEncoder";
|
|
15
16
|
|
|
16
17
|
export namespace ProtobufMessageProgrammer {
|
|
17
|
-
export const
|
|
18
|
+
export const write =
|
|
18
19
|
({ checker }: IProject) =>
|
|
19
20
|
(type: ts.Type) => {
|
|
20
21
|
// PARSE TARGET TYPE
|
|
21
22
|
const collection: MetadataCollection = new MetadataCollection();
|
|
22
|
-
|
|
23
|
-
ProtocolFactory.metadata(checker)(collection)(type);
|
|
24
|
-
|
|
25
|
-
// CONVERT TO PROTOCOL MESSAGES
|
|
26
|
-
const dict: Map<string, IProtocolMessage> = new Map();
|
|
27
|
-
ProtocolFactory.analyze(collection)(dict)(metadata);
|
|
23
|
+
ProtobufFactory.metadata("message")(checker)(collection)(type);
|
|
28
24
|
|
|
29
25
|
// STRINGIFY
|
|
30
26
|
const hierarchies: Map<string, Hierarchy> = new Map();
|
|
31
|
-
for (const
|
|
27
|
+
for (const obj of collection.objects())
|
|
28
|
+
if (is_dynamic_object(obj) === false) emplace(hierarchies)(obj);
|
|
32
29
|
|
|
33
30
|
const content: string =
|
|
34
31
|
`syntax = "proto3";\n\n` +
|
|
35
32
|
[...hierarchies.values()]
|
|
36
|
-
.map((hier) =>
|
|
33
|
+
.map((hier) => write_hierarchy(hier))
|
|
37
34
|
.join("\n\n");
|
|
38
35
|
|
|
39
36
|
// RETURNS
|
|
40
37
|
return ts.factory.createStringLiteral(content);
|
|
41
38
|
};
|
|
42
39
|
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
40
|
+
const emplace = (dict: Map<string, Hierarchy>) => (obj: MetadataObject) => {
|
|
41
|
+
const accessors: string[] = obj.name.split(".");
|
|
42
|
+
accessors.forEach((access, i) => {
|
|
43
|
+
const hierarchy: Hierarchy = MapUtil.take(dict)(access, () => ({
|
|
44
|
+
key: access,
|
|
45
|
+
object: null!,
|
|
46
|
+
children: new Map(),
|
|
47
|
+
}));
|
|
48
|
+
dict = hierarchy.children;
|
|
49
|
+
if (i === accessors.length - 1) hierarchy.object = obj;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
46
52
|
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
const is_dynamic_object = (obj: MetadataObject): boolean =>
|
|
54
|
+
obj.properties.length === 1 &&
|
|
55
|
+
obj.properties[0]!.key.isSoleLiteral() === false;
|
|
56
|
+
|
|
57
|
+
const write_hierarchy = (hierarchy: Hierarchy): string => {
|
|
58
|
+
const elements: string[] = [
|
|
59
|
+
`message ${NameEncoder.encode(hierarchy.key)} {`,
|
|
60
|
+
];
|
|
61
|
+
if (hierarchy.object !== null) {
|
|
62
|
+
const text: string = write_object(hierarchy.object);
|
|
63
|
+
elements.push(...text.split("\n").map((str) => `${TAB}${str}`));
|
|
64
|
+
}
|
|
65
|
+
if (hierarchy.children.size)
|
|
49
66
|
elements.push(
|
|
50
|
-
...
|
|
51
|
-
|
|
52
|
-
Escaper.variable(property.key) &&
|
|
53
|
-
property.key.indexOf("$") === -1
|
|
54
|
-
? property.key
|
|
55
|
-
: `v${index + 1}`;
|
|
56
|
-
if (property.oneOf.length === 1)
|
|
57
|
-
return [
|
|
58
|
-
TAB,
|
|
59
|
-
property.required ? "" : "optional ",
|
|
60
|
-
property.repeated ? "repeated " : "",
|
|
61
|
-
getTypeName(property.oneOf[0]!.type) + " ",
|
|
62
|
-
key,
|
|
63
|
-
" = ",
|
|
64
|
-
`${index++};`,
|
|
65
|
-
].join("");
|
|
66
|
-
return (
|
|
67
|
-
`${TAB}oneof ${key} {\n` +
|
|
68
|
-
property.oneOf
|
|
69
|
-
.map(
|
|
70
|
-
(o, i) =>
|
|
71
|
-
`${TAB}${TAB}${getTypeName(
|
|
72
|
-
o.type,
|
|
73
|
-
)} ${key}_o${i} = ${index++};`,
|
|
74
|
-
)
|
|
75
|
-
.join("\n") +
|
|
76
|
-
`\n${TAB}}`
|
|
77
|
-
);
|
|
78
|
-
}),
|
|
79
|
-
);
|
|
80
|
-
if (message !== null && children.size) elements.push("\n");
|
|
81
|
-
if (children.size)
|
|
82
|
-
elements.push(
|
|
83
|
-
[...children.values()]
|
|
84
|
-
.map((child) => stringify(child))
|
|
67
|
+
[...hierarchy.children.values()]
|
|
68
|
+
.map((child) => write_hierarchy(child))
|
|
85
69
|
.map((body) =>
|
|
86
70
|
body
|
|
87
71
|
.split("\n")
|
|
@@ -91,35 +75,92 @@ export namespace ProtobufMessageProgrammer {
|
|
|
91
75
|
.join("\n\n"),
|
|
92
76
|
);
|
|
93
77
|
elements.push("}");
|
|
94
|
-
|
|
95
78
|
return elements.join("\n");
|
|
96
79
|
};
|
|
97
80
|
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
81
|
+
const write_object = (obj: MetadataObject): string => {
|
|
82
|
+
const ptr: IPointer<number> = { value: 0 };
|
|
83
|
+
return obj.properties
|
|
84
|
+
.map((prop) => {
|
|
85
|
+
const key: string = prop.key.getSoleLiteral()!;
|
|
86
|
+
const type: string = decode(ptr)(prop.tags)(prop.value);
|
|
87
|
+
return type.indexOf("${name}") !== -1
|
|
88
|
+
? type.replace("${name}", key)
|
|
89
|
+
: `${
|
|
90
|
+
prop.value.arrays.length || key.startsWith("map<")
|
|
91
|
+
? ""
|
|
92
|
+
: !prop.value.isRequired() || prop.value.nullable
|
|
93
|
+
? "optional "
|
|
94
|
+
: "required"
|
|
95
|
+
}${type} ${key} = ${++ptr.value};`;
|
|
96
|
+
})
|
|
97
|
+
.join("\n");
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const decode =
|
|
101
|
+
(ptr: IPointer<number>) =>
|
|
102
|
+
(tags: IMetadataTag[]) =>
|
|
103
|
+
(meta: Metadata): string => {
|
|
104
|
+
const elements: Set<string> = new Set();
|
|
105
|
+
if (meta.natives.length) elements.add("bytes");
|
|
106
|
+
if (meta.templates.length) elements.add("string");
|
|
107
|
+
for (const atomic of meta.atomics)
|
|
108
|
+
elements.add(decode_atomic(tags)(atomic));
|
|
109
|
+
for (const constant of meta.constants)
|
|
110
|
+
elements.add(decode_atomic(tags)(constant.type));
|
|
111
|
+
for (const array of meta.arrays)
|
|
112
|
+
elements.add(`repeated ${decode(ptr)(tags)(array.value)}`);
|
|
113
|
+
for (const obj of meta.objects)
|
|
114
|
+
elements.add(
|
|
115
|
+
is_dynamic_object(obj)
|
|
116
|
+
? decode_map(ptr)(tags)(obj.properties[0]!)
|
|
117
|
+
: NameEncoder.encode(obj.name),
|
|
118
|
+
);
|
|
119
|
+
for (const map of meta.maps)
|
|
120
|
+
elements.add(decode_map(ptr)(tags)(map));
|
|
121
|
+
return elements.size === 1
|
|
122
|
+
? [...elements][0]!
|
|
123
|
+
: [
|
|
124
|
+
"oneof ${name} {",
|
|
125
|
+
...[...elements].map(
|
|
126
|
+
(str, i) =>
|
|
127
|
+
`${TAB}${str} v${i + 1} = ${++ptr.value};`,
|
|
128
|
+
),
|
|
129
|
+
"}",
|
|
130
|
+
].join("\n");
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const decode_atomic =
|
|
134
|
+
(tags: IMetadataTag[]) =>
|
|
135
|
+
(literal: Atomic.Literal): string => {
|
|
136
|
+
if (literal === "boolean") return "bool";
|
|
137
|
+
else if (literal === "bigint") return "int64";
|
|
138
|
+
else if (literal === "number") {
|
|
139
|
+
const type = tags.find((t) => t.kind === "type") as
|
|
140
|
+
| IMetadataTag.INumberType
|
|
141
|
+
| undefined;
|
|
142
|
+
return type?.value ?? "double";
|
|
143
|
+
}
|
|
144
|
+
return literal;
|
|
110
145
|
};
|
|
111
146
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
147
|
+
const decode_map =
|
|
148
|
+
(ptr: IPointer<number>) =>
|
|
149
|
+
(tags: IMetadataTag[]) =>
|
|
150
|
+
(prop: Metadata.Entry): string =>
|
|
151
|
+
`map<${decode(ptr)([])(prop.key)}, ${decode(ptr)(tags)(
|
|
152
|
+
prop.value,
|
|
153
|
+
)}>`;
|
|
117
154
|
}
|
|
118
155
|
|
|
119
156
|
interface Hierarchy {
|
|
120
157
|
key: string;
|
|
121
|
-
|
|
158
|
+
object: MetadataObject | null;
|
|
122
159
|
children: Map<string, Hierarchy>;
|
|
123
160
|
}
|
|
124
161
|
|
|
162
|
+
interface IPointer<T> {
|
|
163
|
+
value: T;
|
|
164
|
+
}
|
|
165
|
+
|
|
125
166
|
const TAB = " ".repeat(4);
|
|
@@ -320,7 +320,8 @@ export class Metadata {
|
|
|
320
320
|
*/
|
|
321
321
|
public isUnionBucket(): boolean {
|
|
322
322
|
const size: number = this.bucket();
|
|
323
|
-
const emended: number =
|
|
323
|
+
const emended: number =
|
|
324
|
+
!!this.atomics.length && !!this.constants.length ? size - 1 : size;
|
|
324
325
|
return emended > 1;
|
|
325
326
|
}
|
|
326
327
|
|
package/src/utils/PatternUtil.ts
CHANGED
|
@@ -17,7 +17,10 @@ export namespace PatternUtil {
|
|
|
17
17
|
.replace(/-/g, "\\x2d");
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
export const NUMBER =
|
|
20
|
+
export const NUMBER =
|
|
21
|
+
"[+-]?" + // optional sign
|
|
22
|
+
"\\d+(?:\\.\\d+)?" + // integer or decimal
|
|
23
|
+
"(?:[eE][+-]?\\d+)?"; // optional exponent
|
|
21
24
|
export const BOOLEAN = "true|false";
|
|
22
25
|
export const STRING = "(.*)";
|
|
23
26
|
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __values = (this && this.__values) || function(o) {
|
|
3
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
-
if (m) return m.call(o);
|
|
5
|
-
if (o && typeof o.length === "number") return {
|
|
6
|
-
next: function () {
|
|
7
|
-
if (o && i >= o.length) o = void 0;
|
|
8
|
-
return { value: o && o[i++], done: !o };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.ProtocolFactory = void 0;
|
|
15
|
-
var MetadataFactory_1 = require("./MetadataFactory");
|
|
16
|
-
var emplace_protocol_object_1 = require("./internal/protocols/emplace_protocol_object");
|
|
17
|
-
var iterate_protocol_main_1 = require("./internal/protocols/iterate_protocol_main");
|
|
18
|
-
var ProtocolFactory;
|
|
19
|
-
(function (ProtocolFactory) {
|
|
20
|
-
ProtocolFactory.metadata = function (checker) {
|
|
21
|
-
return function (collection) {
|
|
22
|
-
return function (type) {
|
|
23
|
-
return MetadataFactory_1.MetadataFactory.analyze(checker)({
|
|
24
|
-
resolve: false,
|
|
25
|
-
constant: true,
|
|
26
|
-
absorb: true,
|
|
27
|
-
validate: function (meta) {
|
|
28
|
-
if (meta.any)
|
|
29
|
-
throw new Error("Error on typia.message(): any type is not supported in protocol buffer.");
|
|
30
|
-
else if (meta.functional && meta.size() !== 1)
|
|
31
|
-
throw new Error("Error on typia.message(): functional type is not supported in protocol buffer.");
|
|
32
|
-
else if (meta.objects.find(function (o) { return o.name === "__Main"; }))
|
|
33
|
-
throw new Error("Error on typia.message(): reserved type \"__Main\" has been detected.");
|
|
34
|
-
else if (meta.objects.find(function (o) { return o.name === "__Timestamp"; }))
|
|
35
|
-
throw new Error("Error on typia.message(): reserved type \"__Timestamp\" has been detected.");
|
|
36
|
-
else if (meta.objects.some(function (o) {
|
|
37
|
-
return o.properties.some(function (p) { return !is_atomic_key(p.key); });
|
|
38
|
-
}) ||
|
|
39
|
-
meta.maps.some(function (m) { return !is_atomic_key(m.key); }))
|
|
40
|
-
throw new Error("Error on typia.message(): only atomic key type is supported in protocol buffer.");
|
|
41
|
-
},
|
|
42
|
-
})(collection)(type);
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
ProtocolFactory.analyze = function (collection) {
|
|
47
|
-
return function (dict) {
|
|
48
|
-
return function (meta) {
|
|
49
|
-
var e_1, _a;
|
|
50
|
-
try {
|
|
51
|
-
for (var _b = __values(collection.objects()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
52
|
-
var obj = _c.value;
|
|
53
|
-
(0, emplace_protocol_object_1.emplace_protocol_object)(dict)(obj);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e_1) throw e_1.error; }
|
|
62
|
-
}
|
|
63
|
-
(0, iterate_protocol_main_1.iterate_protocol_main)(dict)(meta);
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
var is_atomic_key = function (key) {
|
|
68
|
-
var e_2, _a, e_3, _b;
|
|
69
|
-
if (key.required &&
|
|
70
|
-
key.nullable === false &&
|
|
71
|
-
key.functional === false &&
|
|
72
|
-
key.resolved === null &&
|
|
73
|
-
key.size() ===
|
|
74
|
-
key.atomics.length +
|
|
75
|
-
key.constants
|
|
76
|
-
.map(function (c) { return c.values.length; })
|
|
77
|
-
.reduce(function (a, b) { return a + b; }, 0) +
|
|
78
|
-
key.templates.length) {
|
|
79
|
-
var set = new Set();
|
|
80
|
-
try {
|
|
81
|
-
for (var _c = __values(key.atomics), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
82
|
-
var atomic = _d.value;
|
|
83
|
-
set.add(atomic);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
87
|
-
finally {
|
|
88
|
-
try {
|
|
89
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
90
|
-
}
|
|
91
|
-
finally { if (e_2) throw e_2.error; }
|
|
92
|
-
}
|
|
93
|
-
try {
|
|
94
|
-
for (var _e = __values(key.constants), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
95
|
-
var constant = _f.value;
|
|
96
|
-
set.add(constant.type);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
100
|
-
finally {
|
|
101
|
-
try {
|
|
102
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
103
|
-
}
|
|
104
|
-
finally { if (e_3) throw e_3.error; }
|
|
105
|
-
}
|
|
106
|
-
if (key.templates.length)
|
|
107
|
-
set.add("string");
|
|
108
|
-
return set.size === 1;
|
|
109
|
-
}
|
|
110
|
-
return false;
|
|
111
|
-
};
|
|
112
|
-
})(ProtocolFactory || (exports.ProtocolFactory = ProtocolFactory = {}));
|
|
113
|
-
//# sourceMappingURL=ProtocolFactory.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ProtocolFactory.js","sourceRoot":"","sources":["../../src/factories/ProtocolFactory.ts"],"names":[],"mappings":"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"}
|