typia 5.1.0-dev.20230924 → 5.1.0-dev.20230925
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/README.md +1 -1
- package/lib/functional/$HeadersReader.d.ts +3 -3
- package/lib/functional/$HeadersReader.js +21 -3
- package/lib/functional/$HeadersReader.js.map +1 -1
- package/lib/functional/$ParameterReader.d.ts +6 -0
- package/lib/functional/$ParameterReader.js +35 -0
- package/lib/functional/$ParameterReader.js.map +1 -0
- package/lib/functional/$QueryReader.js +22 -2
- package/lib/functional/$QueryReader.js.map +1 -1
- package/lib/functional/Namespace.js +2 -0
- package/lib/functional/Namespace.js.map +1 -1
- package/lib/http.d.ts +2 -2
- package/lib/http.js +2 -0
- package/lib/http.js.map +1 -1
- package/lib/programmers/http/HttpHeadersProgrammer.js +30 -46
- package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpParameterProgrammer.js +10 -29
- package/lib/programmers/http/HttpParameterProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpQueryProgrammer.js +15 -11
- package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -1
- package/package.json +1 -1
- package/src/factories/MetadataCollection.ts +274 -274
- package/src/functional/$HeadersReader.ts +28 -9
- package/src/functional/$ParameterReader.ts +31 -0
- package/src/functional/$QueryReader.ts +53 -32
- package/src/functional/Namespace.ts +2 -0
- package/src/http.ts +1149 -1141
- package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
- package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -77
- package/src/programmers/http/HttpAssertQueryProgrammer.ts +77 -77
- package/src/programmers/http/HttpHeadersProgrammer.ts +328 -321
- package/src/programmers/http/HttpIsHeadersProgrammer.ts +87 -87
- package/src/programmers/http/HttpIsQueryProgrammer.ts +87 -87
- package/src/programmers/http/HttpParameterProgrammer.ts +104 -168
- package/src/programmers/http/HttpQueryProgrammer.ts +286 -267
- package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -77
- package/src/programmers/http/HttpValidateQueryProgrammer.ts +77 -77
- package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +12 -12
- package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +12 -12
- package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -9
- package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -9
- package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -9
- package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -9
- package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -9
- package/src/transformers/features/http/CreateHttpValidateHeadersTransformer copy.ts +12 -12
- package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +12 -12
- package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +10 -10
- package/src/transformers/features/http/HttpAssertQueryTransformer.ts +10 -10
- package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -9
- package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -9
- package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -9
- package/src/transformers/features/http/HttpParameterTransformer.ts +9 -9
- package/src/transformers/features/http/HttpQueryTransformer.ts +9 -9
- package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -10
- package/src/transformers/features/http/HttpValidateQueryTransformer.ts +10 -10
|
@@ -1,267 +1,286 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
4
|
-
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
5
|
-
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
6
|
-
import { StatementFactory } from "../../factories/StatementFactory";
|
|
7
|
-
import { TypeFactory } from "../../factories/TypeFactory";
|
|
8
|
-
|
|
9
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
10
|
-
import {
|
|
11
|
-
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
12
|
-
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
13
|
-
|
|
14
|
-
import { IProject } from "../../transformers/IProject";
|
|
15
|
-
import { TransformerError } from "../../transformers/TransformerError";
|
|
16
|
-
|
|
17
|
-
import { Atomic } from "../../typings/Atomic";
|
|
18
|
-
|
|
19
|
-
import { Escaper } from "../../utils/Escaper";
|
|
20
|
-
|
|
21
|
-
import { FunctionImporter } from "../helpers/FunctionImporeter";
|
|
22
|
-
import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
|
|
23
|
-
|
|
24
|
-
export namespace HttpQueryProgrammer {
|
|
25
|
-
export const INPUT_TYPE = "string | URLSearchParams";
|
|
26
|
-
|
|
27
|
-
export const write =
|
|
28
|
-
(project: IProject) =>
|
|
29
|
-
(modulo: ts.LeftHandSideExpression) =>
|
|
30
|
-
(type: ts.Type, name?: string): ts.ArrowFunction => {
|
|
31
|
-
// GET OBJECT TYPE
|
|
32
|
-
const importer: FunctionImporter = new FunctionImporter(
|
|
33
|
-
modulo.getText(),
|
|
34
|
-
);
|
|
35
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
36
|
-
const result = MetadataFactory.analyze(project.checker)({
|
|
37
|
-
escape: false,
|
|
38
|
-
constant: true,
|
|
39
|
-
absorb: true,
|
|
40
|
-
validate,
|
|
41
|
-
})(collection)(type);
|
|
42
|
-
if (result.success === false)
|
|
43
|
-
throw TransformerError.from(`typia.http.${importer.method}`)(
|
|
44
|
-
result.errors,
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
// DO TRANSFORM
|
|
48
|
-
const object: MetadataObject = result.data.objects[0]!;
|
|
49
|
-
const statements: ts.Statement[] = decode_object(importer)(object);
|
|
50
|
-
return ts.factory.createArrowFunction(
|
|
51
|
-
undefined,
|
|
52
|
-
undefined,
|
|
53
|
-
[
|
|
54
|
-
IdentifierFactory.parameter(
|
|
55
|
-
"input",
|
|
56
|
-
ts.factory.createTypeReferenceNode(INPUT_TYPE),
|
|
57
|
-
),
|
|
58
|
-
],
|
|
59
|
-
ts.factory.createTypeReferenceNode(
|
|
60
|
-
`typia.Resolved<${
|
|
61
|
-
name ?? TypeFactory.getFullName(project.checker)(type)
|
|
62
|
-
}>`,
|
|
63
|
-
),
|
|
64
|
-
undefined,
|
|
65
|
-
ts.factory.createBlock(
|
|
66
|
-
[...importer.declare(modulo), ...statements],
|
|
67
|
-
true,
|
|
68
|
-
),
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const validate = (
|
|
73
|
-
meta: Metadata,
|
|
74
|
-
explore: MetadataFactory.IExplore,
|
|
75
|
-
): string[] => {
|
|
76
|
-
const errors: string[] = [];
|
|
77
|
-
const insert = (msg: string) => errors.push(msg);
|
|
78
|
-
|
|
79
|
-
if (explore.top === true) {
|
|
80
|
-
// TOP MUST BE ONLY OBJECT
|
|
81
|
-
if (meta.objects.length !== 1 || meta.bucket() !== 1)
|
|
82
|
-
insert("only one object type is allowed.");
|
|
83
|
-
if (meta.nullable === true)
|
|
84
|
-
insert("query parameters cannot be null.");
|
|
85
|
-
if (meta.isRequired() === false)
|
|
86
|
-
insert("query parameters cannot be undefined.");
|
|
87
|
-
} else if (
|
|
88
|
-
explore.nested !== null &&
|
|
89
|
-
explore.nested instanceof
|
|
90
|
-
) {
|
|
91
|
-
const atomics = HttpMetadataUtil.atomics(meta);
|
|
92
|
-
const expected: number =
|
|
93
|
-
meta.atomics.length +
|
|
94
|
-
meta.templates.length +
|
|
95
|
-
meta.constants
|
|
96
|
-
.map((c) => c.values.length)
|
|
97
|
-
.reduce((a, b) => a + b, 0);
|
|
98
|
-
if (atomics.size > 1) insert("union type is not allowed in array.");
|
|
99
|
-
if (meta.nullable) insert("nullable type is not allowed in array.");
|
|
100
|
-
if (meta.isRequired() === false)
|
|
101
|
-
insert("optional type is not allowed in array.");
|
|
102
|
-
if (meta.size() !== expected)
|
|
103
|
-
insert("only atomic or constant types are allowed in array.");
|
|
104
|
-
} else if (explore.object && explore.property !== null) {
|
|
105
|
-
//----
|
|
106
|
-
// COMMON
|
|
107
|
-
//----
|
|
108
|
-
// PROPERTY MUST BE SOLE
|
|
109
|
-
if (typeof explore.property === "object")
|
|
110
|
-
insert("dynamic property is not allowed.");
|
|
111
|
-
// DO NOT ALLOW TUPLE TYPE
|
|
112
|
-
if (meta.tuples.length) insert("tuple type is not allowed.");
|
|
113
|
-
// DO NOT ALLOW UNION TYPE
|
|
114
|
-
if (HttpMetadataUtil.isUnion(meta))
|
|
115
|
-
insert("union type is not allowed.");
|
|
116
|
-
// DO NOT ALLOW NESTED OBJECT
|
|
117
|
-
if (
|
|
118
|
-
meta.objects.length ||
|
|
119
|
-
meta.sets.length ||
|
|
120
|
-
meta.maps.length ||
|
|
121
|
-
meta.natives.length
|
|
122
|
-
)
|
|
123
|
-
insert("nested object type is not allowed.");
|
|
124
|
-
|
|
125
|
-
//----
|
|
126
|
-
// ARRAY CASES
|
|
127
|
-
//----
|
|
128
|
-
const isArray: boolean =
|
|
129
|
-
meta.arrays.length > 1 || meta.tuples.length > 1;
|
|
130
|
-
// ARRAY TYPE MUST BE REQUIRED
|
|
131
|
-
if (isArray && meta.isRequired() === false)
|
|
132
|
-
insert("optional type is not allowed when array.");
|
|
133
|
-
// SET-COOKIE MUST BE ARRAY
|
|
134
|
-
if (explore.property === "set-cookie" && !isArray)
|
|
135
|
-
insert("set-cookie property must be array.");
|
|
136
|
-
}
|
|
137
|
-
return errors;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const decode_object =
|
|
141
|
-
(importer: FunctionImporter) =>
|
|
142
|
-
(object: MetadataObject): ts.Statement[] => {
|
|
143
|
-
const input: ts.Identifier = ts.factory.createIdentifier("input");
|
|
144
|
-
const output: ts.Identifier = ts.factory.createIdentifier("output");
|
|
145
|
-
const optionals: string[] = [];
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
),
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
[
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
),
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
4
|
+
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
5
|
+
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
6
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
7
|
+
import { TypeFactory } from "../../factories/TypeFactory";
|
|
8
|
+
|
|
9
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
10
|
+
import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
|
|
11
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
12
|
+
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
13
|
+
|
|
14
|
+
import { IProject } from "../../transformers/IProject";
|
|
15
|
+
import { TransformerError } from "../../transformers/TransformerError";
|
|
16
|
+
|
|
17
|
+
import { Atomic } from "../../typings/Atomic";
|
|
18
|
+
|
|
19
|
+
import { Escaper } from "../../utils/Escaper";
|
|
20
|
+
|
|
21
|
+
import { FunctionImporter } from "../helpers/FunctionImporeter";
|
|
22
|
+
import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
|
|
23
|
+
|
|
24
|
+
export namespace HttpQueryProgrammer {
|
|
25
|
+
export const INPUT_TYPE = "string | URLSearchParams";
|
|
26
|
+
|
|
27
|
+
export const write =
|
|
28
|
+
(project: IProject) =>
|
|
29
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
30
|
+
(type: ts.Type, name?: string): ts.ArrowFunction => {
|
|
31
|
+
// GET OBJECT TYPE
|
|
32
|
+
const importer: FunctionImporter = new FunctionImporter(
|
|
33
|
+
modulo.getText(),
|
|
34
|
+
);
|
|
35
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
36
|
+
const result = MetadataFactory.analyze(project.checker)({
|
|
37
|
+
escape: false,
|
|
38
|
+
constant: true,
|
|
39
|
+
absorb: true,
|
|
40
|
+
validate,
|
|
41
|
+
})(collection)(type);
|
|
42
|
+
if (result.success === false)
|
|
43
|
+
throw TransformerError.from(`typia.http.${importer.method}`)(
|
|
44
|
+
result.errors,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// DO TRANSFORM
|
|
48
|
+
const object: MetadataObject = result.data.objects[0]!;
|
|
49
|
+
const statements: ts.Statement[] = decode_object(importer)(object);
|
|
50
|
+
return ts.factory.createArrowFunction(
|
|
51
|
+
undefined,
|
|
52
|
+
undefined,
|
|
53
|
+
[
|
|
54
|
+
IdentifierFactory.parameter(
|
|
55
|
+
"input",
|
|
56
|
+
ts.factory.createTypeReferenceNode(INPUT_TYPE),
|
|
57
|
+
),
|
|
58
|
+
],
|
|
59
|
+
ts.factory.createTypeReferenceNode(
|
|
60
|
+
`typia.Resolved<${
|
|
61
|
+
name ?? TypeFactory.getFullName(project.checker)(type)
|
|
62
|
+
}>`,
|
|
63
|
+
),
|
|
64
|
+
undefined,
|
|
65
|
+
ts.factory.createBlock(
|
|
66
|
+
[...importer.declare(modulo), ...statements],
|
|
67
|
+
true,
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const validate = (
|
|
73
|
+
meta: Metadata,
|
|
74
|
+
explore: MetadataFactory.IExplore,
|
|
75
|
+
): string[] => {
|
|
76
|
+
const errors: string[] = [];
|
|
77
|
+
const insert = (msg: string) => errors.push(msg);
|
|
78
|
+
|
|
79
|
+
if (explore.top === true) {
|
|
80
|
+
// TOP MUST BE ONLY OBJECT
|
|
81
|
+
if (meta.objects.length !== 1 || meta.bucket() !== 1)
|
|
82
|
+
insert("only one object type is allowed.");
|
|
83
|
+
if (meta.nullable === true)
|
|
84
|
+
insert("query parameters cannot be null.");
|
|
85
|
+
if (meta.isRequired() === false)
|
|
86
|
+
insert("query parameters cannot be undefined.");
|
|
87
|
+
} else if (
|
|
88
|
+
explore.nested !== null &&
|
|
89
|
+
explore.nested instanceof MetadataArrayType
|
|
90
|
+
) {
|
|
91
|
+
const atomics = HttpMetadataUtil.atomics(meta);
|
|
92
|
+
const expected: number =
|
|
93
|
+
meta.atomics.length +
|
|
94
|
+
meta.templates.length +
|
|
95
|
+
meta.constants
|
|
96
|
+
.map((c) => c.values.length)
|
|
97
|
+
.reduce((a, b) => a + b, 0);
|
|
98
|
+
if (atomics.size > 1) insert("union type is not allowed in array.");
|
|
99
|
+
if (meta.nullable) insert("nullable type is not allowed in array.");
|
|
100
|
+
if (meta.isRequired() === false)
|
|
101
|
+
insert("optional type is not allowed in array.");
|
|
102
|
+
if (meta.size() !== expected)
|
|
103
|
+
insert("only atomic or constant types are allowed in array.");
|
|
104
|
+
} else if (explore.object && explore.property !== null) {
|
|
105
|
+
//----
|
|
106
|
+
// COMMON
|
|
107
|
+
//----
|
|
108
|
+
// PROPERTY MUST BE SOLE
|
|
109
|
+
if (typeof explore.property === "object")
|
|
110
|
+
insert("dynamic property is not allowed.");
|
|
111
|
+
// DO NOT ALLOW TUPLE TYPE
|
|
112
|
+
if (meta.tuples.length) insert("tuple type is not allowed.");
|
|
113
|
+
// DO NOT ALLOW UNION TYPE
|
|
114
|
+
if (HttpMetadataUtil.isUnion(meta))
|
|
115
|
+
insert("union type is not allowed.");
|
|
116
|
+
// DO NOT ALLOW NESTED OBJECT
|
|
117
|
+
if (
|
|
118
|
+
meta.objects.length ||
|
|
119
|
+
meta.sets.length ||
|
|
120
|
+
meta.maps.length ||
|
|
121
|
+
meta.natives.length
|
|
122
|
+
)
|
|
123
|
+
insert("nested object type is not allowed.");
|
|
124
|
+
|
|
125
|
+
//----
|
|
126
|
+
// ARRAY CASES
|
|
127
|
+
//----
|
|
128
|
+
const isArray: boolean =
|
|
129
|
+
meta.arrays.length > 1 || meta.tuples.length > 1;
|
|
130
|
+
// ARRAY TYPE MUST BE REQUIRED
|
|
131
|
+
if (isArray && meta.isRequired() === false)
|
|
132
|
+
insert("optional type is not allowed when array.");
|
|
133
|
+
// SET-COOKIE MUST BE ARRAY
|
|
134
|
+
if (explore.property === "set-cookie" && !isArray)
|
|
135
|
+
insert("set-cookie property must be array.");
|
|
136
|
+
}
|
|
137
|
+
return errors;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const decode_object =
|
|
141
|
+
(importer: FunctionImporter) =>
|
|
142
|
+
(object: MetadataObject): ts.Statement[] => {
|
|
143
|
+
const input: ts.Identifier = ts.factory.createIdentifier("input");
|
|
144
|
+
const output: ts.Identifier = ts.factory.createIdentifier("output");
|
|
145
|
+
const optionals: string[] = [];
|
|
146
|
+
|
|
147
|
+
return [
|
|
148
|
+
ts.factory.createExpressionStatement(
|
|
149
|
+
ts.factory.createBinaryExpression(
|
|
150
|
+
input,
|
|
151
|
+
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
152
|
+
ts.factory.createAsExpression(
|
|
153
|
+
ts.factory.createCallExpression(
|
|
154
|
+
importer.use("params"),
|
|
155
|
+
undefined,
|
|
156
|
+
[input],
|
|
157
|
+
),
|
|
158
|
+
ts.factory.createTypeReferenceNode(
|
|
159
|
+
"URLSearchParams",
|
|
160
|
+
),
|
|
161
|
+
),
|
|
162
|
+
),
|
|
163
|
+
),
|
|
164
|
+
StatementFactory.constant(
|
|
165
|
+
"output",
|
|
166
|
+
ts.factory.createObjectLiteralExpression(
|
|
167
|
+
object.properties.map((prop) => {
|
|
168
|
+
if (
|
|
169
|
+
!prop.value.isRequired() &&
|
|
170
|
+
prop.value.arrays.length +
|
|
171
|
+
prop.value.tuples.length >
|
|
172
|
+
0
|
|
173
|
+
)
|
|
174
|
+
optionals.push(
|
|
175
|
+
prop.key.constants[0]!.values[0] as string,
|
|
176
|
+
);
|
|
177
|
+
return decode_regular_property(importer)(prop);
|
|
178
|
+
}),
|
|
179
|
+
true,
|
|
180
|
+
),
|
|
181
|
+
),
|
|
182
|
+
...optionals.map((key) => {
|
|
183
|
+
const access = IdentifierFactory.access(output)(key);
|
|
184
|
+
return ts.factory.createIfStatement(
|
|
185
|
+
ts.factory.createStrictEquality(
|
|
186
|
+
ts.factory.createNumericLiteral(0),
|
|
187
|
+
IdentifierFactory.access(access)("length"),
|
|
188
|
+
),
|
|
189
|
+
ts.factory.createExpressionStatement(
|
|
190
|
+
ts.factory.createDeleteExpression(access),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
}),
|
|
194
|
+
ts.factory.createReturnStatement(
|
|
195
|
+
ts.factory.createAsExpression(
|
|
196
|
+
output,
|
|
197
|
+
TypeFactory.keyword("any"),
|
|
198
|
+
),
|
|
199
|
+
),
|
|
200
|
+
];
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const decode_regular_property =
|
|
204
|
+
(importer: FunctionImporter) =>
|
|
205
|
+
(property: MetadataProperty): ts.PropertyAssignment => {
|
|
206
|
+
const key: string = property.key.constants[0]!.values[0] as string;
|
|
207
|
+
const value: Metadata = property.value;
|
|
208
|
+
|
|
209
|
+
const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
|
|
210
|
+
.length
|
|
211
|
+
? [value.atomics[0]!.type, false]
|
|
212
|
+
: value.constants.length
|
|
213
|
+
? [value.constants[0]!.type, false]
|
|
214
|
+
: value.templates.length
|
|
215
|
+
? ["string", false]
|
|
216
|
+
: (() => {
|
|
217
|
+
const meta =
|
|
218
|
+
value.arrays[0]?.type.value ??
|
|
219
|
+
value.tuples[0]!.type.elements[0]!;
|
|
220
|
+
return meta.atomics.length
|
|
221
|
+
? [meta.atomics[0]!.type, true]
|
|
222
|
+
: meta.templates.length
|
|
223
|
+
? ["string", true]
|
|
224
|
+
: [meta.constants[0]!.type, true];
|
|
225
|
+
})();
|
|
226
|
+
return ts.factory.createPropertyAssignment(
|
|
227
|
+
Escaper.variable(key)
|
|
228
|
+
? key
|
|
229
|
+
: ts.factory.createStringLiteral(key),
|
|
230
|
+
isArray
|
|
231
|
+
? ts.factory.createCallExpression(
|
|
232
|
+
IdentifierFactory.access(
|
|
233
|
+
ts.factory.createCallExpression(
|
|
234
|
+
ts.factory.createIdentifier("input.getAll"),
|
|
235
|
+
undefined,
|
|
236
|
+
[ts.factory.createStringLiteral(key)],
|
|
237
|
+
),
|
|
238
|
+
)("map"),
|
|
239
|
+
undefined,
|
|
240
|
+
[
|
|
241
|
+
ts.factory.createArrowFunction(
|
|
242
|
+
undefined,
|
|
243
|
+
undefined,
|
|
244
|
+
[IdentifierFactory.parameter("elem")],
|
|
245
|
+
undefined,
|
|
246
|
+
undefined,
|
|
247
|
+
decode_value(importer)(type)(false)(
|
|
248
|
+
ts.factory.createIdentifier("elem"),
|
|
249
|
+
),
|
|
250
|
+
),
|
|
251
|
+
],
|
|
252
|
+
)
|
|
253
|
+
: decode_value(importer)(type)(
|
|
254
|
+
value.nullable === false &&
|
|
255
|
+
value.isRequired() === false,
|
|
256
|
+
)(
|
|
257
|
+
ts.factory.createCallExpression(
|
|
258
|
+
ts.factory.createIdentifier("input.get"),
|
|
259
|
+
undefined,
|
|
260
|
+
[ts.factory.createStringLiteral(key)],
|
|
261
|
+
),
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const decode_value =
|
|
267
|
+
(importer: FunctionImporter) =>
|
|
268
|
+
(type: Atomic.Literal) =>
|
|
269
|
+
(onlyUndefindable: boolean) =>
|
|
270
|
+
(value: ts.Expression) => {
|
|
271
|
+
const call = ts.factory.createCallExpression(
|
|
272
|
+
importer.use(type),
|
|
273
|
+
undefined,
|
|
274
|
+
[value],
|
|
275
|
+
);
|
|
276
|
+
return onlyUndefindable
|
|
277
|
+
? ts.factory.createBinaryExpression(
|
|
278
|
+
call,
|
|
279
|
+
ts.factory.createToken(
|
|
280
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
281
|
+
),
|
|
282
|
+
ts.factory.createIdentifier("undefined"),
|
|
283
|
+
)
|
|
284
|
+
: call;
|
|
285
|
+
};
|
|
286
|
+
}
|