typia 6.7.0 → 6.7.1

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 (85) hide show
  1. package/lib/programmers/AssertProgrammer.d.ts +0 -1
  2. package/lib/programmers/AssertProgrammer.js.map +1 -1
  3. package/lib/programmers/CheckerProgrammer.d.ts +1 -1
  4. package/lib/programmers/FeatureProgrammer.d.ts +1 -1
  5. package/lib/programmers/IsProgrammer.d.ts +1 -1
  6. package/lib/programmers/RandomProgrammer.d.ts +10 -1
  7. package/lib/programmers/RandomProgrammer.js +62 -37
  8. package/lib/programmers/RandomProgrammer.js.map +1 -1
  9. package/lib/programmers/ValidateProgrammer.d.ts +1 -1
  10. package/lib/programmers/json/JsonIsParseProgrammer.d.ts +1 -1
  11. package/lib/programmers/json/JsonIsStringifyProgrammer.d.ts +1 -1
  12. package/lib/programmers/json/JsonStringifyProgrammer.d.ts +1 -1
  13. package/lib/programmers/json/JsonStringifyProgrammer.js +1 -3
  14. package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
  15. package/lib/programmers/json/JsonValidateParseProgrammer.d.ts +1 -1
  16. package/lib/programmers/json/JsonValidateStringifyProgrammer.d.ts +1 -1
  17. package/lib/programmers/misc/MiscCloneProgrammer.d.ts +1 -1
  18. package/lib/programmers/misc/MiscCloneProgrammer.js +33 -14
  19. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  20. package/lib/programmers/misc/MiscIsCloneProgrammer.d.ts +1 -1
  21. package/lib/programmers/misc/MiscIsPruneProgrammer.d.ts +1 -1
  22. package/lib/programmers/misc/MiscPruneProgrammer.d.ts +1 -1
  23. package/lib/programmers/misc/MiscValidateCloneProgrammer.d.ts +1 -1
  24. package/lib/programmers/misc/MiscValidatePruneProgrammer.d.ts +1 -1
  25. package/lib/programmers/notations/NotationGeneralProgrammer.d.ts +1 -1
  26. package/lib/programmers/notations/NotationGeneralProgrammer.js +30 -11
  27. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  28. package/lib/programmers/notations/NotationValidateGeneralProgrammer.d.ts +1 -1
  29. package/package.json +1 -1
  30. package/src/programmers/AssertProgrammer.ts +397 -398
  31. package/src/programmers/CheckerProgrammer.ts +1138 -1138
  32. package/src/programmers/FeatureProgrammer.ts +549 -549
  33. package/src/programmers/IsProgrammer.ts +1 -1
  34. package/src/programmers/RandomProgrammer.ts +112 -77
  35. package/src/programmers/ValidateProgrammer.ts +382 -382
  36. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +141 -141
  37. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +108 -108
  38. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +98 -98
  39. package/src/programmers/functional/FunctionalIsFunctionProgrammer.ts +72 -72
  40. package/src/programmers/functional/FunctionalIsParametersProgrammer.ts +101 -101
  41. package/src/programmers/functional/FunctionalIsReturnProgrammer.ts +106 -106
  42. package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +123 -123
  43. package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +267 -267
  44. package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +126 -126
  45. package/src/programmers/helpers/FunctionImporter.ts +97 -97
  46. package/src/programmers/http/HttpAssertFormDataProgrammer.ts +91 -91
  47. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +91 -91
  48. package/src/programmers/http/HttpAssertQueryProgrammer.ts +93 -93
  49. package/src/programmers/http/HttpFormDataProgrammer.ts +278 -278
  50. package/src/programmers/http/HttpHeadersProgrammer.ts +347 -347
  51. package/src/programmers/http/HttpIsFormDataProgrammer.ts +102 -102
  52. package/src/programmers/http/HttpIsHeadersProgrammer.ts +102 -102
  53. package/src/programmers/http/HttpIsQueryProgrammer.ts +104 -104
  54. package/src/programmers/http/HttpQueryProgrammer.ts +298 -298
  55. package/src/programmers/http/HttpValidateFormDataProgrammer.ts +85 -85
  56. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +85 -85
  57. package/src/programmers/http/HttpValidateQueryProgrammer.ts +87 -87
  58. package/src/programmers/json/JsonAssertParseProgrammer.ts +96 -96
  59. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +104 -104
  60. package/src/programmers/json/JsonIsParseProgrammer.ts +110 -110
  61. package/src/programmers/json/JsonIsStringifyProgrammer.ts +102 -102
  62. package/src/programmers/json/JsonStringifyProgrammer.ts +909 -910
  63. package/src/programmers/json/JsonValidateParseProgrammer.ts +87 -87
  64. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +111 -111
  65. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +87 -87
  66. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +87 -87
  67. package/src/programmers/misc/MiscCloneProgrammer.ts +781 -759
  68. package/src/programmers/misc/MiscIsCloneProgrammer.ts +93 -93
  69. package/src/programmers/misc/MiscIsPruneProgrammer.ts +94 -94
  70. package/src/programmers/misc/MiscPruneProgrammer.ts +560 -560
  71. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +104 -104
  72. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +98 -98
  73. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +91 -91
  74. package/src/programmers/notations/NotationGeneralProgrammer.ts +709 -685
  75. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +97 -97
  76. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +107 -107
  77. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +91 -91
  78. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +95 -95
  79. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +646 -646
  80. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +852 -852
  81. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +104 -104
  82. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +93 -93
  83. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +85 -85
  84. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +109 -109
  85. package/src/transformers/internal/GenericTransformer.ts +104 -104
@@ -1,347 +1,347 @@
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 { MetadataFactory } from "../../factories/MetadataFactory";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
12
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
13
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { Atomic } from "../../typings/Atomic";
19
-
20
- import { Escaper } from "../../utils/Escaper";
21
- import { MapUtil } from "../../utils/MapUtil";
22
-
23
- import { FeatureProgrammer } from "../FeatureProgrammer";
24
- import { FunctionImporter } from "../helpers/FunctionImporter";
25
- import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
26
-
27
- export namespace HttpHeadersProgrammer {
28
- export const INPUT_TYPE = "Record<string, string | string[] | undefined>";
29
-
30
- export const decompose = (props: {
31
- project: IProject;
32
- importer: FunctionImporter;
33
- type: ts.Type;
34
- name: string | undefined;
35
- }): FeatureProgrammer.IDecomposed => {
36
- // ANALYZE TYPE
37
- const collection: MetadataCollection = new MetadataCollection();
38
- const result = MetadataFactory.analyze(
39
- props.project.checker,
40
- props.project.context,
41
- )({
42
- escape: false,
43
- constant: true,
44
- absorb: true,
45
- validate,
46
- })(collection)(props.type);
47
- if (result.success === false)
48
- throw TransformerError.from(`typia.http.${props.importer.method}`)(
49
- result.errors,
50
- );
51
-
52
- // DO TRANSFORM
53
- const object: MetadataObject = result.data.objects[0]!;
54
- const statements: ts.Statement[] = decode_object(props.importer)(object);
55
- return {
56
- functions: {},
57
- statements: [],
58
- arrow: ts.factory.createArrowFunction(
59
- undefined,
60
- undefined,
61
- [
62
- IdentifierFactory.parameter(
63
- "input",
64
- ts.factory.createTypeReferenceNode(INPUT_TYPE),
65
- ),
66
- ],
67
- ts.factory.createImportTypeNode(
68
- ts.factory.createLiteralTypeNode(
69
- ts.factory.createStringLiteral("typia"),
70
- ),
71
- undefined,
72
- ts.factory.createIdentifier("Resolved"),
73
- [
74
- ts.factory.createTypeReferenceNode(
75
- props.name ??
76
- TypeFactory.getFullName(props.project.checker)(props.type),
77
- ),
78
- ],
79
- false,
80
- ),
81
- undefined,
82
- ts.factory.createBlock(statements, true),
83
- ),
84
- };
85
- };
86
-
87
- export const write =
88
- (project: IProject) =>
89
- (modulo: ts.LeftHandSideExpression) =>
90
- (type: ts.Type, name?: string): ts.CallExpression => {
91
- const importer: FunctionImporter = new FunctionImporter(modulo.getText());
92
- const result: FeatureProgrammer.IDecomposed = decompose({
93
- project,
94
- importer,
95
- type,
96
- name,
97
- });
98
- return FeatureProgrammer.writeDecomposed({
99
- modulo,
100
- importer,
101
- result,
102
- });
103
- };
104
-
105
- export const validate = (
106
- meta: Metadata,
107
- explore: MetadataFactory.IExplore,
108
- ): string[] => {
109
- const errors: string[] = [];
110
- const insert = (msg: string) => errors.push(msg);
111
-
112
- if (explore.top === true) {
113
- // TOP MUST BE ONLY OBJECT
114
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
115
- insert("only one object type is allowed.");
116
- if (meta.nullable === true) insert("headers cannot be null.");
117
- if (meta.isRequired() === false) insert("headers cannot be null.");
118
- } else if (
119
- explore.nested !== null &&
120
- explore.nested instanceof MetadataArrayType
121
- ) {
122
- //----
123
- // ARRAY
124
- //----
125
- const atomics = HttpMetadataUtil.atomics(meta);
126
- const expected: number =
127
- meta.atomics.length +
128
- meta.templates.length +
129
- meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
130
- if (atomics.size > 1) insert("union type is not allowed in array.");
131
- if (meta.size() !== expected)
132
- insert("only atomic or constant types are allowed in array.");
133
- if (meta.nullable === true)
134
- insert("nullable type is not allowed in array.");
135
- if (meta.isRequired() === false)
136
- insert("optional type is not allowed in array.");
137
- } else if (explore.object && explore.property !== null) {
138
- //----
139
- // COMMON
140
- //----
141
- // PROPERTY MUST BE SOLE
142
- if (typeof explore.property === "object")
143
- insert("dynamic property is not allowed.");
144
- // DO NOT ALLOW TUPLE TYPE
145
- if (meta.tuples.length) insert("tuple type is not allowed.");
146
- // DO NOT ALLOW UNION TYPE
147
- if (HttpMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
148
- // DO NOT ALLOW NESTED OBJECT
149
- if (
150
- meta.objects.length ||
151
- meta.sets.length ||
152
- meta.maps.length ||
153
- meta.natives.length
154
- )
155
- insert("nested object type is not allowed.");
156
- // DO NOT ALLOW NULLABLE
157
- if (meta.nullable === true) insert("nullable type is not allowed.");
158
-
159
- //----
160
- // SPECIAL KEY NAMES
161
- //----
162
- const isArray: boolean =
163
- meta.arrays.length >= 1 || meta.tuples.length >= 1;
164
- // SET-COOKIE MUST BE ARRAY
165
- if (
166
- typeof explore.property === "string" &&
167
- explore.property.toLowerCase() === "set-cookie" &&
168
- isArray === false
169
- )
170
- insert(`${explore.property} property must be array.`);
171
- // MUST BE SINGULAR CASE
172
- if (
173
- typeof explore.property === "string" &&
174
- SINGULAR.has(explore.property.toLowerCase()) &&
175
- isArray === true
176
- )
177
- insert("property cannot be array.");
178
- } else if (explore.object && explore.property === null) {
179
- const counter: Map<string, Set<string>> = new Map();
180
- for (const prop of explore.object.properties) {
181
- const key: string | null = prop.key.getSoleLiteral();
182
- if (key === null) continue;
183
-
184
- MapUtil.take(counter)(key.toLowerCase(), () => new Set()).add(key);
185
- }
186
- for (const [key, set] of counter)
187
- if (set.size > 1)
188
- insert(
189
- `duplicated keys when converting to lowercase letters: [${[
190
- ...set,
191
- ].join(", ")}] -> ${key}`,
192
- );
193
- }
194
- return errors;
195
- };
196
-
197
- const decode_object =
198
- (importer: FunctionImporter) =>
199
- (object: MetadataObject): ts.Statement[] => {
200
- const output: ts.Identifier = ts.factory.createIdentifier("output");
201
- const optionals: string[] = [];
202
- return [
203
- StatementFactory.constant(
204
- "output",
205
- ts.factory.createObjectLiteralExpression(
206
- object.properties.map((prop) => {
207
- if (
208
- !prop.value.isRequired() &&
209
- prop.value.arrays.length + prop.value.tuples.length > 0
210
- )
211
- optionals.push(
212
- prop.key.constants[0]!.values[0]!.value as string,
213
- );
214
- return decode_regular_property(importer)(prop);
215
- }),
216
- true,
217
- ),
218
- ),
219
- ...optionals.map((key) => {
220
- const access = IdentifierFactory.access(output)(key);
221
- return ts.factory.createIfStatement(
222
- ts.factory.createStrictEquality(
223
- ExpressionFactory.number(0),
224
- IdentifierFactory.access(access)("length"),
225
- ),
226
- ts.factory.createExpressionStatement(
227
- ts.factory.createDeleteExpression(access),
228
- ),
229
- );
230
- }),
231
- ts.factory.createReturnStatement(
232
- ts.factory.createAsExpression(output, TypeFactory.keyword("any")),
233
- ),
234
- ];
235
- };
236
-
237
- const decode_regular_property =
238
- (importer: FunctionImporter) =>
239
- (property: MetadataProperty): ts.PropertyAssignment => {
240
- const key: string = property.key.constants[0]!.values[0]!.value as string;
241
- const value: Metadata = property.value;
242
-
243
- const [type, isArray]: [Atomic.Literal, boolean] = value.atomics.length
244
- ? [value.atomics[0]!.type, false]
245
- : value.constants.length
246
- ? [value.constants[0]!.type, false]
247
- : value.templates.length
248
- ? ["string", false]
249
- : (() => {
250
- const meta: Metadata =
251
- value.arrays[0]?.type.value ??
252
- value.tuples[0]!.type.elements[0]!;
253
- return meta.atomics.length
254
- ? [meta.atomics[0]!.type, true]
255
- : meta.templates.length
256
- ? ["string", true]
257
- : [meta.constants[0]!.type, true];
258
- })();
259
- const accessor = IdentifierFactory.access(
260
- ts.factory.createIdentifier("input"),
261
- )(key.toLowerCase());
262
-
263
- return ts.factory.createPropertyAssignment(
264
- Escaper.variable(key) ? key : ts.factory.createStringLiteral(key),
265
- isArray
266
- ? key === "set-cookie"
267
- ? accessor
268
- : decode_array(importer)(type)(key)(value)(accessor)
269
- : decode_value(importer)(type)(accessor),
270
- );
271
- };
272
-
273
- const decode_value =
274
- (importer: FunctionImporter) =>
275
- (type: Atomic.Literal) =>
276
- (value: ts.Expression) =>
277
- type === "string"
278
- ? value
279
- : ts.factory.createCallExpression(importer.use(type), undefined, [
280
- value,
281
- ]);
282
-
283
- const decode_array =
284
- (importer: FunctionImporter) =>
285
- (type: Atomic.Literal) =>
286
- (key: string) =>
287
- (value: Metadata) =>
288
- (accessor: ts.Expression) => {
289
- const split: ts.CallChain = ts.factory.createCallChain(
290
- ts.factory.createPropertyAccessChain(
291
- ts.factory.createCallChain(
292
- ts.factory.createPropertyAccessChain(
293
- accessor,
294
- ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
295
- ts.factory.createIdentifier("split"),
296
- ),
297
- undefined,
298
- undefined,
299
- [ts.factory.createStringLiteral(key === "cookie" ? "; " : ", ")],
300
- ),
301
- ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
302
- ts.factory.createIdentifier("map"),
303
- ),
304
- undefined,
305
- undefined,
306
- [importer.use(type)],
307
- );
308
- return ts.factory.createConditionalExpression(
309
- ExpressionFactory.isArray(accessor),
310
- undefined,
311
- ts.factory.createCallExpression(
312
- IdentifierFactory.access(accessor)("map"),
313
- undefined,
314
- [importer.use(type)],
315
- ),
316
- undefined,
317
- value.isRequired() === false
318
- ? split
319
- : ts.factory.createBinaryExpression(
320
- split,
321
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
322
- ts.factory.createArrayLiteralExpression([], false),
323
- ),
324
- );
325
- };
326
- }
327
-
328
- const SINGULAR: Set<string> = new Set([
329
- "age",
330
- "authorization",
331
- "content-length",
332
- "content-type",
333
- "etag",
334
- "expires",
335
- "from",
336
- "host",
337
- "if-modified-since",
338
- "if-unmodified-since",
339
- "last-modified",
340
- "location",
341
- "max-forwards",
342
- "proxy-authorization",
343
- "referer",
344
- "retry-after",
345
- "server",
346
- "user-agent",
347
- ]);
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 { MetadataFactory } from "../../factories/MetadataFactory";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
12
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
13
+ import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { Atomic } from "../../typings/Atomic";
19
+
20
+ import { Escaper } from "../../utils/Escaper";
21
+ import { MapUtil } from "../../utils/MapUtil";
22
+
23
+ import { FeatureProgrammer } from "../FeatureProgrammer";
24
+ import { FunctionImporter } from "../helpers/FunctionImporter";
25
+ import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
26
+
27
+ export namespace HttpHeadersProgrammer {
28
+ export const INPUT_TYPE = "Record<string, string | string[] | undefined>";
29
+
30
+ export const decompose = (props: {
31
+ project: IProject;
32
+ importer: FunctionImporter;
33
+ type: ts.Type;
34
+ name: string | undefined;
35
+ }): FeatureProgrammer.IDecomposed => {
36
+ // ANALYZE TYPE
37
+ const collection: MetadataCollection = new MetadataCollection();
38
+ const result = MetadataFactory.analyze(
39
+ props.project.checker,
40
+ props.project.context,
41
+ )({
42
+ escape: false,
43
+ constant: true,
44
+ absorb: true,
45
+ validate,
46
+ })(collection)(props.type);
47
+ if (result.success === false)
48
+ throw TransformerError.from(`typia.http.${props.importer.method}`)(
49
+ result.errors,
50
+ );
51
+
52
+ // DO TRANSFORM
53
+ const object: MetadataObject = result.data.objects[0]!;
54
+ const statements: ts.Statement[] = decode_object(props.importer)(object);
55
+ return {
56
+ functions: {},
57
+ statements: [],
58
+ arrow: ts.factory.createArrowFunction(
59
+ undefined,
60
+ undefined,
61
+ [
62
+ IdentifierFactory.parameter(
63
+ "input",
64
+ ts.factory.createTypeReferenceNode(INPUT_TYPE),
65
+ ),
66
+ ],
67
+ ts.factory.createImportTypeNode(
68
+ ts.factory.createLiteralTypeNode(
69
+ ts.factory.createStringLiteral("typia"),
70
+ ),
71
+ undefined,
72
+ ts.factory.createIdentifier("Resolved"),
73
+ [
74
+ ts.factory.createTypeReferenceNode(
75
+ props.name ??
76
+ TypeFactory.getFullName(props.project.checker)(props.type),
77
+ ),
78
+ ],
79
+ false,
80
+ ),
81
+ undefined,
82
+ ts.factory.createBlock(statements, true),
83
+ ),
84
+ };
85
+ };
86
+
87
+ export const write =
88
+ (project: IProject) =>
89
+ (modulo: ts.LeftHandSideExpression) =>
90
+ (type: ts.Type, name?: string): ts.CallExpression => {
91
+ const importer: FunctionImporter = new FunctionImporter(modulo.getText());
92
+ const result: FeatureProgrammer.IDecomposed = decompose({
93
+ project,
94
+ importer,
95
+ type,
96
+ name,
97
+ });
98
+ return FeatureProgrammer.writeDecomposed({
99
+ modulo,
100
+ importer,
101
+ result,
102
+ });
103
+ };
104
+
105
+ export const validate = (
106
+ meta: Metadata,
107
+ explore: MetadataFactory.IExplore,
108
+ ): string[] => {
109
+ const errors: string[] = [];
110
+ const insert = (msg: string) => errors.push(msg);
111
+
112
+ if (explore.top === true) {
113
+ // TOP MUST BE ONLY OBJECT
114
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
115
+ insert("only one object type is allowed.");
116
+ if (meta.nullable === true) insert("headers cannot be null.");
117
+ if (meta.isRequired() === false) insert("headers cannot be null.");
118
+ } else if (
119
+ explore.nested !== null &&
120
+ explore.nested instanceof MetadataArrayType
121
+ ) {
122
+ //----
123
+ // ARRAY
124
+ //----
125
+ const atomics = HttpMetadataUtil.atomics(meta);
126
+ const expected: number =
127
+ meta.atomics.length +
128
+ meta.templates.length +
129
+ meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
130
+ if (atomics.size > 1) insert("union type is not allowed in array.");
131
+ if (meta.size() !== expected)
132
+ insert("only atomic or constant types are allowed in array.");
133
+ if (meta.nullable === true)
134
+ insert("nullable type is not allowed in array.");
135
+ if (meta.isRequired() === false)
136
+ insert("optional type is not allowed in array.");
137
+ } else if (explore.object && explore.property !== null) {
138
+ //----
139
+ // COMMON
140
+ //----
141
+ // PROPERTY MUST BE SOLE
142
+ if (typeof explore.property === "object")
143
+ insert("dynamic property is not allowed.");
144
+ // DO NOT ALLOW TUPLE TYPE
145
+ if (meta.tuples.length) insert("tuple type is not allowed.");
146
+ // DO NOT ALLOW UNION TYPE
147
+ if (HttpMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
148
+ // DO NOT ALLOW NESTED OBJECT
149
+ if (
150
+ meta.objects.length ||
151
+ meta.sets.length ||
152
+ meta.maps.length ||
153
+ meta.natives.length
154
+ )
155
+ insert("nested object type is not allowed.");
156
+ // DO NOT ALLOW NULLABLE
157
+ if (meta.nullable === true) insert("nullable type is not allowed.");
158
+
159
+ //----
160
+ // SPECIAL KEY NAMES
161
+ //----
162
+ const isArray: boolean =
163
+ meta.arrays.length >= 1 || meta.tuples.length >= 1;
164
+ // SET-COOKIE MUST BE ARRAY
165
+ if (
166
+ typeof explore.property === "string" &&
167
+ explore.property.toLowerCase() === "set-cookie" &&
168
+ isArray === false
169
+ )
170
+ insert(`${explore.property} property must be array.`);
171
+ // MUST BE SINGULAR CASE
172
+ if (
173
+ typeof explore.property === "string" &&
174
+ SINGULAR.has(explore.property.toLowerCase()) &&
175
+ isArray === true
176
+ )
177
+ insert("property cannot be array.");
178
+ } else if (explore.object && explore.property === null) {
179
+ const counter: Map<string, Set<string>> = new Map();
180
+ for (const prop of explore.object.properties) {
181
+ const key: string | null = prop.key.getSoleLiteral();
182
+ if (key === null) continue;
183
+
184
+ MapUtil.take(counter)(key.toLowerCase(), () => new Set()).add(key);
185
+ }
186
+ for (const [key, set] of counter)
187
+ if (set.size > 1)
188
+ insert(
189
+ `duplicated keys when converting to lowercase letters: [${[
190
+ ...set,
191
+ ].join(", ")}] -> ${key}`,
192
+ );
193
+ }
194
+ return errors;
195
+ };
196
+
197
+ const decode_object =
198
+ (importer: FunctionImporter) =>
199
+ (object: MetadataObject): ts.Statement[] => {
200
+ const output: ts.Identifier = ts.factory.createIdentifier("output");
201
+ const optionals: string[] = [];
202
+ return [
203
+ StatementFactory.constant(
204
+ "output",
205
+ ts.factory.createObjectLiteralExpression(
206
+ object.properties.map((prop) => {
207
+ if (
208
+ !prop.value.isRequired() &&
209
+ prop.value.arrays.length + prop.value.tuples.length > 0
210
+ )
211
+ optionals.push(
212
+ prop.key.constants[0]!.values[0]!.value as string,
213
+ );
214
+ return decode_regular_property(importer)(prop);
215
+ }),
216
+ true,
217
+ ),
218
+ ),
219
+ ...optionals.map((key) => {
220
+ const access = IdentifierFactory.access(output)(key);
221
+ return ts.factory.createIfStatement(
222
+ ts.factory.createStrictEquality(
223
+ ExpressionFactory.number(0),
224
+ IdentifierFactory.access(access)("length"),
225
+ ),
226
+ ts.factory.createExpressionStatement(
227
+ ts.factory.createDeleteExpression(access),
228
+ ),
229
+ );
230
+ }),
231
+ ts.factory.createReturnStatement(
232
+ ts.factory.createAsExpression(output, TypeFactory.keyword("any")),
233
+ ),
234
+ ];
235
+ };
236
+
237
+ const decode_regular_property =
238
+ (importer: FunctionImporter) =>
239
+ (property: MetadataProperty): ts.PropertyAssignment => {
240
+ const key: string = property.key.constants[0]!.values[0]!.value as string;
241
+ const value: Metadata = property.value;
242
+
243
+ const [type, isArray]: [Atomic.Literal, boolean] = value.atomics.length
244
+ ? [value.atomics[0]!.type, false]
245
+ : value.constants.length
246
+ ? [value.constants[0]!.type, false]
247
+ : value.templates.length
248
+ ? ["string", false]
249
+ : (() => {
250
+ const meta: Metadata =
251
+ value.arrays[0]?.type.value ??
252
+ value.tuples[0]!.type.elements[0]!;
253
+ return meta.atomics.length
254
+ ? [meta.atomics[0]!.type, true]
255
+ : meta.templates.length
256
+ ? ["string", true]
257
+ : [meta.constants[0]!.type, true];
258
+ })();
259
+ const accessor = IdentifierFactory.access(
260
+ ts.factory.createIdentifier("input"),
261
+ )(key.toLowerCase());
262
+
263
+ return ts.factory.createPropertyAssignment(
264
+ Escaper.variable(key) ? key : ts.factory.createStringLiteral(key),
265
+ isArray
266
+ ? key === "set-cookie"
267
+ ? accessor
268
+ : decode_array(importer)(type)(key)(value)(accessor)
269
+ : decode_value(importer)(type)(accessor),
270
+ );
271
+ };
272
+
273
+ const decode_value =
274
+ (importer: FunctionImporter) =>
275
+ (type: Atomic.Literal) =>
276
+ (value: ts.Expression) =>
277
+ type === "string"
278
+ ? value
279
+ : ts.factory.createCallExpression(importer.use(type), undefined, [
280
+ value,
281
+ ]);
282
+
283
+ const decode_array =
284
+ (importer: FunctionImporter) =>
285
+ (type: Atomic.Literal) =>
286
+ (key: string) =>
287
+ (value: Metadata) =>
288
+ (accessor: ts.Expression) => {
289
+ const split: ts.CallChain = ts.factory.createCallChain(
290
+ ts.factory.createPropertyAccessChain(
291
+ ts.factory.createCallChain(
292
+ ts.factory.createPropertyAccessChain(
293
+ accessor,
294
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
295
+ ts.factory.createIdentifier("split"),
296
+ ),
297
+ undefined,
298
+ undefined,
299
+ [ts.factory.createStringLiteral(key === "cookie" ? "; " : ", ")],
300
+ ),
301
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
302
+ ts.factory.createIdentifier("map"),
303
+ ),
304
+ undefined,
305
+ undefined,
306
+ [importer.use(type)],
307
+ );
308
+ return ts.factory.createConditionalExpression(
309
+ ExpressionFactory.isArray(accessor),
310
+ undefined,
311
+ ts.factory.createCallExpression(
312
+ IdentifierFactory.access(accessor)("map"),
313
+ undefined,
314
+ [importer.use(type)],
315
+ ),
316
+ undefined,
317
+ value.isRequired() === false
318
+ ? split
319
+ : ts.factory.createBinaryExpression(
320
+ split,
321
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
322
+ ts.factory.createArrayLiteralExpression([], false),
323
+ ),
324
+ );
325
+ };
326
+ }
327
+
328
+ const SINGULAR: Set<string> = new Set([
329
+ "age",
330
+ "authorization",
331
+ "content-length",
332
+ "content-type",
333
+ "etag",
334
+ "expires",
335
+ "from",
336
+ "host",
337
+ "if-modified-since",
338
+ "if-unmodified-since",
339
+ "last-modified",
340
+ "location",
341
+ "max-forwards",
342
+ "proxy-authorization",
343
+ "referer",
344
+ "retry-after",
345
+ "server",
346
+ "user-agent",
347
+ ]);