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,655 +1,655 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { ExpressionFactory } from "../../factories/ExpressionFactory";
|
|
4
|
-
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
5
|
-
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
6
|
-
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
7
|
-
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
8
|
-
import { StatementFactory } from "../../factories/StatementFactory";
|
|
9
|
-
import { TypeFactory } from "../../factories/TypeFactory";
|
|
10
|
-
|
|
11
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
12
|
-
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
13
|
-
import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
|
|
14
|
-
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
15
|
-
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
16
|
-
|
|
17
|
-
import { IProject } from "../../transformers/IProject";
|
|
18
|
-
|
|
19
|
-
import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
|
|
20
|
-
|
|
21
|
-
import { FunctionImporter } from "../helpers/FunctionImporeter";
|
|
22
|
-
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
23
|
-
|
|
24
|
-
export namespace ProtobufDecodeProgrammer {
|
|
25
|
-
export const write =
|
|
26
|
-
(project: IProject) =>
|
|
27
|
-
(modulo: ts.LeftHandSideExpression) =>
|
|
28
|
-
(type: ts.Type, name?: string): ts.ArrowFunction => {
|
|
29
|
-
const importer: FunctionImporter = new FunctionImporter(
|
|
30
|
-
modulo.getText(),
|
|
31
|
-
);
|
|
32
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
33
|
-
const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
|
|
34
|
-
project.checker,
|
|
35
|
-
)(collection)(type);
|
|
36
|
-
|
|
37
|
-
const functors = collection
|
|
38
|
-
.objects()
|
|
39
|
-
.filter((obj) => ProtobufUtil.isStaticObject(obj))
|
|
40
|
-
.map((obj) =>
|
|
41
|
-
StatementFactory.constant(
|
|
42
|
-
`${PREFIX}o${obj.index}`,
|
|
43
|
-
write_object_function(project)(importer)(obj),
|
|
44
|
-
),
|
|
45
|
-
);
|
|
46
|
-
const reader = StatementFactory.constant(
|
|
47
|
-
"reader",
|
|
48
|
-
ts.factory.createNewExpression(
|
|
49
|
-
importer.use("Reader"),
|
|
50
|
-
undefined,
|
|
51
|
-
[ts.factory.createIdentifier("input")],
|
|
52
|
-
),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
return ts.factory.createArrowFunction(
|
|
56
|
-
undefined,
|
|
57
|
-
undefined,
|
|
58
|
-
[
|
|
59
|
-
IdentifierFactory.parameter(
|
|
60
|
-
"input",
|
|
61
|
-
ts.factory.createTypeReferenceNode("Uint8Array"),
|
|
62
|
-
),
|
|
63
|
-
],
|
|
64
|
-
ts.factory.createTypeReferenceNode(
|
|
65
|
-
`typia.Resolved<${
|
|
66
|
-
name ?? TypeFactory.getFullName(project.checker)(type)
|
|
67
|
-
}>`,
|
|
68
|
-
),
|
|
69
|
-
undefined,
|
|
70
|
-
ts.factory.createBlock(
|
|
71
|
-
[
|
|
72
|
-
...importer.declare(modulo),
|
|
73
|
-
...functors,
|
|
74
|
-
reader,
|
|
75
|
-
ts.factory.createReturnStatement(
|
|
76
|
-
decode_regular_object(true)(meta.objects[0]!),
|
|
77
|
-
),
|
|
78
|
-
],
|
|
79
|
-
true,
|
|
80
|
-
),
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const write_object_function =
|
|
85
|
-
(project: IProject) =>
|
|
86
|
-
(importer: FunctionImporter) =>
|
|
87
|
-
(obj: MetadataObject): ts.ArrowFunction =>
|
|
88
|
-
ts.factory.createArrowFunction(
|
|
89
|
-
undefined,
|
|
90
|
-
undefined,
|
|
91
|
-
[
|
|
92
|
-
IdentifierFactory.parameter("reader"),
|
|
93
|
-
IdentifierFactory.parameter(
|
|
94
|
-
"length",
|
|
95
|
-
TypeFactory.keyword("number"),
|
|
96
|
-
ts.factory.createNumericLiteral(-1),
|
|
97
|
-
),
|
|
98
|
-
],
|
|
99
|
-
TypeFactory.keyword("any"),
|
|
100
|
-
undefined,
|
|
101
|
-
ts.factory.createBlock(
|
|
102
|
-
[
|
|
103
|
-
ts.factory.createExpressionStatement(
|
|
104
|
-
ts.factory.createBinaryExpression(
|
|
105
|
-
ts.factory.createIdentifier("length"),
|
|
106
|
-
ts.factory.createToken(
|
|
107
|
-
ts.SyntaxKind.EqualsToken,
|
|
108
|
-
),
|
|
109
|
-
ts.factory.createConditionalExpression(
|
|
110
|
-
ts.factory.createLessThan(
|
|
111
|
-
ts.factory.createIdentifier("length"),
|
|
112
|
-
ts.factory.createNumericLiteral(0),
|
|
113
|
-
),
|
|
114
|
-
undefined,
|
|
115
|
-
ts.factory.createCallExpression(
|
|
116
|
-
IdentifierFactory.access(READER())(
|
|
117
|
-
"size",
|
|
118
|
-
),
|
|
119
|
-
undefined,
|
|
120
|
-
undefined,
|
|
121
|
-
),
|
|
122
|
-
undefined,
|
|
123
|
-
ts.factory.createAdd(
|
|
124
|
-
ts.factory.createCallExpression(
|
|
125
|
-
IdentifierFactory.access(READER())(
|
|
126
|
-
"index",
|
|
127
|
-
),
|
|
128
|
-
undefined,
|
|
129
|
-
undefined,
|
|
130
|
-
),
|
|
131
|
-
ts.factory.createIdentifier("length"),
|
|
132
|
-
),
|
|
133
|
-
),
|
|
134
|
-
),
|
|
135
|
-
),
|
|
136
|
-
...write_object_function_body(project)(importer)({
|
|
137
|
-
condition: ts.factory.createLessThan(
|
|
138
|
-
ts.factory.createCallExpression(
|
|
139
|
-
IdentifierFactory.access(READER())("index"),
|
|
140
|
-
undefined,
|
|
141
|
-
undefined,
|
|
142
|
-
),
|
|
143
|
-
ts.factory.createIdentifier("length"),
|
|
144
|
-
),
|
|
145
|
-
tag: "tag",
|
|
146
|
-
output: "output",
|
|
147
|
-
})(obj.properties),
|
|
148
|
-
ts.factory.createReturnStatement(
|
|
149
|
-
ts.factory.createIdentifier("output"),
|
|
150
|
-
),
|
|
151
|
-
],
|
|
152
|
-
true,
|
|
153
|
-
),
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
const write_object_function_body =
|
|
157
|
-
(project: IProject) =>
|
|
158
|
-
(importer: FunctionImporter) =>
|
|
159
|
-
(props: { condition: ts.Expression; tag: string; output: string }) =>
|
|
160
|
-
(properties: MetadataProperty[]): ts.Statement[] => {
|
|
161
|
-
let i: number = 1;
|
|
162
|
-
const clauses: ts.CaseClause[] = properties
|
|
163
|
-
.map((p) => {
|
|
164
|
-
const clause = decode_property(project)(importer)(i)(
|
|
165
|
-
IdentifierFactory.access(
|
|
166
|
-
ts.factory.createIdentifier(props.output),
|
|
167
|
-
)(p.key.getSoleLiteral()!),
|
|
168
|
-
p.value,
|
|
169
|
-
);
|
|
170
|
-
i += ProtobufUtil.size(p.value);
|
|
171
|
-
return clause;
|
|
172
|
-
})
|
|
173
|
-
.flat();
|
|
174
|
-
return [
|
|
175
|
-
StatementFactory.constant(
|
|
176
|
-
props.output,
|
|
177
|
-
ts.factory.createObjectLiteralExpression(
|
|
178
|
-
properties.map((p) =>
|
|
179
|
-
ts.factory.createPropertyAssignment(
|
|
180
|
-
IdentifierFactory.identifier(
|
|
181
|
-
p.key.getSoleLiteral()!,
|
|
182
|
-
),
|
|
183
|
-
write_property_default_value(p.value),
|
|
184
|
-
),
|
|
185
|
-
),
|
|
186
|
-
true,
|
|
187
|
-
),
|
|
188
|
-
),
|
|
189
|
-
ts.factory.createWhileStatement(
|
|
190
|
-
props.condition,
|
|
191
|
-
ts.factory.createBlock([
|
|
192
|
-
StatementFactory.constant(
|
|
193
|
-
props.tag,
|
|
194
|
-
ts.factory.createCallExpression(
|
|
195
|
-
IdentifierFactory.access(READER())("uint32"),
|
|
196
|
-
undefined,
|
|
197
|
-
undefined,
|
|
198
|
-
),
|
|
199
|
-
),
|
|
200
|
-
ts.factory.createSwitchStatement(
|
|
201
|
-
ts.factory.createUnsignedRightShift(
|
|
202
|
-
ts.factory.createIdentifier(props.tag),
|
|
203
|
-
ts.factory.createNumericLiteral(3),
|
|
204
|
-
),
|
|
205
|
-
ts.factory.createCaseBlock([
|
|
206
|
-
...clauses,
|
|
207
|
-
ts.factory.createDefaultClause([
|
|
208
|
-
ts.factory.createExpressionStatement(
|
|
209
|
-
ts.factory.createCallExpression(
|
|
210
|
-
IdentifierFactory.access(READER())(
|
|
211
|
-
"skipType",
|
|
212
|
-
),
|
|
213
|
-
undefined,
|
|
214
|
-
[
|
|
215
|
-
ts.factory.createBitwiseAnd(
|
|
216
|
-
ts.factory.createIdentifier(
|
|
217
|
-
props.tag,
|
|
218
|
-
),
|
|
219
|
-
ts.factory.createNumericLiteral(
|
|
220
|
-
7,
|
|
221
|
-
),
|
|
222
|
-
),
|
|
223
|
-
],
|
|
224
|
-
),
|
|
225
|
-
),
|
|
226
|
-
ts.factory.createBreakStatement(),
|
|
227
|
-
]),
|
|
228
|
-
]),
|
|
229
|
-
),
|
|
230
|
-
]),
|
|
231
|
-
),
|
|
232
|
-
];
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
const write_property_default_value = (value: Metadata) =>
|
|
236
|
-
ts.factory.createAsExpression(
|
|
237
|
-
value.nullable
|
|
238
|
-
? ts.factory.createNull()
|
|
239
|
-
: value.isRequired() === false
|
|
240
|
-
? ts.factory.createIdentifier("undefined")
|
|
241
|
-
: value.arrays.length
|
|
242
|
-
? ts.factory.createArrayLiteralExpression()
|
|
243
|
-
: value.maps.length
|
|
244
|
-
? ts.factory.createNewExpression(
|
|
245
|
-
ts.factory.createIdentifier("Map"),
|
|
246
|
-
undefined,
|
|
247
|
-
[],
|
|
248
|
-
)
|
|
249
|
-
: value.natives.length
|
|
250
|
-
? ts.factory.createNewExpression(
|
|
251
|
-
ts.factory.createIdentifier("Uint8Array"),
|
|
252
|
-
undefined,
|
|
253
|
-
[],
|
|
254
|
-
)
|
|
255
|
-
: value.atomics.some((a) => a.type === "string") ||
|
|
256
|
-
value.constants.some(
|
|
257
|
-
(c) =>
|
|
258
|
-
c.type === "string" && c.values.some((v) => v === ""),
|
|
259
|
-
) ||
|
|
260
|
-
value.templates.some(
|
|
261
|
-
(tpl) =>
|
|
262
|
-
tpl.length === 1 && tpl[0]!.getName() === "string",
|
|
263
|
-
)
|
|
264
|
-
? ts.factory.createStringLiteral("")
|
|
265
|
-
: value.objects.length &&
|
|
266
|
-
value.objects.some((obj) => !ProtobufUtil.isStaticObject(obj))
|
|
267
|
-
? ts.factory.createObjectLiteralExpression()
|
|
268
|
-
: ts.factory.createIdentifier("undefined"),
|
|
269
|
-
TypeFactory.keyword("any"),
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
/* -----------------------------------------------------------
|
|
273
|
-
DECODERS
|
|
274
|
-
----------------------------------------------------------- */
|
|
275
|
-
const decode_property =
|
|
276
|
-
(project: IProject) =>
|
|
277
|
-
(importer: FunctionImporter) =>
|
|
278
|
-
(index: number) =>
|
|
279
|
-
(
|
|
280
|
-
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
281
|
-
meta: Metadata,
|
|
282
|
-
): ts.CaseClause[] => {
|
|
283
|
-
const clauses: ts.CaseClause[] = [];
|
|
284
|
-
const emplace =
|
|
285
|
-
(name: string) => (v: ts.Expression | ts.Statement[]) =>
|
|
286
|
-
clauses.push(
|
|
287
|
-
ts.factory.createCaseClause(
|
|
288
|
-
ts.factory.createNumericLiteral(index++),
|
|
289
|
-
Array.isArray(v)
|
|
290
|
-
? [
|
|
291
|
-
ts.factory.createExpressionStatement(
|
|
292
|
-
ts.factory.createIdentifier(
|
|
293
|
-
`// type: ${name}`,
|
|
294
|
-
),
|
|
295
|
-
),
|
|
296
|
-
...v,
|
|
297
|
-
ts.factory.createBreakStatement(),
|
|
298
|
-
]
|
|
299
|
-
: [
|
|
300
|
-
ts.factory.createExpressionStatement(
|
|
301
|
-
ts.factory.createIdentifier(
|
|
302
|
-
`// ${name}`,
|
|
303
|
-
),
|
|
304
|
-
),
|
|
305
|
-
ts.factory.createExpressionStatement(
|
|
306
|
-
ts.factory.createBinaryExpression(
|
|
307
|
-
accessor,
|
|
308
|
-
ts.factory.createToken(
|
|
309
|
-
ts.SyntaxKind.EqualsToken,
|
|
310
|
-
),
|
|
311
|
-
v,
|
|
312
|
-
),
|
|
313
|
-
),
|
|
314
|
-
ts.factory.createBreakStatement(),
|
|
315
|
-
],
|
|
316
|
-
),
|
|
317
|
-
);
|
|
318
|
-
|
|
319
|
-
const required: boolean = meta.isRequired() && !meta.nullable;
|
|
320
|
-
for (const atomic of ProtobufUtil.getAtomics(meta))
|
|
321
|
-
emplace(atomic)(decode_atomic(meta)(atomic));
|
|
322
|
-
if (meta.natives.length) emplace("bytes")(decode_bytes("bytes"));
|
|
323
|
-
for (const array of meta.arrays)
|
|
324
|
-
emplace(`Array<${array.type.value.getName()}>`)(
|
|
325
|
-
decode_array(accessor, array, required),
|
|
326
|
-
);
|
|
327
|
-
for (const map of meta.maps)
|
|
328
|
-
emplace(`Map<string, ${map.value.getName()}>`)(
|
|
329
|
-
decode_map(project)(importer)(accessor, map, required),
|
|
330
|
-
);
|
|
331
|
-
for (const obj of meta.objects)
|
|
332
|
-
emplace(obj.name)(
|
|
333
|
-
ProtobufUtil.isStaticObject(obj)
|
|
334
|
-
? decode_regular_object(false)(obj)
|
|
335
|
-
: decode_dynamic_object(project)(importer)(
|
|
336
|
-
accessor,
|
|
337
|
-
obj,
|
|
338
|
-
required,
|
|
339
|
-
),
|
|
340
|
-
);
|
|
341
|
-
return clauses;
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
const decode_atomic =
|
|
345
|
-
(meta: Metadata) =>
|
|
346
|
-
(atomic: ProtobufAtomic): ts.Expression => {
|
|
347
|
-
if (atomic === "string") return decode_bytes("string");
|
|
348
|
-
|
|
349
|
-
const call: ts.CallExpression = ts.factory.createCallExpression(
|
|
350
|
-
IdentifierFactory.access(ts.factory.createIdentifier("reader"))(
|
|
351
|
-
atomic,
|
|
352
|
-
),
|
|
353
|
-
undefined,
|
|
354
|
-
undefined,
|
|
355
|
-
);
|
|
356
|
-
if (atomic !== "int64" && atomic !== "uint64") return call;
|
|
357
|
-
|
|
358
|
-
const isNumber: boolean = ProtobufUtil.getNumbers(meta).some(
|
|
359
|
-
(n) => n === atomic,
|
|
360
|
-
);
|
|
361
|
-
return isNumber
|
|
362
|
-
? ts.factory.createCallExpression(
|
|
363
|
-
ts.factory.createIdentifier("Number"),
|
|
364
|
-
undefined,
|
|
365
|
-
[call],
|
|
366
|
-
)
|
|
367
|
-
: call;
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
|
|
371
|
-
ts.factory.createCallExpression(
|
|
372
|
-
IdentifierFactory.access(ts.factory.createIdentifier("reader"))(
|
|
373
|
-
method,
|
|
374
|
-
),
|
|
375
|
-
undefined,
|
|
376
|
-
undefined,
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
const decode_array = (
|
|
380
|
-
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
381
|
-
array: MetadataArray,
|
|
382
|
-
required: boolean,
|
|
383
|
-
): ts.Statement[] => {
|
|
384
|
-
const statements: Array<ts.Expression | ts.Statement> = [];
|
|
385
|
-
if (required === false)
|
|
386
|
-
statements.push(
|
|
387
|
-
ts.factory.createBinaryExpression(
|
|
388
|
-
accessor,
|
|
389
|
-
ts.factory.createToken(
|
|
390
|
-
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
391
|
-
),
|
|
392
|
-
ts.factory.createAsExpression(
|
|
393
|
-
ts.factory.createArrayLiteralExpression(),
|
|
394
|
-
ts.factory.createTypeReferenceNode("any[]"),
|
|
395
|
-
),
|
|
396
|
-
),
|
|
397
|
-
);
|
|
398
|
-
const atomics = ProtobufUtil.getAtomics(array.type.value);
|
|
399
|
-
const decoder = atomics.length
|
|
400
|
-
? () => decode_atomic(array.type.value)(atomics[0]!)
|
|
401
|
-
: array.type.value.natives.length
|
|
402
|
-
? () => decode_bytes("bytes")
|
|
403
|
-
: array.type.value.objects.length
|
|
404
|
-
? () => decode_regular_object(false)(array.type.value.objects[0]!)
|
|
405
|
-
: null;
|
|
406
|
-
if (decoder === null) throw new Error("Never reach here.");
|
|
407
|
-
else if (atomics.length && atomics[0] !== "string") {
|
|
408
|
-
statements.push(
|
|
409
|
-
ts.factory.createIfStatement(
|
|
410
|
-
ts.factory.createStrictEquality(
|
|
411
|
-
ts.factory.createNumericLiteral(2),
|
|
412
|
-
ts.factory.createBitwiseAnd(
|
|
413
|
-
ts.factory.createIdentifier("tag"),
|
|
414
|
-
ts.factory.createNumericLiteral(7),
|
|
415
|
-
),
|
|
416
|
-
),
|
|
417
|
-
ts.factory.createBlock(
|
|
418
|
-
[
|
|
419
|
-
StatementFactory.constant(
|
|
420
|
-
"piece",
|
|
421
|
-
ts.factory.createAdd(
|
|
422
|
-
ts.factory.createCallExpression(
|
|
423
|
-
IdentifierFactory.access(READER())(
|
|
424
|
-
"uint32",
|
|
425
|
-
),
|
|
426
|
-
undefined,
|
|
427
|
-
undefined,
|
|
428
|
-
),
|
|
429
|
-
ts.factory.createCallExpression(
|
|
430
|
-
IdentifierFactory.access(READER())(
|
|
431
|
-
"index",
|
|
432
|
-
),
|
|
433
|
-
undefined,
|
|
434
|
-
undefined,
|
|
435
|
-
),
|
|
436
|
-
),
|
|
437
|
-
),
|
|
438
|
-
ts.factory.createWhileStatement(
|
|
439
|
-
ts.factory.createLessThan(
|
|
440
|
-
ts.factory.createCallExpression(
|
|
441
|
-
IdentifierFactory.access(READER())(
|
|
442
|
-
"index",
|
|
443
|
-
),
|
|
444
|
-
undefined,
|
|
445
|
-
undefined,
|
|
446
|
-
),
|
|
447
|
-
ts.factory.createIdentifier("piece"),
|
|
448
|
-
),
|
|
449
|
-
ts.factory.createExpressionStatement(
|
|
450
|
-
ts.factory.createCallExpression(
|
|
451
|
-
IdentifierFactory.access(accessor)(
|
|
452
|
-
"push",
|
|
453
|
-
),
|
|
454
|
-
undefined,
|
|
455
|
-
[decoder()],
|
|
456
|
-
),
|
|
457
|
-
),
|
|
458
|
-
),
|
|
459
|
-
],
|
|
460
|
-
true,
|
|
461
|
-
),
|
|
462
|
-
ts.factory.createExpressionStatement(
|
|
463
|
-
ts.factory.createCallExpression(
|
|
464
|
-
IdentifierFactory.access(accessor)("push"),
|
|
465
|
-
undefined,
|
|
466
|
-
[decoder()],
|
|
467
|
-
),
|
|
468
|
-
),
|
|
469
|
-
),
|
|
470
|
-
);
|
|
471
|
-
} else
|
|
472
|
-
statements.push(
|
|
473
|
-
ts.factory.createCallExpression(
|
|
474
|
-
IdentifierFactory.access(accessor)("push"),
|
|
475
|
-
undefined,
|
|
476
|
-
[decoder()],
|
|
477
|
-
),
|
|
478
|
-
);
|
|
479
|
-
return statements.map((stmt) =>
|
|
480
|
-
ts.isExpression(stmt)
|
|
481
|
-
? ts.factory.createExpressionStatement(stmt)
|
|
482
|
-
: stmt,
|
|
483
|
-
);
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
const decode_regular_object =
|
|
487
|
-
(top: boolean) =>
|
|
488
|
-
(obj: MetadataObject): ts.Expression =>
|
|
489
|
-
ts.factory.createCallExpression(
|
|
490
|
-
ts.factory.createIdentifier(`${PREFIX}o${obj.index}`),
|
|
491
|
-
undefined,
|
|
492
|
-
[
|
|
493
|
-
ts.factory.createIdentifier("reader"),
|
|
494
|
-
...(top
|
|
495
|
-
? []
|
|
496
|
-
: [
|
|
497
|
-
ts.factory.createCallExpression(
|
|
498
|
-
IdentifierFactory.access(READER())("uint32"),
|
|
499
|
-
undefined,
|
|
500
|
-
undefined,
|
|
501
|
-
),
|
|
502
|
-
]),
|
|
503
|
-
],
|
|
504
|
-
);
|
|
505
|
-
|
|
506
|
-
const decode_dynamic_object =
|
|
507
|
-
(project: IProject) =>
|
|
508
|
-
(importer: FunctionImporter) =>
|
|
509
|
-
(
|
|
510
|
-
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
511
|
-
obj: MetadataObject,
|
|
512
|
-
required: boolean,
|
|
513
|
-
): ts.Statement[] => {
|
|
514
|
-
const top = obj.properties[0]!;
|
|
515
|
-
return decode_entry(project)(importer)({
|
|
516
|
-
initializer: () =>
|
|
517
|
-
ts.factory.createBinaryExpression(
|
|
518
|
-
accessor,
|
|
519
|
-
ts.factory.createToken(
|
|
520
|
-
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
521
|
-
),
|
|
522
|
-
ts.factory.createObjectLiteralExpression(),
|
|
523
|
-
),
|
|
524
|
-
setter: () =>
|
|
525
|
-
ts.factory.createBinaryExpression(
|
|
526
|
-
ts.factory.createElementAccessExpression(
|
|
527
|
-
accessor,
|
|
528
|
-
ts.factory.createIdentifier("entry.key"),
|
|
529
|
-
),
|
|
530
|
-
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
531
|
-
ts.factory.createIdentifier("entry.value"),
|
|
532
|
-
),
|
|
533
|
-
})(
|
|
534
|
-
MetadataProperty.create({
|
|
535
|
-
...top,
|
|
536
|
-
key: (() => {
|
|
537
|
-
const key: Metadata = Metadata.initialize();
|
|
538
|
-
key.atomics.push(
|
|
539
|
-
MetadataAtomic.create({
|
|
540
|
-
type: "string",
|
|
541
|
-
tags: [],
|
|
542
|
-
}),
|
|
543
|
-
);
|
|
544
|
-
return key;
|
|
545
|
-
})(),
|
|
546
|
-
}),
|
|
547
|
-
required,
|
|
548
|
-
);
|
|
549
|
-
};
|
|
550
|
-
|
|
551
|
-
const decode_map =
|
|
552
|
-
(project: IProject) =>
|
|
553
|
-
(importer: FunctionImporter) =>
|
|
554
|
-
(
|
|
555
|
-
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
556
|
-
map: Metadata.Entry,
|
|
557
|
-
required: boolean,
|
|
558
|
-
): ts.Statement[] =>
|
|
559
|
-
decode_entry(project)(importer)({
|
|
560
|
-
initializer: () =>
|
|
561
|
-
ts.factory.createBinaryExpression(
|
|
562
|
-
accessor,
|
|
563
|
-
ts.factory.createToken(
|
|
564
|
-
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
565
|
-
),
|
|
566
|
-
ts.factory.createNewExpression(
|
|
567
|
-
ts.factory.createIdentifier("Map"),
|
|
568
|
-
[
|
|
569
|
-
TypeFactory.keyword("any"),
|
|
570
|
-
TypeFactory.keyword("any"),
|
|
571
|
-
],
|
|
572
|
-
[],
|
|
573
|
-
),
|
|
574
|
-
),
|
|
575
|
-
setter: () =>
|
|
576
|
-
ts.factory.createCallExpression(
|
|
577
|
-
IdentifierFactory.access(accessor)("set"),
|
|
578
|
-
undefined,
|
|
579
|
-
[
|
|
580
|
-
ts.factory.createIdentifier("entry.key"),
|
|
581
|
-
ts.factory.createIdentifier("entry.value"),
|
|
582
|
-
],
|
|
583
|
-
),
|
|
584
|
-
})(map, required);
|
|
585
|
-
|
|
586
|
-
const decode_entry =
|
|
587
|
-
(project: IProject) =>
|
|
588
|
-
(importer: FunctionImporter) =>
|
|
589
|
-
(props: {
|
|
590
|
-
initializer: () => ts.Expression;
|
|
591
|
-
setter: () => ts.Expression;
|
|
592
|
-
}) =>
|
|
593
|
-
(map: Metadata.Entry, required: boolean): ts.Statement[] => {
|
|
594
|
-
const statements: ts.Statement[] = [
|
|
595
|
-
...(required
|
|
596
|
-
? []
|
|
597
|
-
: [
|
|
598
|
-
ts.factory.createExpressionStatement(
|
|
599
|
-
props.initializer(),
|
|
600
|
-
),
|
|
601
|
-
]),
|
|
602
|
-
StatementFactory.constant(
|
|
603
|
-
"piece",
|
|
604
|
-
ts.factory.createAdd(
|
|
605
|
-
ts.factory.createCallExpression(
|
|
606
|
-
IdentifierFactory.access(READER())("uint32"),
|
|
607
|
-
undefined,
|
|
608
|
-
undefined,
|
|
609
|
-
),
|
|
610
|
-
ts.factory.createCallExpression(
|
|
611
|
-
IdentifierFactory.access(READER())("index"),
|
|
612
|
-
undefined,
|
|
613
|
-
undefined,
|
|
614
|
-
),
|
|
615
|
-
),
|
|
616
|
-
),
|
|
617
|
-
...write_object_function_body(project)(importer)({
|
|
618
|
-
condition: ts.factory.createLessThan(
|
|
619
|
-
ts.factory.createCallExpression(
|
|
620
|
-
IdentifierFactory.access(READER())("index"),
|
|
621
|
-
undefined,
|
|
622
|
-
undefined,
|
|
623
|
-
),
|
|
624
|
-
ts.factory.createIdentifier("piece"),
|
|
625
|
-
),
|
|
626
|
-
tag: "kind",
|
|
627
|
-
output: "entry",
|
|
628
|
-
})([
|
|
629
|
-
MetadataProperty.create({
|
|
630
|
-
key: MetadataFactory.soleLiteral("key"),
|
|
631
|
-
value: map.key,
|
|
632
|
-
description: null,
|
|
633
|
-
jsDocTags: [],
|
|
634
|
-
}),
|
|
635
|
-
MetadataProperty.create({
|
|
636
|
-
key: MetadataFactory.soleLiteral("value"),
|
|
637
|
-
value: map.value,
|
|
638
|
-
description: null,
|
|
639
|
-
jsDocTags: [],
|
|
640
|
-
}),
|
|
641
|
-
]),
|
|
642
|
-
ts.factory.createExpressionStatement(props.setter()),
|
|
643
|
-
];
|
|
644
|
-
return [
|
|
645
|
-
ts.factory.createExpressionStatement(
|
|
646
|
-
ExpressionFactory.selfCall(
|
|
647
|
-
ts.factory.createBlock(statements, true),
|
|
648
|
-
),
|
|
649
|
-
),
|
|
650
|
-
];
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
const PREFIX = "$pd";
|
|
655
|
-
const READER = () => ts.factory.createIdentifier("reader");
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { ExpressionFactory } from "../../factories/ExpressionFactory";
|
|
4
|
+
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
5
|
+
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
6
|
+
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
7
|
+
import { ProtobufFactory } from "../../factories/ProtobufFactory";
|
|
8
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
9
|
+
import { TypeFactory } from "../../factories/TypeFactory";
|
|
10
|
+
|
|
11
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
12
|
+
import { MetadataArray } from "../../schemas/metadata/MetadataArray";
|
|
13
|
+
import { MetadataAtomic } from "../../schemas/metadata/MetadataAtomic";
|
|
14
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
15
|
+
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
16
|
+
|
|
17
|
+
import { IProject } from "../../transformers/IProject";
|
|
18
|
+
|
|
19
|
+
import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
|
|
20
|
+
|
|
21
|
+
import { FunctionImporter } from "../helpers/FunctionImporeter";
|
|
22
|
+
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
23
|
+
|
|
24
|
+
export namespace ProtobufDecodeProgrammer {
|
|
25
|
+
export const write =
|
|
26
|
+
(project: IProject) =>
|
|
27
|
+
(modulo: ts.LeftHandSideExpression) =>
|
|
28
|
+
(type: ts.Type, name?: string): ts.ArrowFunction => {
|
|
29
|
+
const importer: FunctionImporter = new FunctionImporter(
|
|
30
|
+
modulo.getText(),
|
|
31
|
+
);
|
|
32
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
33
|
+
const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
|
|
34
|
+
project.checker,
|
|
35
|
+
)(collection)(type);
|
|
36
|
+
|
|
37
|
+
const functors = collection
|
|
38
|
+
.objects()
|
|
39
|
+
.filter((obj) => ProtobufUtil.isStaticObject(obj))
|
|
40
|
+
.map((obj) =>
|
|
41
|
+
StatementFactory.constant(
|
|
42
|
+
`${PREFIX}o${obj.index}`,
|
|
43
|
+
write_object_function(project)(importer)(obj),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
const reader = StatementFactory.constant(
|
|
47
|
+
"reader",
|
|
48
|
+
ts.factory.createNewExpression(
|
|
49
|
+
importer.use("Reader"),
|
|
50
|
+
undefined,
|
|
51
|
+
[ts.factory.createIdentifier("input")],
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return ts.factory.createArrowFunction(
|
|
56
|
+
undefined,
|
|
57
|
+
undefined,
|
|
58
|
+
[
|
|
59
|
+
IdentifierFactory.parameter(
|
|
60
|
+
"input",
|
|
61
|
+
ts.factory.createTypeReferenceNode("Uint8Array"),
|
|
62
|
+
),
|
|
63
|
+
],
|
|
64
|
+
ts.factory.createTypeReferenceNode(
|
|
65
|
+
`typia.Resolved<${
|
|
66
|
+
name ?? TypeFactory.getFullName(project.checker)(type)
|
|
67
|
+
}>`,
|
|
68
|
+
),
|
|
69
|
+
undefined,
|
|
70
|
+
ts.factory.createBlock(
|
|
71
|
+
[
|
|
72
|
+
...importer.declare(modulo),
|
|
73
|
+
...functors,
|
|
74
|
+
reader,
|
|
75
|
+
ts.factory.createReturnStatement(
|
|
76
|
+
decode_regular_object(true)(meta.objects[0]!),
|
|
77
|
+
),
|
|
78
|
+
],
|
|
79
|
+
true,
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const write_object_function =
|
|
85
|
+
(project: IProject) =>
|
|
86
|
+
(importer: FunctionImporter) =>
|
|
87
|
+
(obj: MetadataObject): ts.ArrowFunction =>
|
|
88
|
+
ts.factory.createArrowFunction(
|
|
89
|
+
undefined,
|
|
90
|
+
undefined,
|
|
91
|
+
[
|
|
92
|
+
IdentifierFactory.parameter("reader"),
|
|
93
|
+
IdentifierFactory.parameter(
|
|
94
|
+
"length",
|
|
95
|
+
TypeFactory.keyword("number"),
|
|
96
|
+
ts.factory.createNumericLiteral(-1),
|
|
97
|
+
),
|
|
98
|
+
],
|
|
99
|
+
TypeFactory.keyword("any"),
|
|
100
|
+
undefined,
|
|
101
|
+
ts.factory.createBlock(
|
|
102
|
+
[
|
|
103
|
+
ts.factory.createExpressionStatement(
|
|
104
|
+
ts.factory.createBinaryExpression(
|
|
105
|
+
ts.factory.createIdentifier("length"),
|
|
106
|
+
ts.factory.createToken(
|
|
107
|
+
ts.SyntaxKind.EqualsToken,
|
|
108
|
+
),
|
|
109
|
+
ts.factory.createConditionalExpression(
|
|
110
|
+
ts.factory.createLessThan(
|
|
111
|
+
ts.factory.createIdentifier("length"),
|
|
112
|
+
ts.factory.createNumericLiteral(0),
|
|
113
|
+
),
|
|
114
|
+
undefined,
|
|
115
|
+
ts.factory.createCallExpression(
|
|
116
|
+
IdentifierFactory.access(READER())(
|
|
117
|
+
"size",
|
|
118
|
+
),
|
|
119
|
+
undefined,
|
|
120
|
+
undefined,
|
|
121
|
+
),
|
|
122
|
+
undefined,
|
|
123
|
+
ts.factory.createAdd(
|
|
124
|
+
ts.factory.createCallExpression(
|
|
125
|
+
IdentifierFactory.access(READER())(
|
|
126
|
+
"index",
|
|
127
|
+
),
|
|
128
|
+
undefined,
|
|
129
|
+
undefined,
|
|
130
|
+
),
|
|
131
|
+
ts.factory.createIdentifier("length"),
|
|
132
|
+
),
|
|
133
|
+
),
|
|
134
|
+
),
|
|
135
|
+
),
|
|
136
|
+
...write_object_function_body(project)(importer)({
|
|
137
|
+
condition: ts.factory.createLessThan(
|
|
138
|
+
ts.factory.createCallExpression(
|
|
139
|
+
IdentifierFactory.access(READER())("index"),
|
|
140
|
+
undefined,
|
|
141
|
+
undefined,
|
|
142
|
+
),
|
|
143
|
+
ts.factory.createIdentifier("length"),
|
|
144
|
+
),
|
|
145
|
+
tag: "tag",
|
|
146
|
+
output: "output",
|
|
147
|
+
})(obj.properties),
|
|
148
|
+
ts.factory.createReturnStatement(
|
|
149
|
+
ts.factory.createIdentifier("output"),
|
|
150
|
+
),
|
|
151
|
+
],
|
|
152
|
+
true,
|
|
153
|
+
),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const write_object_function_body =
|
|
157
|
+
(project: IProject) =>
|
|
158
|
+
(importer: FunctionImporter) =>
|
|
159
|
+
(props: { condition: ts.Expression; tag: string; output: string }) =>
|
|
160
|
+
(properties: MetadataProperty[]): ts.Statement[] => {
|
|
161
|
+
let i: number = 1;
|
|
162
|
+
const clauses: ts.CaseClause[] = properties
|
|
163
|
+
.map((p) => {
|
|
164
|
+
const clause = decode_property(project)(importer)(i)(
|
|
165
|
+
IdentifierFactory.access(
|
|
166
|
+
ts.factory.createIdentifier(props.output),
|
|
167
|
+
)(p.key.getSoleLiteral()!),
|
|
168
|
+
p.value,
|
|
169
|
+
);
|
|
170
|
+
i += ProtobufUtil.size(p.value);
|
|
171
|
+
return clause;
|
|
172
|
+
})
|
|
173
|
+
.flat();
|
|
174
|
+
return [
|
|
175
|
+
StatementFactory.constant(
|
|
176
|
+
props.output,
|
|
177
|
+
ts.factory.createObjectLiteralExpression(
|
|
178
|
+
properties.map((p) =>
|
|
179
|
+
ts.factory.createPropertyAssignment(
|
|
180
|
+
IdentifierFactory.identifier(
|
|
181
|
+
p.key.getSoleLiteral()!,
|
|
182
|
+
),
|
|
183
|
+
write_property_default_value(p.value),
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
true,
|
|
187
|
+
),
|
|
188
|
+
),
|
|
189
|
+
ts.factory.createWhileStatement(
|
|
190
|
+
props.condition,
|
|
191
|
+
ts.factory.createBlock([
|
|
192
|
+
StatementFactory.constant(
|
|
193
|
+
props.tag,
|
|
194
|
+
ts.factory.createCallExpression(
|
|
195
|
+
IdentifierFactory.access(READER())("uint32"),
|
|
196
|
+
undefined,
|
|
197
|
+
undefined,
|
|
198
|
+
),
|
|
199
|
+
),
|
|
200
|
+
ts.factory.createSwitchStatement(
|
|
201
|
+
ts.factory.createUnsignedRightShift(
|
|
202
|
+
ts.factory.createIdentifier(props.tag),
|
|
203
|
+
ts.factory.createNumericLiteral(3),
|
|
204
|
+
),
|
|
205
|
+
ts.factory.createCaseBlock([
|
|
206
|
+
...clauses,
|
|
207
|
+
ts.factory.createDefaultClause([
|
|
208
|
+
ts.factory.createExpressionStatement(
|
|
209
|
+
ts.factory.createCallExpression(
|
|
210
|
+
IdentifierFactory.access(READER())(
|
|
211
|
+
"skipType",
|
|
212
|
+
),
|
|
213
|
+
undefined,
|
|
214
|
+
[
|
|
215
|
+
ts.factory.createBitwiseAnd(
|
|
216
|
+
ts.factory.createIdentifier(
|
|
217
|
+
props.tag,
|
|
218
|
+
),
|
|
219
|
+
ts.factory.createNumericLiteral(
|
|
220
|
+
7,
|
|
221
|
+
),
|
|
222
|
+
),
|
|
223
|
+
],
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
ts.factory.createBreakStatement(),
|
|
227
|
+
]),
|
|
228
|
+
]),
|
|
229
|
+
),
|
|
230
|
+
]),
|
|
231
|
+
),
|
|
232
|
+
];
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const write_property_default_value = (value: Metadata) =>
|
|
236
|
+
ts.factory.createAsExpression(
|
|
237
|
+
value.nullable
|
|
238
|
+
? ts.factory.createNull()
|
|
239
|
+
: value.isRequired() === false
|
|
240
|
+
? ts.factory.createIdentifier("undefined")
|
|
241
|
+
: value.arrays.length
|
|
242
|
+
? ts.factory.createArrayLiteralExpression()
|
|
243
|
+
: value.maps.length
|
|
244
|
+
? ts.factory.createNewExpression(
|
|
245
|
+
ts.factory.createIdentifier("Map"),
|
|
246
|
+
undefined,
|
|
247
|
+
[],
|
|
248
|
+
)
|
|
249
|
+
: value.natives.length
|
|
250
|
+
? ts.factory.createNewExpression(
|
|
251
|
+
ts.factory.createIdentifier("Uint8Array"),
|
|
252
|
+
undefined,
|
|
253
|
+
[],
|
|
254
|
+
)
|
|
255
|
+
: value.atomics.some((a) => a.type === "string") ||
|
|
256
|
+
value.constants.some(
|
|
257
|
+
(c) =>
|
|
258
|
+
c.type === "string" && c.values.some((v) => v === ""),
|
|
259
|
+
) ||
|
|
260
|
+
value.templates.some(
|
|
261
|
+
(tpl) =>
|
|
262
|
+
tpl.length === 1 && tpl[0]!.getName() === "string",
|
|
263
|
+
)
|
|
264
|
+
? ts.factory.createStringLiteral("")
|
|
265
|
+
: value.objects.length &&
|
|
266
|
+
value.objects.some((obj) => !ProtobufUtil.isStaticObject(obj))
|
|
267
|
+
? ts.factory.createObjectLiteralExpression()
|
|
268
|
+
: ts.factory.createIdentifier("undefined"),
|
|
269
|
+
TypeFactory.keyword("any"),
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
/* -----------------------------------------------------------
|
|
273
|
+
DECODERS
|
|
274
|
+
----------------------------------------------------------- */
|
|
275
|
+
const decode_property =
|
|
276
|
+
(project: IProject) =>
|
|
277
|
+
(importer: FunctionImporter) =>
|
|
278
|
+
(index: number) =>
|
|
279
|
+
(
|
|
280
|
+
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
281
|
+
meta: Metadata,
|
|
282
|
+
): ts.CaseClause[] => {
|
|
283
|
+
const clauses: ts.CaseClause[] = [];
|
|
284
|
+
const emplace =
|
|
285
|
+
(name: string) => (v: ts.Expression | ts.Statement[]) =>
|
|
286
|
+
clauses.push(
|
|
287
|
+
ts.factory.createCaseClause(
|
|
288
|
+
ts.factory.createNumericLiteral(index++),
|
|
289
|
+
Array.isArray(v)
|
|
290
|
+
? [
|
|
291
|
+
ts.factory.createExpressionStatement(
|
|
292
|
+
ts.factory.createIdentifier(
|
|
293
|
+
`// type: ${name}`,
|
|
294
|
+
),
|
|
295
|
+
),
|
|
296
|
+
...v,
|
|
297
|
+
ts.factory.createBreakStatement(),
|
|
298
|
+
]
|
|
299
|
+
: [
|
|
300
|
+
ts.factory.createExpressionStatement(
|
|
301
|
+
ts.factory.createIdentifier(
|
|
302
|
+
`// ${name}`,
|
|
303
|
+
),
|
|
304
|
+
),
|
|
305
|
+
ts.factory.createExpressionStatement(
|
|
306
|
+
ts.factory.createBinaryExpression(
|
|
307
|
+
accessor,
|
|
308
|
+
ts.factory.createToken(
|
|
309
|
+
ts.SyntaxKind.EqualsToken,
|
|
310
|
+
),
|
|
311
|
+
v,
|
|
312
|
+
),
|
|
313
|
+
),
|
|
314
|
+
ts.factory.createBreakStatement(),
|
|
315
|
+
],
|
|
316
|
+
),
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const required: boolean = meta.isRequired() && !meta.nullable;
|
|
320
|
+
for (const atomic of ProtobufUtil.getAtomics(meta))
|
|
321
|
+
emplace(atomic)(decode_atomic(meta)(atomic));
|
|
322
|
+
if (meta.natives.length) emplace("bytes")(decode_bytes("bytes"));
|
|
323
|
+
for (const array of meta.arrays)
|
|
324
|
+
emplace(`Array<${array.type.value.getName()}>`)(
|
|
325
|
+
decode_array(accessor, array, required),
|
|
326
|
+
);
|
|
327
|
+
for (const map of meta.maps)
|
|
328
|
+
emplace(`Map<string, ${map.value.getName()}>`)(
|
|
329
|
+
decode_map(project)(importer)(accessor, map, required),
|
|
330
|
+
);
|
|
331
|
+
for (const obj of meta.objects)
|
|
332
|
+
emplace(obj.name)(
|
|
333
|
+
ProtobufUtil.isStaticObject(obj)
|
|
334
|
+
? decode_regular_object(false)(obj)
|
|
335
|
+
: decode_dynamic_object(project)(importer)(
|
|
336
|
+
accessor,
|
|
337
|
+
obj,
|
|
338
|
+
required,
|
|
339
|
+
),
|
|
340
|
+
);
|
|
341
|
+
return clauses;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const decode_atomic =
|
|
345
|
+
(meta: Metadata) =>
|
|
346
|
+
(atomic: ProtobufAtomic): ts.Expression => {
|
|
347
|
+
if (atomic === "string") return decode_bytes("string");
|
|
348
|
+
|
|
349
|
+
const call: ts.CallExpression = ts.factory.createCallExpression(
|
|
350
|
+
IdentifierFactory.access(ts.factory.createIdentifier("reader"))(
|
|
351
|
+
atomic,
|
|
352
|
+
),
|
|
353
|
+
undefined,
|
|
354
|
+
undefined,
|
|
355
|
+
);
|
|
356
|
+
if (atomic !== "int64" && atomic !== "uint64") return call;
|
|
357
|
+
|
|
358
|
+
const isNumber: boolean = ProtobufUtil.getNumbers(meta).some(
|
|
359
|
+
(n) => n === atomic,
|
|
360
|
+
);
|
|
361
|
+
return isNumber
|
|
362
|
+
? ts.factory.createCallExpression(
|
|
363
|
+
ts.factory.createIdentifier("Number"),
|
|
364
|
+
undefined,
|
|
365
|
+
[call],
|
|
366
|
+
)
|
|
367
|
+
: call;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const decode_bytes = (method: "bytes" | "string"): ts.Expression =>
|
|
371
|
+
ts.factory.createCallExpression(
|
|
372
|
+
IdentifierFactory.access(ts.factory.createIdentifier("reader"))(
|
|
373
|
+
method,
|
|
374
|
+
),
|
|
375
|
+
undefined,
|
|
376
|
+
undefined,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const decode_array = (
|
|
380
|
+
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
381
|
+
array: MetadataArray,
|
|
382
|
+
required: boolean,
|
|
383
|
+
): ts.Statement[] => {
|
|
384
|
+
const statements: Array<ts.Expression | ts.Statement> = [];
|
|
385
|
+
if (required === false)
|
|
386
|
+
statements.push(
|
|
387
|
+
ts.factory.createBinaryExpression(
|
|
388
|
+
accessor,
|
|
389
|
+
ts.factory.createToken(
|
|
390
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
391
|
+
),
|
|
392
|
+
ts.factory.createAsExpression(
|
|
393
|
+
ts.factory.createArrayLiteralExpression(),
|
|
394
|
+
ts.factory.createTypeReferenceNode("any[]"),
|
|
395
|
+
),
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
const atomics = ProtobufUtil.getAtomics(array.type.value);
|
|
399
|
+
const decoder = atomics.length
|
|
400
|
+
? () => decode_atomic(array.type.value)(atomics[0]!)
|
|
401
|
+
: array.type.value.natives.length
|
|
402
|
+
? () => decode_bytes("bytes")
|
|
403
|
+
: array.type.value.objects.length
|
|
404
|
+
? () => decode_regular_object(false)(array.type.value.objects[0]!)
|
|
405
|
+
: null;
|
|
406
|
+
if (decoder === null) throw new Error("Never reach here.");
|
|
407
|
+
else if (atomics.length && atomics[0] !== "string") {
|
|
408
|
+
statements.push(
|
|
409
|
+
ts.factory.createIfStatement(
|
|
410
|
+
ts.factory.createStrictEquality(
|
|
411
|
+
ts.factory.createNumericLiteral(2),
|
|
412
|
+
ts.factory.createBitwiseAnd(
|
|
413
|
+
ts.factory.createIdentifier("tag"),
|
|
414
|
+
ts.factory.createNumericLiteral(7),
|
|
415
|
+
),
|
|
416
|
+
),
|
|
417
|
+
ts.factory.createBlock(
|
|
418
|
+
[
|
|
419
|
+
StatementFactory.constant(
|
|
420
|
+
"piece",
|
|
421
|
+
ts.factory.createAdd(
|
|
422
|
+
ts.factory.createCallExpression(
|
|
423
|
+
IdentifierFactory.access(READER())(
|
|
424
|
+
"uint32",
|
|
425
|
+
),
|
|
426
|
+
undefined,
|
|
427
|
+
undefined,
|
|
428
|
+
),
|
|
429
|
+
ts.factory.createCallExpression(
|
|
430
|
+
IdentifierFactory.access(READER())(
|
|
431
|
+
"index",
|
|
432
|
+
),
|
|
433
|
+
undefined,
|
|
434
|
+
undefined,
|
|
435
|
+
),
|
|
436
|
+
),
|
|
437
|
+
),
|
|
438
|
+
ts.factory.createWhileStatement(
|
|
439
|
+
ts.factory.createLessThan(
|
|
440
|
+
ts.factory.createCallExpression(
|
|
441
|
+
IdentifierFactory.access(READER())(
|
|
442
|
+
"index",
|
|
443
|
+
),
|
|
444
|
+
undefined,
|
|
445
|
+
undefined,
|
|
446
|
+
),
|
|
447
|
+
ts.factory.createIdentifier("piece"),
|
|
448
|
+
),
|
|
449
|
+
ts.factory.createExpressionStatement(
|
|
450
|
+
ts.factory.createCallExpression(
|
|
451
|
+
IdentifierFactory.access(accessor)(
|
|
452
|
+
"push",
|
|
453
|
+
),
|
|
454
|
+
undefined,
|
|
455
|
+
[decoder()],
|
|
456
|
+
),
|
|
457
|
+
),
|
|
458
|
+
),
|
|
459
|
+
],
|
|
460
|
+
true,
|
|
461
|
+
),
|
|
462
|
+
ts.factory.createExpressionStatement(
|
|
463
|
+
ts.factory.createCallExpression(
|
|
464
|
+
IdentifierFactory.access(accessor)("push"),
|
|
465
|
+
undefined,
|
|
466
|
+
[decoder()],
|
|
467
|
+
),
|
|
468
|
+
),
|
|
469
|
+
),
|
|
470
|
+
);
|
|
471
|
+
} else
|
|
472
|
+
statements.push(
|
|
473
|
+
ts.factory.createCallExpression(
|
|
474
|
+
IdentifierFactory.access(accessor)("push"),
|
|
475
|
+
undefined,
|
|
476
|
+
[decoder()],
|
|
477
|
+
),
|
|
478
|
+
);
|
|
479
|
+
return statements.map((stmt) =>
|
|
480
|
+
ts.isExpression(stmt)
|
|
481
|
+
? ts.factory.createExpressionStatement(stmt)
|
|
482
|
+
: stmt,
|
|
483
|
+
);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const decode_regular_object =
|
|
487
|
+
(top: boolean) =>
|
|
488
|
+
(obj: MetadataObject): ts.Expression =>
|
|
489
|
+
ts.factory.createCallExpression(
|
|
490
|
+
ts.factory.createIdentifier(`${PREFIX}o${obj.index}`),
|
|
491
|
+
undefined,
|
|
492
|
+
[
|
|
493
|
+
ts.factory.createIdentifier("reader"),
|
|
494
|
+
...(top
|
|
495
|
+
? []
|
|
496
|
+
: [
|
|
497
|
+
ts.factory.createCallExpression(
|
|
498
|
+
IdentifierFactory.access(READER())("uint32"),
|
|
499
|
+
undefined,
|
|
500
|
+
undefined,
|
|
501
|
+
),
|
|
502
|
+
]),
|
|
503
|
+
],
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
const decode_dynamic_object =
|
|
507
|
+
(project: IProject) =>
|
|
508
|
+
(importer: FunctionImporter) =>
|
|
509
|
+
(
|
|
510
|
+
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
511
|
+
obj: MetadataObject,
|
|
512
|
+
required: boolean,
|
|
513
|
+
): ts.Statement[] => {
|
|
514
|
+
const top = obj.properties[0]!;
|
|
515
|
+
return decode_entry(project)(importer)({
|
|
516
|
+
initializer: () =>
|
|
517
|
+
ts.factory.createBinaryExpression(
|
|
518
|
+
accessor,
|
|
519
|
+
ts.factory.createToken(
|
|
520
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
521
|
+
),
|
|
522
|
+
ts.factory.createObjectLiteralExpression(),
|
|
523
|
+
),
|
|
524
|
+
setter: () =>
|
|
525
|
+
ts.factory.createBinaryExpression(
|
|
526
|
+
ts.factory.createElementAccessExpression(
|
|
527
|
+
accessor,
|
|
528
|
+
ts.factory.createIdentifier("entry.key"),
|
|
529
|
+
),
|
|
530
|
+
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
531
|
+
ts.factory.createIdentifier("entry.value"),
|
|
532
|
+
),
|
|
533
|
+
})(
|
|
534
|
+
MetadataProperty.create({
|
|
535
|
+
...top,
|
|
536
|
+
key: (() => {
|
|
537
|
+
const key: Metadata = Metadata.initialize();
|
|
538
|
+
key.atomics.push(
|
|
539
|
+
MetadataAtomic.create({
|
|
540
|
+
type: "string",
|
|
541
|
+
tags: [],
|
|
542
|
+
}),
|
|
543
|
+
);
|
|
544
|
+
return key;
|
|
545
|
+
})(),
|
|
546
|
+
}),
|
|
547
|
+
required,
|
|
548
|
+
);
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
const decode_map =
|
|
552
|
+
(project: IProject) =>
|
|
553
|
+
(importer: FunctionImporter) =>
|
|
554
|
+
(
|
|
555
|
+
accessor: ts.ElementAccessExpression | ts.PropertyAccessExpression,
|
|
556
|
+
map: Metadata.Entry,
|
|
557
|
+
required: boolean,
|
|
558
|
+
): ts.Statement[] =>
|
|
559
|
+
decode_entry(project)(importer)({
|
|
560
|
+
initializer: () =>
|
|
561
|
+
ts.factory.createBinaryExpression(
|
|
562
|
+
accessor,
|
|
563
|
+
ts.factory.createToken(
|
|
564
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
565
|
+
),
|
|
566
|
+
ts.factory.createNewExpression(
|
|
567
|
+
ts.factory.createIdentifier("Map"),
|
|
568
|
+
[
|
|
569
|
+
TypeFactory.keyword("any"),
|
|
570
|
+
TypeFactory.keyword("any"),
|
|
571
|
+
],
|
|
572
|
+
[],
|
|
573
|
+
),
|
|
574
|
+
),
|
|
575
|
+
setter: () =>
|
|
576
|
+
ts.factory.createCallExpression(
|
|
577
|
+
IdentifierFactory.access(accessor)("set"),
|
|
578
|
+
undefined,
|
|
579
|
+
[
|
|
580
|
+
ts.factory.createIdentifier("entry.key"),
|
|
581
|
+
ts.factory.createIdentifier("entry.value"),
|
|
582
|
+
],
|
|
583
|
+
),
|
|
584
|
+
})(map, required);
|
|
585
|
+
|
|
586
|
+
const decode_entry =
|
|
587
|
+
(project: IProject) =>
|
|
588
|
+
(importer: FunctionImporter) =>
|
|
589
|
+
(props: {
|
|
590
|
+
initializer: () => ts.Expression;
|
|
591
|
+
setter: () => ts.Expression;
|
|
592
|
+
}) =>
|
|
593
|
+
(map: Metadata.Entry, required: boolean): ts.Statement[] => {
|
|
594
|
+
const statements: ts.Statement[] = [
|
|
595
|
+
...(required
|
|
596
|
+
? []
|
|
597
|
+
: [
|
|
598
|
+
ts.factory.createExpressionStatement(
|
|
599
|
+
props.initializer(),
|
|
600
|
+
),
|
|
601
|
+
]),
|
|
602
|
+
StatementFactory.constant(
|
|
603
|
+
"piece",
|
|
604
|
+
ts.factory.createAdd(
|
|
605
|
+
ts.factory.createCallExpression(
|
|
606
|
+
IdentifierFactory.access(READER())("uint32"),
|
|
607
|
+
undefined,
|
|
608
|
+
undefined,
|
|
609
|
+
),
|
|
610
|
+
ts.factory.createCallExpression(
|
|
611
|
+
IdentifierFactory.access(READER())("index"),
|
|
612
|
+
undefined,
|
|
613
|
+
undefined,
|
|
614
|
+
),
|
|
615
|
+
),
|
|
616
|
+
),
|
|
617
|
+
...write_object_function_body(project)(importer)({
|
|
618
|
+
condition: ts.factory.createLessThan(
|
|
619
|
+
ts.factory.createCallExpression(
|
|
620
|
+
IdentifierFactory.access(READER())("index"),
|
|
621
|
+
undefined,
|
|
622
|
+
undefined,
|
|
623
|
+
),
|
|
624
|
+
ts.factory.createIdentifier("piece"),
|
|
625
|
+
),
|
|
626
|
+
tag: "kind",
|
|
627
|
+
output: "entry",
|
|
628
|
+
})([
|
|
629
|
+
MetadataProperty.create({
|
|
630
|
+
key: MetadataFactory.soleLiteral("key"),
|
|
631
|
+
value: map.key,
|
|
632
|
+
description: null,
|
|
633
|
+
jsDocTags: [],
|
|
634
|
+
}),
|
|
635
|
+
MetadataProperty.create({
|
|
636
|
+
key: MetadataFactory.soleLiteral("value"),
|
|
637
|
+
value: map.value,
|
|
638
|
+
description: null,
|
|
639
|
+
jsDocTags: [],
|
|
640
|
+
}),
|
|
641
|
+
]),
|
|
642
|
+
ts.factory.createExpressionStatement(props.setter()),
|
|
643
|
+
];
|
|
644
|
+
return [
|
|
645
|
+
ts.factory.createExpressionStatement(
|
|
646
|
+
ExpressionFactory.selfCall(
|
|
647
|
+
ts.factory.createBlock(statements, true),
|
|
648
|
+
),
|
|
649
|
+
),
|
|
650
|
+
];
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const PREFIX = "$pd";
|
|
655
|
+
const READER = () => ts.factory.createIdentifier("reader");
|