typia 5.0.4 → 5.0.5-dev.20230921
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/IRandomGenerator.ts +35 -35
- package/src/IValidation.ts +21 -21
- package/src/Primitive.ts +135 -135
- package/src/Resolved.ts +116 -116
- package/src/TypeGuardError.ts +36 -36
- package/src/executable/TypiaGenerateWizard.ts +85 -85
- package/src/executable/TypiaSetupWizard.ts +153 -153
- package/src/executable/setup/ArgumentParser.ts +45 -45
- package/src/executable/setup/CommandExecutor.ts +8 -8
- package/src/executable/setup/FileRetriever.ts +22 -22
- package/src/executable/setup/PackageManager.ts +71 -71
- package/src/executable/setup/PluginConfigurator.ts +70 -70
- package/src/executable/typia.ts +52 -52
- package/src/factories/CommentFactory.ts +84 -84
- package/src/factories/ExpressionFactory.ts +159 -159
- package/src/factories/IdentifierFactory.ts +74 -74
- package/src/factories/JsonMetadataFactory.ts +43 -43
- package/src/factories/LiteralFactory.ts +47 -47
- package/src/factories/MetadataCollection.ts +269 -269
- package/src/factories/MetadataCommentTagFactory.ts +629 -629
- package/src/factories/MetadataFactory.ts +214 -214
- package/src/factories/MetadataTypeTagFactory.ts +313 -313
- package/src/factories/NumericRangeFactory.ts +33 -33
- package/src/factories/ProtobufFactory.ts +272 -272
- package/src/factories/StatementFactory.ts +72 -72
- package/src/factories/TemplateFactory.ts +58 -58
- package/src/factories/TypeFactory.ts +119 -119
- package/src/factories/ValueFactory.ts +12 -12
- package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
- package/src/factories/internal/metadata/emend_metadata_atomics.ts +40 -40
- package/src/factories/internal/metadata/emplace_metadata_alias.ts +41 -41
- package/src/factories/internal/metadata/emplace_metadata_array_type.ts +41 -41
- package/src/factories/internal/metadata/emplace_metadata_object.ts +150 -150
- package/src/factories/internal/metadata/emplace_metadata_tuple.ts +60 -60
- package/src/factories/internal/metadata/explore_metadata.ts +32 -32
- package/src/factories/internal/metadata/iterate_metadata.ts +101 -101
- package/src/factories/internal/metadata/iterate_metadata_alias.ts +35 -35
- package/src/factories/internal/metadata/iterate_metadata_array.ts +37 -37
- package/src/factories/internal/metadata/iterate_metadata_atomic.ts +62 -62
- package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
- package/src/factories/internal/metadata/iterate_metadata_collection.ts +141 -141
- package/src/factories/internal/metadata/iterate_metadata_comment_tags.ts +26 -26
- package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
- package/src/factories/internal/metadata/iterate_metadata_intersection.ts +197 -197
- package/src/factories/internal/metadata/iterate_metadata_map.ts +57 -57
- package/src/factories/internal/metadata/iterate_metadata_native.ts +210 -210
- package/src/factories/internal/metadata/iterate_metadata_object.ts +44 -44
- package/src/factories/internal/metadata/iterate_metadata_resolve.ts +52 -52
- package/src/factories/internal/metadata/iterate_metadata_set.ts +42 -42
- package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
- package/src/factories/internal/metadata/iterate_metadata_template.ts +47 -47
- package/src/factories/internal/metadata/iterate_metadata_tuple.ts +37 -37
- package/src/factories/internal/metadata/iterate_metadata_union.ts +27 -27
- package/src/functional/$ProtobufReader.ts +201 -201
- package/src/functional/$ProtobufSizer.ts +147 -147
- package/src/functional/$ProtobufWriter.ts +151 -151
- package/src/functional/$any.ts +4 -4
- package/src/functional/$clone.ts +4 -4
- package/src/functional/$dictionary.ts +25 -25
- package/src/functional/$every.ts +11 -11
- package/src/functional/$guard.ts +35 -35
- package/src/functional/$is_between.ts +2 -2
- package/src/functional/$join.ts +46 -46
- package/src/functional/$number.ts +13 -13
- package/src/functional/$report.ts +15 -15
- package/src/functional/$rest.ts +3 -3
- package/src/functional/$string.ts +50 -50
- package/src/functional/$strlen.ts +7 -7
- package/src/functional/$tail.ts +5 -5
- package/src/functional/$varint.ts +130 -130
- package/src/functional/$zigzag.ts +39 -39
- package/src/functional/IProtobufWriter.ts +18 -18
- package/src/functional/Namespace.ts +133 -133
- package/src/index.ts +4 -4
- package/src/json.ts +648 -648
- package/src/misc.ts +651 -651
- package/src/module.ts +656 -656
- package/src/programmers/AssertProgrammer.ts +317 -317
- package/src/programmers/CheckerProgrammer.ts +1137 -1137
- package/src/programmers/FeatureProgrammer.ts +478 -478
- package/src/programmers/IsProgrammer.ts +252 -252
- package/src/programmers/RandomProgrammer.ts +878 -878
- package/src/programmers/TypiaProgrammer.ts +163 -163
- package/src/programmers/ValidateProgrammer.ts +346 -346
- package/src/programmers/helpers/AtomicPredicator.ts +31 -31
- package/src/programmers/helpers/CloneJoiner.ts +144 -144
- package/src/programmers/helpers/FunctionImporeter.ts +91 -91
- package/src/programmers/helpers/ICheckEntry.ts +13 -13
- package/src/programmers/helpers/IExpressionEntry.ts +12 -12
- package/src/programmers/helpers/OptionPredicator.ts +15 -15
- package/src/programmers/helpers/ProtobufUtil.ts +125 -125
- package/src/programmers/helpers/ProtobufWire.ts +34 -34
- package/src/programmers/helpers/PruneJoiner.ts +143 -143
- package/src/programmers/helpers/RandomJoiner.ts +151 -151
- package/src/programmers/helpers/RandomRanger.ts +173 -173
- package/src/programmers/helpers/StringifyJoinder.ts +113 -113
- package/src/programmers/helpers/StringifyPredicator.ts +12 -12
- package/src/programmers/helpers/UnionExplorer.ts +305 -305
- package/src/programmers/helpers/UnionPredicator.ts +81 -81
- package/src/programmers/helpers/disable_function_importer_declare.ts +32 -32
- package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
- package/src/programmers/internal/application_alias.ts +65 -65
- package/src/programmers/internal/application_array.ts +53 -53
- package/src/programmers/internal/application_boolean.ts +15 -15
- package/src/programmers/internal/application_constant.ts +26 -26
- package/src/programmers/internal/application_default.ts +17 -17
- package/src/programmers/internal/application_default_string.ts +37 -37
- package/src/programmers/internal/application_escaped.ts +55 -55
- package/src/programmers/internal/application_native.ts +39 -39
- package/src/programmers/internal/application_number.ts +88 -88
- package/src/programmers/internal/application_object.ts +162 -162
- package/src/programmers/internal/application_schema.ts +175 -175
- package/src/programmers/internal/application_string.ts +51 -51
- package/src/programmers/internal/application_templates.ts +25 -25
- package/src/programmers/internal/application_tuple.ts +58 -58
- package/src/programmers/internal/check_array_length.ts +41 -41
- package/src/programmers/internal/check_bigint.ts +45 -45
- package/src/programmers/internal/check_dynamic_properties.ts +194 -194
- package/src/programmers/internal/check_everything.ts +28 -28
- package/src/programmers/internal/check_native.ts +21 -21
- package/src/programmers/internal/check_number.ts +105 -105
- package/src/programmers/internal/check_object.ts +55 -55
- package/src/programmers/internal/check_string.ts +45 -45
- package/src/programmers/internal/check_template.ts +61 -61
- package/src/programmers/internal/check_union_array_like.ts +323 -323
- package/src/programmers/internal/decode_union_object.ts +84 -84
- package/src/programmers/internal/feature_object_entries.ts +58 -58
- package/src/programmers/internal/metadata_to_pattern.ts +34 -34
- package/src/programmers/internal/prune_object_properties.ts +60 -60
- package/src/programmers/internal/random_custom.ts +37 -37
- package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
- package/src/programmers/internal/stringify_native.ts +7 -7
- package/src/programmers/internal/stringify_regular_properties.ts +83 -83
- package/src/programmers/internal/template_to_pattern.ts +15 -15
- package/src/programmers/internal/wrap_metadata_rest_tuple.ts +20 -20
- package/src/programmers/json/JsonApplicationProgrammer.ts +50 -50
- package/src/programmers/json/JsonAssertParseProgrammer.ts +71 -71
- package/src/programmers/json/JsonAssertStringifyProgrammer.ts +66 -66
- package/src/programmers/json/JsonIsParseProgrammer.ts +77 -77
- package/src/programmers/json/JsonIsStringifyProgrammer.ts +76 -76
- package/src/programmers/json/JsonStringifyProgrammer.ts +964 -964
- package/src/programmers/json/JsonValidateParseProgrammer.ts +68 -68
- package/src/programmers/json/JsonValidateStringifyProgrammer.ts +84 -84
- package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
- package/src/programmers/misc/MiscAssertPruneProgrammer.ts +68 -68
- package/src/programmers/misc/MiscCloneProgrammer.ts +774 -774
- package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
- package/src/programmers/misc/MiscIsPruneProgrammer.ts +73 -73
- package/src/programmers/misc/MiscLiteralsProgrammer.ts +69 -69
- package/src/programmers/misc/MiscPruneProgrammer.ts +550 -550
- package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
- package/src/programmers/misc/MiscValidatePruneProgrammer.ts +78 -78
- package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +75 -75
- package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +66 -66
- package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +655 -655
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +883 -883
- package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +85 -85
- package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +76 -76
- package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +165 -165
- package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +75 -75
- package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +86 -86
- package/src/protobuf.ts +881 -881
- package/src/schemas/json/IJsonApplication.ts +8 -8
- package/src/schemas/json/IJsonComponents.ts +33 -33
- package/src/schemas/json/IJsonSchema.ts +110 -110
- package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
- package/src/schemas/metadata/IMetadata.ts +34 -34
- package/src/schemas/metadata/IMetadataAlias.ts +12 -12
- package/src/schemas/metadata/IMetadataApplication.ts +7 -7
- package/src/schemas/metadata/IMetadataArray.ts +7 -7
- package/src/schemas/metadata/IMetadataArrayType.ts +10 -10
- package/src/schemas/metadata/IMetadataAtomic.ts +6 -6
- package/src/schemas/metadata/IMetadataCollection.ts +11 -11
- package/src/schemas/metadata/IMetadataConstant.ts +16 -16
- package/src/schemas/metadata/IMetadataDictionary.ts +14 -14
- package/src/schemas/metadata/IMetadataEntry.ts +6 -6
- package/src/schemas/metadata/IMetadataEscaped.ts +6 -6
- package/src/schemas/metadata/IMetadataObject.ts +18 -18
- package/src/schemas/metadata/IMetadataProperty.ts +9 -9
- package/src/schemas/metadata/IMetadataTuple.ts +7 -7
- package/src/schemas/metadata/IMetadataTupleType.ts +10 -10
- package/src/schemas/metadata/IMetadataTypeTag.ts +8 -8
- package/src/schemas/metadata/Metadata.ts +685 -685
- package/src/schemas/metadata/MetadataAlias.ts +61 -61
- package/src/schemas/metadata/MetadataArray.ts +49 -49
- package/src/schemas/metadata/MetadataArrayType.ts +57 -57
- package/src/schemas/metadata/MetadataAtomic.ts +42 -42
- package/src/schemas/metadata/MetadataConstant.ts +3 -3
- package/src/schemas/metadata/MetadataEscaped.ts +51 -51
- package/src/schemas/metadata/MetadataObject.ts +139 -139
- package/src/schemas/metadata/MetadataProperty.ts +59 -59
- package/src/schemas/metadata/MetadataTuple.ts +32 -32
- package/src/schemas/metadata/MetadataTupleType.ts +67 -67
- package/src/tags/ExclusiveMaximum.ts +13 -13
- package/src/tags/ExclusiveMinimum.ts +13 -13
- package/src/tags/Format.ts +30 -30
- package/src/tags/MaxItems.ts +9 -9
- package/src/tags/MaxLength.ts +9 -9
- package/src/tags/Maximum.ts +13 -13
- package/src/tags/MinItems.ts +9 -9
- package/src/tags/MinLength.ts +9 -9
- package/src/tags/Minimum.ts +13 -13
- package/src/tags/MultipleOf.ts +15 -15
- package/src/tags/Pattern.ts +8 -8
- package/src/tags/TagBase.ts +68 -68
- package/src/tags/Type.ts +27 -27
- package/src/tags/index.ts +13 -13
- package/src/transform.ts +35 -35
- package/src/transformers/CallExpressionTransformer.ts +243 -243
- package/src/transformers/FileTransformer.ts +57 -57
- package/src/transformers/IProject.ts +15 -15
- package/src/transformers/ITransformOptions.ts +62 -62
- package/src/transformers/ImportTransformer.ts +66 -66
- package/src/transformers/NodeTransformer.ts +13 -13
- package/src/transformers/TransformerError.ts +55 -55
- package/src/transformers/features/AssertTransformer.ts +11 -11
- package/src/transformers/features/CreateAssertTransformer.ts +13 -13
- package/src/transformers/features/CreateIsTransformer.ts +11 -11
- package/src/transformers/features/CreateRandomTransformer.ts +43 -43
- package/src/transformers/features/CreateValidateTransformer.ts +13 -13
- package/src/transformers/features/IsTransformer.ts +11 -11
- package/src/transformers/features/RandomTransformer.ts +46 -46
- package/src/transformers/features/ValidateTransformer.ts +11 -11
- package/src/transformers/features/json/JsonApplicationTransformer.ts +118 -118
- package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
- package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
- package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
- package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +10 -10
- package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
- package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
- package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
- package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
- package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
- package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
- package/src/transformers/features/misc/MetadataTransformer.ts +61 -61
- package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscLiteralsTransformer.ts +32 -32
- package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
- package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +9 -9
- package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +9 -9
- package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +10 -10
- package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +33 -33
- package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +12 -12
- package/src/transformers/internal/GenericTransformer.ts +104 -104
- package/src/typings/Atomic.ts +18 -18
- package/src/typings/ClassProperties.ts +5 -5
- package/src/typings/Customizable.ts +5 -5
- package/src/typings/OmitNever.ts +3 -3
- package/src/typings/ProtobufAtomic.ts +19 -19
- package/src/typings/SpecialFields.ts +3 -3
- package/src/typings/ValidationPipe.ts +9 -9
- package/src/typings/Writable.ts +11 -11
- package/src/utils/ArrayUtil.ts +45 -45
- package/src/utils/Escaper.ts +46 -46
- package/src/utils/MapUtil.ts +12 -12
- package/src/utils/NameEncoder.ts +32 -32
- package/src/utils/PatternUtil.ts +33 -33
- package/src/utils/RandomGenerator.ts +83 -83
- package/src/utils/Singleton.ts +17 -17
|
@@ -1,478 +1,478 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { IdentifierFactory } from "../factories/IdentifierFactory";
|
|
4
|
-
import { MetadataCollection } from "../factories/MetadataCollection";
|
|
5
|
-
import { StatementFactory } from "../factories/StatementFactory";
|
|
6
|
-
import { TypeFactory } from "../factories/TypeFactory";
|
|
7
|
-
import { ValueFactory } from "../factories/ValueFactory";
|
|
8
|
-
|
|
9
|
-
import { Metadata } from "../schemas/metadata/Metadata";
|
|
10
|
-
import { MetadataArray } from "../schemas/metadata/MetadataArray";
|
|
11
|
-
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
12
|
-
|
|
13
|
-
import { IProject } from "../transformers/IProject";
|
|
14
|
-
|
|
15
|
-
import { CheckerProgrammer } from "./CheckerProgrammer";
|
|
16
|
-
import { FunctionImporter } from "./helpers/FunctionImporeter";
|
|
17
|
-
import { IExpressionEntry } from "./helpers/IExpressionEntry";
|
|
18
|
-
import { UnionExplorer } from "./helpers/UnionExplorer";
|
|
19
|
-
import { feature_object_entries } from "./internal/feature_object_entries";
|
|
20
|
-
|
|
21
|
-
export namespace FeatureProgrammer {
|
|
22
|
-
/* -----------------------------------------------------------
|
|
23
|
-
PARAMETERS
|
|
24
|
-
----------------------------------------------------------- */
|
|
25
|
-
export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
|
|
26
|
-
types: IConfig.ITypes;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Prefix name of internal functions for specific types.
|
|
30
|
-
*/
|
|
31
|
-
prefix: string;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Whether to archive access path or not.
|
|
35
|
-
*/
|
|
36
|
-
path: boolean;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Whether to trace exception or not.
|
|
40
|
-
*/
|
|
41
|
-
trace: boolean;
|
|
42
|
-
|
|
43
|
-
addition?(collection: MetadataCollection): ts.Statement[];
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Initializer of metadata.
|
|
47
|
-
*/
|
|
48
|
-
initializer: (
|
|
49
|
-
project: IProject,
|
|
50
|
-
) => (
|
|
51
|
-
importer: FunctionImporter,
|
|
52
|
-
) => (type: ts.Type) => [MetadataCollection, Metadata];
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Decoder, station of every types.
|
|
56
|
-
*/
|
|
57
|
-
decoder: () => Decoder<Metadata, Output>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Object configurator.
|
|
61
|
-
*/
|
|
62
|
-
objector: IConfig.IObjector<Output>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Generator of functions for object types.
|
|
66
|
-
*/
|
|
67
|
-
generator: IConfig.IGenerator;
|
|
68
|
-
}
|
|
69
|
-
export namespace IConfig {
|
|
70
|
-
export interface ITypes {
|
|
71
|
-
input: (type: ts.Type, name?: string) => ts.TypeNode;
|
|
72
|
-
output: (type: ts.Type, name?: string) => ts.TypeNode;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface IObjector<
|
|
76
|
-
Output extends ts.ConciseBody = ts.ConciseBody,
|
|
77
|
-
> {
|
|
78
|
-
/**
|
|
79
|
-
* Type checker when union object type comes.
|
|
80
|
-
*/
|
|
81
|
-
checker: () => Decoder<Metadata, ts.Expression>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Decoder, function call expression generator of specific typed objects.
|
|
85
|
-
*/
|
|
86
|
-
decoder: () => Decoder<MetadataObject, ts.Expression>;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Joiner of expressions from properties.
|
|
90
|
-
*/
|
|
91
|
-
joiner(
|
|
92
|
-
input: ts.Expression,
|
|
93
|
-
entries: IExpressionEntry<Output>[],
|
|
94
|
-
parent: MetadataObject,
|
|
95
|
-
): ts.ConciseBody;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Union type specificator.
|
|
99
|
-
*
|
|
100
|
-
* Expression of an algorithm specifying object type and calling
|
|
101
|
-
* the `decoder` function of the specified object type.
|
|
102
|
-
*/
|
|
103
|
-
unionizer: Decoder<MetadataObject[], ts.Expression>;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Handler of union type specification failure.
|
|
107
|
-
*
|
|
108
|
-
* @param value Expression of input parameter
|
|
109
|
-
* @param expected Expected type name
|
|
110
|
-
* @param explore Exploration info
|
|
111
|
-
* @returns Statement of failure
|
|
112
|
-
*/
|
|
113
|
-
failure(
|
|
114
|
-
value: ts.Expression,
|
|
115
|
-
expected: string,
|
|
116
|
-
explore?: IExplore,
|
|
117
|
-
): ts.Statement;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Transformer of type checking expression by discrimination.
|
|
121
|
-
*
|
|
122
|
-
* When an object type has been specified by a discrimination without full
|
|
123
|
-
* iteration, the `unionizer` will decode the object instance after
|
|
124
|
-
* the last type checking.
|
|
125
|
-
*
|
|
126
|
-
* In such circumtance, you can transform the last type checking function.
|
|
127
|
-
*
|
|
128
|
-
* @param exp Current expression about type checking
|
|
129
|
-
* @returns Transformed expression
|
|
130
|
-
* @deprecated
|
|
131
|
-
*/
|
|
132
|
-
is?(exp: ts.Expression): ts.Expression;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Transformer of non-undefined type checking by discrimination.
|
|
136
|
-
*
|
|
137
|
-
* When specifying an union type of objects, `typia` tries to find
|
|
138
|
-
* descrimination way just by checking only one property type.
|
|
139
|
-
* If succeeded to find the discrimination way, `typia` will check the target
|
|
140
|
-
* property type and in the checking, non-undefined type checking would be
|
|
141
|
-
* done.
|
|
142
|
-
*
|
|
143
|
-
* In such process, you can transform the non-undefined type checking.
|
|
144
|
-
*
|
|
145
|
-
* @param exp
|
|
146
|
-
* @returns Transformed expression
|
|
147
|
-
* @deprecated
|
|
148
|
-
*/
|
|
149
|
-
required?(exp: ts.Expression): ts.Expression;
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Conditon wrapper when unable to specify any object type.
|
|
153
|
-
*
|
|
154
|
-
* When failed to specify an object type through discrimination, full
|
|
155
|
-
* iteration type checking would be happend. In such circumstance, you
|
|
156
|
-
* can wrap the condition with additional function.
|
|
157
|
-
*
|
|
158
|
-
* @param condition Current condition
|
|
159
|
-
* @returns A function wrapped current condition
|
|
160
|
-
*/
|
|
161
|
-
full?: (
|
|
162
|
-
condition: ts.Expression,
|
|
163
|
-
) => (
|
|
164
|
-
input: ts.Expression,
|
|
165
|
-
expected: string,
|
|
166
|
-
explore: IExplore,
|
|
167
|
-
) => ts.Expression;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Return type.
|
|
171
|
-
*/
|
|
172
|
-
type?: ts.TypeNode;
|
|
173
|
-
}
|
|
174
|
-
export interface IGenerator {
|
|
175
|
-
objects?(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
176
|
-
unions?(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
177
|
-
arrays(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
178
|
-
tuples(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export interface IExplore {
|
|
183
|
-
tracable: boolean;
|
|
184
|
-
source: "top" | "function";
|
|
185
|
-
from: "top" | "array" | "object";
|
|
186
|
-
postfix: string;
|
|
187
|
-
start?: number;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface Decoder<
|
|
191
|
-
T,
|
|
192
|
-
Output extends ts.ConciseBody = ts.ConciseBody,
|
|
193
|
-
> {
|
|
194
|
-
(input: ts.Expression, target: T, explore: IExplore): Output;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/* -----------------------------------------------------------
|
|
198
|
-
GENERATORS
|
|
199
|
-
----------------------------------------------------------- */
|
|
200
|
-
export const write =
|
|
201
|
-
(project: IProject) =>
|
|
202
|
-
(config: IConfig) =>
|
|
203
|
-
(importer: FunctionImporter) =>
|
|
204
|
-
(type: ts.Type, name?: string) => {
|
|
205
|
-
const [collection, meta] =
|
|
206
|
-
config.initializer(project)(importer)(type);
|
|
207
|
-
|
|
208
|
-
// ITERATE OVER ALL METADATA
|
|
209
|
-
const output: ts.ConciseBody = config.decoder()(
|
|
210
|
-
ValueFactory.INPUT(),
|
|
211
|
-
meta,
|
|
212
|
-
{
|
|
213
|
-
tracable: config.path || config.trace,
|
|
214
|
-
source: "top",
|
|
215
|
-
from: "top",
|
|
216
|
-
postfix: '""',
|
|
217
|
-
},
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
// RETURNS THE OPTIMAL ARROW FUNCTION
|
|
221
|
-
const functions = {
|
|
222
|
-
objects: (
|
|
223
|
-
config.generator.objects?.() ??
|
|
224
|
-
write_object_functions(config)(importer)
|
|
225
|
-
)(collection),
|
|
226
|
-
unions: (
|
|
227
|
-
config.generator.unions?.() ?? write_union_functions(config)
|
|
228
|
-
)(collection),
|
|
229
|
-
arrays: config.generator.arrays()(collection),
|
|
230
|
-
tuples: config.generator.tuples()(collection),
|
|
231
|
-
};
|
|
232
|
-
const added: ts.Statement[] = (config.addition ?? (() => []))(
|
|
233
|
-
collection,
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
return ts.factory.createArrowFunction(
|
|
237
|
-
undefined,
|
|
238
|
-
undefined,
|
|
239
|
-
parameterDeclarations(config)(config.types.input(type, name))(
|
|
240
|
-
ValueFactory.INPUT(),
|
|
241
|
-
),
|
|
242
|
-
config.types.output(type, name),
|
|
243
|
-
undefined,
|
|
244
|
-
ts.factory.createBlock(
|
|
245
|
-
[
|
|
246
|
-
...added,
|
|
247
|
-
...functions.objects.filter((_, i) =>
|
|
248
|
-
importer.hasLocal(`${config.prefix}o${i}`),
|
|
249
|
-
),
|
|
250
|
-
...functions.unions.filter((_, i) =>
|
|
251
|
-
importer.hasLocal(`${config.prefix}u${i}`),
|
|
252
|
-
),
|
|
253
|
-
...functions.arrays.filter((_, i) =>
|
|
254
|
-
importer.hasLocal(`${config.prefix}a${i}`),
|
|
255
|
-
),
|
|
256
|
-
...functions.tuples.filter((_, i) =>
|
|
257
|
-
importer.hasLocal(`${config.prefix}t${i}`),
|
|
258
|
-
),
|
|
259
|
-
...(ts.isBlock(output)
|
|
260
|
-
? output.statements
|
|
261
|
-
: [ts.factory.createReturnStatement(output)]),
|
|
262
|
-
],
|
|
263
|
-
true,
|
|
264
|
-
),
|
|
265
|
-
);
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
export const write_object_functions =
|
|
269
|
-
(config: IConfig) =>
|
|
270
|
-
(importer: FunctionImporter) =>
|
|
271
|
-
(collection: MetadataCollection) =>
|
|
272
|
-
collection
|
|
273
|
-
.objects()
|
|
274
|
-
.map((obj) =>
|
|
275
|
-
StatementFactory.constant(
|
|
276
|
-
`${config.prefix}o${obj.index}`,
|
|
277
|
-
ts.factory.createArrowFunction(
|
|
278
|
-
undefined,
|
|
279
|
-
undefined,
|
|
280
|
-
parameterDeclarations(config)(
|
|
281
|
-
TypeFactory.keyword("any"),
|
|
282
|
-
)(ValueFactory.INPUT()),
|
|
283
|
-
config.objector.type ?? TypeFactory.keyword("any"),
|
|
284
|
-
undefined,
|
|
285
|
-
config.objector.joiner(
|
|
286
|
-
ts.factory.createIdentifier("input"),
|
|
287
|
-
feature_object_entries(config)(importer)(obj)(
|
|
288
|
-
ts.factory.createIdentifier("input"),
|
|
289
|
-
),
|
|
290
|
-
obj,
|
|
291
|
-
),
|
|
292
|
-
),
|
|
293
|
-
),
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
export const write_union_functions =
|
|
297
|
-
(config: IConfig) => (collection: MetadataCollection) =>
|
|
298
|
-
collection
|
|
299
|
-
.unions()
|
|
300
|
-
.map((union, i) =>
|
|
301
|
-
StatementFactory.constant(
|
|
302
|
-
`${config.prefix}u${i}`,
|
|
303
|
-
write_union(config)(union),
|
|
304
|
-
),
|
|
305
|
-
);
|
|
306
|
-
|
|
307
|
-
const write_union = (config: IConfig) => {
|
|
308
|
-
const explorer = UnionExplorer.object(config);
|
|
309
|
-
const input = ValueFactory.INPUT();
|
|
310
|
-
|
|
311
|
-
return (meta: MetadataObject[]) =>
|
|
312
|
-
ts.factory.createArrowFunction(
|
|
313
|
-
undefined,
|
|
314
|
-
undefined,
|
|
315
|
-
parameterDeclarations(config)(TypeFactory.keyword("any"))(
|
|
316
|
-
ValueFactory.INPUT(),
|
|
317
|
-
),
|
|
318
|
-
TypeFactory.keyword("any"),
|
|
319
|
-
undefined,
|
|
320
|
-
explorer(input, meta, {
|
|
321
|
-
tracable: config.path || config.trace,
|
|
322
|
-
source: "function",
|
|
323
|
-
from: "object",
|
|
324
|
-
postfix: "",
|
|
325
|
-
}),
|
|
326
|
-
);
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
/* -----------------------------------------------------------
|
|
330
|
-
DECODERS
|
|
331
|
-
----------------------------------------------------------- */
|
|
332
|
-
export const decode_array =
|
|
333
|
-
(config: Pick<IConfig, "trace" | "path" | "decoder" | "prefix">) =>
|
|
334
|
-
(importer: FunctionImporter) =>
|
|
335
|
-
(
|
|
336
|
-
combiner: (
|
|
337
|
-
input: ts.Expression,
|
|
338
|
-
arrow: ts.ArrowFunction,
|
|
339
|
-
) => ts.Expression,
|
|
340
|
-
) => {
|
|
341
|
-
const rand: string = importer.increment().toString();
|
|
342
|
-
const tail =
|
|
343
|
-
config.path || config.trace
|
|
344
|
-
? [
|
|
345
|
-
IdentifierFactory.parameter(
|
|
346
|
-
"_index" + rand,
|
|
347
|
-
TypeFactory.keyword("number"),
|
|
348
|
-
),
|
|
349
|
-
]
|
|
350
|
-
: [];
|
|
351
|
-
|
|
352
|
-
return (
|
|
353
|
-
input: ts.Expression,
|
|
354
|
-
array: MetadataArray,
|
|
355
|
-
explore: IExplore,
|
|
356
|
-
) => {
|
|
357
|
-
const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
|
|
358
|
-
undefined,
|
|
359
|
-
undefined,
|
|
360
|
-
[
|
|
361
|
-
IdentifierFactory.parameter(
|
|
362
|
-
"elem",
|
|
363
|
-
TypeFactory.keyword("any"),
|
|
364
|
-
),
|
|
365
|
-
...tail,
|
|
366
|
-
],
|
|
367
|
-
undefined,
|
|
368
|
-
undefined,
|
|
369
|
-
config.decoder()(
|
|
370
|
-
ValueFactory.INPUT("elem"),
|
|
371
|
-
array.type.value,
|
|
372
|
-
{
|
|
373
|
-
tracable: explore.tracable,
|
|
374
|
-
source: explore.source,
|
|
375
|
-
from: "array",
|
|
376
|
-
postfix: index(explore.start ?? null)(
|
|
377
|
-
explore.postfix,
|
|
378
|
-
)(rand),
|
|
379
|
-
},
|
|
380
|
-
),
|
|
381
|
-
);
|
|
382
|
-
return combiner(input, arrow);
|
|
383
|
-
};
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
export const decode_object =
|
|
387
|
-
(config: Pick<IConfig, "trace" | "path" | "prefix">) =>
|
|
388
|
-
(importer: FunctionImporter) =>
|
|
389
|
-
(input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
|
|
390
|
-
ts.factory.createCallExpression(
|
|
391
|
-
ts.factory.createIdentifier(
|
|
392
|
-
importer.useLocal(`${config.prefix}o${obj.index}`),
|
|
393
|
-
),
|
|
394
|
-
undefined,
|
|
395
|
-
argumentsArray(config)(explore)(input),
|
|
396
|
-
);
|
|
397
|
-
|
|
398
|
-
/* -----------------------------------------------------------
|
|
399
|
-
UTILITIES FOR INTERNAL FUNCTIONS
|
|
400
|
-
----------------------------------------------------------- */
|
|
401
|
-
export const index =
|
|
402
|
-
(start: number | null) => (prev: string) => (rand: string) => {
|
|
403
|
-
const tail: string =
|
|
404
|
-
start !== null
|
|
405
|
-
? `"[" + (${start} + _index${rand}) + "]"`
|
|
406
|
-
: `"[" + _index${rand} + "]"`;
|
|
407
|
-
if (prev === "") return tail;
|
|
408
|
-
else if (prev[prev.length - 1] === `"`)
|
|
409
|
-
return prev.substring(0, prev.length - 1) + tail.substring(1);
|
|
410
|
-
return prev + ` + ${tail}`;
|
|
411
|
-
};
|
|
412
|
-
|
|
413
|
-
export const argumentsArray =
|
|
414
|
-
(config: Pick<IConfig, "path" | "trace">) =>
|
|
415
|
-
(explore: FeatureProgrammer.IExplore) => {
|
|
416
|
-
const tail: ts.Expression[] =
|
|
417
|
-
config.path === false && config.trace === false
|
|
418
|
-
? []
|
|
419
|
-
: config.path === true && config.trace === true
|
|
420
|
-
? [
|
|
421
|
-
ts.factory.createIdentifier(
|
|
422
|
-
explore.postfix
|
|
423
|
-
? `_path + ${explore.postfix}`
|
|
424
|
-
: "_path",
|
|
425
|
-
),
|
|
426
|
-
explore.source === "function"
|
|
427
|
-
? ts.factory.createIdentifier(
|
|
428
|
-
`${explore.tracable} && _exceptionable`,
|
|
429
|
-
)
|
|
430
|
-
: explore.tracable
|
|
431
|
-
? ts.factory.createTrue()
|
|
432
|
-
: ts.factory.createFalse(),
|
|
433
|
-
]
|
|
434
|
-
: config.path === true
|
|
435
|
-
? [
|
|
436
|
-
ts.factory.createIdentifier(
|
|
437
|
-
explore.postfix
|
|
438
|
-
? `_path + ${explore.postfix}`
|
|
439
|
-
: "_path",
|
|
440
|
-
),
|
|
441
|
-
]
|
|
442
|
-
: [
|
|
443
|
-
explore.source === "function"
|
|
444
|
-
? ts.factory.createIdentifier(
|
|
445
|
-
`${explore.tracable} && _exceptionable`,
|
|
446
|
-
)
|
|
447
|
-
: explore.tracable
|
|
448
|
-
? ts.factory.createTrue()
|
|
449
|
-
: ts.factory.createFalse(),
|
|
450
|
-
];
|
|
451
|
-
return (input: ts.Expression) => [input, ...tail];
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
export const parameterDeclarations =
|
|
455
|
-
(props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
|
|
456
|
-
(type: ts.TypeNode) => {
|
|
457
|
-
const tail: ts.ParameterDeclaration[] = [];
|
|
458
|
-
if (props.path)
|
|
459
|
-
tail.push(
|
|
460
|
-
IdentifierFactory.parameter(
|
|
461
|
-
"_path",
|
|
462
|
-
TypeFactory.keyword("string"),
|
|
463
|
-
),
|
|
464
|
-
);
|
|
465
|
-
if (props.trace)
|
|
466
|
-
tail.push(
|
|
467
|
-
IdentifierFactory.parameter(
|
|
468
|
-
"_exceptionable",
|
|
469
|
-
TypeFactory.keyword("boolean"),
|
|
470
|
-
ts.factory.createTrue(),
|
|
471
|
-
),
|
|
472
|
-
);
|
|
473
|
-
return (input: ts.Identifier): ts.ParameterDeclaration[] => [
|
|
474
|
-
IdentifierFactory.parameter(input, type),
|
|
475
|
-
...tail,
|
|
476
|
-
];
|
|
477
|
-
};
|
|
478
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { IdentifierFactory } from "../factories/IdentifierFactory";
|
|
4
|
+
import { MetadataCollection } from "../factories/MetadataCollection";
|
|
5
|
+
import { StatementFactory } from "../factories/StatementFactory";
|
|
6
|
+
import { TypeFactory } from "../factories/TypeFactory";
|
|
7
|
+
import { ValueFactory } from "../factories/ValueFactory";
|
|
8
|
+
|
|
9
|
+
import { Metadata } from "../schemas/metadata/Metadata";
|
|
10
|
+
import { MetadataArray } from "../schemas/metadata/MetadataArray";
|
|
11
|
+
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
12
|
+
|
|
13
|
+
import { IProject } from "../transformers/IProject";
|
|
14
|
+
|
|
15
|
+
import { CheckerProgrammer } from "./CheckerProgrammer";
|
|
16
|
+
import { FunctionImporter } from "./helpers/FunctionImporeter";
|
|
17
|
+
import { IExpressionEntry } from "./helpers/IExpressionEntry";
|
|
18
|
+
import { UnionExplorer } from "./helpers/UnionExplorer";
|
|
19
|
+
import { feature_object_entries } from "./internal/feature_object_entries";
|
|
20
|
+
|
|
21
|
+
export namespace FeatureProgrammer {
|
|
22
|
+
/* -----------------------------------------------------------
|
|
23
|
+
PARAMETERS
|
|
24
|
+
----------------------------------------------------------- */
|
|
25
|
+
export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
|
|
26
|
+
types: IConfig.ITypes;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Prefix name of internal functions for specific types.
|
|
30
|
+
*/
|
|
31
|
+
prefix: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether to archive access path or not.
|
|
35
|
+
*/
|
|
36
|
+
path: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether to trace exception or not.
|
|
40
|
+
*/
|
|
41
|
+
trace: boolean;
|
|
42
|
+
|
|
43
|
+
addition?(collection: MetadataCollection): ts.Statement[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Initializer of metadata.
|
|
47
|
+
*/
|
|
48
|
+
initializer: (
|
|
49
|
+
project: IProject,
|
|
50
|
+
) => (
|
|
51
|
+
importer: FunctionImporter,
|
|
52
|
+
) => (type: ts.Type) => [MetadataCollection, Metadata];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Decoder, station of every types.
|
|
56
|
+
*/
|
|
57
|
+
decoder: () => Decoder<Metadata, Output>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Object configurator.
|
|
61
|
+
*/
|
|
62
|
+
objector: IConfig.IObjector<Output>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Generator of functions for object types.
|
|
66
|
+
*/
|
|
67
|
+
generator: IConfig.IGenerator;
|
|
68
|
+
}
|
|
69
|
+
export namespace IConfig {
|
|
70
|
+
export interface ITypes {
|
|
71
|
+
input: (type: ts.Type, name?: string) => ts.TypeNode;
|
|
72
|
+
output: (type: ts.Type, name?: string) => ts.TypeNode;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface IObjector<
|
|
76
|
+
Output extends ts.ConciseBody = ts.ConciseBody,
|
|
77
|
+
> {
|
|
78
|
+
/**
|
|
79
|
+
* Type checker when union object type comes.
|
|
80
|
+
*/
|
|
81
|
+
checker: () => Decoder<Metadata, ts.Expression>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Decoder, function call expression generator of specific typed objects.
|
|
85
|
+
*/
|
|
86
|
+
decoder: () => Decoder<MetadataObject, ts.Expression>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Joiner of expressions from properties.
|
|
90
|
+
*/
|
|
91
|
+
joiner(
|
|
92
|
+
input: ts.Expression,
|
|
93
|
+
entries: IExpressionEntry<Output>[],
|
|
94
|
+
parent: MetadataObject,
|
|
95
|
+
): ts.ConciseBody;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Union type specificator.
|
|
99
|
+
*
|
|
100
|
+
* Expression of an algorithm specifying object type and calling
|
|
101
|
+
* the `decoder` function of the specified object type.
|
|
102
|
+
*/
|
|
103
|
+
unionizer: Decoder<MetadataObject[], ts.Expression>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Handler of union type specification failure.
|
|
107
|
+
*
|
|
108
|
+
* @param value Expression of input parameter
|
|
109
|
+
* @param expected Expected type name
|
|
110
|
+
* @param explore Exploration info
|
|
111
|
+
* @returns Statement of failure
|
|
112
|
+
*/
|
|
113
|
+
failure(
|
|
114
|
+
value: ts.Expression,
|
|
115
|
+
expected: string,
|
|
116
|
+
explore?: IExplore,
|
|
117
|
+
): ts.Statement;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Transformer of type checking expression by discrimination.
|
|
121
|
+
*
|
|
122
|
+
* When an object type has been specified by a discrimination without full
|
|
123
|
+
* iteration, the `unionizer` will decode the object instance after
|
|
124
|
+
* the last type checking.
|
|
125
|
+
*
|
|
126
|
+
* In such circumtance, you can transform the last type checking function.
|
|
127
|
+
*
|
|
128
|
+
* @param exp Current expression about type checking
|
|
129
|
+
* @returns Transformed expression
|
|
130
|
+
* @deprecated
|
|
131
|
+
*/
|
|
132
|
+
is?(exp: ts.Expression): ts.Expression;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Transformer of non-undefined type checking by discrimination.
|
|
136
|
+
*
|
|
137
|
+
* When specifying an union type of objects, `typia` tries to find
|
|
138
|
+
* descrimination way just by checking only one property type.
|
|
139
|
+
* If succeeded to find the discrimination way, `typia` will check the target
|
|
140
|
+
* property type and in the checking, non-undefined type checking would be
|
|
141
|
+
* done.
|
|
142
|
+
*
|
|
143
|
+
* In such process, you can transform the non-undefined type checking.
|
|
144
|
+
*
|
|
145
|
+
* @param exp
|
|
146
|
+
* @returns Transformed expression
|
|
147
|
+
* @deprecated
|
|
148
|
+
*/
|
|
149
|
+
required?(exp: ts.Expression): ts.Expression;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Conditon wrapper when unable to specify any object type.
|
|
153
|
+
*
|
|
154
|
+
* When failed to specify an object type through discrimination, full
|
|
155
|
+
* iteration type checking would be happend. In such circumstance, you
|
|
156
|
+
* can wrap the condition with additional function.
|
|
157
|
+
*
|
|
158
|
+
* @param condition Current condition
|
|
159
|
+
* @returns A function wrapped current condition
|
|
160
|
+
*/
|
|
161
|
+
full?: (
|
|
162
|
+
condition: ts.Expression,
|
|
163
|
+
) => (
|
|
164
|
+
input: ts.Expression,
|
|
165
|
+
expected: string,
|
|
166
|
+
explore: IExplore,
|
|
167
|
+
) => ts.Expression;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Return type.
|
|
171
|
+
*/
|
|
172
|
+
type?: ts.TypeNode;
|
|
173
|
+
}
|
|
174
|
+
export interface IGenerator {
|
|
175
|
+
objects?(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
176
|
+
unions?(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
177
|
+
arrays(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
178
|
+
tuples(): (col: MetadataCollection) => ts.VariableStatement[];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface IExplore {
|
|
183
|
+
tracable: boolean;
|
|
184
|
+
source: "top" | "function";
|
|
185
|
+
from: "top" | "array" | "object";
|
|
186
|
+
postfix: string;
|
|
187
|
+
start?: number;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface Decoder<
|
|
191
|
+
T,
|
|
192
|
+
Output extends ts.ConciseBody = ts.ConciseBody,
|
|
193
|
+
> {
|
|
194
|
+
(input: ts.Expression, target: T, explore: IExplore): Output;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* -----------------------------------------------------------
|
|
198
|
+
GENERATORS
|
|
199
|
+
----------------------------------------------------------- */
|
|
200
|
+
export const write =
|
|
201
|
+
(project: IProject) =>
|
|
202
|
+
(config: IConfig) =>
|
|
203
|
+
(importer: FunctionImporter) =>
|
|
204
|
+
(type: ts.Type, name?: string) => {
|
|
205
|
+
const [collection, meta] =
|
|
206
|
+
config.initializer(project)(importer)(type);
|
|
207
|
+
|
|
208
|
+
// ITERATE OVER ALL METADATA
|
|
209
|
+
const output: ts.ConciseBody = config.decoder()(
|
|
210
|
+
ValueFactory.INPUT(),
|
|
211
|
+
meta,
|
|
212
|
+
{
|
|
213
|
+
tracable: config.path || config.trace,
|
|
214
|
+
source: "top",
|
|
215
|
+
from: "top",
|
|
216
|
+
postfix: '""',
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// RETURNS THE OPTIMAL ARROW FUNCTION
|
|
221
|
+
const functions = {
|
|
222
|
+
objects: (
|
|
223
|
+
config.generator.objects?.() ??
|
|
224
|
+
write_object_functions(config)(importer)
|
|
225
|
+
)(collection),
|
|
226
|
+
unions: (
|
|
227
|
+
config.generator.unions?.() ?? write_union_functions(config)
|
|
228
|
+
)(collection),
|
|
229
|
+
arrays: config.generator.arrays()(collection),
|
|
230
|
+
tuples: config.generator.tuples()(collection),
|
|
231
|
+
};
|
|
232
|
+
const added: ts.Statement[] = (config.addition ?? (() => []))(
|
|
233
|
+
collection,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
return ts.factory.createArrowFunction(
|
|
237
|
+
undefined,
|
|
238
|
+
undefined,
|
|
239
|
+
parameterDeclarations(config)(config.types.input(type, name))(
|
|
240
|
+
ValueFactory.INPUT(),
|
|
241
|
+
),
|
|
242
|
+
config.types.output(type, name),
|
|
243
|
+
undefined,
|
|
244
|
+
ts.factory.createBlock(
|
|
245
|
+
[
|
|
246
|
+
...added,
|
|
247
|
+
...functions.objects.filter((_, i) =>
|
|
248
|
+
importer.hasLocal(`${config.prefix}o${i}`),
|
|
249
|
+
),
|
|
250
|
+
...functions.unions.filter((_, i) =>
|
|
251
|
+
importer.hasLocal(`${config.prefix}u${i}`),
|
|
252
|
+
),
|
|
253
|
+
...functions.arrays.filter((_, i) =>
|
|
254
|
+
importer.hasLocal(`${config.prefix}a${i}`),
|
|
255
|
+
),
|
|
256
|
+
...functions.tuples.filter((_, i) =>
|
|
257
|
+
importer.hasLocal(`${config.prefix}t${i}`),
|
|
258
|
+
),
|
|
259
|
+
...(ts.isBlock(output)
|
|
260
|
+
? output.statements
|
|
261
|
+
: [ts.factory.createReturnStatement(output)]),
|
|
262
|
+
],
|
|
263
|
+
true,
|
|
264
|
+
),
|
|
265
|
+
);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export const write_object_functions =
|
|
269
|
+
(config: IConfig) =>
|
|
270
|
+
(importer: FunctionImporter) =>
|
|
271
|
+
(collection: MetadataCollection) =>
|
|
272
|
+
collection
|
|
273
|
+
.objects()
|
|
274
|
+
.map((obj) =>
|
|
275
|
+
StatementFactory.constant(
|
|
276
|
+
`${config.prefix}o${obj.index}`,
|
|
277
|
+
ts.factory.createArrowFunction(
|
|
278
|
+
undefined,
|
|
279
|
+
undefined,
|
|
280
|
+
parameterDeclarations(config)(
|
|
281
|
+
TypeFactory.keyword("any"),
|
|
282
|
+
)(ValueFactory.INPUT()),
|
|
283
|
+
config.objector.type ?? TypeFactory.keyword("any"),
|
|
284
|
+
undefined,
|
|
285
|
+
config.objector.joiner(
|
|
286
|
+
ts.factory.createIdentifier("input"),
|
|
287
|
+
feature_object_entries(config)(importer)(obj)(
|
|
288
|
+
ts.factory.createIdentifier("input"),
|
|
289
|
+
),
|
|
290
|
+
obj,
|
|
291
|
+
),
|
|
292
|
+
),
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
export const write_union_functions =
|
|
297
|
+
(config: IConfig) => (collection: MetadataCollection) =>
|
|
298
|
+
collection
|
|
299
|
+
.unions()
|
|
300
|
+
.map((union, i) =>
|
|
301
|
+
StatementFactory.constant(
|
|
302
|
+
`${config.prefix}u${i}`,
|
|
303
|
+
write_union(config)(union),
|
|
304
|
+
),
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const write_union = (config: IConfig) => {
|
|
308
|
+
const explorer = UnionExplorer.object(config);
|
|
309
|
+
const input = ValueFactory.INPUT();
|
|
310
|
+
|
|
311
|
+
return (meta: MetadataObject[]) =>
|
|
312
|
+
ts.factory.createArrowFunction(
|
|
313
|
+
undefined,
|
|
314
|
+
undefined,
|
|
315
|
+
parameterDeclarations(config)(TypeFactory.keyword("any"))(
|
|
316
|
+
ValueFactory.INPUT(),
|
|
317
|
+
),
|
|
318
|
+
TypeFactory.keyword("any"),
|
|
319
|
+
undefined,
|
|
320
|
+
explorer(input, meta, {
|
|
321
|
+
tracable: config.path || config.trace,
|
|
322
|
+
source: "function",
|
|
323
|
+
from: "object",
|
|
324
|
+
postfix: "",
|
|
325
|
+
}),
|
|
326
|
+
);
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/* -----------------------------------------------------------
|
|
330
|
+
DECODERS
|
|
331
|
+
----------------------------------------------------------- */
|
|
332
|
+
export const decode_array =
|
|
333
|
+
(config: Pick<IConfig, "trace" | "path" | "decoder" | "prefix">) =>
|
|
334
|
+
(importer: FunctionImporter) =>
|
|
335
|
+
(
|
|
336
|
+
combiner: (
|
|
337
|
+
input: ts.Expression,
|
|
338
|
+
arrow: ts.ArrowFunction,
|
|
339
|
+
) => ts.Expression,
|
|
340
|
+
) => {
|
|
341
|
+
const rand: string = importer.increment().toString();
|
|
342
|
+
const tail =
|
|
343
|
+
config.path || config.trace
|
|
344
|
+
? [
|
|
345
|
+
IdentifierFactory.parameter(
|
|
346
|
+
"_index" + rand,
|
|
347
|
+
TypeFactory.keyword("number"),
|
|
348
|
+
),
|
|
349
|
+
]
|
|
350
|
+
: [];
|
|
351
|
+
|
|
352
|
+
return (
|
|
353
|
+
input: ts.Expression,
|
|
354
|
+
array: MetadataArray,
|
|
355
|
+
explore: IExplore,
|
|
356
|
+
) => {
|
|
357
|
+
const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
|
|
358
|
+
undefined,
|
|
359
|
+
undefined,
|
|
360
|
+
[
|
|
361
|
+
IdentifierFactory.parameter(
|
|
362
|
+
"elem",
|
|
363
|
+
TypeFactory.keyword("any"),
|
|
364
|
+
),
|
|
365
|
+
...tail,
|
|
366
|
+
],
|
|
367
|
+
undefined,
|
|
368
|
+
undefined,
|
|
369
|
+
config.decoder()(
|
|
370
|
+
ValueFactory.INPUT("elem"),
|
|
371
|
+
array.type.value,
|
|
372
|
+
{
|
|
373
|
+
tracable: explore.tracable,
|
|
374
|
+
source: explore.source,
|
|
375
|
+
from: "array",
|
|
376
|
+
postfix: index(explore.start ?? null)(
|
|
377
|
+
explore.postfix,
|
|
378
|
+
)(rand),
|
|
379
|
+
},
|
|
380
|
+
),
|
|
381
|
+
);
|
|
382
|
+
return combiner(input, arrow);
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
export const decode_object =
|
|
387
|
+
(config: Pick<IConfig, "trace" | "path" | "prefix">) =>
|
|
388
|
+
(importer: FunctionImporter) =>
|
|
389
|
+
(input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
|
|
390
|
+
ts.factory.createCallExpression(
|
|
391
|
+
ts.factory.createIdentifier(
|
|
392
|
+
importer.useLocal(`${config.prefix}o${obj.index}`),
|
|
393
|
+
),
|
|
394
|
+
undefined,
|
|
395
|
+
argumentsArray(config)(explore)(input),
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
/* -----------------------------------------------------------
|
|
399
|
+
UTILITIES FOR INTERNAL FUNCTIONS
|
|
400
|
+
----------------------------------------------------------- */
|
|
401
|
+
export const index =
|
|
402
|
+
(start: number | null) => (prev: string) => (rand: string) => {
|
|
403
|
+
const tail: string =
|
|
404
|
+
start !== null
|
|
405
|
+
? `"[" + (${start} + _index${rand}) + "]"`
|
|
406
|
+
: `"[" + _index${rand} + "]"`;
|
|
407
|
+
if (prev === "") return tail;
|
|
408
|
+
else if (prev[prev.length - 1] === `"`)
|
|
409
|
+
return prev.substring(0, prev.length - 1) + tail.substring(1);
|
|
410
|
+
return prev + ` + ${tail}`;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
export const argumentsArray =
|
|
414
|
+
(config: Pick<IConfig, "path" | "trace">) =>
|
|
415
|
+
(explore: FeatureProgrammer.IExplore) => {
|
|
416
|
+
const tail: ts.Expression[] =
|
|
417
|
+
config.path === false && config.trace === false
|
|
418
|
+
? []
|
|
419
|
+
: config.path === true && config.trace === true
|
|
420
|
+
? [
|
|
421
|
+
ts.factory.createIdentifier(
|
|
422
|
+
explore.postfix
|
|
423
|
+
? `_path + ${explore.postfix}`
|
|
424
|
+
: "_path",
|
|
425
|
+
),
|
|
426
|
+
explore.source === "function"
|
|
427
|
+
? ts.factory.createIdentifier(
|
|
428
|
+
`${explore.tracable} && _exceptionable`,
|
|
429
|
+
)
|
|
430
|
+
: explore.tracable
|
|
431
|
+
? ts.factory.createTrue()
|
|
432
|
+
: ts.factory.createFalse(),
|
|
433
|
+
]
|
|
434
|
+
: config.path === true
|
|
435
|
+
? [
|
|
436
|
+
ts.factory.createIdentifier(
|
|
437
|
+
explore.postfix
|
|
438
|
+
? `_path + ${explore.postfix}`
|
|
439
|
+
: "_path",
|
|
440
|
+
),
|
|
441
|
+
]
|
|
442
|
+
: [
|
|
443
|
+
explore.source === "function"
|
|
444
|
+
? ts.factory.createIdentifier(
|
|
445
|
+
`${explore.tracable} && _exceptionable`,
|
|
446
|
+
)
|
|
447
|
+
: explore.tracable
|
|
448
|
+
? ts.factory.createTrue()
|
|
449
|
+
: ts.factory.createFalse(),
|
|
450
|
+
];
|
|
451
|
+
return (input: ts.Expression) => [input, ...tail];
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
export const parameterDeclarations =
|
|
455
|
+
(props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
|
|
456
|
+
(type: ts.TypeNode) => {
|
|
457
|
+
const tail: ts.ParameterDeclaration[] = [];
|
|
458
|
+
if (props.path)
|
|
459
|
+
tail.push(
|
|
460
|
+
IdentifierFactory.parameter(
|
|
461
|
+
"_path",
|
|
462
|
+
TypeFactory.keyword("string"),
|
|
463
|
+
),
|
|
464
|
+
);
|
|
465
|
+
if (props.trace)
|
|
466
|
+
tail.push(
|
|
467
|
+
IdentifierFactory.parameter(
|
|
468
|
+
"_exceptionable",
|
|
469
|
+
TypeFactory.keyword("boolean"),
|
|
470
|
+
ts.factory.createTrue(),
|
|
471
|
+
),
|
|
472
|
+
);
|
|
473
|
+
return (input: ts.Identifier): ts.ParameterDeclaration[] => [
|
|
474
|
+
IdentifierFactory.parameter(input, type),
|
|
475
|
+
...tail,
|
|
476
|
+
];
|
|
477
|
+
};
|
|
478
|
+
}
|