typia 9.7.2 → 10.0.0-dev.20251107
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/LICENSE +21 -21
- package/README.md +153 -153
- package/lib/factories/ProtobufFactory.js +1 -1
- package/lib/factories/ProtobufFactory.mjs +1 -1
- package/lib/programmers/internal/json_schema_station.d.mts +2 -2
- package/lib/programmers/internal/json_schema_station.d.ts +2 -2
- package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
- package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
- package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
- package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
- package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
- package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
- package/package.json +121 -121
- package/src/AssertionGuard.ts +41 -41
- package/src/CamelCase.ts +75 -75
- package/src/IRandomGenerator.ts +337 -337
- package/src/IReadableURLSearchParams.ts +9 -9
- package/src/PascalCase.ts +71 -71
- package/src/Primitive.ts +90 -90
- package/src/Resolved.ts +72 -72
- package/src/SnakeCase.ts +127 -127
- package/src/TypeGuardError.ts +216 -216
- package/src/factories/MetadataCollection.ts +270 -270
- package/src/factories/MetadataCommentTagFactory.ts +632 -632
- package/src/factories/MetadataFactory.ts +402 -402
- package/src/factories/ProtobufFactory.ts +873 -873
- package/src/functional.ts +705 -705
- package/src/http.ts +972 -972
- package/src/internal/_ProtobufReader.ts +188 -188
- package/src/internal/_ProtobufSizer.ts +137 -137
- package/src/internal/_ProtobufWriter.ts +135 -135
- package/src/internal/_jsonStringifyString.ts +42 -42
- package/src/json.ts +643 -643
- package/src/llm.ts +615 -615
- package/src/misc.ts +594 -594
- package/src/module.ts +889 -889
- package/src/notations.ts +751 -751
- package/src/programmers/FeatureProgrammer.ts +605 -605
- package/src/programmers/ImportProgrammer.ts +179 -179
- package/src/programmers/RandomProgrammer.ts +1195 -1195
- package/src/programmers/helpers/ProtobufWire.ts +34 -34
- package/src/programmers/internal/check_array_length.ts +43 -43
- package/src/programmers/internal/check_bigint.ts +46 -46
- package/src/programmers/internal/check_dynamic_key.ts +197 -197
- package/src/programmers/internal/check_dynamic_properties.ts +231 -231
- package/src/programmers/internal/check_everything.ts +21 -21
- package/src/programmers/internal/check_native.ts +23 -23
- package/src/programmers/internal/check_number.ts +108 -108
- package/src/programmers/internal/check_object.ts +72 -72
- package/src/programmers/internal/check_string.ts +46 -46
- package/src/programmers/internal/check_template.ts +46 -46
- package/src/programmers/internal/check_union_array_like.ts +331 -331
- package/src/programmers/internal/decode_union_object.ts +110 -110
- package/src/programmers/internal/feature_object_entries.ts +59 -59
- package/src/programmers/internal/json_schema_escaped.ts +78 -78
- package/src/programmers/internal/json_schema_object.ts +150 -150
- package/src/programmers/internal/json_schema_station.ts +2 -2
- package/src/programmers/internal/metadata_to_pattern.ts +40 -40
- package/src/programmers/internal/postfix_of_tuple.ts +3 -3
- package/src/programmers/internal/prune_object_properties.ts +69 -69
- package/src/programmers/internal/stringify_dynamic_properties.ts +158 -158
- package/src/programmers/internal/stringify_native.ts +5 -5
- package/src/programmers/internal/stringify_regular_properties.ts +77 -77
- package/src/programmers/internal/template_to_pattern.ts +21 -21
- package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -21
- package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
- package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
- package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
- package/src/protobuf.ts +820 -820
- package/src/reflect.ts +46 -46
- package/src/schemas/json/IJsonApplication.ts +77 -77
- package/src/schemas/json/IJsonSchemaCollection.ts +212 -212
- package/src/schemas/json/IJsonSchemaUnit.ts +263 -263
- package/src/schemas/metadata/IMetadataTypeTag.ts +14 -14
- package/src/schemas/metadata/Metadata.ts +669 -669
- package/src/schemas/metadata/MetadataAliasType.ts +57 -57
- package/src/schemas/metadata/MetadataApplication.ts +40 -40
- package/src/schemas/metadata/MetadataArray.ts +47 -47
- package/src/schemas/metadata/MetadataArrayType.ts +51 -51
- package/src/schemas/metadata/MetadataAtomic.ts +85 -85
- package/src/schemas/metadata/MetadataEscaped.ts +45 -45
- package/src/schemas/metadata/MetadataFunction.ts +45 -45
- package/src/schemas/metadata/MetadataObject.ts +46 -46
- package/src/schemas/metadata/MetadataObjectType.ts +137 -137
- package/src/schemas/metadata/MetadataParameter.ts +52 -52
- package/src/schemas/metadata/MetadataProperty.ts +53 -53
- package/src/schemas/metadata/MetadataTemplate.ts +78 -78
- package/src/schemas/metadata/MetadataTuple.ts +28 -28
- package/src/schemas/metadata/MetadataTupleType.ts +61 -61
- package/src/tags/Constant.ts +47 -47
- package/src/tags/ContentMediaType.ts +27 -27
- package/src/tags/Default.ts +52 -52
- package/src/tags/Example.ts +56 -56
- package/src/tags/Examples.ts +56 -56
- package/src/tags/ExclusiveMaximum.ts +44 -44
- package/src/tags/ExclusiveMinimum.ts +44 -44
- package/src/tags/Format.ts +78 -78
- package/src/tags/JsonSchemaPlugin.ts +36 -36
- package/src/tags/MaxItems.ts +31 -31
- package/src/tags/MaxLength.ts +25 -25
- package/src/tags/Maximum.ts +39 -39
- package/src/tags/MinItems.ts +31 -31
- package/src/tags/MinLength.ts +25 -25
- package/src/tags/Minimum.ts +39 -39
- package/src/tags/MultipleOf.ts +42 -42
- package/src/tags/Pattern.ts +49 -49
- package/src/tags/Sequence.ts +37 -37
- package/src/tags/TagBase.ts +102 -102
- package/src/tags/Type.ts +64 -64
- package/src/tags/UniqueItems.ts +34 -34
- package/src/tags/internal/FormatCheatSheet.ts +71 -71
- package/src/transformers/ITransformOptions.ts +70 -70
- package/src/transformers/ImportTransformer.ts +253 -253
- package/src/transformers/NoTransformConfigurationError.ts +16 -16
- package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -224
- package/src/typings/Equal.ts +18 -18
|
@@ -1,331 +1,331 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { ExpressionFactory } from "../../factories/ExpressionFactory";
|
|
4
|
-
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
5
|
-
import { StatementFactory } from "../../factories/StatementFactory";
|
|
6
|
-
import { TypeFactory } from "../../factories/TypeFactory";
|
|
7
|
-
|
|
8
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
9
|
-
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
10
|
-
import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
|
|
11
|
-
import { MetadataMap } from "../../schemas/metadata/MetadataMap";
|
|
12
|
-
import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
|
|
13
|
-
|
|
14
|
-
import { CheckerProgrammer } from "../CheckerProgrammer";
|
|
15
|
-
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
16
|
-
import { UnionExplorer } from "../helpers/UnionExplorer";
|
|
17
|
-
|
|
18
|
-
/** @internal */
|
|
19
|
-
export const check_union_array_like = <
|
|
20
|
-
Origin extends Metadata | MetadataArray | MetadataTuple | MetadataMap,
|
|
21
|
-
Category extends MetadataArray | MetadataTuple,
|
|
22
|
-
Element,
|
|
23
|
-
>(props: {
|
|
24
|
-
config: check_union_array_like.IConfig<Category, Element>;
|
|
25
|
-
accessor: check_union_array_like.IAccessor<Origin, Category, Element>;
|
|
26
|
-
parameters: ts.ParameterDeclaration[];
|
|
27
|
-
input: ts.Expression;
|
|
28
|
-
definitions: Origin[];
|
|
29
|
-
explore: FeatureProgrammer.IExplore;
|
|
30
|
-
}): ts.ArrowFunction => {
|
|
31
|
-
// ONLY ONE TYPE
|
|
32
|
-
const targets: Array<Category> = props.definitions.map(
|
|
33
|
-
props.accessor.transform,
|
|
34
|
-
);
|
|
35
|
-
if (targets.length === 1)
|
|
36
|
-
return ts.factory.createArrowFunction(
|
|
37
|
-
undefined,
|
|
38
|
-
undefined,
|
|
39
|
-
props.parameters,
|
|
40
|
-
undefined,
|
|
41
|
-
undefined,
|
|
42
|
-
props.config.decoder({
|
|
43
|
-
input: props.accessor.array(props.input),
|
|
44
|
-
definition: targets[0]!,
|
|
45
|
-
explore: props.explore,
|
|
46
|
-
}),
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const array = ts.factory.createIdentifier("array");
|
|
50
|
-
const top = ts.factory.createIdentifier("top");
|
|
51
|
-
|
|
52
|
-
const statements: ts.Statement[] = [];
|
|
53
|
-
const tupleList: MetadataTuple[] = targets.filter(
|
|
54
|
-
(t) => t instanceof MetadataTuple,
|
|
55
|
-
) as MetadataTuple[];
|
|
56
|
-
const arrayList: MetadataArray[] = targets.filter(
|
|
57
|
-
(t) => t instanceof MetadataArray,
|
|
58
|
-
) as MetadataArray[];
|
|
59
|
-
|
|
60
|
-
const predicate = (meta: Category): ts.Expression =>
|
|
61
|
-
ts.factory.createAsExpression(
|
|
62
|
-
ts.factory.createArrayLiteralExpression(
|
|
63
|
-
[
|
|
64
|
-
ts.factory.createArrowFunction(
|
|
65
|
-
undefined,
|
|
66
|
-
undefined,
|
|
67
|
-
[
|
|
68
|
-
IdentifierFactory.parameter(
|
|
69
|
-
"top",
|
|
70
|
-
meta instanceof MetadataArrayType
|
|
71
|
-
? TypeFactory.keyword("any")
|
|
72
|
-
: ts.factory.createTypeReferenceNode("any[]"),
|
|
73
|
-
),
|
|
74
|
-
],
|
|
75
|
-
TypeFactory.keyword("any"),
|
|
76
|
-
undefined,
|
|
77
|
-
props.config.checker({
|
|
78
|
-
input: ts.factory.createIdentifier("top"),
|
|
79
|
-
definition: props.accessor.element(meta),
|
|
80
|
-
explore: {
|
|
81
|
-
...props.explore,
|
|
82
|
-
tracable: false,
|
|
83
|
-
postfix: meta instanceof MetadataArrayType ? `"[0]"` : "",
|
|
84
|
-
},
|
|
85
|
-
container: array,
|
|
86
|
-
}),
|
|
87
|
-
),
|
|
88
|
-
ts.factory.createArrowFunction(
|
|
89
|
-
undefined,
|
|
90
|
-
undefined,
|
|
91
|
-
[
|
|
92
|
-
IdentifierFactory.parameter(
|
|
93
|
-
"entire",
|
|
94
|
-
ts.factory.createTypeReferenceNode("any[]"),
|
|
95
|
-
),
|
|
96
|
-
],
|
|
97
|
-
TypeFactory.keyword("any"),
|
|
98
|
-
undefined,
|
|
99
|
-
props.config.decoder({
|
|
100
|
-
input: ts.factory.createIdentifier("entire"),
|
|
101
|
-
definition: meta,
|
|
102
|
-
explore: {
|
|
103
|
-
...props.explore,
|
|
104
|
-
tracable: true,
|
|
105
|
-
},
|
|
106
|
-
}),
|
|
107
|
-
),
|
|
108
|
-
],
|
|
109
|
-
true,
|
|
110
|
-
),
|
|
111
|
-
ts.factory.createTypeReferenceNode("const"),
|
|
112
|
-
);
|
|
113
|
-
const iterate = (props: {
|
|
114
|
-
init: string;
|
|
115
|
-
from: ts.Expression;
|
|
116
|
-
if: ts.IfStatement;
|
|
117
|
-
}): ts.ForOfStatement =>
|
|
118
|
-
ts.factory.createForOfStatement(
|
|
119
|
-
undefined,
|
|
120
|
-
ts.factory.createVariableDeclarationList(
|
|
121
|
-
[ts.factory.createVariableDeclaration(props.init)],
|
|
122
|
-
ts.NodeFlags.Const,
|
|
123
|
-
),
|
|
124
|
-
props.from,
|
|
125
|
-
props.if,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (tupleList.length)
|
|
129
|
-
statements.push(
|
|
130
|
-
StatementFactory.constant({
|
|
131
|
-
name: "array",
|
|
132
|
-
value: props.accessor.array(props.input),
|
|
133
|
-
}),
|
|
134
|
-
StatementFactory.constant({
|
|
135
|
-
name: "tuplePredicators",
|
|
136
|
-
value: ts.factory.createArrayLiteralExpression(
|
|
137
|
-
tupleList.map((x) => predicate(x as Category)),
|
|
138
|
-
true,
|
|
139
|
-
),
|
|
140
|
-
}),
|
|
141
|
-
iterate({
|
|
142
|
-
init: "pred",
|
|
143
|
-
from: ts.factory.createIdentifier("tuplePredicators"),
|
|
144
|
-
if: ts.factory.createIfStatement(
|
|
145
|
-
ts.factory.createCallExpression(
|
|
146
|
-
ts.factory.createIdentifier("pred[0]"),
|
|
147
|
-
undefined,
|
|
148
|
-
[array],
|
|
149
|
-
),
|
|
150
|
-
ts.factory.createReturnStatement(
|
|
151
|
-
ts.factory.createCallExpression(
|
|
152
|
-
ts.factory.createIdentifier(`pred[1]`),
|
|
153
|
-
undefined,
|
|
154
|
-
[array],
|
|
155
|
-
),
|
|
156
|
-
),
|
|
157
|
-
),
|
|
158
|
-
}),
|
|
159
|
-
);
|
|
160
|
-
if (arrayList.length) {
|
|
161
|
-
if (tupleList.length === 0)
|
|
162
|
-
statements.push(
|
|
163
|
-
StatementFactory.constant({
|
|
164
|
-
name: "array",
|
|
165
|
-
value: props.accessor.array(props.input),
|
|
166
|
-
}),
|
|
167
|
-
);
|
|
168
|
-
statements.push(
|
|
169
|
-
StatementFactory.constant({
|
|
170
|
-
name: "top",
|
|
171
|
-
value: props.accessor.front(props.input),
|
|
172
|
-
}),
|
|
173
|
-
ts.factory.createIfStatement(
|
|
174
|
-
ts.factory.createStrictEquality(
|
|
175
|
-
ExpressionFactory.number(0),
|
|
176
|
-
props.accessor.size(props.input),
|
|
177
|
-
),
|
|
178
|
-
ts.isReturnStatement(props.config.empty)
|
|
179
|
-
? props.config.empty
|
|
180
|
-
: ts.factory.createReturnStatement(props.config.empty),
|
|
181
|
-
),
|
|
182
|
-
StatementFactory.constant({
|
|
183
|
-
name: "arrayPredicators",
|
|
184
|
-
value: ts.factory.createArrayLiteralExpression(
|
|
185
|
-
arrayList.map((x) => predicate(x as Category)),
|
|
186
|
-
true,
|
|
187
|
-
),
|
|
188
|
-
}),
|
|
189
|
-
StatementFactory.constant({
|
|
190
|
-
name: "passed",
|
|
191
|
-
value: ts.factory.createCallExpression(
|
|
192
|
-
IdentifierFactory.access(
|
|
193
|
-
ts.factory.createIdentifier("arrayPredicators"),
|
|
194
|
-
"filter",
|
|
195
|
-
),
|
|
196
|
-
undefined,
|
|
197
|
-
[
|
|
198
|
-
ts.factory.createArrowFunction(
|
|
199
|
-
undefined,
|
|
200
|
-
undefined,
|
|
201
|
-
[IdentifierFactory.parameter("pred")],
|
|
202
|
-
undefined,
|
|
203
|
-
undefined,
|
|
204
|
-
ts.factory.createCallExpression(
|
|
205
|
-
ts.factory.createIdentifier("pred[0]"),
|
|
206
|
-
undefined,
|
|
207
|
-
[top],
|
|
208
|
-
),
|
|
209
|
-
),
|
|
210
|
-
],
|
|
211
|
-
),
|
|
212
|
-
}),
|
|
213
|
-
ts.factory.createIfStatement(
|
|
214
|
-
ts.factory.createStrictEquality(
|
|
215
|
-
ExpressionFactory.number(1),
|
|
216
|
-
ts.factory.createIdentifier("passed.length"),
|
|
217
|
-
),
|
|
218
|
-
ts.factory.createReturnStatement(
|
|
219
|
-
ts.factory.createCallExpression(
|
|
220
|
-
ts.factory.createElementAccessExpression(
|
|
221
|
-
ts.factory.createNonNullExpression(
|
|
222
|
-
ts.factory.createIdentifier("passed[0]"),
|
|
223
|
-
),
|
|
224
|
-
1,
|
|
225
|
-
),
|
|
226
|
-
undefined,
|
|
227
|
-
[array],
|
|
228
|
-
),
|
|
229
|
-
),
|
|
230
|
-
ts.factory.createIfStatement(
|
|
231
|
-
ts.factory.createLessThan(
|
|
232
|
-
ExpressionFactory.number(1),
|
|
233
|
-
ts.factory.createIdentifier("passed.length"),
|
|
234
|
-
),
|
|
235
|
-
iterate({
|
|
236
|
-
init: "pred",
|
|
237
|
-
from: ts.factory.createIdentifier("passed"),
|
|
238
|
-
if: ts.factory.createIfStatement(
|
|
239
|
-
ts.factory.createCallExpression(
|
|
240
|
-
IdentifierFactory.access(array, "every"),
|
|
241
|
-
undefined,
|
|
242
|
-
[
|
|
243
|
-
ts.factory.createArrowFunction(
|
|
244
|
-
undefined,
|
|
245
|
-
undefined,
|
|
246
|
-
[
|
|
247
|
-
IdentifierFactory.parameter(
|
|
248
|
-
"value",
|
|
249
|
-
TypeFactory.keyword("any"),
|
|
250
|
-
),
|
|
251
|
-
],
|
|
252
|
-
undefined,
|
|
253
|
-
undefined,
|
|
254
|
-
ts.factory.createStrictEquality(
|
|
255
|
-
props.config.success,
|
|
256
|
-
ts.factory.createCallExpression(
|
|
257
|
-
ts.factory.createIdentifier("pred[0]"),
|
|
258
|
-
undefined,
|
|
259
|
-
[ts.factory.createIdentifier("value")],
|
|
260
|
-
),
|
|
261
|
-
),
|
|
262
|
-
),
|
|
263
|
-
],
|
|
264
|
-
),
|
|
265
|
-
ts.factory.createReturnStatement(
|
|
266
|
-
ts.factory.createCallExpression(
|
|
267
|
-
ts.factory.createIdentifier(`pred[1]`),
|
|
268
|
-
undefined,
|
|
269
|
-
[ts.factory.createIdentifier("array")],
|
|
270
|
-
),
|
|
271
|
-
),
|
|
272
|
-
),
|
|
273
|
-
}),
|
|
274
|
-
),
|
|
275
|
-
),
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
statements.push(
|
|
279
|
-
props.config.failure({
|
|
280
|
-
input: props.input,
|
|
281
|
-
expected: `(${targets
|
|
282
|
-
.map((t) => props.accessor.name(t, props.accessor.element(t)))
|
|
283
|
-
.join(" | ")})`,
|
|
284
|
-
explore: props.explore,
|
|
285
|
-
}),
|
|
286
|
-
);
|
|
287
|
-
return ts.factory.createArrowFunction(
|
|
288
|
-
undefined,
|
|
289
|
-
undefined,
|
|
290
|
-
props.parameters,
|
|
291
|
-
undefined,
|
|
292
|
-
undefined,
|
|
293
|
-
ts.factory.createBlock(statements, true),
|
|
294
|
-
);
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
/** @internal */
|
|
298
|
-
export namespace check_union_array_like {
|
|
299
|
-
export interface IConfig<
|
|
300
|
-
Category extends MetadataArray | MetadataTuple,
|
|
301
|
-
Element,
|
|
302
|
-
> {
|
|
303
|
-
checker(props: {
|
|
304
|
-
input: ts.Expression;
|
|
305
|
-
definition: Element;
|
|
306
|
-
explore: FeatureProgrammer.IExplore;
|
|
307
|
-
container: ts.Expression;
|
|
308
|
-
}): ts.Expression;
|
|
309
|
-
decoder: UnionExplorer.Decoder<Category>;
|
|
310
|
-
empty: ts.ReturnStatement | ts.Expression;
|
|
311
|
-
success: ts.Expression;
|
|
312
|
-
failure(props: {
|
|
313
|
-
input: ts.Expression;
|
|
314
|
-
expected: string;
|
|
315
|
-
explore: CheckerProgrammer.IExplore;
|
|
316
|
-
}): ts.Statement;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
export interface IAccessor<
|
|
320
|
-
Origin,
|
|
321
|
-
Category extends MetadataArray | MetadataTuple,
|
|
322
|
-
Element,
|
|
323
|
-
> {
|
|
324
|
-
transform(origin: Origin): Category;
|
|
325
|
-
element(meta: Category): Element;
|
|
326
|
-
name(meta: Category, elem: Element): string;
|
|
327
|
-
front(input: ts.Expression): ts.Expression;
|
|
328
|
-
array(input: ts.Expression): ts.Expression;
|
|
329
|
-
size(input: ts.Expression): ts.Expression;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { ExpressionFactory } from "../../factories/ExpressionFactory";
|
|
4
|
+
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
5
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
6
|
+
import { TypeFactory } from "../../factories/TypeFactory";
|
|
7
|
+
|
|
8
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
9
|
+
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
10
|
+
import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
|
|
11
|
+
import { MetadataMap } from "../../schemas/metadata/MetadataMap";
|
|
12
|
+
import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
|
|
13
|
+
|
|
14
|
+
import { CheckerProgrammer } from "../CheckerProgrammer";
|
|
15
|
+
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
16
|
+
import { UnionExplorer } from "../helpers/UnionExplorer";
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
export const check_union_array_like = <
|
|
20
|
+
Origin extends Metadata | MetadataArray | MetadataTuple | MetadataMap,
|
|
21
|
+
Category extends MetadataArray | MetadataTuple,
|
|
22
|
+
Element,
|
|
23
|
+
>(props: {
|
|
24
|
+
config: check_union_array_like.IConfig<Category, Element>;
|
|
25
|
+
accessor: check_union_array_like.IAccessor<Origin, Category, Element>;
|
|
26
|
+
parameters: ts.ParameterDeclaration[];
|
|
27
|
+
input: ts.Expression;
|
|
28
|
+
definitions: Origin[];
|
|
29
|
+
explore: FeatureProgrammer.IExplore;
|
|
30
|
+
}): ts.ArrowFunction => {
|
|
31
|
+
// ONLY ONE TYPE
|
|
32
|
+
const targets: Array<Category> = props.definitions.map(
|
|
33
|
+
props.accessor.transform,
|
|
34
|
+
);
|
|
35
|
+
if (targets.length === 1)
|
|
36
|
+
return ts.factory.createArrowFunction(
|
|
37
|
+
undefined,
|
|
38
|
+
undefined,
|
|
39
|
+
props.parameters,
|
|
40
|
+
undefined,
|
|
41
|
+
undefined,
|
|
42
|
+
props.config.decoder({
|
|
43
|
+
input: props.accessor.array(props.input),
|
|
44
|
+
definition: targets[0]!,
|
|
45
|
+
explore: props.explore,
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const array = ts.factory.createIdentifier("array");
|
|
50
|
+
const top = ts.factory.createIdentifier("top");
|
|
51
|
+
|
|
52
|
+
const statements: ts.Statement[] = [];
|
|
53
|
+
const tupleList: MetadataTuple[] = targets.filter(
|
|
54
|
+
(t) => t instanceof MetadataTuple,
|
|
55
|
+
) as MetadataTuple[];
|
|
56
|
+
const arrayList: MetadataArray[] = targets.filter(
|
|
57
|
+
(t) => t instanceof MetadataArray,
|
|
58
|
+
) as MetadataArray[];
|
|
59
|
+
|
|
60
|
+
const predicate = (meta: Category): ts.Expression =>
|
|
61
|
+
ts.factory.createAsExpression(
|
|
62
|
+
ts.factory.createArrayLiteralExpression(
|
|
63
|
+
[
|
|
64
|
+
ts.factory.createArrowFunction(
|
|
65
|
+
undefined,
|
|
66
|
+
undefined,
|
|
67
|
+
[
|
|
68
|
+
IdentifierFactory.parameter(
|
|
69
|
+
"top",
|
|
70
|
+
meta instanceof MetadataArrayType
|
|
71
|
+
? TypeFactory.keyword("any")
|
|
72
|
+
: ts.factory.createTypeReferenceNode("any[]"),
|
|
73
|
+
),
|
|
74
|
+
],
|
|
75
|
+
TypeFactory.keyword("any"),
|
|
76
|
+
undefined,
|
|
77
|
+
props.config.checker({
|
|
78
|
+
input: ts.factory.createIdentifier("top"),
|
|
79
|
+
definition: props.accessor.element(meta),
|
|
80
|
+
explore: {
|
|
81
|
+
...props.explore,
|
|
82
|
+
tracable: false,
|
|
83
|
+
postfix: meta instanceof MetadataArrayType ? `"[0]"` : "",
|
|
84
|
+
},
|
|
85
|
+
container: array,
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
ts.factory.createArrowFunction(
|
|
89
|
+
undefined,
|
|
90
|
+
undefined,
|
|
91
|
+
[
|
|
92
|
+
IdentifierFactory.parameter(
|
|
93
|
+
"entire",
|
|
94
|
+
ts.factory.createTypeReferenceNode("any[]"),
|
|
95
|
+
),
|
|
96
|
+
],
|
|
97
|
+
TypeFactory.keyword("any"),
|
|
98
|
+
undefined,
|
|
99
|
+
props.config.decoder({
|
|
100
|
+
input: ts.factory.createIdentifier("entire"),
|
|
101
|
+
definition: meta,
|
|
102
|
+
explore: {
|
|
103
|
+
...props.explore,
|
|
104
|
+
tracable: true,
|
|
105
|
+
},
|
|
106
|
+
}),
|
|
107
|
+
),
|
|
108
|
+
],
|
|
109
|
+
true,
|
|
110
|
+
),
|
|
111
|
+
ts.factory.createTypeReferenceNode("const"),
|
|
112
|
+
);
|
|
113
|
+
const iterate = (props: {
|
|
114
|
+
init: string;
|
|
115
|
+
from: ts.Expression;
|
|
116
|
+
if: ts.IfStatement;
|
|
117
|
+
}): ts.ForOfStatement =>
|
|
118
|
+
ts.factory.createForOfStatement(
|
|
119
|
+
undefined,
|
|
120
|
+
ts.factory.createVariableDeclarationList(
|
|
121
|
+
[ts.factory.createVariableDeclaration(props.init)],
|
|
122
|
+
ts.NodeFlags.Const,
|
|
123
|
+
),
|
|
124
|
+
props.from,
|
|
125
|
+
props.if,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (tupleList.length)
|
|
129
|
+
statements.push(
|
|
130
|
+
StatementFactory.constant({
|
|
131
|
+
name: "array",
|
|
132
|
+
value: props.accessor.array(props.input),
|
|
133
|
+
}),
|
|
134
|
+
StatementFactory.constant({
|
|
135
|
+
name: "tuplePredicators",
|
|
136
|
+
value: ts.factory.createArrayLiteralExpression(
|
|
137
|
+
tupleList.map((x) => predicate(x as Category)),
|
|
138
|
+
true,
|
|
139
|
+
),
|
|
140
|
+
}),
|
|
141
|
+
iterate({
|
|
142
|
+
init: "pred",
|
|
143
|
+
from: ts.factory.createIdentifier("tuplePredicators"),
|
|
144
|
+
if: ts.factory.createIfStatement(
|
|
145
|
+
ts.factory.createCallExpression(
|
|
146
|
+
ts.factory.createIdentifier("pred[0]"),
|
|
147
|
+
undefined,
|
|
148
|
+
[array],
|
|
149
|
+
),
|
|
150
|
+
ts.factory.createReturnStatement(
|
|
151
|
+
ts.factory.createCallExpression(
|
|
152
|
+
ts.factory.createIdentifier(`pred[1]`),
|
|
153
|
+
undefined,
|
|
154
|
+
[array],
|
|
155
|
+
),
|
|
156
|
+
),
|
|
157
|
+
),
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
if (arrayList.length) {
|
|
161
|
+
if (tupleList.length === 0)
|
|
162
|
+
statements.push(
|
|
163
|
+
StatementFactory.constant({
|
|
164
|
+
name: "array",
|
|
165
|
+
value: props.accessor.array(props.input),
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
statements.push(
|
|
169
|
+
StatementFactory.constant({
|
|
170
|
+
name: "top",
|
|
171
|
+
value: props.accessor.front(props.input),
|
|
172
|
+
}),
|
|
173
|
+
ts.factory.createIfStatement(
|
|
174
|
+
ts.factory.createStrictEquality(
|
|
175
|
+
ExpressionFactory.number(0),
|
|
176
|
+
props.accessor.size(props.input),
|
|
177
|
+
),
|
|
178
|
+
ts.isReturnStatement(props.config.empty)
|
|
179
|
+
? props.config.empty
|
|
180
|
+
: ts.factory.createReturnStatement(props.config.empty),
|
|
181
|
+
),
|
|
182
|
+
StatementFactory.constant({
|
|
183
|
+
name: "arrayPredicators",
|
|
184
|
+
value: ts.factory.createArrayLiteralExpression(
|
|
185
|
+
arrayList.map((x) => predicate(x as Category)),
|
|
186
|
+
true,
|
|
187
|
+
),
|
|
188
|
+
}),
|
|
189
|
+
StatementFactory.constant({
|
|
190
|
+
name: "passed",
|
|
191
|
+
value: ts.factory.createCallExpression(
|
|
192
|
+
IdentifierFactory.access(
|
|
193
|
+
ts.factory.createIdentifier("arrayPredicators"),
|
|
194
|
+
"filter",
|
|
195
|
+
),
|
|
196
|
+
undefined,
|
|
197
|
+
[
|
|
198
|
+
ts.factory.createArrowFunction(
|
|
199
|
+
undefined,
|
|
200
|
+
undefined,
|
|
201
|
+
[IdentifierFactory.parameter("pred")],
|
|
202
|
+
undefined,
|
|
203
|
+
undefined,
|
|
204
|
+
ts.factory.createCallExpression(
|
|
205
|
+
ts.factory.createIdentifier("pred[0]"),
|
|
206
|
+
undefined,
|
|
207
|
+
[top],
|
|
208
|
+
),
|
|
209
|
+
),
|
|
210
|
+
],
|
|
211
|
+
),
|
|
212
|
+
}),
|
|
213
|
+
ts.factory.createIfStatement(
|
|
214
|
+
ts.factory.createStrictEquality(
|
|
215
|
+
ExpressionFactory.number(1),
|
|
216
|
+
ts.factory.createIdentifier("passed.length"),
|
|
217
|
+
),
|
|
218
|
+
ts.factory.createReturnStatement(
|
|
219
|
+
ts.factory.createCallExpression(
|
|
220
|
+
ts.factory.createElementAccessExpression(
|
|
221
|
+
ts.factory.createNonNullExpression(
|
|
222
|
+
ts.factory.createIdentifier("passed[0]"),
|
|
223
|
+
),
|
|
224
|
+
1,
|
|
225
|
+
),
|
|
226
|
+
undefined,
|
|
227
|
+
[array],
|
|
228
|
+
),
|
|
229
|
+
),
|
|
230
|
+
ts.factory.createIfStatement(
|
|
231
|
+
ts.factory.createLessThan(
|
|
232
|
+
ExpressionFactory.number(1),
|
|
233
|
+
ts.factory.createIdentifier("passed.length"),
|
|
234
|
+
),
|
|
235
|
+
iterate({
|
|
236
|
+
init: "pred",
|
|
237
|
+
from: ts.factory.createIdentifier("passed"),
|
|
238
|
+
if: ts.factory.createIfStatement(
|
|
239
|
+
ts.factory.createCallExpression(
|
|
240
|
+
IdentifierFactory.access(array, "every"),
|
|
241
|
+
undefined,
|
|
242
|
+
[
|
|
243
|
+
ts.factory.createArrowFunction(
|
|
244
|
+
undefined,
|
|
245
|
+
undefined,
|
|
246
|
+
[
|
|
247
|
+
IdentifierFactory.parameter(
|
|
248
|
+
"value",
|
|
249
|
+
TypeFactory.keyword("any"),
|
|
250
|
+
),
|
|
251
|
+
],
|
|
252
|
+
undefined,
|
|
253
|
+
undefined,
|
|
254
|
+
ts.factory.createStrictEquality(
|
|
255
|
+
props.config.success,
|
|
256
|
+
ts.factory.createCallExpression(
|
|
257
|
+
ts.factory.createIdentifier("pred[0]"),
|
|
258
|
+
undefined,
|
|
259
|
+
[ts.factory.createIdentifier("value")],
|
|
260
|
+
),
|
|
261
|
+
),
|
|
262
|
+
),
|
|
263
|
+
],
|
|
264
|
+
),
|
|
265
|
+
ts.factory.createReturnStatement(
|
|
266
|
+
ts.factory.createCallExpression(
|
|
267
|
+
ts.factory.createIdentifier(`pred[1]`),
|
|
268
|
+
undefined,
|
|
269
|
+
[ts.factory.createIdentifier("array")],
|
|
270
|
+
),
|
|
271
|
+
),
|
|
272
|
+
),
|
|
273
|
+
}),
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
statements.push(
|
|
279
|
+
props.config.failure({
|
|
280
|
+
input: props.input,
|
|
281
|
+
expected: `(${targets
|
|
282
|
+
.map((t) => props.accessor.name(t, props.accessor.element(t)))
|
|
283
|
+
.join(" | ")})`,
|
|
284
|
+
explore: props.explore,
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
return ts.factory.createArrowFunction(
|
|
288
|
+
undefined,
|
|
289
|
+
undefined,
|
|
290
|
+
props.parameters,
|
|
291
|
+
undefined,
|
|
292
|
+
undefined,
|
|
293
|
+
ts.factory.createBlock(statements, true),
|
|
294
|
+
);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
/** @internal */
|
|
298
|
+
export namespace check_union_array_like {
|
|
299
|
+
export interface IConfig<
|
|
300
|
+
Category extends MetadataArray | MetadataTuple,
|
|
301
|
+
Element,
|
|
302
|
+
> {
|
|
303
|
+
checker(props: {
|
|
304
|
+
input: ts.Expression;
|
|
305
|
+
definition: Element;
|
|
306
|
+
explore: FeatureProgrammer.IExplore;
|
|
307
|
+
container: ts.Expression;
|
|
308
|
+
}): ts.Expression;
|
|
309
|
+
decoder: UnionExplorer.Decoder<Category>;
|
|
310
|
+
empty: ts.ReturnStatement | ts.Expression;
|
|
311
|
+
success: ts.Expression;
|
|
312
|
+
failure(props: {
|
|
313
|
+
input: ts.Expression;
|
|
314
|
+
expected: string;
|
|
315
|
+
explore: CheckerProgrammer.IExplore;
|
|
316
|
+
}): ts.Statement;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export interface IAccessor<
|
|
320
|
+
Origin,
|
|
321
|
+
Category extends MetadataArray | MetadataTuple,
|
|
322
|
+
Element,
|
|
323
|
+
> {
|
|
324
|
+
transform(origin: Origin): Category;
|
|
325
|
+
element(meta: Category): Element;
|
|
326
|
+
name(meta: Category, elem: Element): string;
|
|
327
|
+
front(input: ts.Expression): ts.Expression;
|
|
328
|
+
array(input: ts.Expression): ts.Expression;
|
|
329
|
+
size(input: ts.Expression): ts.Expression;
|
|
330
|
+
}
|
|
331
|
+
}
|