typia 7.0.0-dev.20241018 → 7.0.0-dev.20241019

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 (35) hide show
  1. package/lib/factories/ProtobufFactory.js +561 -128
  2. package/lib/factories/ProtobufFactory.js.map +1 -1
  3. package/lib/programmers/helpers/ProtobufUtil.d.ts +2 -0
  4. package/lib/programmers/helpers/ProtobufUtil.js +2 -2
  5. package/lib/programmers/helpers/ProtobufUtil.js.map +1 -1
  6. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +169 -223
  7. package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
  8. package/lib/programmers/protobuf/ProtobufMessageProgrammer.d.ts +1 -1
  9. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +48 -140
  10. package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
  11. package/lib/schemas/metadata/MetadataProperty.d.ts +2 -0
  12. package/lib/schemas/metadata/MetadataProperty.js.map +1 -1
  13. package/lib/schemas/protobuf/IProtobufProperty.d.ts +5 -0
  14. package/lib/schemas/protobuf/IProtobufProperty.js +3 -0
  15. package/lib/schemas/protobuf/IProtobufProperty.js.map +1 -0
  16. package/lib/schemas/protobuf/IProtobufPropertyType.d.ts +28 -0
  17. package/lib/schemas/protobuf/IProtobufPropertyType.js +3 -0
  18. package/lib/schemas/protobuf/IProtobufPropertyType.js.map +1 -0
  19. package/lib/schemas/protobuf/IProtobufSchema.d.ts +38 -0
  20. package/lib/schemas/protobuf/IProtobufSchema.js +3 -0
  21. package/lib/schemas/protobuf/IProtobufSchema.js.map +1 -0
  22. package/lib/utils/{NameEncoder.d.ts → ProtobufNameEncoder.d.ts} +1 -1
  23. package/lib/utils/{NameEncoder.js → ProtobufNameEncoder.js} +7 -7
  24. package/lib/utils/ProtobufNameEncoder.js.map +1 -0
  25. package/package.json +1 -1
  26. package/src/factories/ProtobufFactory.ts +313 -14
  27. package/src/programmers/helpers/ProtobufUtil.ts +1 -1
  28. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +254 -315
  29. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +68 -115
  30. package/src/schemas/metadata/MetadataProperty.ts +2 -4
  31. package/src/schemas/protobuf/IProtobufProperty.ts +6 -0
  32. package/src/schemas/protobuf/IProtobufPropertyType.ts +37 -0
  33. package/src/schemas/protobuf/IProtobufSchema.ts +50 -0
  34. package/src/utils/{NameEncoder.ts → ProtobufNameEncoder.ts} +1 -1
  35. package/lib/utils/NameEncoder.js.map +0 -1
@@ -1,20 +1,19 @@
1
1
  import ts from "typescript";
2
2
 
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
3
4
  import { MetadataCollection } from "../../factories/MetadataCollection";
4
5
  import { ProtobufFactory } from "../../factories/ProtobufFactory";
5
6
 
6
7
  import { Metadata } from "../../schemas/metadata/Metadata";
7
- import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
8
- import { MetadataMap } from "../../schemas/metadata/MetadataMap";
9
8
  import { MetadataObjectType } from "../../schemas/metadata/MetadataObjectType";
10
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
9
+ import { IProtobufProperty } from "../../schemas/protobuf/IProtobufProperty";
10
+ import { IProtobufPropertyType } from "../../schemas/protobuf/IProtobufPropertyType";
11
+ import { IProtobufSchema } from "../../schemas/protobuf/IProtobufSchema";
11
12
 
12
13
  import { ITypiaContext } from "../../transformers/ITypiaContext";
13
14
 
14
15
  import { MapUtil } from "../../utils/MapUtil";
15
- import { NameEncoder } from "../../utils/NameEncoder";
16
-
17
- import { ProtobufUtil } from "../helpers/ProtobufUtil";
16
+ import { ProtobufNameEncoder } from "../../utils/ProtobufNameEncoder";
18
17
 
19
18
  export namespace ProtobufMessageProgrammer {
20
19
  export interface IProps {
@@ -48,7 +47,17 @@ export namespace ProtobufMessageProgrammer {
48
47
  .join("\n\n");
49
48
 
50
49
  // RETURNS
51
- return ts.factory.createStringLiteral(content);
50
+ return ts.factory.createCallExpression(
51
+ IdentifierFactory.access(
52
+ ts.factory.createArrayLiteralExpression(
53
+ content.split("\n").map((str) => ts.factory.createStringLiteral(str)),
54
+ true,
55
+ ),
56
+ "join",
57
+ ),
58
+ undefined,
59
+ [ts.factory.createStringLiteral("\n")],
60
+ );
52
61
  };
53
62
 
54
63
  const emplace = (props: {
@@ -74,11 +83,11 @@ export namespace ProtobufMessageProgrammer {
74
83
 
75
84
  const write_hierarchy = (hierarchy: Hierarchy): string => {
76
85
  const elements: string[] = [
77
- `message ${NameEncoder.encode(hierarchy.key)} {`,
86
+ `message ${ProtobufNameEncoder.encode(hierarchy.key)} {`,
78
87
  ];
79
88
  if (hierarchy.object !== null) {
80
89
  const text: string = write_object(hierarchy.object);
81
- elements.push(...text.split("\n").map((str) => `${TAB}${str}`));
90
+ elements.push(...text.split("\n").map((str) => ` ${str}`));
82
91
  }
83
92
  if (hierarchy.children.size)
84
93
  elements.push(
@@ -87,7 +96,7 @@ export namespace ProtobufMessageProgrammer {
87
96
  .map((body) =>
88
97
  body
89
98
  .split("\n")
90
- .map((line) => `${TAB}${line}`)
99
+ .map((line) => ` ${line}`)
91
100
  .join("\n"),
92
101
  )
93
102
  .join("\n\n"),
@@ -96,24 +105,17 @@ export namespace ProtobufMessageProgrammer {
96
105
  return elements.join("\n");
97
106
  };
98
107
 
99
- const write_object = (object: MetadataObjectType): string => {
100
- const sequence: IPointer<number> = { value: 0 };
101
- return object.properties
108
+ const write_object = (obj: MetadataObjectType): string => {
109
+ return obj.properties
102
110
  .map((p) => {
111
+ if (p.of_protobuf_ === undefined) ProtobufFactory.emplaceObject(obj);
112
+ const schema: IProtobufProperty = p.of_protobuf_!;
103
113
  const key: string = p.key.getSoleLiteral()!;
104
- const type: string = decode({
105
- sequence,
106
- metadata: p.value,
114
+ return decodeProperty({
115
+ key,
116
+ value: p.value,
117
+ union: schema.union,
107
118
  });
108
- return type.indexOf("${name}") !== -1
109
- ? type.replace("${name}", key)
110
- : `${
111
- p.value.arrays.length || type.startsWith("map<")
112
- ? ""
113
- : !p.value.isRequired() || p.value.nullable
114
- ? "optional "
115
- : "required "
116
- }${type} ${key} = ${++sequence.value};`;
117
119
  })
118
120
  .join("\n");
119
121
  };
@@ -121,96 +123,53 @@ export namespace ProtobufMessageProgrammer {
121
123
  /* -----------------------------------------------------------
122
124
  DECODERS
123
125
  ----------------------------------------------------------- */
124
- const decode = (props: {
125
- sequence: IPointer<number>;
126
- metadata: Metadata;
126
+ const decodeProperty = (props: {
127
+ key: string;
128
+ value: Metadata;
129
+ union: IProtobufPropertyType[];
127
130
  }): string => {
128
- const union: Map<string, number | null> = new Map();
129
- for (const native of props.metadata.natives)
130
- if (native.name === "Uint8Array")
131
- union.set("bytes", ProtobufUtil.getSequence(native.tags[0] ?? []));
132
- ProtobufUtil.getAtomics(props.metadata, union);
133
- for (const array of props.metadata.arrays)
134
- union.set(
135
- `repeated ${decode({
136
- sequence: props.sequence,
137
- metadata: array.type.value,
138
- })}`,
139
- ProtobufUtil.getSequence(array.tags[0] ?? []),
140
- );
141
- for (const obj of props.metadata.objects)
142
- union.set(
143
- is_dynamic_object(obj.type)
144
- ? decode_map({
145
- sequence: props.sequence,
146
- entry: MetadataProperty.create({
147
- ...obj.type.properties[0]!,
148
- key: (() => {
149
- const key: Metadata = Metadata.initialize();
150
- key.atomics.push(
151
- MetadataAtomic.create({
152
- type: "string",
153
- tags: [],
154
- }),
155
- );
156
- return key;
157
- })(),
158
- }),
159
- })
160
- : NameEncoder.encode(obj.type.name),
161
- ProtobufUtil.getSequence(obj.tags[0] ?? []),
162
- );
163
- for (const entry of props.metadata.maps)
164
- union.set(
165
- decode_map({
166
- sequence: props.sequence,
167
- entry,
168
- }),
169
- ProtobufUtil.getSequence(entry.tags[0] ?? []),
170
- );
171
-
172
- const sequenced: boolean = Array.from(union.values()).every(
173
- (x) => x !== null,
174
- );
175
- if (sequenced === false)
176
- return union.size === 1
177
- ? union.keys().next().value!
178
- : [
179
- "oneof ${name} {",
180
- ...Array.from(union.keys()).map((str) => {
181
- const index: number = ++props.sequence.value;
182
- return `${TAB}${str} v${index} = ${index};`;
183
- }),
184
- "}",
185
- ].join("\n");
186
- else if (union.size === 1) {
187
- const [key, value] = union.entries().next().value!;
188
- props.sequence.value = value!;
189
- return `${key} $\{name} = ${value}`;
131
+ if (props.union.length === 1) {
132
+ const top = props.union[0]!;
133
+ return [
134
+ ...(top.type === "array" || top.type === "map"
135
+ ? []
136
+ : [
137
+ props.value.isRequired() && props.value.nullable === false
138
+ ? "required"
139
+ : "optional",
140
+ ]),
141
+ decodeSchema(top),
142
+ props.key,
143
+ "=",
144
+ `${top.index};`,
145
+ ].join(" ");
190
146
  }
191
- props.sequence.value = Math.max(
192
- ...Array.from(union.values()).filter((v) => v !== null),
193
- );
194
147
  return [
195
- "oneof ${name} {",
196
- ...Array.from(union.entries()).map(
197
- ([key, value]) => `${TAB}${key} v${value} = ${value};`,
198
- ),
199
- "}",
148
+ `oneof ${props.key} {`,
149
+ ...props.union
150
+ .map((type) => `${decodeSchema(type)} v${type.index} = ${type.index};`)
151
+ .map((str) => str.split("\n"))
152
+ .map((str) => ` ${str}`),
153
+ `}`,
200
154
  ].join("\n");
201
155
  };
202
156
 
203
- const decode_map = (props: {
204
- sequence: IPointer<number>;
205
- entry: MetadataMap | MetadataProperty;
206
- }): string =>
207
- `map<${decode({
208
- sequence: props.sequence,
209
- metadata: props.entry.key,
210
- })}, ${decode({
211
- sequence: props.sequence,
212
- metadata: props.entry.value,
213
- })}>`;
157
+ const decodeSchema = (schema: IProtobufSchema): string => {
158
+ if (
159
+ schema.type === "bytes" ||
160
+ schema.type === "bool" ||
161
+ schema.type === "string"
162
+ )
163
+ return schema.type;
164
+ else if (schema.type === "bigint" || schema.type === "number")
165
+ return schema.name;
166
+ else if (schema.type === "array")
167
+ return `repeated ${decodeSchema(schema.value)}`;
168
+ else if (schema.type === "object")
169
+ return ProtobufNameEncoder.encode(schema.object.name);
170
+ // else if (schema.type === "map")
171
+ return `map<${decodeSchema(schema.key)}, ${decodeSchema(schema.value)}>`;
172
+ };
214
173
  }
215
174
 
216
175
  interface Hierarchy {
@@ -218,9 +177,3 @@ interface Hierarchy {
218
177
  object: MetadataObjectType | null;
219
178
  children: Map<string, Hierarchy>;
220
179
  }
221
-
222
- interface IPointer<T> {
223
- value: T;
224
- }
225
-
226
- const TAB = " ".repeat(2);
@@ -1,5 +1,6 @@
1
1
  import { ClassProperties } from "../../typings/ClassProperties";
2
2
 
3
+ import { IProtobufProperty } from "../protobuf/IProtobufProperty";
3
4
  import { IJsDocTagInfo } from "./IJsDocTagInfo";
4
5
  import { IMetadataDictionary } from "./IMetadataDictionary";
5
6
  import { IMetadataProperty } from "./IMetadataProperty";
@@ -11,10 +12,7 @@ export class MetadataProperty {
11
12
  public readonly description: string | null;
12
13
  public readonly jsDocTags: IJsDocTagInfo[];
13
14
 
14
- /**
15
- * @internal
16
- */
17
- public readonly sequences_?: Set<number>;
15
+ public of_protobuf_?: IProtobufProperty;
18
16
 
19
17
  /* -----------------------------------------------------------
20
18
  CONSTRUCTORS
@@ -0,0 +1,6 @@
1
+ import { IProtobufPropertyType } from "./IProtobufPropertyType";
2
+
3
+ export interface IProtobufProperty {
4
+ fixed: boolean;
5
+ union: IProtobufPropertyType[];
6
+ }
@@ -0,0 +1,37 @@
1
+ import { IProtobufSchema } from "./IProtobufSchema";
2
+
3
+ export type IProtobufPropertyType =
4
+ | IProtobufPropertyType.IByte
5
+ | IProtobufPropertyType.IBoolean
6
+ | IProtobufPropertyType.IBigint
7
+ | IProtobufPropertyType.INumber
8
+ | IProtobufPropertyType.IString
9
+ | IProtobufPropertyType.IArray
10
+ | IProtobufPropertyType.IObject
11
+ | IProtobufPropertyType.IMap;
12
+ export namespace IProtobufPropertyType {
13
+ export interface IByte extends IProtobufSchema.IByte {
14
+ index: number;
15
+ }
16
+ export interface IBoolean extends IProtobufSchema.IBoolean {
17
+ index: number;
18
+ }
19
+ export interface IBigint extends IProtobufSchema.IBigint {
20
+ index: number;
21
+ }
22
+ export interface INumber extends IProtobufSchema.INumber {
23
+ index: number;
24
+ }
25
+ export interface IString extends IProtobufSchema.IString {
26
+ index: number;
27
+ }
28
+ export interface IArray extends IProtobufSchema.IArray {
29
+ index: number;
30
+ }
31
+ export interface IObject extends IProtobufSchema.IObject {
32
+ index: number;
33
+ }
34
+ export interface IMap extends IProtobufSchema.IMap {
35
+ index: number;
36
+ }
37
+ }
@@ -0,0 +1,50 @@
1
+ import { MetadataArrayType } from "../metadata/MetadataArrayType";
2
+ import { MetadataMap } from "../metadata/MetadataMap";
3
+ import { MetadataObjectType } from "../metadata/MetadataObjectType";
4
+
5
+ export type IProtobufSchema =
6
+ | IProtobufSchema.IByte
7
+ | IProtobufSchema.IBoolean
8
+ | IProtobufSchema.IBigint
9
+ | IProtobufSchema.INumber
10
+ | IProtobufSchema.IString
11
+ | IProtobufSchema.IArray
12
+ | IProtobufSchema.IObject
13
+ | IProtobufSchema.IMap;
14
+ export namespace IProtobufSchema {
15
+ export interface IByte {
16
+ type: "bytes";
17
+ }
18
+ export interface IBoolean {
19
+ type: "bool";
20
+ }
21
+ export interface IBigint {
22
+ type: "bigint";
23
+ name: "int64" | "uint64";
24
+ }
25
+ export interface INumber {
26
+ type: "number";
27
+ name: "int32" | "int64" | "uint32" | "uint64" | "float" | "double";
28
+ }
29
+ export interface IString {
30
+ type: "string";
31
+ }
32
+ export interface IArray {
33
+ type: "array";
34
+ array: MetadataArrayType;
35
+ value: Exclude<IProtobufSchema, IArray | IMap>;
36
+ }
37
+ export interface IObject {
38
+ type: "object";
39
+ object: MetadataObjectType;
40
+ }
41
+ export interface IMap {
42
+ type: "map";
43
+ map: MetadataMap | MetadataObjectType;
44
+ key:
45
+ | IProtobufSchema.IBoolean
46
+ | IProtobufSchema.INumber
47
+ | IProtobufSchema.IString;
48
+ value: Exclude<IProtobufSchema, IArray | IMap>;
49
+ }
50
+ }
@@ -1,4 +1,4 @@
1
- export namespace NameEncoder {
1
+ export namespace ProtobufNameEncoder {
2
2
  export const encode = (str: string): string => {
3
3
  for (const [before, after] of REPLACERS)
4
4
  str = str.split(before).join(after);
@@ -1 +0,0 @@
1
- {"version":3,"file":"NameEncoder.js","sourceRoot":"","sources":["../../src/utils/NameEncoder.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAiB,WAAW,CAY3B;AAZD,WAAiB,WAAW;IACb,kBAAM,GAAG,UAAC,GAAW;;;YAChC,KAA8B,IAAA,cAAA,SAAA,SAAS,CAAA,oCAAA;gBAA5B,IAAA,KAAA,8BAAe,EAAd,MAAM,QAAA,EAAE,KAAK,QAAA;gBACvB,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;aAAA;;;;;;;;;QACtC,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;IAEW,kBAAM,GAAG,UAAC,GAAW;;;YAChC,KAA8B,IAAA,cAAA,SAAA,SAAS,CAAA,oCAAA;gBAA5B,IAAA,KAAA,8BAAe,EAAd,MAAM,QAAA,EAAE,KAAK,QAAA;gBACvB,IAAI,KAAK,KAAK,EAAE;oBAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;aAAA;;;;;;;;;QACxD,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;AACJ,CAAC,EAZgB,WAAW,2BAAX,WAAW,QAY3B;AAED,IAAM,SAAS,GAAuB;IACpC,CAAC,GAAG,EAAE,UAAU,CAAC;IACjB,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,OAAO,CAAC;IACd,CAAC,GAAG,EAAE,SAAS,CAAC;IAChB,CAAC,GAAG,EAAE,aAAa,CAAC;IACpB,CAAC,GAAG,EAAE,eAAe,CAAC;IACtB,CAAC,GAAG,EAAE,eAAe,CAAC;IACtB,CAAC,GAAG,EAAE,SAAS,CAAC;CACjB,CAAC"}