typia 5.0.0-dev.20230814 → 5.0.0-dev.20230818
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/ExpressionFactory.d.ts +1 -0
- package/lib/factories/ExpressionFactory.js +33 -0
- package/lib/factories/ExpressionFactory.js.map +1 -1
- package/lib/factories/StatementFactory.d.ts +2 -0
- package/lib/factories/StatementFactory.js +11 -0
- package/lib/factories/StatementFactory.js.map +1 -1
- package/lib/functional/$ProtobufReader.d.ts +33 -0
- package/lib/functional/$ProtobufReader.js +155 -0
- package/lib/functional/$ProtobufReader.js.map +1 -0
- package/lib/functional/$ProtobufSizer.d.ts +36 -0
- package/lib/functional/$ProtobufSizer.js +105 -0
- package/lib/functional/$ProtobufSizer.js.map +1 -0
- package/lib/functional/$ProtobufWriter.d.ts +45 -0
- package/lib/functional/$ProtobufWriter.js +107 -0
- package/lib/functional/$ProtobufWriter.js.map +1 -0
- package/lib/functional/IProtobufWriter.d.ts +15 -0
- package/lib/functional/IProtobufWriter.js +3 -0
- package/lib/functional/IProtobufWriter.js.map +1 -0
- package/lib/functional/Namespace.js +13 -6
- package/lib/functional/Namespace.js.map +1 -1
- package/lib/programmers/FeatureProgrammer.js +2 -2
- package/lib/programmers/FeatureProgrammer.js.map +1 -1
- package/lib/programmers/helpers/CloneJoiner.js +1 -7
- package/lib/programmers/helpers/CloneJoiner.js.map +1 -1
- package/lib/programmers/helpers/ProtobufWire.d.ts +16 -12
- package/lib/programmers/helpers/RandomRanger.js +1 -1
- package/lib/programmers/helpers/RandomRanger.js.map +1 -1
- package/lib/programmers/json/JsonStringifyProgrammer.js +3 -0
- package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +269 -127
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js +8 -2
- package/lib/programmers/protobuf/ProtobufMessageProgrammer.js.map +1 -1
- package/lib/protobuf.js +23 -9
- package/lib/protobuf.js.map +1 -1
- package/lib/schemas/metadata/MetadataObject.js +4 -0
- package/lib/schemas/metadata/MetadataObject.js.map +1 -1
- package/lib/transformers/CallExpressionTransformer.js +0 -4
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/json/JsonApplicationTransformer.js +6 -3
- package/lib/transformers/features/json/JsonApplicationTransformer.js.map +1 -1
- package/package.json +1 -1
- package/src/factories/ExpressionFactory.ts +20 -0
- package/src/factories/StatementFactory.ts +32 -0
- package/src/functional/$ProtobufReader.ts +189 -0
- package/src/functional/$ProtobufSizer.ts +144 -0
- package/src/functional/$ProtobufWriter.ts +152 -0
- package/src/functional/IProtobufWriter.ts +18 -0
- package/src/functional/Namespace.ts +14 -7
- package/src/programmers/FeatureProgrammer.ts +2 -2
- package/src/programmers/helpers/CloneJoiner.ts +1 -25
- package/src/programmers/helpers/ProtobufWire.ts +18 -12
- package/src/programmers/helpers/RandomRanger.ts +1 -1
- package/src/programmers/json/JsonStringifyProgrammer.ts +8 -0
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +544 -201
- package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +4 -1
- package/src/protobuf.ts +23 -29
- package/src/schemas/metadata/MetadataObject.ts +10 -0
- package/src/transformers/CallExpressionTransformer.ts +0 -4
- package/src/transformers/features/json/JsonApplicationTransformer.ts +12 -3
- package/lib/programmers/protobuf/ProtobufSizeProgrammer.d.ts +0 -5
- package/lib/programmers/protobuf/ProtobufSizeProgrammer.js +0 -409
- package/lib/programmers/protobuf/ProtobufSizeProgrammer.js.map +0 -1
- package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.d.ts +0 -3
- package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js +0 -10
- package/lib/transformers/features/protobuf/CreateProtobufSizeTransformer.js.map +0 -1
- package/lib/transformers/features/protobuf/ProtobufSizeTransformer.d.ts +0 -3
- package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js +0 -10
- package/lib/transformers/features/protobuf/ProtobufSizeTransformer.js.map +0 -1
- package/src/programmers/protobuf/ProtobufSizeProgrammer.ts +0 -716
- package/src/transformers/features/protobuf/CreateProtobufSizeTransformer.ts +0 -9
- package/src/transformers/features/protobuf/ProtobufSizeTransformer.ts +0 -9
|
@@ -1,716 +0,0 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { ExpressionFactory } from "../../factories/ExpressionFactory";
|
|
4
|
-
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
5
|
-
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
6
|
-
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
7
|
-
import { StatementFactory } from "../../factories/StatementFactory";
|
|
8
|
-
import { TypeFactory } from "../../factories/TypeFactory";
|
|
9
|
-
|
|
10
|
-
import { $strlen } from "../../functional/$strlen";
|
|
11
|
-
|
|
12
|
-
import { IJsDocTagInfo } from "../../schemas/metadata/IJsDocTagInfo";
|
|
13
|
-
import { IMetadataTag } from "../../schemas/metadata/IMetadataTag";
|
|
14
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
15
|
-
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
16
|
-
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
17
|
-
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
18
|
-
import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
|
|
19
|
-
|
|
20
|
-
import { IProject } from "../../transformers/IProject";
|
|
21
|
-
|
|
22
|
-
import { Atomic } from "../../typings/Atomic";
|
|
23
|
-
|
|
24
|
-
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
25
|
-
import { IsProgrammer } from "../IsProgrammer";
|
|
26
|
-
import { FunctionImporter } from "../helpers/FunctionImporeter";
|
|
27
|
-
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
28
|
-
import { UnionExplorer } from "../helpers/UnionExplorer";
|
|
29
|
-
import { check_native } from "../internal/check_native";
|
|
30
|
-
import { decode_union_object } from "../internal/decode_union_object";
|
|
31
|
-
|
|
32
|
-
export namespace ProtobufSizeProgrammer {
|
|
33
|
-
export const write =
|
|
34
|
-
(project: IProject) => (modulo: ts.LeftHandSideExpression) => {
|
|
35
|
-
const importer: FunctionImporter = new FunctionImporter();
|
|
36
|
-
const copnfig: FeatureProgrammer.IConfig = configure(
|
|
37
|
-
modulo.getText(),
|
|
38
|
-
)(project)(importer);
|
|
39
|
-
return FeatureProgrammer.write(project)({
|
|
40
|
-
...copnfig,
|
|
41
|
-
addition: (collection) => [
|
|
42
|
-
...IsProgrammer.write_function_statements(project)(
|
|
43
|
-
importer,
|
|
44
|
-
)(collection),
|
|
45
|
-
...importer.declare(modulo),
|
|
46
|
-
],
|
|
47
|
-
})(importer);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const write_object_function_body =
|
|
51
|
-
(project: IProject) =>
|
|
52
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
53
|
-
(importer: FunctionImporter) =>
|
|
54
|
-
(obj: MetadataObject): ts.ConciseBody => {
|
|
55
|
-
const explore: FeatureProgrammer.IExplore = {
|
|
56
|
-
tracable: false,
|
|
57
|
-
source: "function",
|
|
58
|
-
from: "object",
|
|
59
|
-
postfix: "",
|
|
60
|
-
};
|
|
61
|
-
if (obj.properties.some((p) => !p.key.isSoleLiteral())) {
|
|
62
|
-
const p = obj.properties[0]!;
|
|
63
|
-
return write_map_function_body(project)(config)(importer)(
|
|
64
|
-
ts.factory.createIdentifier("input"),
|
|
65
|
-
p,
|
|
66
|
-
explore,
|
|
67
|
-
p.tags,
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const zero = ts.factory.createNumericLiteral(0);
|
|
72
|
-
const compute = (p: MetadataProperty) => {
|
|
73
|
-
const input = IdentifierFactory.access(
|
|
74
|
-
ts.factory.createIdentifier("input"),
|
|
75
|
-
)(p.key.getSoleLiteral()!);
|
|
76
|
-
const output = ts.factory.createAdd(
|
|
77
|
-
ts.factory.createNumericLiteral(4),
|
|
78
|
-
decode(project)(config)(importer)(
|
|
79
|
-
input,
|
|
80
|
-
p.value,
|
|
81
|
-
explore,
|
|
82
|
-
p.tags,
|
|
83
|
-
),
|
|
84
|
-
);
|
|
85
|
-
if (p.value.isRequired() === true && p.value.nullable === false)
|
|
86
|
-
return output;
|
|
87
|
-
else if (
|
|
88
|
-
p.value.isRequired() === true &&
|
|
89
|
-
p.value.nullable === true
|
|
90
|
-
)
|
|
91
|
-
return ts.factory.createConditionalExpression(
|
|
92
|
-
ts.factory.createStrictInequality(
|
|
93
|
-
ts.factory.createNull(),
|
|
94
|
-
input,
|
|
95
|
-
),
|
|
96
|
-
undefined,
|
|
97
|
-
output,
|
|
98
|
-
undefined,
|
|
99
|
-
zero,
|
|
100
|
-
);
|
|
101
|
-
else if (
|
|
102
|
-
p.value.isRequired() === false &&
|
|
103
|
-
p.value.nullable === false
|
|
104
|
-
)
|
|
105
|
-
return ts.factory.createConditionalExpression(
|
|
106
|
-
ts.factory.createStrictInequality(
|
|
107
|
-
ts.factory.createIdentifier("undefined"),
|
|
108
|
-
input,
|
|
109
|
-
),
|
|
110
|
-
undefined,
|
|
111
|
-
output,
|
|
112
|
-
undefined,
|
|
113
|
-
zero,
|
|
114
|
-
);
|
|
115
|
-
else
|
|
116
|
-
return ts.factory.createConditionalExpression(
|
|
117
|
-
ts.factory.createLogicalAnd(
|
|
118
|
-
ts.factory.createStrictInequality(
|
|
119
|
-
ts.factory.createIdentifier("undefined"),
|
|
120
|
-
input,
|
|
121
|
-
),
|
|
122
|
-
ts.factory.createStrictInequality(
|
|
123
|
-
ts.factory.createNull(),
|
|
124
|
-
input,
|
|
125
|
-
),
|
|
126
|
-
),
|
|
127
|
-
undefined,
|
|
128
|
-
output,
|
|
129
|
-
undefined,
|
|
130
|
-
zero,
|
|
131
|
-
);
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
return obj.properties
|
|
135
|
-
.map((p) => compute(p))
|
|
136
|
-
.reduce((a, b) => ts.factory.createAdd(a, b));
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const write_map_function_body =
|
|
140
|
-
(project: IProject) =>
|
|
141
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
142
|
-
(importer: FunctionImporter) =>
|
|
143
|
-
(
|
|
144
|
-
input: ts.Expression,
|
|
145
|
-
entry: Metadata.Entry,
|
|
146
|
-
explore: FeatureProgrammer.IExplore,
|
|
147
|
-
tags: IMetadataTag[],
|
|
148
|
-
): ts.Block => {
|
|
149
|
-
const key = ts.factory.createIdentifier("key");
|
|
150
|
-
const value = ts.factory.createIdentifier("value");
|
|
151
|
-
const tuple = ts.factory.createVariableDeclarationList(
|
|
152
|
-
[
|
|
153
|
-
ts.factory.createVariableDeclaration(
|
|
154
|
-
ts.factory.createArrayBindingPattern([
|
|
155
|
-
ts.factory.createBindingElement(
|
|
156
|
-
undefined,
|
|
157
|
-
undefined,
|
|
158
|
-
key,
|
|
159
|
-
),
|
|
160
|
-
ts.factory.createBindingElement(
|
|
161
|
-
undefined,
|
|
162
|
-
undefined,
|
|
163
|
-
value,
|
|
164
|
-
),
|
|
165
|
-
]),
|
|
166
|
-
),
|
|
167
|
-
],
|
|
168
|
-
ts.NodeFlags.Const,
|
|
169
|
-
);
|
|
170
|
-
const block = ts.factory.createBlock(
|
|
171
|
-
[
|
|
172
|
-
decode_atomic(importer)(key, "string", []),
|
|
173
|
-
decode(project)(config)(importer)(
|
|
174
|
-
value,
|
|
175
|
-
entry.value,
|
|
176
|
-
explore,
|
|
177
|
-
tags,
|
|
178
|
-
),
|
|
179
|
-
].map((expr) =>
|
|
180
|
-
ts.factory.createExpressionStatement(
|
|
181
|
-
ts.factory.createBinaryExpression(
|
|
182
|
-
ts.factory.createIdentifier("size"),
|
|
183
|
-
ts.factory.createToken(
|
|
184
|
-
ts.SyntaxKind.PlusEqualsToken,
|
|
185
|
-
),
|
|
186
|
-
expr,
|
|
187
|
-
),
|
|
188
|
-
),
|
|
189
|
-
),
|
|
190
|
-
true,
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
return ts.factory.createBlock([
|
|
194
|
-
StatementFactory.mut(
|
|
195
|
-
"size",
|
|
196
|
-
ts.factory.createNumericLiteral(0),
|
|
197
|
-
),
|
|
198
|
-
ts.factory.createForOfStatement(undefined, tuple, input, block),
|
|
199
|
-
ts.factory.createReturnStatement(
|
|
200
|
-
ts.factory.createIdentifier("size"),
|
|
201
|
-
),
|
|
202
|
-
]);
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
/* -----------------------------------------------------------
|
|
206
|
-
DECODERS
|
|
207
|
-
----------------------------------------------------------- */
|
|
208
|
-
const decode =
|
|
209
|
-
(project: IProject) =>
|
|
210
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
211
|
-
(importer: FunctionImporter) =>
|
|
212
|
-
(
|
|
213
|
-
input: ts.Expression,
|
|
214
|
-
meta: Metadata,
|
|
215
|
-
explore: FeatureProgrammer.IExplore,
|
|
216
|
-
tags: IMetadataTag[],
|
|
217
|
-
): ts.Expression => {
|
|
218
|
-
// STARTS FROM ATOMIC TYPES
|
|
219
|
-
const unions: IUnion[] = ProtobufUtil.atomics(meta).map((type) => ({
|
|
220
|
-
type,
|
|
221
|
-
is: () =>
|
|
222
|
-
ts.factory.createStrictEquality(
|
|
223
|
-
ts.factory.createStringLiteral(type),
|
|
224
|
-
ts.factory.createTypeOfExpression(input),
|
|
225
|
-
),
|
|
226
|
-
value: () => decode_atomic(importer)(input, type, tags),
|
|
227
|
-
}));
|
|
228
|
-
|
|
229
|
-
// CONSIDER ARRAYS
|
|
230
|
-
if (meta.arrays.length) {
|
|
231
|
-
const value: () => ts.Expression =
|
|
232
|
-
meta.arrays.length === 1
|
|
233
|
-
? () =>
|
|
234
|
-
decode_array(project)(config)(importer)(
|
|
235
|
-
input,
|
|
236
|
-
meta.arrays[0]!,
|
|
237
|
-
explore,
|
|
238
|
-
tags,
|
|
239
|
-
)
|
|
240
|
-
: () =>
|
|
241
|
-
explore_arrays(project)(config)(importer)(
|
|
242
|
-
input,
|
|
243
|
-
meta.arrays,
|
|
244
|
-
explore,
|
|
245
|
-
tags,
|
|
246
|
-
);
|
|
247
|
-
unions.push({
|
|
248
|
-
type: "array",
|
|
249
|
-
is: () => ExpressionFactory.isArray(input),
|
|
250
|
-
value,
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// CONSIDER OBJECTS
|
|
255
|
-
if (meta.objects.length)
|
|
256
|
-
unions.push({
|
|
257
|
-
type: "object",
|
|
258
|
-
is: () =>
|
|
259
|
-
ExpressionFactory.isObject({
|
|
260
|
-
checkNull: true,
|
|
261
|
-
checkArray: false,
|
|
262
|
-
})(input),
|
|
263
|
-
value: () =>
|
|
264
|
-
explore_objects(project)(config)(importer)(
|
|
265
|
-
input,
|
|
266
|
-
meta,
|
|
267
|
-
explore,
|
|
268
|
-
),
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
// CONSIDER MAPS
|
|
272
|
-
if (meta.maps.length)
|
|
273
|
-
unions.push({
|
|
274
|
-
type: "map",
|
|
275
|
-
is: () => ExpressionFactory.isInstanceOf("Map")(input),
|
|
276
|
-
value: () =>
|
|
277
|
-
decode_map(project)(config)(importer)(
|
|
278
|
-
input,
|
|
279
|
-
meta.maps[0]!,
|
|
280
|
-
explore,
|
|
281
|
-
tags,
|
|
282
|
-
),
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
// CONSIDER BINARY
|
|
286
|
-
if (meta.natives.length && meta.natives[0] === "Uint8Array")
|
|
287
|
-
unions.push({
|
|
288
|
-
type: "native",
|
|
289
|
-
is: () => check_native("Uint8Array")(input),
|
|
290
|
-
value: () => decode_binary(input),
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
return unions.length === 1
|
|
294
|
-
? unions[0]!.value()
|
|
295
|
-
: ts.factory.createCallExpression(
|
|
296
|
-
ts.factory.createArrowFunction(
|
|
297
|
-
undefined,
|
|
298
|
-
undefined,
|
|
299
|
-
[],
|
|
300
|
-
undefined,
|
|
301
|
-
undefined,
|
|
302
|
-
iterate(importer, input, unions, meta.getName()),
|
|
303
|
-
),
|
|
304
|
-
undefined,
|
|
305
|
-
undefined,
|
|
306
|
-
);
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
const iterate = (
|
|
310
|
-
importer: FunctionImporter,
|
|
311
|
-
input: ts.Expression,
|
|
312
|
-
unions: IUnion[],
|
|
313
|
-
expected: string,
|
|
314
|
-
) =>
|
|
315
|
-
ts.factory.createBlock(
|
|
316
|
-
[
|
|
317
|
-
...unions.map((u) =>
|
|
318
|
-
ts.factory.createIfStatement(
|
|
319
|
-
u.is(),
|
|
320
|
-
ts.factory.createReturnStatement(u.value()),
|
|
321
|
-
),
|
|
322
|
-
),
|
|
323
|
-
create_throw_error(importer)(expected)(input),
|
|
324
|
-
],
|
|
325
|
-
true,
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
const decode_atomic =
|
|
329
|
-
(importer: FunctionImporter) =>
|
|
330
|
-
(input: ts.Expression, type: Atomic.Literal, tags: IMetadataTag[]) =>
|
|
331
|
-
type === "string"
|
|
332
|
-
? ts.factory.createAdd(
|
|
333
|
-
ts.factory.createNumericLiteral(4),
|
|
334
|
-
ts.factory.createCallExpression(
|
|
335
|
-
importer.use("strlen"),
|
|
336
|
-
undefined,
|
|
337
|
-
[input],
|
|
338
|
-
),
|
|
339
|
-
)
|
|
340
|
-
: ts.factory.createNumericLiteral(
|
|
341
|
-
computeAtomicLength(type)(tags)!,
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
const decode_array =
|
|
345
|
-
(project: IProject) =>
|
|
346
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
347
|
-
(importer: FunctionImporter) =>
|
|
348
|
-
(
|
|
349
|
-
input: ts.Expression,
|
|
350
|
-
array: MetadataArray,
|
|
351
|
-
explore: FeatureProgrammer.IExplore,
|
|
352
|
-
tags: IMetadataTag[],
|
|
353
|
-
): ts.Expression => {
|
|
354
|
-
const header = (expr: ts.Expression) =>
|
|
355
|
-
ts.factory.createAdd(ts.factory.createNumericLiteral(4), expr);
|
|
356
|
-
if (array.value.isBinaryUnion() === false) {
|
|
357
|
-
const length: number | null = computeFixedLength(array.value)(
|
|
358
|
-
tags,
|
|
359
|
-
);
|
|
360
|
-
if (length !== null)
|
|
361
|
-
return header(
|
|
362
|
-
ts.factory.createMultiply(
|
|
363
|
-
IdentifierFactory.access(input)("length"),
|
|
364
|
-
ts.factory.createNumericLiteral(length),
|
|
365
|
-
),
|
|
366
|
-
);
|
|
367
|
-
}
|
|
368
|
-
return header(
|
|
369
|
-
ts.factory.createCallExpression(
|
|
370
|
-
ts.factory.createCallExpression(
|
|
371
|
-
IdentifierFactory.access(input)("map"),
|
|
372
|
-
undefined,
|
|
373
|
-
[
|
|
374
|
-
ts.factory.createArrowFunction(
|
|
375
|
-
undefined,
|
|
376
|
-
undefined,
|
|
377
|
-
[IdentifierFactory.parameter("elem")],
|
|
378
|
-
undefined,
|
|
379
|
-
undefined,
|
|
380
|
-
decode(project)(config)(importer)(
|
|
381
|
-
ts.factory.createIdentifier("elem"),
|
|
382
|
-
array.value,
|
|
383
|
-
explore,
|
|
384
|
-
tags,
|
|
385
|
-
),
|
|
386
|
-
),
|
|
387
|
-
],
|
|
388
|
-
),
|
|
389
|
-
undefined,
|
|
390
|
-
[
|
|
391
|
-
ts.factory.createArrowFunction(
|
|
392
|
-
undefined,
|
|
393
|
-
undefined,
|
|
394
|
-
[
|
|
395
|
-
IdentifierFactory.parameter("a"),
|
|
396
|
-
IdentifierFactory.parameter("b"),
|
|
397
|
-
],
|
|
398
|
-
undefined,
|
|
399
|
-
undefined,
|
|
400
|
-
ts.factory.createAdd(
|
|
401
|
-
ts.factory.createIdentifier("a"),
|
|
402
|
-
ts.factory.createIdentifier("b"),
|
|
403
|
-
),
|
|
404
|
-
),
|
|
405
|
-
ts.factory.createNumericLiteral(0),
|
|
406
|
-
],
|
|
407
|
-
),
|
|
408
|
-
);
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
const decode_object = (importer: FunctionImporter) =>
|
|
412
|
-
FeatureProgrammer.decode_object({
|
|
413
|
-
trace: false,
|
|
414
|
-
path: false,
|
|
415
|
-
prefix: PREFIX,
|
|
416
|
-
})(importer);
|
|
417
|
-
|
|
418
|
-
const decode_binary = (input: ts.Expression): ts.Expression =>
|
|
419
|
-
ts.factory.createCallExpression(
|
|
420
|
-
IdentifierFactory.access(input)("writeBytes"),
|
|
421
|
-
undefined,
|
|
422
|
-
[input],
|
|
423
|
-
);
|
|
424
|
-
|
|
425
|
-
const decode_map =
|
|
426
|
-
(project: IProject) =>
|
|
427
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
428
|
-
(importer: FunctionImporter) =>
|
|
429
|
-
(
|
|
430
|
-
input: ts.Expression,
|
|
431
|
-
entry: Metadata.Entry,
|
|
432
|
-
explore: FeatureProgrammer.IExplore,
|
|
433
|
-
tags: IMetadataTag[],
|
|
434
|
-
): ts.Expression => {
|
|
435
|
-
return ts.factory.createCallExpression(
|
|
436
|
-
ts.factory.createArrowFunction(
|
|
437
|
-
undefined,
|
|
438
|
-
undefined,
|
|
439
|
-
[],
|
|
440
|
-
TypeFactory.keyword("any"),
|
|
441
|
-
undefined,
|
|
442
|
-
write_map_function_body(project)(config)(importer)(
|
|
443
|
-
input,
|
|
444
|
-
entry,
|
|
445
|
-
explore,
|
|
446
|
-
tags,
|
|
447
|
-
),
|
|
448
|
-
),
|
|
449
|
-
undefined,
|
|
450
|
-
undefined,
|
|
451
|
-
);
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
/* -----------------------------------------------------------
|
|
455
|
-
EXPLORERS
|
|
456
|
-
----------------------------------------------------------- */
|
|
457
|
-
const explore_objects =
|
|
458
|
-
(_project: IProject) =>
|
|
459
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
460
|
-
(importer: FunctionImporter) =>
|
|
461
|
-
(
|
|
462
|
-
input: ts.Expression,
|
|
463
|
-
meta: Metadata,
|
|
464
|
-
explore: FeatureProgrammer.IExplore,
|
|
465
|
-
) =>
|
|
466
|
-
meta.objects.length === 1
|
|
467
|
-
? decode_object(importer)(input, meta.objects[0]!, explore)
|
|
468
|
-
: ts.factory.createCallExpression(
|
|
469
|
-
ts.factory.createIdentifier(
|
|
470
|
-
importer.useLocal(
|
|
471
|
-
`${config.prefix}u${meta.union_index!}`,
|
|
472
|
-
),
|
|
473
|
-
),
|
|
474
|
-
undefined,
|
|
475
|
-
FeatureProgrammer.argumentsArray(config)(explore)(input),
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
const explore_arrays =
|
|
479
|
-
(project: IProject) =>
|
|
480
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
481
|
-
(importer: FunctionImporter) =>
|
|
482
|
-
(
|
|
483
|
-
input: ts.Expression,
|
|
484
|
-
elements: MetadataArray[],
|
|
485
|
-
explore: FeatureProgrammer.IExplore,
|
|
486
|
-
tags: IMetadataTag[],
|
|
487
|
-
): ts.Expression =>
|
|
488
|
-
explore_array_like_union_types(config)(importer)(
|
|
489
|
-
UnionExplorer.array({
|
|
490
|
-
checker: IsProgrammer.decode(project)(importer),
|
|
491
|
-
decoder: decode_array(project)(config)(importer),
|
|
492
|
-
empty: ts.factory.createStringLiteral("[]"),
|
|
493
|
-
success: ts.factory.createTrue(),
|
|
494
|
-
failure: (input, expected) =>
|
|
495
|
-
create_throw_error(importer)(expected)(input),
|
|
496
|
-
}),
|
|
497
|
-
)(input, elements, explore, tags);
|
|
498
|
-
|
|
499
|
-
const explore_array_like_union_types =
|
|
500
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
501
|
-
(importer: FunctionImporter) =>
|
|
502
|
-
<T extends MetadataArray | MetadataTuple>(
|
|
503
|
-
factory: (
|
|
504
|
-
parameters: ts.ParameterDeclaration[],
|
|
505
|
-
) => (
|
|
506
|
-
input: ts.Expression,
|
|
507
|
-
elements: T[],
|
|
508
|
-
explore: FeatureProgrammer.IExplore,
|
|
509
|
-
tags: IMetadataTag[],
|
|
510
|
-
jsDocTags: IJsDocTagInfo[],
|
|
511
|
-
) => ts.ArrowFunction,
|
|
512
|
-
) =>
|
|
513
|
-
(
|
|
514
|
-
input: ts.Expression,
|
|
515
|
-
elements: T[],
|
|
516
|
-
explore: FeatureProgrammer.IExplore,
|
|
517
|
-
tags: IMetadataTag[],
|
|
518
|
-
): ts.Expression => {
|
|
519
|
-
const arrow =
|
|
520
|
-
(parameters: ts.ParameterDeclaration[]) =>
|
|
521
|
-
(explore: FeatureProgrammer.IExplore) =>
|
|
522
|
-
(input: ts.Expression): ts.ArrowFunction =>
|
|
523
|
-
factory(parameters)(input, elements, explore, tags, []);
|
|
524
|
-
if (elements.every((e) => e.recursive === false))
|
|
525
|
-
ts.factory.createCallExpression(
|
|
526
|
-
arrow([])(explore)(input),
|
|
527
|
-
undefined,
|
|
528
|
-
[],
|
|
529
|
-
);
|
|
530
|
-
|
|
531
|
-
explore = {
|
|
532
|
-
...explore,
|
|
533
|
-
source: "function",
|
|
534
|
-
from: "array",
|
|
535
|
-
};
|
|
536
|
-
return ts.factory.createCallExpression(
|
|
537
|
-
ts.factory.createIdentifier(
|
|
538
|
-
importer.emplaceUnion(
|
|
539
|
-
config.prefix,
|
|
540
|
-
elements.map((e) => e.name).join(" | "),
|
|
541
|
-
() =>
|
|
542
|
-
arrow(
|
|
543
|
-
FeatureProgrammer.parameterDeclarations(config)(
|
|
544
|
-
TypeFactory.keyword("any"),
|
|
545
|
-
)(ts.factory.createIdentifier("input")),
|
|
546
|
-
)({
|
|
547
|
-
...explore,
|
|
548
|
-
postfix: "",
|
|
549
|
-
})(ts.factory.createIdentifier("input")),
|
|
550
|
-
),
|
|
551
|
-
),
|
|
552
|
-
undefined,
|
|
553
|
-
FeatureProgrammer.argumentsArray(config)(explore)(input),
|
|
554
|
-
);
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
/* -----------------------------------------------------------
|
|
558
|
-
CONFIGURATIONS
|
|
559
|
-
----------------------------------------------------------- */
|
|
560
|
-
const PREFIX = "$ps";
|
|
561
|
-
|
|
562
|
-
const configure =
|
|
563
|
-
(method: string) =>
|
|
564
|
-
(project: IProject) =>
|
|
565
|
-
(importer: FunctionImporter): FeatureProgrammer.IConfig => {
|
|
566
|
-
const config: FeatureProgrammer.IConfig<ts.Expression> = {
|
|
567
|
-
types: {
|
|
568
|
-
input: (type, name) =>
|
|
569
|
-
ts.factory.createTypeReferenceNode(
|
|
570
|
-
name ??
|
|
571
|
-
TypeFactory.getFullName(project.checker)(type),
|
|
572
|
-
),
|
|
573
|
-
output: (type, name) =>
|
|
574
|
-
ts.factory.createTypeReferenceNode(
|
|
575
|
-
`typia.Primitive<${
|
|
576
|
-
name ??
|
|
577
|
-
TypeFactory.getFullName(project.checker)(type)
|
|
578
|
-
}>`,
|
|
579
|
-
),
|
|
580
|
-
},
|
|
581
|
-
prefix: PREFIX,
|
|
582
|
-
trace: false,
|
|
583
|
-
path: false,
|
|
584
|
-
initializer: initializer(method),
|
|
585
|
-
decoder: () => decode(project)(config)(importer),
|
|
586
|
-
objector: {
|
|
587
|
-
checker: () => IsProgrammer.decode(project)(importer),
|
|
588
|
-
decoder: () => decode_object(importer),
|
|
589
|
-
joiner: (_input, _entries, obj) =>
|
|
590
|
-
write_object_function_body(project)(config)(importer)(
|
|
591
|
-
obj,
|
|
592
|
-
),
|
|
593
|
-
unionizer: decode_union_object(
|
|
594
|
-
IsProgrammer.decode_object(importer),
|
|
595
|
-
)(decode_object(importer))((exp) => exp)(
|
|
596
|
-
(input, expected) =>
|
|
597
|
-
create_throw_error(importer)(expected)(input),
|
|
598
|
-
),
|
|
599
|
-
failure: (input, expected) =>
|
|
600
|
-
create_throw_error(importer)(expected)(input),
|
|
601
|
-
},
|
|
602
|
-
generator: {
|
|
603
|
-
arrays: () => () => [],
|
|
604
|
-
tuples: () => () => [],
|
|
605
|
-
},
|
|
606
|
-
};
|
|
607
|
-
return config;
|
|
608
|
-
};
|
|
609
|
-
|
|
610
|
-
const initializer =
|
|
611
|
-
(method: string): FeatureProgrammer.IConfig["initializer"] =>
|
|
612
|
-
({ checker }) =>
|
|
613
|
-
(type) => {
|
|
614
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
615
|
-
const meta: Metadata =
|
|
616
|
-
ProtobufFactory.metadata(method)(checker)(collection)(type);
|
|
617
|
-
return [collection, meta];
|
|
618
|
-
};
|
|
619
|
-
|
|
620
|
-
const create_throw_error =
|
|
621
|
-
(importer: FunctionImporter) =>
|
|
622
|
-
(expected: string) =>
|
|
623
|
-
(value: ts.Expression) =>
|
|
624
|
-
ts.factory.createExpressionStatement(
|
|
625
|
-
ts.factory.createCallExpression(
|
|
626
|
-
importer.use("throws"),
|
|
627
|
-
[],
|
|
628
|
-
[
|
|
629
|
-
ts.factory.createObjectLiteralExpression(
|
|
630
|
-
[
|
|
631
|
-
ts.factory.createPropertyAssignment(
|
|
632
|
-
"expected",
|
|
633
|
-
ts.factory.createStringLiteral(expected),
|
|
634
|
-
),
|
|
635
|
-
ts.factory.createPropertyAssignment(
|
|
636
|
-
"value",
|
|
637
|
-
value,
|
|
638
|
-
),
|
|
639
|
-
],
|
|
640
|
-
true,
|
|
641
|
-
),
|
|
642
|
-
],
|
|
643
|
-
),
|
|
644
|
-
);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
const computeFixedLength =
|
|
648
|
-
(meta: Metadata) =>
|
|
649
|
-
(tags: IMetadataTag[]): number | null => {
|
|
650
|
-
if (meta.fixed_ !== undefined) return meta.fixed_;
|
|
651
|
-
return (meta.fixed_ = (() => {
|
|
652
|
-
if (
|
|
653
|
-
meta.isRequired() === false ||
|
|
654
|
-
meta.nullable ||
|
|
655
|
-
meta.natives.length ||
|
|
656
|
-
meta.maps.length ||
|
|
657
|
-
meta.isBinaryUnion()
|
|
658
|
-
)
|
|
659
|
-
return null;
|
|
660
|
-
else if (meta.arrays.length)
|
|
661
|
-
return computeFixedLength(meta.arrays[0]!.value)(tags);
|
|
662
|
-
|
|
663
|
-
const atomics = new Set([
|
|
664
|
-
...meta.atomics,
|
|
665
|
-
...meta.constants.map((c) => c.type),
|
|
666
|
-
...meta.templates.map(() => "string" as const),
|
|
667
|
-
]);
|
|
668
|
-
if (atomics.size)
|
|
669
|
-
return computeAtomicLength(Array.from(atomics)[0]!)(tags);
|
|
670
|
-
else if (meta.objects.length) {
|
|
671
|
-
const obj: MetadataObject = meta.objects[0]!;
|
|
672
|
-
if (obj.properties.some((p) => !p.key.isSoleLiteral()))
|
|
673
|
-
return null;
|
|
674
|
-
else if (
|
|
675
|
-
obj.properties.some(
|
|
676
|
-
(p) => computeFixedLength(p.value)(p.tags) === null,
|
|
677
|
-
)
|
|
678
|
-
)
|
|
679
|
-
return null;
|
|
680
|
-
return obj.properties
|
|
681
|
-
.map(
|
|
682
|
-
(p) =>
|
|
683
|
-
$strlen(p.key.getSoleLiteral()!) +
|
|
684
|
-
computeFixedLength(p.value)(p.tags)!,
|
|
685
|
-
)
|
|
686
|
-
.reduce((a, b) => a + b, 0);
|
|
687
|
-
}
|
|
688
|
-
return null;
|
|
689
|
-
})());
|
|
690
|
-
};
|
|
691
|
-
|
|
692
|
-
const computeAtomicLength =
|
|
693
|
-
(type: Atomic.Literal) =>
|
|
694
|
-
(tags: IMetadataTag[]): number | null => {
|
|
695
|
-
if (type === "string") return null;
|
|
696
|
-
else if (type === "boolean") return 1;
|
|
697
|
-
else if (type === "bigint") return 8;
|
|
698
|
-
|
|
699
|
-
const shorter = tags.find((tag) => tag.kind === "type") as
|
|
700
|
-
| IMetadataTag.INumberType
|
|
701
|
-
| undefined;
|
|
702
|
-
return shorter &&
|
|
703
|
-
(shorter.value === "int" ||
|
|
704
|
-
shorter.value === "uint" ||
|
|
705
|
-
shorter.value === "int32" ||
|
|
706
|
-
shorter.value === "uint32" ||
|
|
707
|
-
shorter.value === "float")
|
|
708
|
-
? 4
|
|
709
|
-
: 8;
|
|
710
|
-
};
|
|
711
|
-
|
|
712
|
-
interface IUnion {
|
|
713
|
-
type: string;
|
|
714
|
-
is: () => ts.Expression;
|
|
715
|
-
value: () => ts.Expression;
|
|
716
|
-
}
|