typia 5.0.0-dev.20230828 → 5.0.0-dev.20230829-2
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/factories/MetadataFactory.js +46 -19
- package/lib/factories/MetadataFactory.js.map +1 -1
- package/lib/factories/ProtobufFactory.js +46 -36
- package/lib/factories/ProtobufFactory.js.map +1 -1
- package/lib/programmers/helpers/ProtobufUtil.js +1 -1
- package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +21 -1
- package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +14 -4
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +22 -1
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
- package/package.json +3 -1
- package/src/factories/MetadataFactory.ts +214 -208
- package/src/factories/ProtobufFactory.ts +272 -275
- package/src/programmers/helpers/ProtobufUtil.ts +125 -125
- package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +655 -639
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +883 -869
- package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +165 -148
|
@@ -1,148 +1,165 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
4
|
-
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
5
|
-
|
|
6
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
]
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
? "
|
|
93
|
-
:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
-----------------------------------------------------------
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
for (const
|
|
110
|
-
elements.add(
|
|
111
|
-
for (const
|
|
112
|
-
elements.add(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
4
|
+
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
5
|
+
|
|
6
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
7
|
+
import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
|
|
8
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
9
|
+
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
10
|
+
|
|
11
|
+
import { IProject } from "../../transformers/IProject";
|
|
12
|
+
|
|
13
|
+
import { MapUtil } from "../../utils/MapUtil";
|
|
14
|
+
import { NameEncoder } from "../../utils/NameEncoder";
|
|
15
|
+
|
|
16
|
+
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
17
|
+
|
|
18
|
+
export namespace ProtobufMessageProgrammer {
|
|
19
|
+
export const write =
|
|
20
|
+
({ checker }: IProject) =>
|
|
21
|
+
(type: ts.Type) => {
|
|
22
|
+
// PARSE TARGET TYPE
|
|
23
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
24
|
+
ProtobufFactory.metadata("message")(checker)(collection)(type);
|
|
25
|
+
|
|
26
|
+
// STRINGIFY
|
|
27
|
+
const hierarchies: Map<string, Hierarchy> = new Map();
|
|
28
|
+
for (const obj of collection.objects())
|
|
29
|
+
if (is_dynamic_object(obj) === false) emplace(hierarchies)(obj);
|
|
30
|
+
|
|
31
|
+
const content: string =
|
|
32
|
+
`syntax = "proto3";\n\n` +
|
|
33
|
+
[...hierarchies.values()]
|
|
34
|
+
.map((hier) => write_hierarchy(hier))
|
|
35
|
+
.join("\n\n");
|
|
36
|
+
|
|
37
|
+
// RETURNS
|
|
38
|
+
return ts.factory.createStringLiteral(content);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const emplace = (dict: Map<string, Hierarchy>) => (obj: MetadataObject) => {
|
|
42
|
+
const accessors: string[] = obj.name.split(".");
|
|
43
|
+
accessors.forEach((access, i) => {
|
|
44
|
+
const hierarchy: Hierarchy = MapUtil.take(dict)(access, () => ({
|
|
45
|
+
key: access,
|
|
46
|
+
object: null!,
|
|
47
|
+
children: new Map(),
|
|
48
|
+
}));
|
|
49
|
+
dict = hierarchy.children;
|
|
50
|
+
if (i === accessors.length - 1) hierarchy.object = obj;
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const is_dynamic_object = (obj: MetadataObject): boolean =>
|
|
55
|
+
obj.properties.length === 1 &&
|
|
56
|
+
obj.properties[0]!.key.isSoleLiteral() === false;
|
|
57
|
+
|
|
58
|
+
const write_hierarchy = (hierarchy: Hierarchy): string => {
|
|
59
|
+
const elements: string[] = [
|
|
60
|
+
`message ${NameEncoder.encode(hierarchy.key)} {`,
|
|
61
|
+
];
|
|
62
|
+
if (hierarchy.object !== null) {
|
|
63
|
+
const text: string = write_object(hierarchy.object);
|
|
64
|
+
elements.push(...text.split("\n").map((str) => `${TAB}${str}`));
|
|
65
|
+
}
|
|
66
|
+
if (hierarchy.children.size)
|
|
67
|
+
elements.push(
|
|
68
|
+
[...hierarchy.children.values()]
|
|
69
|
+
.map((child) => write_hierarchy(child))
|
|
70
|
+
.map((body) =>
|
|
71
|
+
body
|
|
72
|
+
.split("\n")
|
|
73
|
+
.map((line) => `${TAB}${line}`)
|
|
74
|
+
.join("\n"),
|
|
75
|
+
)
|
|
76
|
+
.join("\n\n"),
|
|
77
|
+
);
|
|
78
|
+
elements.push("}");
|
|
79
|
+
return elements.join("\n");
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const write_object = (obj: MetadataObject): string => {
|
|
83
|
+
const ptr: IPointer<number> = { value: 0 };
|
|
84
|
+
return obj.properties
|
|
85
|
+
.map((prop) => {
|
|
86
|
+
const key: string = prop.key.getSoleLiteral()!;
|
|
87
|
+
const type: string = decode(ptr)(prop.value);
|
|
88
|
+
return type.indexOf("${name}") !== -1
|
|
89
|
+
? type.replace("${name}", key)
|
|
90
|
+
: `${
|
|
91
|
+
prop.value.arrays.length || type.startsWith("map<")
|
|
92
|
+
? ""
|
|
93
|
+
: !prop.value.isRequired() || prop.value.nullable
|
|
94
|
+
? "optional "
|
|
95
|
+
: "required "
|
|
96
|
+
}${type} ${key} = ${++ptr.value};`;
|
|
97
|
+
})
|
|
98
|
+
.join("\n");
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/* -----------------------------------------------------------
|
|
102
|
+
DECODERS
|
|
103
|
+
----------------------------------------------------------- */
|
|
104
|
+
const decode =
|
|
105
|
+
(ptr: IPointer<number>) =>
|
|
106
|
+
(meta: Metadata): string => {
|
|
107
|
+
const elements: Set<string> = new Set();
|
|
108
|
+
if (meta.natives.length) elements.add("bytes");
|
|
109
|
+
for (const atomic of ProtobufUtil.getAtomics(meta))
|
|
110
|
+
elements.add(atomic);
|
|
111
|
+
for (const array of meta.arrays)
|
|
112
|
+
elements.add(`repeated ${decode(ptr)(array.type.value)}`);
|
|
113
|
+
for (const obj of meta.objects)
|
|
114
|
+
elements.add(
|
|
115
|
+
is_dynamic_object(obj)
|
|
116
|
+
? decode_map(ptr)(
|
|
117
|
+
MetadataProperty.create({
|
|
118
|
+
...obj.properties[0]!,
|
|
119
|
+
key: (() => {
|
|
120
|
+
const key: Metadata =
|
|
121
|
+
Metadata.initialize();
|
|
122
|
+
key.atomics.push(
|
|
123
|
+
MetadataAtomic.create({
|
|
124
|
+
type: "string",
|
|
125
|
+
tags: [],
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
return key;
|
|
129
|
+
})(),
|
|
130
|
+
}),
|
|
131
|
+
)
|
|
132
|
+
: NameEncoder.encode(obj.name),
|
|
133
|
+
);
|
|
134
|
+
for (const map of meta.maps) elements.add(decode_map(ptr)(map));
|
|
135
|
+
return elements.size === 1
|
|
136
|
+
? [...elements][0]!
|
|
137
|
+
: [
|
|
138
|
+
"oneof ${name} {",
|
|
139
|
+
...[...elements].map(
|
|
140
|
+
(str) =>
|
|
141
|
+
`${TAB}${str} v${
|
|
142
|
+
ptr.value + 1
|
|
143
|
+
} = ${++ptr.value};`,
|
|
144
|
+
),
|
|
145
|
+
"}",
|
|
146
|
+
].join("\n");
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const decode_map =
|
|
150
|
+
(ptr: IPointer<number>) =>
|
|
151
|
+
(prop: Metadata.Entry): string =>
|
|
152
|
+
`map<${decode(ptr)(prop.key)}, ${decode(ptr)(prop.value)}>`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
interface Hierarchy {
|
|
156
|
+
key: string;
|
|
157
|
+
object: MetadataObject | null;
|
|
158
|
+
children: Map<string, Hierarchy>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface IPointer<T> {
|
|
162
|
+
value: T;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const TAB = " ".repeat(4);
|