typia 5.0.0-dev.20230829-4 → 5.0.0-dev.20230830

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,165 +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 { 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);
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);
package/src/protobuf.ts CHANGED
@@ -75,17 +75,17 @@ export function message(): never {
75
75
  * For reference, as Protocol Buffer handles binary data directly, there's no way
76
76
  * when `input` binary data was not encoded from the `T` typed value. In that case,
77
77
  * unexpected behavior or internal error would be occured. Therefore, I recommend you
78
- * to encode binary data of Protocol Buffer from type safe encode function like below.
78
+ * to encode binary data of Protocol Buffer from type safe encode functions like below.
79
79
  * Use {@link encode} function only when you can ensure it.
80
80
  *
81
81
  * - {@link assertEncode}
82
82
  * - {@link isEncode}
83
83
  * - {@link validateEncode}
84
84
  *
85
- * Also, `typia` is providing a type safe decoder like {@link assertDecode}, but it
86
- * is just for additional validation like whether a string value meets constant
87
- * restriction, or a numeric value is keepking its range limit, represented by
88
- * [comment tags](https://typia.io/docs/validators/tags). Thus, I repeat that,
85
+ * Also, `typia` is providing type safe decoders like {@link assertDecode}, but it
86
+ * is just for additional type validation like `number & Minimum<7>` or
87
+ * `string & Format<"uuid">` cases, that are represented by
88
+ * [custom tags](https://typia.io/docs/validators/tags). Thus, I repeat that,
89
89
  * you've to ensure the type safety when using decoder functions.
90
90
  *
91
91
  * @tempate Expected type of decoded value
@@ -105,17 +105,17 @@ export function decode(input: Uint8Array): never;
105
105
  * For reference, as Protocol Buffer handles binary data directly, there's no way
106
106
  * when `input` binary data was not encoded from the `T` typed value. In that case,
107
107
  * unexpected behavior or internal error would be occured. Therefore, I recommend you
108
- * to encode binary data of Protocol Buffer from type safe encode function like below.
108
+ * to encode binary data of Protocol Buffer from type safe encode functions like below.
109
109
  * Use {@link encode} function only when you can ensure it.
110
110
  *
111
111
  * - {@link assertEncode}
112
112
  * - {@link isEncode}
113
113
  * - {@link validateEncode}
114
114
  *
115
- * Also, `typia` is providing a type safe decoder like {@link assertDecode}, but it
116
- * is just for additional validation like whether a string value meets constant
117
- * restriction, or a numeric value is keepking its range limit, represented by
118
- * [comment tags](https://typia.io/docs/validators/tags). Thus, I repeat that,
115
+ * Also, `typia` is providing type safe decoders like {@link assertDecode}, but it
116
+ * is just for additional type validation like `number & Minimum<7>` or
117
+ * `string & Format<"uuid">` cases, that are represented by
118
+ * [custom tags](https://typia.io/docs/validators/tags). Thus, I repeat that,
119
119
  * you've to ensure the type safety when using decoder functions.
120
120
  *
121
121
  * @tempate Expected type of decoded value
@@ -146,10 +146,10 @@ Object.assign(decode, Namespace.protobuf.decode("decode"));
146
146
  * {@link TypeGuardError} instead.
147
147
  *
148
148
  * However, note that, this validation is not always safe. It just performs additional
149
- * type checking like whether a string value meets constant restriction, or a
150
- * numeric value is keepking its range limit, represented by
151
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
152
- * decode functions, you have to ensure the type safety by yourself.
149
+ * type assertion like `number & Minimum<7>` or `string & Format<"uuid">` cases,
150
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
151
+ * Therefore, when using `typia.protobuf.assertDecode<T>()` function, you have to
152
+ * ensure the type safety by yourself.
153
153
  *
154
154
  * In such type safety reason, I recommend you to use type safe encode functions.
155
155
  *
@@ -175,10 +175,10 @@ export function assertDecode(input: Uint8Array): never;
175
175
  * {@link TypeGuardError} instead.
176
176
  *
177
177
  * However, note that, this validation is not always safe. It just performs additional
178
- * type checking like whether a string value meets constant restriction, or a
179
- * numeric value is keepking its range limit, represented by
180
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
181
- * decode functions, you have to ensure the type safety by yourself.
178
+ * type assertion like `number & Minimum<7>` or `string & Format<"uuid">` cases,
179
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
180
+ * Therefore, when using `typia.protobuf.assertDecode<T>()` function, you have to
181
+ * ensure the type safety by yourself.
182
182
  *
183
183
  * In such type safety reason, I recommend you to use type safe encode functions.
184
184
  *
@@ -215,10 +215,10 @@ Object.assign(assertDecode, Namespace.protobuf.decode("assertDecode"));
215
215
  * `null` value instead.
216
216
  *
217
217
  * However, note that, this validation is not always safe. It just performs additional
218
- * type checking like whether a string value meets constant restriction, or a
219
- * numeric value is keepking its range limit, represented by
220
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
221
- * decode functions, you have to ensure the type safety by yourself.
218
+ * type checking like `number & Minimum<7>` or `string & Format<"uuid">` cases,
219
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
220
+ * Therefore, when using `typia.protobuf.isDecode<T>()` function, you have to
221
+ * ensure the type safety by yourself.
222
222
  *
223
223
  * In such type safety reason, I recommend you to use type safe encode functions.
224
224
  *
@@ -244,10 +244,10 @@ export function isDecode(input: Uint8Array): never;
244
244
  * `null` value instead.
245
245
  *
246
246
  * However, note that, this validation is not always safe. It just performs additional
247
- * type checking like whether a string value meets constant restriction, or a
248
- * numeric value is keepking its range limit, represented by
249
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
250
- * decode functions, you have to ensure the type safety by yourself.
247
+ * type checking like `number & Minimum<7>` or `string & Format<"uuid">` cases,
248
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
249
+ * Therefore, when using `typia.protobuf.isDecode<T>()` function, you have to
250
+ * ensure the type safety by yourself.
251
251
  *
252
252
  * In such type safety reason, I recommend you to use type safe encode functions.
253
253
  *
@@ -285,10 +285,10 @@ Object.assign(isDecode, Namespace.protobuf.decode("isDecode"));
285
285
  * {@link IValidation.IFailure} value instead with detailed error reasons.
286
286
  *
287
287
  * However, note that, this validation is not always safe. It just performs additional
288
- * type checking like whether a string value meets constant restriction, or a
289
- * numeric value is keepking its range limit, represented by
290
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
291
- * decode functions, you have to ensure the type safety by yourself.
288
+ * type validation like `number & Minimum<7>` or `string & Format<"uuid">` cases,
289
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
290
+ * Therefore, when using `typia.protobuf.validateDecode<T>()` function, you have to
291
+ * ensure the type safety by yourself.
292
292
  *
293
293
  * In such type safety reason, I recommend you to use type safe encode functions.
294
294
  *
@@ -315,10 +315,10 @@ export function validateDecode(input: Uint8Array): never;
315
315
  * {@link IValidation.IFailure} value instead with detailed error reasons.
316
316
  *
317
317
  * However, note that, this validation is not always safe. It just performs additional
318
- * type checking like whether a string value meets constant restriction, or a
319
- * numeric value is keepking its range limit, represented by
320
- * [comment tags](https://typia.io/docs/validators/tags). Therefore, when using
321
- * decode functions, you have to ensure the type safety by yourself.
318
+ * type validation like `number & Minimum<7>` or `string & Format<"uuid">` cases,
319
+ * that are represented by [custom tags](https://typia.io/docs/validators/tags).
320
+ * Therefore, when using `typia.protobuf.validateDecode<T>()` function, you have to
321
+ * ensure the type safety by yourself.
322
322
  *
323
323
  * In such type safety reason, I recommend you to use type safe encode functions.
324
324
  *
@@ -45,11 +45,6 @@ export namespace FileTransformer {
45
45
  message: exp.message,
46
46
  code: `(${exp.code})` as any,
47
47
  });
48
- console.log(
49
- diagnostic.file.fileName,
50
- diagnostic.start,
51
- diagnostic.length,
52
- );
53
48
  project.extras.addDiagnostic(diagnostic);
54
49
  return null;
55
50
  }