typia 7.0.0-dev.20240922 → 7.0.0-dev.20240924
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/programmers/FeatureProgrammer.d.ts +2 -2
- package/lib/programmers/FeatureProgrammer.js +2 -2
- package/lib/programmers/json/JsonStringifyProgrammer.js +519 -494
- package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js +456 -405
- package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscLiteralsProgrammer.js +3 -3
- package/lib/programmers/misc/MiscLiteralsProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscPruneProgrammer.js +336 -300
- package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
- package/package.json +1 -1
- package/src/programmers/FeatureProgrammer.ts +3 -3
- package/src/programmers/json/JsonStringifyProgrammer.ts +787 -678
- package/src/programmers/misc/MiscCloneProgrammer.ts +737 -600
- package/src/programmers/misc/MiscLiteralsProgrammer.ts +4 -4
- package/src/programmers/misc/MiscPruneProgrammer.ts +508 -412
|
@@ -11,6 +11,7 @@ import { ValueFactory } from "../../factories/ValueFactory";
|
|
|
11
11
|
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
12
12
|
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
13
13
|
import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
|
|
14
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
14
15
|
import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
|
|
15
16
|
import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
|
|
16
17
|
|
|
@@ -19,8 +20,6 @@ import { ITypiaContext } from "../../transformers/ITypiaContext";
|
|
|
19
20
|
|
|
20
21
|
import { Atomic } from "../../typings/Atomic";
|
|
21
22
|
|
|
22
|
-
import { ArrayUtil } from "../../utils/ArrayUtil";
|
|
23
|
-
|
|
24
23
|
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
25
24
|
import { IsProgrammer } from "../IsProgrammer";
|
|
26
25
|
import { AtomicPredicator } from "../helpers/AtomicPredicator";
|
|
@@ -45,9 +44,7 @@ export namespace JsonStringifyProgrammer {
|
|
|
45
44
|
type: ts.Type;
|
|
46
45
|
name: string | undefined;
|
|
47
46
|
}): FeatureProgrammer.IDecomposed => {
|
|
48
|
-
const config: FeatureProgrammer.IConfig = configure(props
|
|
49
|
-
props.importer,
|
|
50
|
-
);
|
|
47
|
+
const config: FeatureProgrammer.IConfig = configure(props);
|
|
51
48
|
if (props.validated === false)
|
|
52
49
|
config.addition = (collection) =>
|
|
53
50
|
IsProgrammer.write_function_statements(props.context)(props.importer)(
|
|
@@ -87,589 +84,631 @@ export namespace JsonStringifyProgrammer {
|
|
|
87
84
|
});
|
|
88
85
|
};
|
|
89
86
|
|
|
90
|
-
const write_array_functions =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
)(ts.factory.createIdentifier("input")),
|
|
87
|
+
const write_array_functions = (props: {
|
|
88
|
+
config: FeatureProgrammer.IConfig;
|
|
89
|
+
importer: FunctionImporter;
|
|
90
|
+
collection: MetadataCollection;
|
|
91
|
+
}): ts.VariableStatement[] =>
|
|
92
|
+
props.collection
|
|
93
|
+
.arrays()
|
|
94
|
+
.filter((a) => a.recursive)
|
|
95
|
+
.map((type, i) =>
|
|
96
|
+
StatementFactory.constant(
|
|
97
|
+
`${props.config.prefix}a${i}`,
|
|
98
|
+
ts.factory.createArrowFunction(
|
|
99
|
+
undefined,
|
|
100
|
+
undefined,
|
|
101
|
+
FeatureProgrammer.parameterDeclarations(props.config)(
|
|
106
102
|
TypeFactory.keyword("any"),
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
103
|
+
)(ts.factory.createIdentifier("input")),
|
|
104
|
+
TypeFactory.keyword("any"),
|
|
105
|
+
undefined,
|
|
106
|
+
decode_array_inline({
|
|
107
|
+
config: props.config,
|
|
108
|
+
importer: props.importer,
|
|
109
|
+
input: ts.factory.createIdentifier("input"),
|
|
110
|
+
array: MetadataArray.create({
|
|
111
|
+
type,
|
|
112
|
+
tags: [],
|
|
113
|
+
}),
|
|
114
|
+
explore: {
|
|
115
|
+
tracable: props.config.trace,
|
|
116
|
+
source: "function",
|
|
117
|
+
from: "array",
|
|
118
|
+
postfix: "",
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
122
121
|
),
|
|
123
|
-
)
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
124
|
|
|
125
|
-
const write_tuple_functions =
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
)(ts.factory.createIdentifier("input")),
|
|
125
|
+
const write_tuple_functions = (props: {
|
|
126
|
+
context: ITypiaContext;
|
|
127
|
+
config: FeatureProgrammer.IConfig;
|
|
128
|
+
importer: FunctionImporter;
|
|
129
|
+
collection: MetadataCollection;
|
|
130
|
+
}): ts.VariableStatement[] =>
|
|
131
|
+
props.collection
|
|
132
|
+
.tuples()
|
|
133
|
+
.filter((t) => t.recursive)
|
|
134
|
+
.map((tuple, i) =>
|
|
135
|
+
StatementFactory.constant(
|
|
136
|
+
`${props.config.prefix}t${i}`,
|
|
137
|
+
ts.factory.createArrowFunction(
|
|
138
|
+
undefined,
|
|
139
|
+
undefined,
|
|
140
|
+
FeatureProgrammer.parameterDeclarations(props.config)(
|
|
142
141
|
TypeFactory.keyword("any"),
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
142
|
+
)(ts.factory.createIdentifier("input")),
|
|
143
|
+
TypeFactory.keyword("any"),
|
|
144
|
+
undefined,
|
|
145
|
+
decode_tuple_inline({
|
|
146
|
+
context: props.context,
|
|
147
|
+
config: props.config,
|
|
148
|
+
importer: props.importer,
|
|
149
|
+
input: ts.factory.createIdentifier("input"),
|
|
150
|
+
tuple,
|
|
151
|
+
explore: {
|
|
152
|
+
tracable: props.config.trace,
|
|
153
|
+
source: "function",
|
|
154
|
+
from: "array",
|
|
155
|
+
postfix: "",
|
|
156
|
+
},
|
|
157
|
+
}),
|
|
155
158
|
),
|
|
156
|
-
)
|
|
159
|
+
),
|
|
160
|
+
);
|
|
157
161
|
|
|
158
162
|
/* -----------------------------------------------------------
|
|
159
163
|
DECODERS
|
|
160
164
|
----------------------------------------------------------- */
|
|
161
|
-
const decode =
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
165
|
+
const decode = (props: {
|
|
166
|
+
context: ITypiaContext;
|
|
167
|
+
config: FeatureProgrammer.IConfig;
|
|
168
|
+
importer: FunctionImporter;
|
|
169
|
+
input: ts.Expression;
|
|
170
|
+
metadata: Metadata;
|
|
171
|
+
explore: FeatureProgrammer.IExplore;
|
|
172
|
+
}): ts.Expression => {
|
|
173
|
+
// ANY TYPE
|
|
174
|
+
if (props.metadata.any === true)
|
|
175
|
+
return wrap_required({
|
|
176
|
+
input: props.input,
|
|
177
|
+
metadata: props.metadata,
|
|
178
|
+
explore: props.explore,
|
|
179
|
+
expression: wrap_functional({
|
|
180
|
+
input: props.input,
|
|
181
|
+
metadata: props.metadata,
|
|
182
|
+
explore: props.explore,
|
|
183
|
+
expression: ts.factory.createCallExpression(
|
|
184
|
+
ts.factory.createIdentifier("JSON.stringify"),
|
|
185
|
+
undefined,
|
|
186
|
+
[props.input],
|
|
187
|
+
),
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// ONLY NULL OR UNDEFINED
|
|
192
|
+
const size: number = props.metadata.size();
|
|
193
|
+
if (
|
|
194
|
+
size === 0 &&
|
|
195
|
+
(props.metadata.isRequired() === false ||
|
|
196
|
+
props.metadata.nullable === true)
|
|
197
|
+
) {
|
|
198
|
+
if (
|
|
199
|
+
props.metadata.isRequired() === false &&
|
|
200
|
+
props.metadata.nullable === true
|
|
201
|
+
)
|
|
202
|
+
return props.explore.from === "array"
|
|
203
|
+
? ts.factory.createStringLiteral("null")
|
|
204
|
+
: ts.factory.createConditionalExpression(
|
|
205
|
+
ts.factory.createStrictEquality(
|
|
206
|
+
ts.factory.createNull(),
|
|
207
|
+
props.input,
|
|
208
|
+
),
|
|
183
209
|
undefined,
|
|
184
|
-
|
|
185
|
-
|
|
210
|
+
ts.factory.createStringLiteral("null"),
|
|
211
|
+
undefined,
|
|
212
|
+
ts.factory.createIdentifier("undefined"),
|
|
213
|
+
);
|
|
214
|
+
else if (props.metadata.isRequired() === false)
|
|
215
|
+
return props.explore.from === "array"
|
|
216
|
+
? ts.factory.createStringLiteral("null")
|
|
217
|
+
: ts.factory.createIdentifier("undefined");
|
|
218
|
+
else return ts.factory.createStringLiteral("null");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//----
|
|
222
|
+
// LIST UP UNION TYPES
|
|
223
|
+
//----
|
|
224
|
+
const unions: IUnion[] = [];
|
|
225
|
+
|
|
226
|
+
// toJSON() METHOD
|
|
227
|
+
if (props.metadata.escaped !== null)
|
|
228
|
+
unions.push({
|
|
229
|
+
type: "resolved",
|
|
230
|
+
is:
|
|
231
|
+
props.metadata.escaped.original.size() === 1 &&
|
|
232
|
+
props.metadata.escaped.original.natives[0] === "Date"
|
|
233
|
+
? () => check_native("Date")(props.input)
|
|
234
|
+
: () => IsProgrammer.decode_to_json(false)(props.input),
|
|
235
|
+
value: () =>
|
|
236
|
+
decode_to_json({
|
|
237
|
+
...props,
|
|
238
|
+
metadata: props.metadata.escaped!.returns,
|
|
186
239
|
}),
|
|
187
|
-
|
|
240
|
+
});
|
|
241
|
+
else if (props.metadata.functions.length)
|
|
242
|
+
unions.push({
|
|
243
|
+
type: "functional",
|
|
244
|
+
is: () => IsProgrammer.decode_functional(props.input),
|
|
245
|
+
value: () => decode_functional(props.explore),
|
|
246
|
+
});
|
|
188
247
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
:
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
248
|
+
// TEMPLATES
|
|
249
|
+
if (props.metadata.templates.length)
|
|
250
|
+
if (AtomicPredicator.template(props.metadata)) {
|
|
251
|
+
const partial = Metadata.initialize();
|
|
252
|
+
partial.atomics.push(
|
|
253
|
+
MetadataAtomic.create({ type: "string", tags: [] }),
|
|
254
|
+
),
|
|
255
|
+
unions.push({
|
|
256
|
+
type: "template literal",
|
|
257
|
+
is: () =>
|
|
258
|
+
IsProgrammer.decode(props.context)(props.importer)(
|
|
259
|
+
props.input,
|
|
260
|
+
partial,
|
|
261
|
+
props.explore,
|
|
262
|
+
),
|
|
263
|
+
value: () =>
|
|
264
|
+
decode_atomic({
|
|
265
|
+
...props,
|
|
266
|
+
type: "string",
|
|
267
|
+
}),
|
|
268
|
+
});
|
|
210
269
|
}
|
|
211
270
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
271
|
+
// CONSTANTS
|
|
272
|
+
for (const constant of props.metadata.constants)
|
|
273
|
+
if (
|
|
274
|
+
AtomicPredicator.constant({
|
|
275
|
+
metadata: props.metadata,
|
|
276
|
+
name: constant.type,
|
|
277
|
+
}) === false
|
|
278
|
+
)
|
|
279
|
+
continue;
|
|
280
|
+
else if (constant.type !== "string")
|
|
219
281
|
unions.push({
|
|
220
|
-
type: "
|
|
221
|
-
is:
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
282
|
+
type: "atomic",
|
|
283
|
+
is: () =>
|
|
284
|
+
IsProgrammer.decode(props.context)(props.importer)(
|
|
285
|
+
props.input,
|
|
286
|
+
(() => {
|
|
287
|
+
const partial = Metadata.initialize();
|
|
288
|
+
partial.atomics.push(
|
|
289
|
+
MetadataAtomic.create({
|
|
290
|
+
type: constant.type,
|
|
291
|
+
tags: [],
|
|
292
|
+
}),
|
|
293
|
+
);
|
|
294
|
+
return partial;
|
|
295
|
+
})(),
|
|
296
|
+
props.explore,
|
|
231
297
|
),
|
|
298
|
+
value: () =>
|
|
299
|
+
decode_atomic({
|
|
300
|
+
...props,
|
|
301
|
+
type: constant.type,
|
|
302
|
+
}),
|
|
232
303
|
});
|
|
233
|
-
else if (metadata.
|
|
304
|
+
else if (props.metadata.templates.length === 0)
|
|
234
305
|
unions.push({
|
|
235
|
-
type: "
|
|
236
|
-
is: () =>
|
|
237
|
-
|
|
306
|
+
type: "const string",
|
|
307
|
+
is: () =>
|
|
308
|
+
IsProgrammer.decode(props.context)(props.importer)(
|
|
309
|
+
props.input,
|
|
310
|
+
(() => {
|
|
311
|
+
const partial = Metadata.initialize();
|
|
312
|
+
partial.atomics.push(
|
|
313
|
+
MetadataAtomic.create({
|
|
314
|
+
type: "string",
|
|
315
|
+
tags: [],
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
318
|
+
return partial;
|
|
319
|
+
})(),
|
|
320
|
+
props.explore,
|
|
321
|
+
),
|
|
322
|
+
value: () =>
|
|
323
|
+
decode_constant_string({
|
|
324
|
+
...props,
|
|
325
|
+
values: [...constant.values.map((v) => v.value)] as string[],
|
|
326
|
+
}),
|
|
238
327
|
});
|
|
239
328
|
|
|
240
|
-
|
|
329
|
+
/// ATOMICS
|
|
330
|
+
for (const a of props.metadata.atomics)
|
|
241
331
|
if (
|
|
242
|
-
|
|
243
|
-
|
|
332
|
+
AtomicPredicator.atomic({
|
|
333
|
+
metadata: props.metadata,
|
|
334
|
+
name: a.type,
|
|
335
|
+
})
|
|
244
336
|
)
|
|
245
|
-
if (AtomicPredicator.template(metadata)) {
|
|
246
|
-
const partial = Metadata.initialize();
|
|
247
|
-
partial.atomics.push(
|
|
248
|
-
MetadataAtomic.create({ type: "string", tags: [] }),
|
|
249
|
-
),
|
|
250
|
-
unions.push({
|
|
251
|
-
type: "template literal",
|
|
252
|
-
is: () =>
|
|
253
|
-
IsProgrammer.decode(project)(importer)(input, partial, explore),
|
|
254
|
-
value: () =>
|
|
255
|
-
decode_atomic(project)(importer)(input, "string", explore),
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// CONSTANTS
|
|
260
|
-
for (const constant of metadata.constants)
|
|
261
|
-
if (
|
|
262
|
-
AtomicPredicator.constant({
|
|
263
|
-
metadata,
|
|
264
|
-
name: constant.type,
|
|
265
|
-
}) === false
|
|
266
|
-
)
|
|
267
|
-
continue;
|
|
268
|
-
else if (constant.type !== "string")
|
|
269
|
-
unions.push({
|
|
270
|
-
type: "atomic",
|
|
271
|
-
is: () =>
|
|
272
|
-
IsProgrammer.decode(project)(importer)(
|
|
273
|
-
input,
|
|
274
|
-
(() => {
|
|
275
|
-
const partial = Metadata.initialize();
|
|
276
|
-
partial.atomics.push(
|
|
277
|
-
MetadataAtomic.create({
|
|
278
|
-
type: constant.type,
|
|
279
|
-
tags: [],
|
|
280
|
-
}),
|
|
281
|
-
);
|
|
282
|
-
return partial;
|
|
283
|
-
})(),
|
|
284
|
-
explore,
|
|
285
|
-
),
|
|
286
|
-
value: () =>
|
|
287
|
-
decode_atomic(project)(importer)(input, constant.type, explore),
|
|
288
|
-
});
|
|
289
|
-
else if (metadata.templates.length === 0)
|
|
290
|
-
unions.push({
|
|
291
|
-
type: "const string",
|
|
292
|
-
is: () =>
|
|
293
|
-
IsProgrammer.decode(project)(importer)(
|
|
294
|
-
input,
|
|
295
|
-
(() => {
|
|
296
|
-
const partial = Metadata.initialize();
|
|
297
|
-
partial.atomics.push(
|
|
298
|
-
MetadataAtomic.create({
|
|
299
|
-
type: "string",
|
|
300
|
-
tags: [],
|
|
301
|
-
}),
|
|
302
|
-
);
|
|
303
|
-
return partial;
|
|
304
|
-
})(),
|
|
305
|
-
explore,
|
|
306
|
-
),
|
|
307
|
-
value: () =>
|
|
308
|
-
decode_constant_string(project)(importer)(
|
|
309
|
-
input,
|
|
310
|
-
[...constant.values.map((v) => v.value)] as string[],
|
|
311
|
-
explore,
|
|
312
|
-
),
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
/// ATOMICS
|
|
316
|
-
for (const a of metadata.atomics)
|
|
317
|
-
if (
|
|
318
|
-
AtomicPredicator.atomic({
|
|
319
|
-
metadata,
|
|
320
|
-
name: a.type,
|
|
321
|
-
})
|
|
322
|
-
)
|
|
323
|
-
unions.push({
|
|
324
|
-
type: "atomic",
|
|
325
|
-
is: () =>
|
|
326
|
-
IsProgrammer.decode(project)(importer)(
|
|
327
|
-
input,
|
|
328
|
-
(() => {
|
|
329
|
-
const partial = Metadata.initialize();
|
|
330
|
-
partial.atomics.push(a);
|
|
331
|
-
return partial;
|
|
332
|
-
})(),
|
|
333
|
-
explore,
|
|
334
|
-
),
|
|
335
|
-
value: () =>
|
|
336
|
-
decode_atomic(project)(importer)(input, a.type, explore),
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
// TUPLES
|
|
340
|
-
for (const tuple of metadata.tuples)
|
|
341
337
|
unions.push({
|
|
342
|
-
type: "
|
|
338
|
+
type: "atomic",
|
|
343
339
|
is: () =>
|
|
344
|
-
IsProgrammer.decode(
|
|
345
|
-
input,
|
|
340
|
+
IsProgrammer.decode(props.context)(props.importer)(
|
|
341
|
+
props.input,
|
|
346
342
|
(() => {
|
|
347
343
|
const partial = Metadata.initialize();
|
|
348
|
-
partial.
|
|
344
|
+
partial.atomics.push(a);
|
|
349
345
|
return partial;
|
|
350
346
|
})(),
|
|
351
|
-
explore,
|
|
347
|
+
props.explore,
|
|
352
348
|
),
|
|
353
349
|
value: () =>
|
|
354
|
-
|
|
350
|
+
decode_atomic({
|
|
351
|
+
...props,
|
|
352
|
+
type: a.type,
|
|
353
|
+
}),
|
|
355
354
|
});
|
|
356
355
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
{
|
|
378
|
-
...explore,
|
|
379
|
-
from: "array",
|
|
380
|
-
},
|
|
381
|
-
);
|
|
356
|
+
// TUPLES
|
|
357
|
+
for (const tuple of props.metadata.tuples)
|
|
358
|
+
unions.push({
|
|
359
|
+
type: "tuple",
|
|
360
|
+
is: () =>
|
|
361
|
+
IsProgrammer.decode(props.context)(props.importer)(
|
|
362
|
+
props.input,
|
|
363
|
+
(() => {
|
|
364
|
+
const partial = Metadata.initialize();
|
|
365
|
+
partial.tuples.push(tuple);
|
|
366
|
+
return partial;
|
|
367
|
+
})(),
|
|
368
|
+
props.explore,
|
|
369
|
+
),
|
|
370
|
+
value: () =>
|
|
371
|
+
decode_tuple({
|
|
372
|
+
...props,
|
|
373
|
+
tuple,
|
|
374
|
+
}),
|
|
375
|
+
});
|
|
382
376
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
377
|
+
// ARRAYS
|
|
378
|
+
if (props.metadata.arrays.length) {
|
|
379
|
+
const value: () => ts.Expression =
|
|
380
|
+
props.metadata.arrays.length === 1
|
|
381
|
+
? () =>
|
|
382
|
+
decode_array({
|
|
383
|
+
...props,
|
|
384
|
+
array: props.metadata.arrays[0]!,
|
|
385
|
+
explore: {
|
|
386
|
+
...props.explore,
|
|
387
|
+
from: "array",
|
|
388
|
+
},
|
|
389
|
+
})
|
|
390
|
+
: props.metadata.arrays.some((elem) => elem.type.value.any)
|
|
391
|
+
? () =>
|
|
392
|
+
ts.factory.createCallExpression(
|
|
393
|
+
ts.factory.createIdentifier("JSON.stringify"),
|
|
394
|
+
undefined,
|
|
395
|
+
[props.input],
|
|
396
|
+
)
|
|
397
|
+
: () =>
|
|
398
|
+
explore_arrays({
|
|
399
|
+
...props,
|
|
400
|
+
arrays: props.metadata.arrays,
|
|
401
|
+
explore: {
|
|
402
|
+
...props.explore,
|
|
403
|
+
from: "array",
|
|
404
|
+
},
|
|
405
|
+
});
|
|
389
406
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
value: () =>
|
|
397
|
-
AtomicPredicator.native(native)
|
|
398
|
-
? decode_atomic(project)(importer)(
|
|
399
|
-
input,
|
|
400
|
-
native.toLowerCase() as Atomic.Literal,
|
|
401
|
-
explore,
|
|
402
|
-
)
|
|
403
|
-
: ts.factory.createStringLiteral("{}"),
|
|
404
|
-
});
|
|
407
|
+
unions.push({
|
|
408
|
+
type: "array",
|
|
409
|
+
is: () => ExpressionFactory.isArray(props.input),
|
|
410
|
+
value,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
405
413
|
|
|
406
|
-
|
|
407
|
-
|
|
414
|
+
// BUILT-IN CLASSES
|
|
415
|
+
if (props.metadata.natives.length)
|
|
416
|
+
for (const native of props.metadata.natives)
|
|
408
417
|
unions.push({
|
|
409
418
|
type: "object",
|
|
410
|
-
is: () =>
|
|
411
|
-
value: () =>
|
|
419
|
+
is: () => check_native(native)(props.input),
|
|
420
|
+
value: () =>
|
|
421
|
+
AtomicPredicator.native(native)
|
|
422
|
+
? decode_atomic({
|
|
423
|
+
...props,
|
|
424
|
+
type: native.toLowerCase() as Atomic.Literal,
|
|
425
|
+
})
|
|
426
|
+
: ts.factory.createStringLiteral("{}"),
|
|
412
427
|
});
|
|
413
428
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
429
|
+
// SETS
|
|
430
|
+
if (props.metadata.sets.length)
|
|
431
|
+
unions.push({
|
|
432
|
+
type: "object",
|
|
433
|
+
is: () => ExpressionFactory.isInstanceOf("Set")(props.input),
|
|
434
|
+
value: () => ts.factory.createStringLiteral("{}"),
|
|
435
|
+
});
|
|
421
436
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
437
|
+
// MAPS
|
|
438
|
+
if (props.metadata.maps.length)
|
|
439
|
+
unions.push({
|
|
440
|
+
type: "object",
|
|
441
|
+
is: () => ExpressionFactory.isInstanceOf("Map")(props.input),
|
|
442
|
+
value: () => ts.factory.createStringLiteral("{}"),
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
// OBJECTS
|
|
446
|
+
if (props.metadata.objects.length)
|
|
447
|
+
unions.push({
|
|
448
|
+
type: "object",
|
|
449
|
+
is: () =>
|
|
450
|
+
ExpressionFactory.isObject({
|
|
451
|
+
checkNull: true,
|
|
452
|
+
checkArray: props.metadata.objects.some((objects) =>
|
|
453
|
+
objects.properties.every(
|
|
454
|
+
(prop) => !prop.key.isSoleLiteral() || !prop.value.isRequired(),
|
|
434
455
|
),
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
456
|
+
),
|
|
457
|
+
})(props.input),
|
|
458
|
+
value: () =>
|
|
459
|
+
explore_objects({
|
|
460
|
+
...props,
|
|
461
|
+
explore: {
|
|
462
|
+
...props.explore,
|
|
439
463
|
from: "object",
|
|
440
|
-
}
|
|
441
|
-
|
|
464
|
+
},
|
|
465
|
+
}),
|
|
466
|
+
});
|
|
442
467
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
468
|
+
//----
|
|
469
|
+
// RETURNS
|
|
470
|
+
//----
|
|
471
|
+
// CHECK NULL AND UNDEFINED
|
|
472
|
+
const wrapper = (output: ts.Expression) =>
|
|
473
|
+
wrap_required({
|
|
474
|
+
input: props.input,
|
|
475
|
+
metadata: props.metadata,
|
|
476
|
+
explore: props.explore,
|
|
477
|
+
expression: wrap_nullable({
|
|
478
|
+
input: props.input,
|
|
479
|
+
metadata: props.metadata,
|
|
480
|
+
expression: output,
|
|
481
|
+
}),
|
|
482
|
+
});
|
|
453
483
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
484
|
+
// DIRECT RETURN
|
|
485
|
+
if (unions.length === 0)
|
|
486
|
+
return ts.factory.createCallExpression(
|
|
487
|
+
ts.factory.createIdentifier("JSON.stringify"),
|
|
488
|
+
undefined,
|
|
489
|
+
[props.input],
|
|
490
|
+
);
|
|
491
|
+
else if (unions.length === 1) return wrapper(unions[0]!.value());
|
|
462
492
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
undefined,
|
|
471
|
-
undefined,
|
|
472
|
-
iterate({
|
|
473
|
-
importer,
|
|
474
|
-
input,
|
|
475
|
-
unions,
|
|
476
|
-
expected: metadata.getName(),
|
|
477
|
-
}),
|
|
478
|
-
),
|
|
493
|
+
// RETURN WITH TYPE CHECKING
|
|
494
|
+
return wrapper(
|
|
495
|
+
ts.factory.createCallExpression(
|
|
496
|
+
ts.factory.createArrowFunction(
|
|
497
|
+
undefined,
|
|
498
|
+
undefined,
|
|
499
|
+
[],
|
|
479
500
|
undefined,
|
|
480
501
|
undefined,
|
|
502
|
+
iterate({
|
|
503
|
+
importer: props.importer,
|
|
504
|
+
input: props.input,
|
|
505
|
+
expected: props.metadata.getName(),
|
|
506
|
+
unions,
|
|
507
|
+
}),
|
|
481
508
|
),
|
|
482
|
-
|
|
483
|
-
|
|
509
|
+
undefined,
|
|
510
|
+
undefined,
|
|
511
|
+
),
|
|
512
|
+
);
|
|
513
|
+
};
|
|
484
514
|
|
|
485
|
-
const decode_object = (
|
|
515
|
+
const decode_object = (props: {
|
|
516
|
+
importer: FunctionImporter;
|
|
517
|
+
input: ts.Expression;
|
|
518
|
+
object: MetadataObject;
|
|
519
|
+
explore: FeatureProgrammer.IExplore;
|
|
520
|
+
}): ts.CallExpression =>
|
|
486
521
|
FeatureProgrammer.decode_object({
|
|
487
522
|
trace: false,
|
|
488
523
|
path: false,
|
|
489
524
|
prefix: PREFIX,
|
|
490
|
-
})(importer);
|
|
491
|
-
|
|
492
|
-
const decode_array =
|
|
493
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
494
|
-
(importer: FunctionImporter) =>
|
|
495
|
-
(
|
|
496
|
-
input: ts.Expression,
|
|
497
|
-
array: MetadataArray,
|
|
498
|
-
explore: FeatureProgrammer.IExplore,
|
|
499
|
-
) =>
|
|
500
|
-
array.type.recursive
|
|
501
|
-
? ts.factory.createCallExpression(
|
|
502
|
-
ts.factory.createIdentifier(
|
|
503
|
-
importer.useLocal(`${config.prefix}a${array.type.index}`),
|
|
504
|
-
),
|
|
505
|
-
undefined,
|
|
506
|
-
FeatureProgrammer.argumentsArray(config)({
|
|
507
|
-
...explore,
|
|
508
|
-
source: "function",
|
|
509
|
-
from: "array",
|
|
510
|
-
})(input),
|
|
511
|
-
)
|
|
512
|
-
: decode_array_inline(config)(importer)(input, array, explore);
|
|
513
|
-
|
|
514
|
-
const decode_array_inline =
|
|
515
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
516
|
-
(importer: FunctionImporter) =>
|
|
517
|
-
(
|
|
518
|
-
input: ts.Expression,
|
|
519
|
-
array: MetadataArray,
|
|
520
|
-
explore: FeatureProgrammer.IExplore,
|
|
521
|
-
) =>
|
|
522
|
-
FeatureProgrammer.decode_array(config)(importer)(StringifyJoiner.array)(
|
|
523
|
-
input,
|
|
524
|
-
array,
|
|
525
|
-
explore,
|
|
526
|
-
);
|
|
525
|
+
})(props.importer)(props.input, props.object, props.explore);
|
|
527
526
|
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
importer.useLocal(`${config.prefix}t${tuple.type.index}`),
|
|
527
|
+
const decode_array = (props: {
|
|
528
|
+
config: FeatureProgrammer.IConfig;
|
|
529
|
+
importer: FunctionImporter;
|
|
530
|
+
input: ts.Expression;
|
|
531
|
+
array: MetadataArray;
|
|
532
|
+
explore: FeatureProgrammer.IExplore;
|
|
533
|
+
}) =>
|
|
534
|
+
props.array.type.recursive
|
|
535
|
+
? ts.factory.createCallExpression(
|
|
536
|
+
ts.factory.createIdentifier(
|
|
537
|
+
props.importer.useLocal(
|
|
538
|
+
`${props.config.prefix}a${props.array.type.index}`,
|
|
541
539
|
),
|
|
542
|
-
undefined,
|
|
543
|
-
FeatureProgrammer.argumentsArray(config)({
|
|
544
|
-
...explore,
|
|
545
|
-
source: "function",
|
|
546
|
-
})(input),
|
|
547
|
-
)
|
|
548
|
-
: decode_tuple_inline(project)(config)(importer)(
|
|
549
|
-
input,
|
|
550
|
-
tuple.type,
|
|
551
|
-
explore,
|
|
552
|
-
);
|
|
553
|
-
|
|
554
|
-
const decode_tuple_inline =
|
|
555
|
-
(project: ITypiaContext) =>
|
|
556
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
557
|
-
(importer: FunctionImporter) =>
|
|
558
|
-
(
|
|
559
|
-
input: ts.Expression,
|
|
560
|
-
tuple: MetadataTupleType,
|
|
561
|
-
explore: FeatureProgrammer.IExplore,
|
|
562
|
-
): ts.Expression => {
|
|
563
|
-
const elements: ts.Expression[] = tuple.elements
|
|
564
|
-
.filter((elem) => elem.rest === null)
|
|
565
|
-
.map((elem, index) =>
|
|
566
|
-
decode(project)(config)(importer)(
|
|
567
|
-
ts.factory.createElementAccessExpression(input, index),
|
|
568
|
-
elem,
|
|
569
|
-
{
|
|
570
|
-
...explore,
|
|
571
|
-
from: "array",
|
|
572
|
-
postfix: explore.postfix.length
|
|
573
|
-
? `${postfix_of_tuple(explore.postfix)}[${index}]"`
|
|
574
|
-
: `"[${index}]"`,
|
|
575
|
-
},
|
|
576
540
|
),
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
541
|
+
undefined,
|
|
542
|
+
FeatureProgrammer.argumentsArray(props.config)({
|
|
543
|
+
...props.explore,
|
|
544
|
+
source: "function",
|
|
545
|
+
from: "array",
|
|
546
|
+
})(props.input),
|
|
547
|
+
)
|
|
548
|
+
: decode_array_inline(props);
|
|
549
|
+
|
|
550
|
+
const decode_array_inline = (props: {
|
|
551
|
+
config: FeatureProgrammer.IConfig;
|
|
552
|
+
importer: FunctionImporter;
|
|
553
|
+
input: ts.Expression;
|
|
554
|
+
array: MetadataArray;
|
|
555
|
+
explore: FeatureProgrammer.IExplore;
|
|
556
|
+
}) =>
|
|
557
|
+
FeatureProgrammer.decode_array(props.config)(props.importer)(
|
|
558
|
+
StringifyJoiner.array,
|
|
559
|
+
)(props.input, props.array, props.explore);
|
|
560
|
+
|
|
561
|
+
const decode_tuple = (props: {
|
|
562
|
+
context: ITypiaContext;
|
|
563
|
+
config: FeatureProgrammer.IConfig;
|
|
564
|
+
importer: FunctionImporter;
|
|
565
|
+
input: ts.Expression;
|
|
566
|
+
tuple: MetadataTuple;
|
|
567
|
+
explore: FeatureProgrammer.IExplore;
|
|
568
|
+
}): ts.Expression =>
|
|
569
|
+
props.tuple.type.recursive
|
|
570
|
+
? ts.factory.createCallExpression(
|
|
571
|
+
ts.factory.createIdentifier(
|
|
572
|
+
props.importer.useLocal(
|
|
573
|
+
`${props.config.prefix}t${props.tuple.type.index}`,
|
|
574
|
+
),
|
|
588
575
|
),
|
|
589
|
-
|
|
590
|
-
{
|
|
591
|
-
...explore,
|
|
592
|
-
|
|
576
|
+
undefined,
|
|
577
|
+
FeatureProgrammer.argumentsArray(props.config)({
|
|
578
|
+
...props.explore,
|
|
579
|
+
source: "function",
|
|
580
|
+
})(props.input),
|
|
581
|
+
)
|
|
582
|
+
: decode_tuple_inline({
|
|
583
|
+
...props,
|
|
584
|
+
tuple: props.tuple.type,
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
const decode_tuple_inline = (props: {
|
|
588
|
+
context: ITypiaContext;
|
|
589
|
+
config: FeatureProgrammer.IConfig;
|
|
590
|
+
importer: FunctionImporter;
|
|
591
|
+
input: ts.Expression;
|
|
592
|
+
tuple: MetadataTupleType;
|
|
593
|
+
explore: FeatureProgrammer.IExplore;
|
|
594
|
+
}): ts.Expression => {
|
|
595
|
+
const elements: ts.Expression[] = props.tuple.elements
|
|
596
|
+
.filter((elem) => elem.rest === null)
|
|
597
|
+
.map((elem, index) =>
|
|
598
|
+
decode({
|
|
599
|
+
...props,
|
|
600
|
+
input: ts.factory.createElementAccessExpression(props.input, index),
|
|
601
|
+
metadata: elem,
|
|
602
|
+
explore: {
|
|
603
|
+
...props.explore,
|
|
604
|
+
from: "array",
|
|
605
|
+
postfix: props.explore.postfix.length
|
|
606
|
+
? `${postfix_of_tuple(props.explore.postfix)}[${index}]"`
|
|
607
|
+
: `"[${index}]"`,
|
|
593
608
|
},
|
|
594
|
-
)
|
|
595
|
-
|
|
596
|
-
|
|
609
|
+
}),
|
|
610
|
+
);
|
|
611
|
+
const rest = (() => {
|
|
612
|
+
if (props.tuple.elements.length === 0) return null;
|
|
613
|
+
const last = props.tuple.elements.at(-1)!;
|
|
614
|
+
if (last.rest === null) return null;
|
|
615
|
+
|
|
616
|
+
const code = decode({
|
|
617
|
+
...props,
|
|
618
|
+
input: ts.factory.createCallExpression(
|
|
619
|
+
IdentifierFactory.access(props.input)("slice"),
|
|
597
620
|
undefined,
|
|
598
|
-
[
|
|
599
|
-
)
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
621
|
+
[ExpressionFactory.number(props.tuple.elements.length - 1)],
|
|
622
|
+
),
|
|
623
|
+
metadata: wrap_metadata_rest_tuple(props.tuple.elements.at(-1)!.rest!),
|
|
624
|
+
explore: {
|
|
625
|
+
...props.explore,
|
|
626
|
+
start: props.tuple.elements.length - 1,
|
|
627
|
+
},
|
|
604
628
|
});
|
|
605
|
-
|
|
629
|
+
return ts.factory.createCallExpression(
|
|
630
|
+
props.importer.use("rest"),
|
|
631
|
+
undefined,
|
|
632
|
+
[code],
|
|
633
|
+
);
|
|
634
|
+
})();
|
|
635
|
+
return StringifyJoiner.tuple({
|
|
636
|
+
elements,
|
|
637
|
+
rest,
|
|
638
|
+
});
|
|
639
|
+
};
|
|
606
640
|
|
|
607
|
-
const decode_atomic =
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
641
|
+
const decode_atomic = (props: {
|
|
642
|
+
context: ITypiaContext;
|
|
643
|
+
importer: FunctionImporter;
|
|
644
|
+
input: ts.Expression;
|
|
645
|
+
type: string;
|
|
646
|
+
explore: FeatureProgrammer.IExplore;
|
|
647
|
+
}): ts.Expression => {
|
|
648
|
+
if (props.type === "string")
|
|
649
|
+
return ts.factory.createCallExpression(
|
|
650
|
+
props.importer.use("string"),
|
|
651
|
+
undefined,
|
|
652
|
+
[props.input],
|
|
653
|
+
);
|
|
654
|
+
else if (
|
|
655
|
+
props.type === "number" &&
|
|
656
|
+
OptionPredicator.numeric(props.context.options)
|
|
657
|
+
)
|
|
658
|
+
props = {
|
|
659
|
+
...props,
|
|
660
|
+
input: ts.factory.createCallExpression(
|
|
661
|
+
props.importer.use("number"),
|
|
662
|
+
undefined,
|
|
663
|
+
[props.input],
|
|
664
|
+
),
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
return props.explore.from !== "top"
|
|
668
|
+
? props.input
|
|
669
|
+
: ts.factory.createCallExpression(
|
|
670
|
+
IdentifierFactory.access(props.input)("toString"),
|
|
618
671
|
undefined,
|
|
619
|
-
[input],
|
|
620
|
-
);
|
|
621
|
-
else if (type === "number" && OptionPredicator.numeric(project.options))
|
|
622
|
-
input = ts.factory.createCallExpression(
|
|
623
|
-
importer.use("number"),
|
|
624
672
|
undefined,
|
|
625
|
-
[input],
|
|
626
673
|
);
|
|
674
|
+
};
|
|
627
675
|
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
)
|
|
645
|
-
|
|
646
|
-
return [
|
|
647
|
-
ts.factory.createStringLiteral('"'),
|
|
648
|
-
input,
|
|
649
|
-
ts.factory.createStringLiteral('"'),
|
|
650
|
-
].reduce((x, y) => ts.factory.createAdd(x, y));
|
|
651
|
-
else return decode_atomic(project)(importer)(input, "string", explore);
|
|
652
|
-
};
|
|
676
|
+
const decode_constant_string = (props: {
|
|
677
|
+
context: ITypiaContext;
|
|
678
|
+
importer: FunctionImporter;
|
|
679
|
+
input: ts.Expression;
|
|
680
|
+
values: string[];
|
|
681
|
+
explore: FeatureProgrammer.IExplore;
|
|
682
|
+
}): ts.Expression => {
|
|
683
|
+
if (props.values.every((v) => !StringifyPredicator.require_escape(v)))
|
|
684
|
+
return [
|
|
685
|
+
ts.factory.createStringLiteral('"'),
|
|
686
|
+
props.input,
|
|
687
|
+
ts.factory.createStringLiteral('"'),
|
|
688
|
+
].reduce((x, y) => ts.factory.createAdd(x, y));
|
|
689
|
+
return decode_atomic({
|
|
690
|
+
...props,
|
|
691
|
+
type: "string",
|
|
692
|
+
});
|
|
693
|
+
};
|
|
653
694
|
|
|
654
|
-
const decode_to_json =
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
);
|
|
672
|
-
};
|
|
695
|
+
const decode_to_json = (props: {
|
|
696
|
+
context: ITypiaContext;
|
|
697
|
+
config: FeatureProgrammer.IConfig;
|
|
698
|
+
importer: FunctionImporter;
|
|
699
|
+
input: ts.Expression;
|
|
700
|
+
metadata: Metadata;
|
|
701
|
+
explore: FeatureProgrammer.IExplore;
|
|
702
|
+
}): ts.Expression => {
|
|
703
|
+
return decode({
|
|
704
|
+
...props,
|
|
705
|
+
input: ts.factory.createCallExpression(
|
|
706
|
+
IdentifierFactory.access(props.input)("toJSON"),
|
|
707
|
+
undefined,
|
|
708
|
+
[],
|
|
709
|
+
),
|
|
710
|
+
});
|
|
711
|
+
};
|
|
673
712
|
|
|
674
713
|
const decode_functional = (explore: FeatureProgrammer.IExplore) =>
|
|
675
714
|
explore.from === "array"
|
|
@@ -679,137 +718,166 @@ export namespace JsonStringifyProgrammer {
|
|
|
679
718
|
/* -----------------------------------------------------------
|
|
680
719
|
EXPLORERS
|
|
681
720
|
----------------------------------------------------------- */
|
|
682
|
-
const explore_objects =
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
721
|
+
const explore_objects = (props: {
|
|
722
|
+
config: FeatureProgrammer.IConfig;
|
|
723
|
+
importer: FunctionImporter;
|
|
724
|
+
input: ts.Expression;
|
|
725
|
+
metadata: Metadata;
|
|
726
|
+
explore: FeatureProgrammer.IExplore;
|
|
727
|
+
}) =>
|
|
728
|
+
props.metadata.objects.length === 1
|
|
729
|
+
? decode_object({
|
|
730
|
+
importer: props.importer,
|
|
731
|
+
input: props.input,
|
|
732
|
+
object: props.metadata.objects[0]!,
|
|
733
|
+
explore: props.explore,
|
|
734
|
+
})
|
|
735
|
+
: ts.factory.createCallExpression(
|
|
736
|
+
ts.factory.createIdentifier(
|
|
737
|
+
props.importer.useLocal(`${PREFIX}u${props.metadata.union_index!}`),
|
|
738
|
+
),
|
|
739
|
+
undefined,
|
|
740
|
+
FeatureProgrammer.argumentsArray(props.config)(props.explore)(
|
|
741
|
+
props.input,
|
|
742
|
+
),
|
|
743
|
+
);
|
|
744
|
+
|
|
745
|
+
const explore_arrays = (props: {
|
|
746
|
+
context: ITypiaContext;
|
|
747
|
+
config: FeatureProgrammer.IConfig;
|
|
748
|
+
importer: FunctionImporter;
|
|
749
|
+
input: ts.Expression;
|
|
750
|
+
arrays: MetadataArray[];
|
|
751
|
+
explore: FeatureProgrammer.IExplore;
|
|
752
|
+
}): ts.Expression =>
|
|
753
|
+
explore_array_like_union_types({
|
|
754
|
+
...props,
|
|
755
|
+
elements: props.arrays,
|
|
756
|
+
factory: (next) =>
|
|
710
757
|
UnionExplorer.array({
|
|
711
|
-
checker: IsProgrammer.decode(
|
|
712
|
-
decoder:
|
|
758
|
+
checker: IsProgrammer.decode(props.context)(props.importer),
|
|
759
|
+
decoder: (input, array, explore) =>
|
|
760
|
+
decode_array({
|
|
761
|
+
config: props.config,
|
|
762
|
+
importer: props.importer,
|
|
763
|
+
input,
|
|
764
|
+
array,
|
|
765
|
+
explore,
|
|
766
|
+
}),
|
|
713
767
|
empty: ts.factory.createStringLiteral("[]"),
|
|
714
768
|
success: ts.factory.createTrue(),
|
|
715
769
|
failure: (input, expected) =>
|
|
716
|
-
create_throw_error({
|
|
770
|
+
create_throw_error({
|
|
771
|
+
importer: props.importer,
|
|
772
|
+
expected,
|
|
773
|
+
input,
|
|
774
|
+
}),
|
|
775
|
+
})(next.parameters)(next.input, next.elements, next.explore),
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
const explore_array_like_union_types = <
|
|
779
|
+
T extends MetadataArray | MetadataTuple,
|
|
780
|
+
>(props: {
|
|
781
|
+
config: FeatureProgrammer.IConfig;
|
|
782
|
+
importer: FunctionImporter;
|
|
783
|
+
factory: (next: {
|
|
784
|
+
parameters: ts.ParameterDeclaration[];
|
|
785
|
+
input: ts.Expression;
|
|
786
|
+
elements: T[];
|
|
787
|
+
explore: FeatureProgrammer.IExplore;
|
|
788
|
+
}) => ts.ArrowFunction;
|
|
789
|
+
input: ts.Expression;
|
|
790
|
+
elements: T[];
|
|
791
|
+
explore: FeatureProgrammer.IExplore;
|
|
792
|
+
}): ts.Expression => {
|
|
793
|
+
const arrow = (next: {
|
|
794
|
+
parameters: ts.ParameterDeclaration[];
|
|
795
|
+
explore: FeatureProgrammer.IExplore;
|
|
796
|
+
input: ts.Expression;
|
|
797
|
+
}): ts.ArrowFunction =>
|
|
798
|
+
props.factory({
|
|
799
|
+
elements: props.elements,
|
|
800
|
+
parameters: next.parameters,
|
|
801
|
+
input: next.input,
|
|
802
|
+
explore: next.explore,
|
|
803
|
+
});
|
|
804
|
+
if (props.elements.every((e) => e.type.recursive === false))
|
|
805
|
+
ts.factory.createCallExpression(
|
|
806
|
+
arrow({
|
|
807
|
+
parameters: [],
|
|
808
|
+
explore: props.explore,
|
|
809
|
+
input: props.input,
|
|
717
810
|
}),
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
(config: FeatureProgrammer.IConfig) =>
|
|
722
|
-
(importer: FunctionImporter) =>
|
|
723
|
-
<T extends MetadataArray | MetadataTuple>(
|
|
724
|
-
factory: (
|
|
725
|
-
parameters: ts.ParameterDeclaration[],
|
|
726
|
-
) => (
|
|
727
|
-
input: ts.Expression,
|
|
728
|
-
elements: T[],
|
|
729
|
-
explore: FeatureProgrammer.IExplore,
|
|
730
|
-
) => ts.ArrowFunction,
|
|
731
|
-
) =>
|
|
732
|
-
(
|
|
733
|
-
input: ts.Expression,
|
|
734
|
-
elements: T[],
|
|
735
|
-
explore: FeatureProgrammer.IExplore,
|
|
736
|
-
): ts.Expression => {
|
|
737
|
-
const arrow =
|
|
738
|
-
(parameters: ts.ParameterDeclaration[]) =>
|
|
739
|
-
(explore: FeatureProgrammer.IExplore) =>
|
|
740
|
-
(input: ts.Expression): ts.ArrowFunction =>
|
|
741
|
-
factory(parameters)(input, elements, explore);
|
|
742
|
-
if (elements.every((e) => e.type.recursive === false))
|
|
743
|
-
ts.factory.createCallExpression(
|
|
744
|
-
arrow([])(explore)(input),
|
|
745
|
-
undefined,
|
|
746
|
-
[],
|
|
747
|
-
);
|
|
811
|
+
undefined,
|
|
812
|
+
[],
|
|
813
|
+
);
|
|
748
814
|
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
815
|
+
const explore: FeatureProgrammer.IExplore = {
|
|
816
|
+
...props.explore,
|
|
817
|
+
source: "function",
|
|
818
|
+
from: "array",
|
|
819
|
+
};
|
|
820
|
+
return ts.factory.createCallExpression(
|
|
821
|
+
ts.factory.createIdentifier(
|
|
822
|
+
props.importer.emplaceUnion(
|
|
823
|
+
props.config.prefix,
|
|
824
|
+
props.elements.map((e) => e.type.name).join(" | "),
|
|
825
|
+
() =>
|
|
826
|
+
arrow({
|
|
827
|
+
parameters: FeatureProgrammer.parameterDeclarations(props.config)(
|
|
828
|
+
TypeFactory.keyword("any"),
|
|
829
|
+
)(ts.factory.createIdentifier("input")),
|
|
830
|
+
explore: {
|
|
765
831
|
...explore,
|
|
766
832
|
postfix: "",
|
|
767
|
-
}
|
|
768
|
-
|
|
833
|
+
},
|
|
834
|
+
input: ts.factory.createIdentifier("input"),
|
|
835
|
+
}),
|
|
769
836
|
),
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
)
|
|
773
|
-
|
|
837
|
+
),
|
|
838
|
+
undefined,
|
|
839
|
+
FeatureProgrammer.argumentsArray(props.config)(explore)(props.input),
|
|
840
|
+
);
|
|
841
|
+
};
|
|
774
842
|
|
|
775
843
|
/* -----------------------------------------------------------
|
|
776
844
|
RETURN SCRIPTS
|
|
777
845
|
----------------------------------------------------------- */
|
|
778
|
-
const wrap_required = (
|
|
779
|
-
input: ts.Expression
|
|
780
|
-
|
|
781
|
-
explore: FeatureProgrammer.IExplore
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
846
|
+
const wrap_required = (props: {
|
|
847
|
+
input: ts.Expression;
|
|
848
|
+
metadata: Metadata;
|
|
849
|
+
explore: FeatureProgrammer.IExplore;
|
|
850
|
+
expression: ts.Expression;
|
|
851
|
+
}): ts.Expression => {
|
|
852
|
+
if (props.metadata.isRequired() === true && props.metadata.any === false)
|
|
853
|
+
return props.expression;
|
|
854
|
+
return ts.factory.createConditionalExpression(
|
|
855
|
+
ts.factory.createStrictInequality(
|
|
856
|
+
ts.factory.createIdentifier("undefined"),
|
|
857
|
+
props.input,
|
|
858
|
+
),
|
|
859
|
+
undefined,
|
|
860
|
+
props.expression,
|
|
861
|
+
undefined,
|
|
862
|
+
props.explore.from === "array"
|
|
863
|
+
? ts.factory.createStringLiteral("null")
|
|
864
|
+
: ts.factory.createIdentifier("undefined"),
|
|
865
|
+
);
|
|
798
866
|
};
|
|
799
867
|
|
|
800
|
-
const wrap_nullable = (
|
|
801
|
-
input: ts.Expression
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
868
|
+
const wrap_nullable = (props: {
|
|
869
|
+
input: ts.Expression;
|
|
870
|
+
metadata: Metadata;
|
|
871
|
+
expression: ts.Expression;
|
|
872
|
+
}): ts.Expression => {
|
|
873
|
+
if (props.metadata.nullable === false) return props.expression;
|
|
874
|
+
return ts.factory.createConditionalExpression(
|
|
875
|
+
ts.factory.createStrictInequality(ts.factory.createNull(), props.input),
|
|
876
|
+
undefined,
|
|
877
|
+
props.expression,
|
|
878
|
+
undefined,
|
|
879
|
+
ts.factory.createStringLiteral("null"),
|
|
880
|
+
);
|
|
813
881
|
};
|
|
814
882
|
|
|
815
883
|
const wrap_functional = (props: {
|
|
@@ -855,52 +923,93 @@ export namespace JsonStringifyProgrammer {
|
|
|
855
923
|
----------------------------------------------------------- */
|
|
856
924
|
const PREFIX = "$s";
|
|
857
925
|
|
|
858
|
-
const configure =
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
),
|
|
867
|
-
output: () => TypeFactory.keyword("string"),
|
|
868
|
-
},
|
|
869
|
-
prefix: PREFIX,
|
|
870
|
-
trace: false,
|
|
871
|
-
path: false,
|
|
872
|
-
initializer,
|
|
873
|
-
decoder: () => decode(project)(config)(importer),
|
|
874
|
-
objector: {
|
|
875
|
-
checker: () => IsProgrammer.decode(project)(importer),
|
|
876
|
-
decoder: () => decode_object(importer),
|
|
877
|
-
joiner: (props) =>
|
|
878
|
-
StringifyJoiner.object({
|
|
879
|
-
...props,
|
|
880
|
-
importer,
|
|
881
|
-
}),
|
|
882
|
-
unionizer: decode_union_object(
|
|
883
|
-
IsProgrammer.decode_object(project)(importer),
|
|
884
|
-
)(decode_object(importer))((exp) => exp)((input, expected) =>
|
|
885
|
-
create_throw_error({ importer, expected, input }),
|
|
926
|
+
const configure = (props: {
|
|
927
|
+
context: ITypiaContext;
|
|
928
|
+
importer: FunctionImporter;
|
|
929
|
+
}): FeatureProgrammer.IConfig => {
|
|
930
|
+
const config: FeatureProgrammer.IConfig<ts.Expression> = {
|
|
931
|
+
types: {
|
|
932
|
+
input: (type, name) =>
|
|
933
|
+
ts.factory.createTypeReferenceNode(
|
|
934
|
+
name ?? TypeFactory.getFullName(props.context.checker)(type),
|
|
886
935
|
),
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
936
|
+
output: () => TypeFactory.keyword("string"),
|
|
937
|
+
},
|
|
938
|
+
prefix: PREFIX,
|
|
939
|
+
trace: false,
|
|
940
|
+
path: false,
|
|
941
|
+
initializer,
|
|
942
|
+
decoder: () => (input, metadata, explore) =>
|
|
943
|
+
decode({
|
|
944
|
+
context: props.context,
|
|
945
|
+
importer: props.importer,
|
|
946
|
+
config,
|
|
947
|
+
input,
|
|
948
|
+
metadata,
|
|
949
|
+
explore,
|
|
950
|
+
}),
|
|
951
|
+
objector: {
|
|
952
|
+
checker: () => IsProgrammer.decode(props.context)(props.importer),
|
|
953
|
+
decoder: () => (input, object, explore) =>
|
|
954
|
+
decode_object({
|
|
955
|
+
importer: props.importer,
|
|
956
|
+
input,
|
|
957
|
+
object,
|
|
958
|
+
explore,
|
|
959
|
+
}),
|
|
960
|
+
joiner: (next) =>
|
|
961
|
+
StringifyJoiner.object({
|
|
962
|
+
...next,
|
|
963
|
+
importer: props.importer,
|
|
964
|
+
}),
|
|
965
|
+
unionizer: decode_union_object(
|
|
966
|
+
IsProgrammer.decode_object(props.context)(props.importer),
|
|
967
|
+
)((input, object, explore) =>
|
|
968
|
+
decode_object({
|
|
969
|
+
importer: props.importer,
|
|
970
|
+
input,
|
|
971
|
+
object,
|
|
972
|
+
explore,
|
|
973
|
+
}),
|
|
974
|
+
)((exp) => exp)((input, expected) =>
|
|
975
|
+
create_throw_error({
|
|
976
|
+
importer: props.importer,
|
|
977
|
+
expected,
|
|
978
|
+
input,
|
|
979
|
+
}),
|
|
980
|
+
),
|
|
981
|
+
failure: (input, expected) =>
|
|
982
|
+
create_throw_error({
|
|
983
|
+
importer: props.importer,
|
|
984
|
+
expected,
|
|
985
|
+
input,
|
|
986
|
+
}),
|
|
987
|
+
},
|
|
988
|
+
generator: {
|
|
989
|
+
arrays: () => (collection) =>
|
|
990
|
+
write_array_functions({
|
|
991
|
+
config,
|
|
992
|
+
importer: props.importer,
|
|
993
|
+
collection,
|
|
994
|
+
}),
|
|
995
|
+
tuples: () => (collection) =>
|
|
996
|
+
write_tuple_functions({
|
|
997
|
+
config,
|
|
998
|
+
context: props.context,
|
|
999
|
+
importer: props.importer,
|
|
1000
|
+
collection,
|
|
1001
|
+
}),
|
|
1002
|
+
},
|
|
896
1003
|
};
|
|
1004
|
+
return config;
|
|
1005
|
+
};
|
|
897
1006
|
|
|
898
1007
|
const initializer: FeatureProgrammer.IConfig["initializer"] =
|
|
899
|
-
(
|
|
1008
|
+
(context) => (importer) => (type) =>
|
|
900
1009
|
JsonMetadataFactory.analyze({
|
|
901
1010
|
method: `typia.json.${importer.method}`,
|
|
902
|
-
checker:
|
|
903
|
-
transformer:
|
|
1011
|
+
checker: context.checker,
|
|
1012
|
+
transformer: context.transformer,
|
|
904
1013
|
type,
|
|
905
1014
|
});
|
|
906
1015
|
|