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