typia 7.0.0-dev.20241007 → 7.0.0-dev.20241008
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/factories/ExpressionFactory.d.ts +1 -1
- package/lib/factories/ValueFactory.d.ts +1 -1
- package/lib/programmers/AssertProgrammer.d.ts +5 -2
- package/lib/programmers/AssertProgrammer.js +12 -5
- package/lib/programmers/AssertProgrammer.js.map +1 -1
- package/lib/programmers/ImportProgrammer.d.ts +25 -4
- package/lib/programmers/ImportProgrammer.js +67 -31
- package/lib/programmers/ImportProgrammer.js.map +1 -1
- package/lib/programmers/RandomProgrammer.js +48 -32
- package/lib/programmers/RandomProgrammer.js.map +1 -1
- package/lib/programmers/ValidateProgrammer.js +10 -4
- package/lib/programmers/ValidateProgrammer.js.map +1 -1
- package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js +1 -1
- package/lib/programmers/functional/FunctionalAssertFunctionProgrammer.js.map +1 -1
- package/lib/programmers/functional/FunctionalValidateFunctionProgrammer.d.ts +1 -0
- package/lib/programmers/functional/FunctionalValidateFunctionProgrammer.js +14 -5
- package/lib/programmers/functional/FunctionalValidateFunctionProgrammer.js.map +1 -1
- package/lib/programmers/functional/FunctionalValidateParametersProgrammer.js +5 -1
- package/lib/programmers/functional/FunctionalValidateParametersProgrammer.js.map +1 -1
- package/lib/programmers/functional/FunctionalValidateReturnProgrammer.js +1 -0
- package/lib/programmers/functional/FunctionalValidateReturnProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpAssertFormDataProgrammer.js +4 -1
- package/lib/programmers/http/HttpAssertFormDataProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpAssertHeadersProgrammer.js +4 -1
- package/lib/programmers/http/HttpAssertHeadersProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpAssertQueryProgrammer.js +4 -1
- package/lib/programmers/http/HttpAssertQueryProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpFormDataProgrammer.js +10 -6
- package/lib/programmers/http/HttpFormDataProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpHeadersProgrammer.js +10 -6
- package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpQueryProgrammer.js +10 -6
- package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpValidateFormDataProgrammer.js +5 -3
- package/lib/programmers/http/HttpValidateFormDataProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpValidateHeadersProgrammer.js +5 -3
- package/lib/programmers/http/HttpValidateHeadersProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpValidateQueryProgrammer.js +5 -3
- package/lib/programmers/http/HttpValidateQueryProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonAssertParseProgrammer.js +14 -7
- package/lib/programmers/json/JsonAssertParseProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonAssertStringifyProgrammer.js +4 -1
- package/lib/programmers/json/JsonAssertStringifyProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonIsParseProgrammer.js +10 -6
- package/lib/programmers/json/JsonIsParseProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonValidateParseProgrammer.js +16 -4
- package/lib/programmers/json/JsonValidateParseProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonValidateStringifyProgrammer.js +7 -3
- package/lib/programmers/json/JsonValidateStringifyProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscAssertCloneProgrammer.js +4 -1
- package/lib/programmers/misc/MiscAssertCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscAssertPruneProgrammer.js +4 -1
- package/lib/programmers/misc/MiscAssertPruneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js +10 -6
- package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscValidateCloneProgrammer.js +5 -3
- package/lib/programmers/misc/MiscValidateCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscValidatePruneProgrammer.js +10 -4
- package/lib/programmers/misc/MiscValidatePruneProgrammer.js.map +1 -1
- package/lib/programmers/notations/NotationAssertGeneralProgrammer.js +4 -1
- package/lib/programmers/notations/NotationAssertGeneralProgrammer.js.map +1 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.d.ts +2 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.js +13 -5
- package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
- package/lib/programmers/notations/NotationValidateGeneralProgrammer.js +5 -3
- package/lib/programmers/notations/NotationValidateGeneralProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufAssertDecodeProgrammer.js +4 -1
- package/lib/programmers/protobuf/ProtobufAssertDecodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufAssertEncodeProgrammer.js +4 -1
- package/lib/programmers/protobuf/ProtobufAssertEncodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js +10 -6
- package/lib/programmers/protobuf/ProtobufDecodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufValidateDecodeProgrammer.js +5 -3
- package/lib/programmers/protobuf/ProtobufValidateDecodeProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufValidateEncodeProgrammer.js +7 -3
- package/lib/programmers/protobuf/ProtobufValidateEncodeProgrammer.js.map +1 -1
- package/package.json +1 -1
- package/src/factories/JsonMetadataFactory.ts +63 -63
- package/src/factories/ProtobufFactory.ts +275 -275
- package/src/programmers/AssertProgrammer.ts +454 -456
- package/src/programmers/CheckerProgrammer.ts +1610 -1610
- package/src/programmers/ImportProgrammer.ts +184 -109
- package/src/programmers/IsProgrammer.ts +273 -273
- package/src/programmers/RandomProgrammer.ts +168 -149
- package/src/programmers/TypiaProgrammer.ts +171 -171
- package/src/programmers/ValidateProgrammer.ts +434 -430
- package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +1 -1
- package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +12 -16
- package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +5 -12
- package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +1 -0
- package/src/programmers/helpers/OptionPredicator.ts +15 -15
- package/src/programmers/http/HttpAssertFormDataProgrammer.ts +99 -96
- package/src/programmers/http/HttpAssertHeadersProgrammer.ts +99 -96
- package/src/programmers/http/HttpAssertQueryProgrammer.ts +105 -102
- package/src/programmers/http/HttpFormDataProgrammer.ts +304 -308
- package/src/programmers/http/HttpHeadersProgrammer.ts +400 -404
- package/src/programmers/http/HttpIsFormDataProgrammer.ts +108 -108
- package/src/programmers/http/HttpIsHeadersProgrammer.ts +108 -108
- package/src/programmers/http/HttpIsQueryProgrammer.ts +114 -114
- package/src/programmers/http/HttpParameterProgrammer.ts +115 -115
- package/src/programmers/http/HttpQueryProgrammer.ts +329 -333
- package/src/programmers/http/HttpValidateFormDataProgrammer.ts +92 -90
- package/src/programmers/http/HttpValidateHeadersProgrammer.ts +92 -90
- package/src/programmers/http/HttpValidateQueryProgrammer.ts +98 -96
- package/src/programmers/json/JsonApplicationProgrammer.ts +92 -92
- package/src/programmers/json/JsonAssertParseProgrammer.ts +103 -104
- package/src/programmers/json/JsonAssertStringifyProgrammer.ts +115 -112
- package/src/programmers/json/JsonIsParseProgrammer.ts +114 -118
- package/src/programmers/json/JsonIsStringifyProgrammer.ts +108 -108
- package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
- package/src/programmers/json/JsonValidateParseProgrammer.ts +105 -95
- package/src/programmers/json/JsonValidateStringifyProgrammer.ts +124 -119
- package/src/programmers/misc/MiscAssertCloneProgrammer.ts +95 -92
- package/src/programmers/misc/MiscAssertPruneProgrammer.ts +116 -113
- package/src/programmers/misc/MiscCloneProgrammer.ts +1025 -1029
- package/src/programmers/misc/MiscIsCloneProgrammer.ts +99 -99
- package/src/programmers/misc/MiscIsPruneProgrammer.ts +97 -97
- package/src/programmers/misc/MiscLiteralsProgrammer.ts +80 -80
- package/src/programmers/misc/MiscPruneProgrammer.ts +725 -725
- package/src/programmers/misc/MiscValidateCloneProgrammer.ts +111 -109
- package/src/programmers/misc/MiscValidatePruneProgrammer.ts +113 -109
- package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +101 -98
- package/src/programmers/notations/NotationGeneralProgrammer.ts +977 -973
- package/src/programmers/notations/NotationIsGeneralProgrammer.ts +105 -105
- package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +119 -117
- package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +98 -95
- package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +102 -99
- package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +708 -711
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +1019 -1019
- package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +109 -109
- package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +98 -98
- package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +92 -90
- package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +119 -114
- package/src/transform.ts +35 -35
- package/src/transformers/CallExpressionTransformer.ts +540 -540
- package/src/transformers/FileTransformer.ts +120 -120
- package/src/transformers/IProgrammerProps.ts +11 -11
- package/src/transformers/ITransformOptions.ts +62 -62
- package/src/transformers/ITransformProps.ts +9 -9
- package/src/transformers/ITypiaContext.ts +18 -18
- package/src/transformers/ImportTransformer.ts +66 -66
- package/src/transformers/NodeTransformer.ts +17 -17
- package/src/transformers/TransformerError.ts +59 -59
- package/src/transformers/features/AssertTransformer.ts +24 -24
- package/src/transformers/features/CreateAssertTransformer.ts +24 -24
- package/src/transformers/features/CreateIsTransformer.ts +18 -18
- package/src/transformers/features/CreateRandomTransformer.ts +43 -43
- package/src/transformers/features/CreateValidateTransformer.ts +18 -18
- package/src/transformers/features/IsTransformer.ts +18 -18
- package/src/transformers/features/RandomTransformer.ts +41 -41
- package/src/transformers/features/ValidateTransformer.ts +18 -18
- package/src/transformers/features/functional/FunctionalGenericTransformer.ts +57 -57
- package/src/transformers/features/http/CreateHttpAssertFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpIsFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpParameterTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpQueryTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpValidateFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +13 -13
- package/src/transformers/features/http/HttpAssertFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/HttpAssertQueryTransformer.ts +13 -13
- package/src/transformers/features/http/HttpFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/HttpHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/HttpIsFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/HttpIsHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/HttpIsQueryTransformer.ts +13 -13
- package/src/transformers/features/http/HttpParameterTransformer.ts +13 -13
- package/src/transformers/features/http/HttpQueryTransformer.ts +13 -13
- package/src/transformers/features/http/HttpValidateFormDataTransformer.ts +13 -13
- package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +13 -13
- package/src/transformers/features/http/HttpValidateQueryTransformer.ts +13 -13
- package/src/transformers/features/json/JsonApplicationTransformer.ts +130 -130
- package/src/transformers/features/json/JsonAssertParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +13 -13
- package/src/transformers/features/json/JsonIsParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonIsStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonStringifyTransformer.ts +13 -13
- package/src/transformers/features/json/JsonValidateParseTransformer.ts +13 -13
- package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +13 -13
- package/src/transformers/features/llm/LlmApplicationTransformer.ts +86 -86
- package/src/transformers/features/llm/LlmSchemaTransformer.ts +59 -59
- package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscIsCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscIsPruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscLiteralsTransformer.ts +35 -35
- package/src/transformers/features/misc/MiscPruneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +13 -13
- package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +13 -13
- package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationGeneralTransformer.ts +18 -18
- package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +20 -20
- package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +20 -20
- package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +35 -35
- package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +13 -13
- package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +13 -13
- package/src/transformers/features/reflect/ReflectMetadataTransformer.ts +69 -69
- package/src/transformers/features/reflect/ReflectNameTransformer.ts +82 -82
- package/src/transformers/internal/GenericTransformer.ts +101 -101
|
@@ -1,1019 +1,1019 @@
|
|
|
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 { NumericRangeFactory } from "../../factories/NumericRangeFactory";
|
|
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 { IProgrammerProps } from "../../transformers/IProgrammerProps";
|
|
18
|
-
import { ITypiaContext } from "../../transformers/ITypiaContext";
|
|
19
|
-
|
|
20
|
-
import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
|
|
21
|
-
|
|
22
|
-
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
23
|
-
import { IsProgrammer } from "../IsProgrammer";
|
|
24
|
-
import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
|
|
25
|
-
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
26
|
-
import { ProtobufWire } from "../helpers/ProtobufWire";
|
|
27
|
-
import { UnionPredicator } from "../helpers/UnionPredicator";
|
|
28
|
-
import { decode_union_object } from "../internal/decode_union_object";
|
|
29
|
-
|
|
30
|
-
export namespace ProtobufEncodeProgrammer {
|
|
31
|
-
export const decompose = (props: {
|
|
32
|
-
context: ITypiaContext;
|
|
33
|
-
modulo: ts.LeftHandSideExpression;
|
|
34
|
-
functor: FunctionProgrammer;
|
|
35
|
-
type: ts.Type;
|
|
36
|
-
name: string | undefined;
|
|
37
|
-
}): FeatureProgrammer.IDecomposed => {
|
|
38
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
39
|
-
const metadata: Metadata = ProtobufFactory.metadata({
|
|
40
|
-
method: props.modulo.getText(),
|
|
41
|
-
checker: props.context.checker,
|
|
42
|
-
transformer: props.context.transformer,
|
|
43
|
-
collection,
|
|
44
|
-
type: props.type,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const callEncoder = (writer: string, factory: ts.NewExpression) =>
|
|
48
|
-
StatementFactory.constant({
|
|
49
|
-
name: writer,
|
|
50
|
-
value: ts.factory.createCallExpression(
|
|
51
|
-
ts.factory.createIdentifier("encoder"),
|
|
52
|
-
undefined,
|
|
53
|
-
[factory, ts.factory.createIdentifier("input")],
|
|
54
|
-
),
|
|
55
|
-
});
|
|
56
|
-
return {
|
|
57
|
-
functions: {
|
|
58
|
-
encoder: StatementFactory.constant({
|
|
59
|
-
name: props.functor.useLocal("encoder"),
|
|
60
|
-
value: write_encoder({
|
|
61
|
-
context: props.context,
|
|
62
|
-
functor: props.functor,
|
|
63
|
-
collection,
|
|
64
|
-
metadata,
|
|
65
|
-
}),
|
|
66
|
-
}),
|
|
67
|
-
},
|
|
68
|
-
statements: [],
|
|
69
|
-
arrow: ts.factory.createArrowFunction(
|
|
70
|
-
undefined,
|
|
71
|
-
undefined,
|
|
72
|
-
[
|
|
73
|
-
IdentifierFactory.parameter(
|
|
74
|
-
"input",
|
|
75
|
-
ts.factory.createTypeReferenceNode(
|
|
76
|
-
props.name ??
|
|
77
|
-
TypeFactory.getFullName({
|
|
78
|
-
checker: props.context.checker,
|
|
79
|
-
type: props.type,
|
|
80
|
-
}),
|
|
81
|
-
),
|
|
82
|
-
),
|
|
83
|
-
],
|
|
84
|
-
ts.factory.createTypeReferenceNode("Uint8Array"),
|
|
85
|
-
undefined,
|
|
86
|
-
ts.factory.createBlock(
|
|
87
|
-
[
|
|
88
|
-
callEncoder(
|
|
89
|
-
"sizer",
|
|
90
|
-
ts.factory.createNewExpression(
|
|
91
|
-
props.context.importer.internal("ProtobufSizer"),
|
|
92
|
-
undefined,
|
|
93
|
-
[],
|
|
94
|
-
),
|
|
95
|
-
),
|
|
96
|
-
callEncoder(
|
|
97
|
-
"writer",
|
|
98
|
-
ts.factory.createNewExpression(
|
|
99
|
-
props.context.importer.internal("ProtobufWriter"),
|
|
100
|
-
undefined,
|
|
101
|
-
[ts.factory.createIdentifier("sizer")],
|
|
102
|
-
),
|
|
103
|
-
),
|
|
104
|
-
ts.factory.createReturnStatement(
|
|
105
|
-
ts.factory.createCallExpression(
|
|
106
|
-
IdentifierFactory.access(WRITER(), "buffer"),
|
|
107
|
-
undefined,
|
|
108
|
-
undefined,
|
|
109
|
-
),
|
|
110
|
-
),
|
|
111
|
-
],
|
|
112
|
-
true,
|
|
113
|
-
),
|
|
114
|
-
),
|
|
115
|
-
};
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export const write = (props: IProgrammerProps): ts.CallExpression => {
|
|
119
|
-
const functor: FunctionProgrammer = new FunctionProgrammer(
|
|
120
|
-
props.modulo.getText(),
|
|
121
|
-
);
|
|
122
|
-
const result: FeatureProgrammer.IDecomposed = decompose({
|
|
123
|
-
...props,
|
|
124
|
-
functor,
|
|
125
|
-
});
|
|
126
|
-
return FeatureProgrammer.writeDecomposed({
|
|
127
|
-
modulo: props.modulo,
|
|
128
|
-
functor,
|
|
129
|
-
result,
|
|
130
|
-
});
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const write_encoder = (props: {
|
|
134
|
-
context: ITypiaContext;
|
|
135
|
-
functor: FunctionProgrammer;
|
|
136
|
-
collection: MetadataCollection;
|
|
137
|
-
metadata: Metadata;
|
|
138
|
-
}): ts.ArrowFunction => {
|
|
139
|
-
const functors = props.collection
|
|
140
|
-
.objects()
|
|
141
|
-
.filter((obj) => ProtobufUtil.isStaticObject(obj))
|
|
142
|
-
.map((object) =>
|
|
143
|
-
StatementFactory.constant({
|
|
144
|
-
name: `${PREFIX}o${object.index}`,
|
|
145
|
-
value: write_object_function({
|
|
146
|
-
context: props.context,
|
|
147
|
-
functor: props.functor,
|
|
148
|
-
input: ts.factory.createIdentifier("input"),
|
|
149
|
-
object,
|
|
150
|
-
explore: {
|
|
151
|
-
source: "function",
|
|
152
|
-
from: "object",
|
|
153
|
-
tracable: false,
|
|
154
|
-
postfix: "",
|
|
155
|
-
},
|
|
156
|
-
}),
|
|
157
|
-
}),
|
|
158
|
-
);
|
|
159
|
-
const main: ts.Block = decode({
|
|
160
|
-
context: props.context,
|
|
161
|
-
functor: props.functor,
|
|
162
|
-
index: null,
|
|
163
|
-
input: ts.factory.createIdentifier("input"),
|
|
164
|
-
metadata: props.metadata,
|
|
165
|
-
explore: {
|
|
166
|
-
source: "top",
|
|
167
|
-
from: "top",
|
|
168
|
-
tracable: false,
|
|
169
|
-
postfix: "",
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
return ts.factory.createArrowFunction(
|
|
173
|
-
undefined,
|
|
174
|
-
undefined,
|
|
175
|
-
[
|
|
176
|
-
IdentifierFactory.parameter("writer"),
|
|
177
|
-
IdentifierFactory.parameter("input"),
|
|
178
|
-
],
|
|
179
|
-
TypeFactory.keyword("any"),
|
|
180
|
-
undefined,
|
|
181
|
-
ts.factory.createBlock(
|
|
182
|
-
[
|
|
183
|
-
...props.functor.declareUnions(),
|
|
184
|
-
...functors,
|
|
185
|
-
...IsProgrammer.write_function_statements(props),
|
|
186
|
-
...main.statements,
|
|
187
|
-
ts.factory.createReturnStatement(
|
|
188
|
-
ts.factory.createIdentifier("writer"),
|
|
189
|
-
),
|
|
190
|
-
],
|
|
191
|
-
true,
|
|
192
|
-
),
|
|
193
|
-
);
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const write_object_function = (props: {
|
|
197
|
-
context: ITypiaContext;
|
|
198
|
-
functor: FunctionProgrammer;
|
|
199
|
-
input: ts.Expression;
|
|
200
|
-
object: MetadataObject;
|
|
201
|
-
explore: FeatureProgrammer.IExplore;
|
|
202
|
-
}): ts.ArrowFunction => {
|
|
203
|
-
let index: number = 1;
|
|
204
|
-
const body: ts.Statement[] = props.object.properties
|
|
205
|
-
.map((p) => {
|
|
206
|
-
const block = decode({
|
|
207
|
-
...props,
|
|
208
|
-
index,
|
|
209
|
-
input: IdentifierFactory.access(props.input, p.key.getSoleLiteral()!),
|
|
210
|
-
metadata: p.value,
|
|
211
|
-
});
|
|
212
|
-
index += ProtobufUtil.size(p.value);
|
|
213
|
-
return [
|
|
214
|
-
ts.factory.createExpressionStatement(
|
|
215
|
-
ts.factory.createIdentifier(
|
|
216
|
-
`// property "${p.key.getSoleLiteral()!}"`,
|
|
217
|
-
),
|
|
218
|
-
),
|
|
219
|
-
...block.statements,
|
|
220
|
-
];
|
|
221
|
-
})
|
|
222
|
-
.flat();
|
|
223
|
-
return ts.factory.createArrowFunction(
|
|
224
|
-
undefined,
|
|
225
|
-
undefined,
|
|
226
|
-
[IdentifierFactory.parameter("input")],
|
|
227
|
-
TypeFactory.keyword("any"),
|
|
228
|
-
undefined,
|
|
229
|
-
ts.factory.createBlock(body, true),
|
|
230
|
-
);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
/* -----------------------------------------------------------
|
|
234
|
-
DECODERS
|
|
235
|
-
----------------------------------------------------------- */
|
|
236
|
-
const decode = (props: {
|
|
237
|
-
context: ITypiaContext;
|
|
238
|
-
functor: FunctionProgrammer;
|
|
239
|
-
index: number | null;
|
|
240
|
-
input: ts.Expression;
|
|
241
|
-
metadata: Metadata;
|
|
242
|
-
explore: FeatureProgrammer.IExplore;
|
|
243
|
-
}): ts.Block => {
|
|
244
|
-
const wrapper: (block: ts.Block) => ts.Block =
|
|
245
|
-
props.metadata.isRequired() && props.metadata.nullable === false
|
|
246
|
-
? (block) => block
|
|
247
|
-
: props.metadata.isRequired() === false &&
|
|
248
|
-
props.metadata.nullable === true
|
|
249
|
-
? (block) =>
|
|
250
|
-
ts.factory.createBlock(
|
|
251
|
-
[
|
|
252
|
-
ts.factory.createIfStatement(
|
|
253
|
-
ts.factory.createLogicalAnd(
|
|
254
|
-
ts.factory.createStrictInequality(
|
|
255
|
-
ts.factory.createIdentifier("undefined"),
|
|
256
|
-
props.input,
|
|
257
|
-
),
|
|
258
|
-
ts.factory.createStrictInequality(
|
|
259
|
-
ts.factory.createNull(),
|
|
260
|
-
props.input,
|
|
261
|
-
),
|
|
262
|
-
),
|
|
263
|
-
block,
|
|
264
|
-
),
|
|
265
|
-
],
|
|
266
|
-
true,
|
|
267
|
-
)
|
|
268
|
-
: props.metadata.isRequired() === false
|
|
269
|
-
? (block) =>
|
|
270
|
-
ts.factory.createBlock(
|
|
271
|
-
[
|
|
272
|
-
ts.factory.createIfStatement(
|
|
273
|
-
ts.factory.createStrictInequality(
|
|
274
|
-
ts.factory.createIdentifier("undefined"),
|
|
275
|
-
props.input,
|
|
276
|
-
),
|
|
277
|
-
block,
|
|
278
|
-
),
|
|
279
|
-
],
|
|
280
|
-
true,
|
|
281
|
-
)
|
|
282
|
-
: (block) =>
|
|
283
|
-
ts.factory.createBlock(
|
|
284
|
-
[
|
|
285
|
-
ts.factory.createIfStatement(
|
|
286
|
-
ts.factory.createStrictInequality(
|
|
287
|
-
ts.factory.createNull(),
|
|
288
|
-
props.input,
|
|
289
|
-
),
|
|
290
|
-
block,
|
|
291
|
-
),
|
|
292
|
-
],
|
|
293
|
-
true,
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
// STARTS FROM ATOMIC TYPES
|
|
297
|
-
const unions: IUnion[] = [];
|
|
298
|
-
const numbers = ProtobufUtil.getNumbers(props.metadata);
|
|
299
|
-
const bigints = ProtobufUtil.getBigints(props.metadata);
|
|
300
|
-
|
|
301
|
-
for (const atom of ProtobufUtil.getAtomics(props.metadata))
|
|
302
|
-
if (atom === "bool")
|
|
303
|
-
unions.push({
|
|
304
|
-
type: "bool",
|
|
305
|
-
is: () =>
|
|
306
|
-
ts.factory.createStrictEquality(
|
|
307
|
-
ts.factory.createStringLiteral("boolean"),
|
|
308
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
309
|
-
),
|
|
310
|
-
value: (index) =>
|
|
311
|
-
decode_bool({
|
|
312
|
-
index,
|
|
313
|
-
input: props.input,
|
|
314
|
-
}),
|
|
315
|
-
});
|
|
316
|
-
else if (
|
|
317
|
-
atom === "int32" ||
|
|
318
|
-
atom === "uint32" ||
|
|
319
|
-
atom === "float" ||
|
|
320
|
-
atom === "double"
|
|
321
|
-
)
|
|
322
|
-
unions.push(
|
|
323
|
-
decode_number({
|
|
324
|
-
candidates: numbers,
|
|
325
|
-
type: atom,
|
|
326
|
-
input: props.input,
|
|
327
|
-
}),
|
|
328
|
-
);
|
|
329
|
-
else if (atom === "int64" || atom === "uint64")
|
|
330
|
-
if (numbers.some((n) => n === atom))
|
|
331
|
-
unions.push(
|
|
332
|
-
decode_number({
|
|
333
|
-
candidates: numbers,
|
|
334
|
-
type: atom,
|
|
335
|
-
input: props.input,
|
|
336
|
-
}),
|
|
337
|
-
);
|
|
338
|
-
else
|
|
339
|
-
unions.push(
|
|
340
|
-
decode_bigint({
|
|
341
|
-
candidates: bigints,
|
|
342
|
-
type: atom,
|
|
343
|
-
input: props.input,
|
|
344
|
-
}),
|
|
345
|
-
);
|
|
346
|
-
else if (atom === "string")
|
|
347
|
-
unions.push({
|
|
348
|
-
type: "string",
|
|
349
|
-
is: () =>
|
|
350
|
-
ts.factory.createStrictEquality(
|
|
351
|
-
ts.factory.createStringLiteral("string"),
|
|
352
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
353
|
-
),
|
|
354
|
-
value: (index) =>
|
|
355
|
-
decode_bytes({
|
|
356
|
-
method: "string",
|
|
357
|
-
index: index!,
|
|
358
|
-
input: props.input,
|
|
359
|
-
}),
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
// CONSIDER BYTES
|
|
363
|
-
if (props.metadata.natives.length)
|
|
364
|
-
unions.push({
|
|
365
|
-
type: "bytes",
|
|
366
|
-
is: () => ExpressionFactory.isInstanceOf("Uint8Array", props.input),
|
|
367
|
-
value: (index) =>
|
|
368
|
-
decode_bytes({
|
|
369
|
-
method: "bytes",
|
|
370
|
-
index: index!,
|
|
371
|
-
input: props.input,
|
|
372
|
-
}),
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
// CONSIDER ARRAYS
|
|
376
|
-
if (props.metadata.arrays.length)
|
|
377
|
-
unions.push({
|
|
378
|
-
type: "array",
|
|
379
|
-
is: () => ExpressionFactory.isArray(props.input),
|
|
380
|
-
value: (index) =>
|
|
381
|
-
decode_array({
|
|
382
|
-
...props,
|
|
383
|
-
array: props.metadata.arrays[0]!,
|
|
384
|
-
explore: {
|
|
385
|
-
...props.explore,
|
|
386
|
-
from: "array",
|
|
387
|
-
},
|
|
388
|
-
index: index!,
|
|
389
|
-
}),
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
// CONSIDER MAPS
|
|
393
|
-
if (props.metadata.maps.length)
|
|
394
|
-
unions.push({
|
|
395
|
-
type: "map",
|
|
396
|
-
is: () => ExpressionFactory.isInstanceOf("Map", props.input),
|
|
397
|
-
value: (index) =>
|
|
398
|
-
decode_map({
|
|
399
|
-
...props,
|
|
400
|
-
index: index!,
|
|
401
|
-
entry: props.metadata.maps[0]!,
|
|
402
|
-
explore: {
|
|
403
|
-
...props.explore,
|
|
404
|
-
from: "array",
|
|
405
|
-
},
|
|
406
|
-
}),
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
// CONSIDER OBJECTS
|
|
410
|
-
if (props.metadata.objects.length)
|
|
411
|
-
unions.push({
|
|
412
|
-
type: "object",
|
|
413
|
-
is: () =>
|
|
414
|
-
ExpressionFactory.isObject({
|
|
415
|
-
checkNull: true,
|
|
416
|
-
checkArray: false,
|
|
417
|
-
input: props.input,
|
|
418
|
-
}),
|
|
419
|
-
value: (index) =>
|
|
420
|
-
explore_objects({
|
|
421
|
-
...props,
|
|
422
|
-
level: 0,
|
|
423
|
-
index,
|
|
424
|
-
objects: props.metadata.objects,
|
|
425
|
-
explore: {
|
|
426
|
-
...props.explore,
|
|
427
|
-
from: "object",
|
|
428
|
-
},
|
|
429
|
-
}),
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
// RETURNS
|
|
433
|
-
if (unions.length === 1) return wrapper(unions[0]!.value(props.index));
|
|
434
|
-
else
|
|
435
|
-
return wrapper(
|
|
436
|
-
iterate({
|
|
437
|
-
context: props.context,
|
|
438
|
-
functor: props.functor,
|
|
439
|
-
index: props.index,
|
|
440
|
-
unions,
|
|
441
|
-
expected: props.metadata.getName(),
|
|
442
|
-
input: props.input,
|
|
443
|
-
}),
|
|
444
|
-
);
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
const iterate = (props: {
|
|
448
|
-
context: ITypiaContext;
|
|
449
|
-
functor: FunctionProgrammer;
|
|
450
|
-
index: number | null;
|
|
451
|
-
unions: IUnion[];
|
|
452
|
-
expected: string;
|
|
453
|
-
input: ts.Expression;
|
|
454
|
-
}) =>
|
|
455
|
-
ts.factory.createBlock(
|
|
456
|
-
[
|
|
457
|
-
props.unions
|
|
458
|
-
.map((u, i) =>
|
|
459
|
-
ts.factory.createIfStatement(
|
|
460
|
-
u.is(),
|
|
461
|
-
u.value(props.index ? props.index + i : null),
|
|
462
|
-
i === props.unions.length - 1
|
|
463
|
-
? create_throw_error(props)
|
|
464
|
-
: undefined,
|
|
465
|
-
),
|
|
466
|
-
)
|
|
467
|
-
.reverse()
|
|
468
|
-
.reduce((a, b) =>
|
|
469
|
-
ts.factory.createIfStatement(b.expression, b.thenStatement, a),
|
|
470
|
-
),
|
|
471
|
-
],
|
|
472
|
-
true,
|
|
473
|
-
);
|
|
474
|
-
|
|
475
|
-
const decode_map = (props: {
|
|
476
|
-
context: ITypiaContext;
|
|
477
|
-
functor: FunctionProgrammer;
|
|
478
|
-
index: number;
|
|
479
|
-
input: ts.Expression;
|
|
480
|
-
entry: Metadata.Entry;
|
|
481
|
-
explore: FeatureProgrammer.IExplore;
|
|
482
|
-
}): ts.Block => {
|
|
483
|
-
const each: ts.Statement[] = [
|
|
484
|
-
ts.factory.createExpressionStatement(
|
|
485
|
-
decode_tag({
|
|
486
|
-
wire: ProtobufWire.LEN,
|
|
487
|
-
index: props.index,
|
|
488
|
-
}),
|
|
489
|
-
),
|
|
490
|
-
ts.factory.createExpressionStatement(
|
|
491
|
-
ts.factory.createCallExpression(
|
|
492
|
-
IdentifierFactory.access(WRITER(), "fork"),
|
|
493
|
-
undefined,
|
|
494
|
-
undefined,
|
|
495
|
-
),
|
|
496
|
-
),
|
|
497
|
-
...decode({
|
|
498
|
-
...props,
|
|
499
|
-
index: 1,
|
|
500
|
-
input: ts.factory.createIdentifier("key"),
|
|
501
|
-
metadata: props.entry.key,
|
|
502
|
-
}).statements,
|
|
503
|
-
...decode({
|
|
504
|
-
...props,
|
|
505
|
-
index: 2,
|
|
506
|
-
input: ts.factory.createIdentifier("value"),
|
|
507
|
-
metadata: props.entry.value,
|
|
508
|
-
}).statements,
|
|
509
|
-
ts.factory.createExpressionStatement(
|
|
510
|
-
ts.factory.createCallExpression(
|
|
511
|
-
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
512
|
-
undefined,
|
|
513
|
-
undefined,
|
|
514
|
-
),
|
|
515
|
-
),
|
|
516
|
-
];
|
|
517
|
-
return ts.factory.createBlock(
|
|
518
|
-
[
|
|
519
|
-
ts.factory.createForOfStatement(
|
|
520
|
-
undefined,
|
|
521
|
-
StatementFactory.entry({
|
|
522
|
-
key: "key",
|
|
523
|
-
value: "value",
|
|
524
|
-
}),
|
|
525
|
-
props.input,
|
|
526
|
-
ts.factory.createBlock(each),
|
|
527
|
-
),
|
|
528
|
-
],
|
|
529
|
-
true,
|
|
530
|
-
);
|
|
531
|
-
};
|
|
532
|
-
|
|
533
|
-
const decode_object = (props: {
|
|
534
|
-
context: ITypiaContext;
|
|
535
|
-
functor: FunctionProgrammer;
|
|
536
|
-
index: number | null;
|
|
537
|
-
input: ts.Expression;
|
|
538
|
-
object: MetadataObject;
|
|
539
|
-
explore: FeatureProgrammer.IExplore;
|
|
540
|
-
}): ts.Block => {
|
|
541
|
-
const top: MetadataProperty = props.object.properties[0]!;
|
|
542
|
-
if (top.key.isSoleLiteral() === false)
|
|
543
|
-
return decode_map({
|
|
544
|
-
...props,
|
|
545
|
-
index: props.index!,
|
|
546
|
-
input: ts.factory.createCallExpression(
|
|
547
|
-
ts.factory.createIdentifier("Object.entries"),
|
|
548
|
-
[],
|
|
549
|
-
[props.input],
|
|
550
|
-
),
|
|
551
|
-
entry: MetadataProperty.create({
|
|
552
|
-
...top,
|
|
553
|
-
key: (() => {
|
|
554
|
-
const key: Metadata = Metadata.initialize();
|
|
555
|
-
key.atomics.push(
|
|
556
|
-
MetadataAtomic.create({
|
|
557
|
-
type: "string",
|
|
558
|
-
tags: [],
|
|
559
|
-
}),
|
|
560
|
-
);
|
|
561
|
-
return key;
|
|
562
|
-
})(),
|
|
563
|
-
}),
|
|
564
|
-
});
|
|
565
|
-
return ts.factory.createBlock(
|
|
566
|
-
[
|
|
567
|
-
ts.factory.createIdentifier(
|
|
568
|
-
`//${props.index !== null ? ` ${props.index} -> ` : ""}${props.object.name}`,
|
|
569
|
-
),
|
|
570
|
-
...(props.index !== null
|
|
571
|
-
? [
|
|
572
|
-
decode_tag({
|
|
573
|
-
wire: ProtobufWire.LEN,
|
|
574
|
-
index: props.index,
|
|
575
|
-
}),
|
|
576
|
-
ts.factory.createCallExpression(
|
|
577
|
-
IdentifierFactory.access(WRITER(), "fork"),
|
|
578
|
-
undefined,
|
|
579
|
-
undefined,
|
|
580
|
-
),
|
|
581
|
-
]
|
|
582
|
-
: []),
|
|
583
|
-
ts.factory.createCallExpression(
|
|
584
|
-
ts.factory.createIdentifier(
|
|
585
|
-
props.functor.useLocal(`${PREFIX}o${props.object.index}`),
|
|
586
|
-
),
|
|
587
|
-
[],
|
|
588
|
-
[props.input],
|
|
589
|
-
),
|
|
590
|
-
...(props.index !== null
|
|
591
|
-
? [
|
|
592
|
-
ts.factory.createCallExpression(
|
|
593
|
-
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
594
|
-
undefined,
|
|
595
|
-
undefined,
|
|
596
|
-
),
|
|
597
|
-
]
|
|
598
|
-
: []),
|
|
599
|
-
].map((expr) => ts.factory.createExpressionStatement(expr)),
|
|
600
|
-
true,
|
|
601
|
-
);
|
|
602
|
-
};
|
|
603
|
-
|
|
604
|
-
const decode_array = (props: {
|
|
605
|
-
context: ITypiaContext;
|
|
606
|
-
functor: FunctionProgrammer;
|
|
607
|
-
index: number;
|
|
608
|
-
input: ts.Expression;
|
|
609
|
-
array: MetadataArray;
|
|
610
|
-
explore: FeatureProgrammer.IExplore;
|
|
611
|
-
}): ts.Block => {
|
|
612
|
-
const wire = get_standalone_wire(props.array.type.value);
|
|
613
|
-
const forLoop = (index: number | null) =>
|
|
614
|
-
ts.factory.createForOfStatement(
|
|
615
|
-
undefined,
|
|
616
|
-
ts.factory.createVariableDeclarationList(
|
|
617
|
-
[ts.factory.createVariableDeclaration("elem")],
|
|
618
|
-
ts.NodeFlags.Const,
|
|
619
|
-
),
|
|
620
|
-
props.input,
|
|
621
|
-
decode({
|
|
622
|
-
...props,
|
|
623
|
-
input: ts.factory.createIdentifier("elem"),
|
|
624
|
-
index,
|
|
625
|
-
metadata: props.array.type.value,
|
|
626
|
-
}),
|
|
627
|
-
);
|
|
628
|
-
const length = (block: ts.Block) =>
|
|
629
|
-
ts.factory.createBlock(
|
|
630
|
-
[
|
|
631
|
-
ts.factory.createIfStatement(
|
|
632
|
-
ts.factory.createStrictInequality(
|
|
633
|
-
ExpressionFactory.number(0),
|
|
634
|
-
IdentifierFactory.access(props.input, "length"),
|
|
635
|
-
),
|
|
636
|
-
block,
|
|
637
|
-
),
|
|
638
|
-
],
|
|
639
|
-
true,
|
|
640
|
-
);
|
|
641
|
-
|
|
642
|
-
if (wire === ProtobufWire.LEN)
|
|
643
|
-
return length(ts.factory.createBlock([forLoop(props.index)], true));
|
|
644
|
-
return length(
|
|
645
|
-
ts.factory.createBlock(
|
|
646
|
-
[
|
|
647
|
-
ts.factory.createExpressionStatement(
|
|
648
|
-
decode_tag({
|
|
649
|
-
wire: ProtobufWire.LEN,
|
|
650
|
-
index: props.index,
|
|
651
|
-
}),
|
|
652
|
-
),
|
|
653
|
-
ts.factory.createExpressionStatement(
|
|
654
|
-
ts.factory.createCallExpression(
|
|
655
|
-
IdentifierFactory.access(WRITER(), "fork"),
|
|
656
|
-
undefined,
|
|
657
|
-
undefined,
|
|
658
|
-
),
|
|
659
|
-
),
|
|
660
|
-
forLoop(null),
|
|
661
|
-
ts.factory.createExpressionStatement(
|
|
662
|
-
ts.factory.createCallExpression(
|
|
663
|
-
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
664
|
-
undefined,
|
|
665
|
-
undefined,
|
|
666
|
-
),
|
|
667
|
-
),
|
|
668
|
-
],
|
|
669
|
-
true,
|
|
670
|
-
),
|
|
671
|
-
);
|
|
672
|
-
};
|
|
673
|
-
|
|
674
|
-
const decode_bool = (props: { index: number | null; input: ts.Expression }) =>
|
|
675
|
-
ts.factory.createBlock(
|
|
676
|
-
[
|
|
677
|
-
...(props.index !== null
|
|
678
|
-
? [
|
|
679
|
-
decode_tag({
|
|
680
|
-
wire: ProtobufWire.VARIANT,
|
|
681
|
-
index: props.index,
|
|
682
|
-
}),
|
|
683
|
-
]
|
|
684
|
-
: []),
|
|
685
|
-
ts.factory.createCallExpression(
|
|
686
|
-
IdentifierFactory.access(WRITER(), "bool"),
|
|
687
|
-
undefined,
|
|
688
|
-
[props.input],
|
|
689
|
-
),
|
|
690
|
-
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
691
|
-
true,
|
|
692
|
-
);
|
|
693
|
-
|
|
694
|
-
const decode_number = (props: {
|
|
695
|
-
candidates: ProtobufAtomic.Numeric[];
|
|
696
|
-
type: ProtobufAtomic.Numeric;
|
|
697
|
-
input: ts.Expression;
|
|
698
|
-
}): IUnion => ({
|
|
699
|
-
type: props.type,
|
|
700
|
-
is: () =>
|
|
701
|
-
props.candidates.length === 1
|
|
702
|
-
? ts.factory.createStrictEquality(
|
|
703
|
-
ts.factory.createStringLiteral("number"),
|
|
704
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
705
|
-
)
|
|
706
|
-
: ts.factory.createLogicalAnd(
|
|
707
|
-
ts.factory.createStrictEquality(
|
|
708
|
-
ts.factory.createStringLiteral("number"),
|
|
709
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
710
|
-
),
|
|
711
|
-
NumericRangeFactory.number(props.type, props.input),
|
|
712
|
-
),
|
|
713
|
-
value: (index) =>
|
|
714
|
-
ts.factory.createBlock(
|
|
715
|
-
[
|
|
716
|
-
...(index !== null
|
|
717
|
-
? [
|
|
718
|
-
decode_tag({
|
|
719
|
-
wire: get_numeric_wire(props.type),
|
|
720
|
-
index,
|
|
721
|
-
}),
|
|
722
|
-
]
|
|
723
|
-
: []),
|
|
724
|
-
ts.factory.createCallExpression(
|
|
725
|
-
IdentifierFactory.access(WRITER(), props.type),
|
|
726
|
-
undefined,
|
|
727
|
-
[props.input],
|
|
728
|
-
),
|
|
729
|
-
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
730
|
-
true,
|
|
731
|
-
),
|
|
732
|
-
});
|
|
733
|
-
|
|
734
|
-
const decode_bigint = (props: {
|
|
735
|
-
candidates: ProtobufAtomic.BigNumeric[];
|
|
736
|
-
type: ProtobufAtomic.BigNumeric;
|
|
737
|
-
input: ts.Expression;
|
|
738
|
-
}): IUnion => ({
|
|
739
|
-
type: props.type,
|
|
740
|
-
is: () =>
|
|
741
|
-
props.candidates.length === 1
|
|
742
|
-
? ts.factory.createStrictEquality(
|
|
743
|
-
ts.factory.createStringLiteral("bigint"),
|
|
744
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
745
|
-
)
|
|
746
|
-
: ts.factory.createLogicalAnd(
|
|
747
|
-
ts.factory.createStrictEquality(
|
|
748
|
-
ts.factory.createStringLiteral("bigint"),
|
|
749
|
-
ts.factory.createTypeOfExpression(props.input),
|
|
750
|
-
),
|
|
751
|
-
NumericRangeFactory.bigint(props.type, props.input),
|
|
752
|
-
),
|
|
753
|
-
value: (index) =>
|
|
754
|
-
ts.factory.createBlock(
|
|
755
|
-
[
|
|
756
|
-
...(index !== null
|
|
757
|
-
? [
|
|
758
|
-
decode_tag({
|
|
759
|
-
wire: ProtobufWire.VARIANT,
|
|
760
|
-
index,
|
|
761
|
-
}),
|
|
762
|
-
]
|
|
763
|
-
: []),
|
|
764
|
-
ts.factory.createCallExpression(
|
|
765
|
-
IdentifierFactory.access(WRITER(), props.type),
|
|
766
|
-
undefined,
|
|
767
|
-
[props.input],
|
|
768
|
-
),
|
|
769
|
-
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
770
|
-
true,
|
|
771
|
-
),
|
|
772
|
-
});
|
|
773
|
-
|
|
774
|
-
const decode_bytes = (props: {
|
|
775
|
-
method: "bytes" | "string";
|
|
776
|
-
index: number;
|
|
777
|
-
input: ts.Expression;
|
|
778
|
-
}): ts.Block =>
|
|
779
|
-
ts.factory.createBlock(
|
|
780
|
-
[
|
|
781
|
-
decode_tag({
|
|
782
|
-
wire: ProtobufWire.LEN,
|
|
783
|
-
index: props.index,
|
|
784
|
-
}),
|
|
785
|
-
ts.factory.createCallExpression(
|
|
786
|
-
IdentifierFactory.access(WRITER(), props.method),
|
|
787
|
-
undefined,
|
|
788
|
-
[props.input],
|
|
789
|
-
),
|
|
790
|
-
].map((expr) => ts.factory.createExpressionStatement(expr)),
|
|
791
|
-
true,
|
|
792
|
-
);
|
|
793
|
-
|
|
794
|
-
const decode_tag = (props: {
|
|
795
|
-
wire: ProtobufWire;
|
|
796
|
-
index: number;
|
|
797
|
-
}): ts.CallExpression =>
|
|
798
|
-
ts.factory.createCallExpression(
|
|
799
|
-
IdentifierFactory.access(WRITER(), "uint32"),
|
|
800
|
-
undefined,
|
|
801
|
-
[ExpressionFactory.number((props.index << 3) | props.wire)],
|
|
802
|
-
);
|
|
803
|
-
|
|
804
|
-
const get_standalone_wire = (metadata: Metadata): ProtobufWire => {
|
|
805
|
-
if (
|
|
806
|
-
metadata.arrays.length ||
|
|
807
|
-
metadata.objects.length ||
|
|
808
|
-
metadata.maps.length ||
|
|
809
|
-
metadata.natives.length
|
|
810
|
-
)
|
|
811
|
-
return ProtobufWire.LEN;
|
|
812
|
-
|
|
813
|
-
const v = ProtobufUtil.getAtomics(metadata)[0]!;
|
|
814
|
-
if (v === "string") return ProtobufWire.LEN;
|
|
815
|
-
else if (
|
|
816
|
-
v === "bool" ||
|
|
817
|
-
v === "int32" ||
|
|
818
|
-
v === "uint32" ||
|
|
819
|
-
v === "int64" ||
|
|
820
|
-
v === "uint64"
|
|
821
|
-
)
|
|
822
|
-
return ProtobufWire.VARIANT;
|
|
823
|
-
else if (v === "float") return ProtobufWire.I32;
|
|
824
|
-
return ProtobufWire.I64;
|
|
825
|
-
};
|
|
826
|
-
|
|
827
|
-
const get_numeric_wire = (type: ProtobufAtomic.Numeric) =>
|
|
828
|
-
type === "double"
|
|
829
|
-
? ProtobufWire.I64
|
|
830
|
-
: type === "float"
|
|
831
|
-
? ProtobufWire.I32
|
|
832
|
-
: ProtobufWire.VARIANT;
|
|
833
|
-
|
|
834
|
-
/* -----------------------------------------------------------
|
|
835
|
-
EXPLORERS
|
|
836
|
-
----------------------------------------------------------- */
|
|
837
|
-
const explore_objects = (props: {
|
|
838
|
-
context: ITypiaContext;
|
|
839
|
-
functor: FunctionProgrammer;
|
|
840
|
-
level: number;
|
|
841
|
-
index: number | null;
|
|
842
|
-
input: ts.Expression;
|
|
843
|
-
objects: MetadataObject[];
|
|
844
|
-
explore: FeatureProgrammer.IExplore;
|
|
845
|
-
indexes?: Map<MetadataObject, number>;
|
|
846
|
-
}): ts.Block => {
|
|
847
|
-
if (props.objects.length === 1)
|
|
848
|
-
return decode_object({
|
|
849
|
-
context: props.context,
|
|
850
|
-
functor: props.functor,
|
|
851
|
-
index: props.indexes
|
|
852
|
-
? props.indexes.get(props.objects[0]!)!
|
|
853
|
-
: props.index,
|
|
854
|
-
input: props.input,
|
|
855
|
-
object: props.objects[0]!,
|
|
856
|
-
explore: props.explore,
|
|
857
|
-
});
|
|
858
|
-
|
|
859
|
-
const expected: string = `(${props.objects.map((t) => t.name).join(" | ")})`;
|
|
860
|
-
|
|
861
|
-
// POSSIBLE TO SPECIALIZE?
|
|
862
|
-
const specList: UnionPredicator.ISpecialized[] = UnionPredicator.object(
|
|
863
|
-
props.objects,
|
|
864
|
-
);
|
|
865
|
-
const indexes: Map<MetadataObject, number> =
|
|
866
|
-
props.indexes ??
|
|
867
|
-
new Map(props.objects.map((t, i) => [t, props.index! + i]));
|
|
868
|
-
|
|
869
|
-
if (specList.length === 0) {
|
|
870
|
-
const condition: ts.Expression = decode_union_object({
|
|
871
|
-
checker: (v) =>
|
|
872
|
-
IsProgrammer.decode_object({
|
|
873
|
-
context: props.context,
|
|
874
|
-
functor: props.functor,
|
|
875
|
-
object: v.object,
|
|
876
|
-
input: v.input,
|
|
877
|
-
explore: v.explore,
|
|
878
|
-
}),
|
|
879
|
-
decoder: (v) =>
|
|
880
|
-
ExpressionFactory.selfCall(
|
|
881
|
-
decode_object({
|
|
882
|
-
context: props.context,
|
|
883
|
-
functor: props.functor,
|
|
884
|
-
index: indexes.get(v.object)!,
|
|
885
|
-
input: v.input,
|
|
886
|
-
object: v.object,
|
|
887
|
-
explore: v.explore,
|
|
888
|
-
}),
|
|
889
|
-
),
|
|
890
|
-
success: (expr) => expr,
|
|
891
|
-
escaper: (v) =>
|
|
892
|
-
create_throw_error({
|
|
893
|
-
context: props.context,
|
|
894
|
-
functor: props.functor,
|
|
895
|
-
expected: v.expected,
|
|
896
|
-
input: v.input,
|
|
897
|
-
}),
|
|
898
|
-
input: props.input,
|
|
899
|
-
objects: props.objects,
|
|
900
|
-
explore: props.explore,
|
|
901
|
-
});
|
|
902
|
-
return StatementFactory.block(condition);
|
|
903
|
-
}
|
|
904
|
-
const remained: MetadataObject[] = props.objects.filter(
|
|
905
|
-
(t) => specList.find((s) => s.object === t) === undefined,
|
|
906
|
-
);
|
|
907
|
-
|
|
908
|
-
// DO SPECIALIZE
|
|
909
|
-
const condition: ts.IfStatement = specList
|
|
910
|
-
.filter((spec) => spec.property.key.getSoleLiteral() !== null)
|
|
911
|
-
.map((spec, i, array) => {
|
|
912
|
-
const key: string = spec.property.key.getSoleLiteral()!;
|
|
913
|
-
const accessor: ts.Expression = IdentifierFactory.access(
|
|
914
|
-
props.input,
|
|
915
|
-
key,
|
|
916
|
-
);
|
|
917
|
-
const pred: ts.Expression = spec.neighbour
|
|
918
|
-
? IsProgrammer.decode({
|
|
919
|
-
context: props.context,
|
|
920
|
-
functor: props.functor,
|
|
921
|
-
input: accessor,
|
|
922
|
-
metadata: spec.property.value,
|
|
923
|
-
explore: {
|
|
924
|
-
...props.explore,
|
|
925
|
-
tracable: false,
|
|
926
|
-
postfix: IdentifierFactory.postfix(key),
|
|
927
|
-
},
|
|
928
|
-
})
|
|
929
|
-
: ExpressionFactory.isRequired(accessor);
|
|
930
|
-
return ts.factory.createIfStatement(
|
|
931
|
-
pred,
|
|
932
|
-
ts.factory.createExpressionStatement(
|
|
933
|
-
ExpressionFactory.selfCall(
|
|
934
|
-
decode_object({
|
|
935
|
-
context: props.context,
|
|
936
|
-
functor: props.functor,
|
|
937
|
-
index: indexes.get(spec.object)!,
|
|
938
|
-
input: props.input,
|
|
939
|
-
object: spec.object,
|
|
940
|
-
explore: props.explore,
|
|
941
|
-
}),
|
|
942
|
-
),
|
|
943
|
-
),
|
|
944
|
-
i === array.length - 1
|
|
945
|
-
? remained.length
|
|
946
|
-
? ts.factory.createExpressionStatement(
|
|
947
|
-
ExpressionFactory.selfCall(
|
|
948
|
-
explore_objects({
|
|
949
|
-
context: props.context,
|
|
950
|
-
functor: props.functor,
|
|
951
|
-
level: props.level + 1,
|
|
952
|
-
index: props.index,
|
|
953
|
-
input: props.input,
|
|
954
|
-
objects: remained,
|
|
955
|
-
explore: props.explore,
|
|
956
|
-
indexes,
|
|
957
|
-
}),
|
|
958
|
-
),
|
|
959
|
-
)
|
|
960
|
-
: create_throw_error({
|
|
961
|
-
context: props.context,
|
|
962
|
-
functor: props.functor,
|
|
963
|
-
input: props.input,
|
|
964
|
-
expected,
|
|
965
|
-
})
|
|
966
|
-
: undefined,
|
|
967
|
-
);
|
|
968
|
-
})
|
|
969
|
-
.reverse()
|
|
970
|
-
.reduce((a, b) =>
|
|
971
|
-
ts.factory.createIfStatement(b.expression, b.thenStatement, a),
|
|
972
|
-
);
|
|
973
|
-
|
|
974
|
-
// RETURNS WITH CONDITIONS
|
|
975
|
-
return ts.factory.createBlock([condition], true);
|
|
976
|
-
};
|
|
977
|
-
|
|
978
|
-
/* -----------------------------------------------------------
|
|
979
|
-
CONFIGURATIONS
|
|
980
|
-
----------------------------------------------------------- */
|
|
981
|
-
const PREFIX = "$pe";
|
|
982
|
-
|
|
983
|
-
const create_throw_error = (props: {
|
|
984
|
-
context: ITypiaContext;
|
|
985
|
-
functor: FunctionProgrammer;
|
|
986
|
-
expected: string;
|
|
987
|
-
input: ts.Expression;
|
|
988
|
-
}) =>
|
|
989
|
-
ts.factory.createExpressionStatement(
|
|
990
|
-
ts.factory.createCallExpression(
|
|
991
|
-
props.context.importer.internal("throwTypeGuardError"),
|
|
992
|
-
[],
|
|
993
|
-
[
|
|
994
|
-
ts.factory.createObjectLiteralExpression(
|
|
995
|
-
[
|
|
996
|
-
ts.factory.createPropertyAssignment(
|
|
997
|
-
"method",
|
|
998
|
-
ts.factory.createStringLiteral(props.functor.method),
|
|
999
|
-
),
|
|
1000
|
-
ts.factory.createPropertyAssignment(
|
|
1001
|
-
"expected",
|
|
1002
|
-
ts.factory.createStringLiteral(props.expected),
|
|
1003
|
-
),
|
|
1004
|
-
ts.factory.createPropertyAssignment("value", props.input),
|
|
1005
|
-
],
|
|
1006
|
-
true,
|
|
1007
|
-
),
|
|
1008
|
-
],
|
|
1009
|
-
),
|
|
1010
|
-
);
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
const WRITER = () => ts.factory.createIdentifier("writer");
|
|
1014
|
-
|
|
1015
|
-
interface IUnion {
|
|
1016
|
-
type: string;
|
|
1017
|
-
is: () => ts.Expression;
|
|
1018
|
-
value: (index: number | null) => ts.Block;
|
|
1019
|
-
}
|
|
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 { NumericRangeFactory } from "../../factories/NumericRangeFactory";
|
|
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 { IProgrammerProps } from "../../transformers/IProgrammerProps";
|
|
18
|
+
import { ITypiaContext } from "../../transformers/ITypiaContext";
|
|
19
|
+
|
|
20
|
+
import { ProtobufAtomic } from "../../typings/ProtobufAtomic";
|
|
21
|
+
|
|
22
|
+
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
23
|
+
import { IsProgrammer } from "../IsProgrammer";
|
|
24
|
+
import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
|
|
25
|
+
import { ProtobufUtil } from "../helpers/ProtobufUtil";
|
|
26
|
+
import { ProtobufWire } from "../helpers/ProtobufWire";
|
|
27
|
+
import { UnionPredicator } from "../helpers/UnionPredicator";
|
|
28
|
+
import { decode_union_object } from "../internal/decode_union_object";
|
|
29
|
+
|
|
30
|
+
export namespace ProtobufEncodeProgrammer {
|
|
31
|
+
export const decompose = (props: {
|
|
32
|
+
context: ITypiaContext;
|
|
33
|
+
modulo: ts.LeftHandSideExpression;
|
|
34
|
+
functor: FunctionProgrammer;
|
|
35
|
+
type: ts.Type;
|
|
36
|
+
name: string | undefined;
|
|
37
|
+
}): FeatureProgrammer.IDecomposed => {
|
|
38
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
39
|
+
const metadata: Metadata = ProtobufFactory.metadata({
|
|
40
|
+
method: props.modulo.getText(),
|
|
41
|
+
checker: props.context.checker,
|
|
42
|
+
transformer: props.context.transformer,
|
|
43
|
+
collection,
|
|
44
|
+
type: props.type,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const callEncoder = (writer: string, factory: ts.NewExpression) =>
|
|
48
|
+
StatementFactory.constant({
|
|
49
|
+
name: writer,
|
|
50
|
+
value: ts.factory.createCallExpression(
|
|
51
|
+
ts.factory.createIdentifier("encoder"),
|
|
52
|
+
undefined,
|
|
53
|
+
[factory, ts.factory.createIdentifier("input")],
|
|
54
|
+
),
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
functions: {
|
|
58
|
+
encoder: StatementFactory.constant({
|
|
59
|
+
name: props.functor.useLocal("encoder"),
|
|
60
|
+
value: write_encoder({
|
|
61
|
+
context: props.context,
|
|
62
|
+
functor: props.functor,
|
|
63
|
+
collection,
|
|
64
|
+
metadata,
|
|
65
|
+
}),
|
|
66
|
+
}),
|
|
67
|
+
},
|
|
68
|
+
statements: [],
|
|
69
|
+
arrow: ts.factory.createArrowFunction(
|
|
70
|
+
undefined,
|
|
71
|
+
undefined,
|
|
72
|
+
[
|
|
73
|
+
IdentifierFactory.parameter(
|
|
74
|
+
"input",
|
|
75
|
+
ts.factory.createTypeReferenceNode(
|
|
76
|
+
props.name ??
|
|
77
|
+
TypeFactory.getFullName({
|
|
78
|
+
checker: props.context.checker,
|
|
79
|
+
type: props.type,
|
|
80
|
+
}),
|
|
81
|
+
),
|
|
82
|
+
),
|
|
83
|
+
],
|
|
84
|
+
ts.factory.createTypeReferenceNode("Uint8Array"),
|
|
85
|
+
undefined,
|
|
86
|
+
ts.factory.createBlock(
|
|
87
|
+
[
|
|
88
|
+
callEncoder(
|
|
89
|
+
"sizer",
|
|
90
|
+
ts.factory.createNewExpression(
|
|
91
|
+
props.context.importer.internal("ProtobufSizer"),
|
|
92
|
+
undefined,
|
|
93
|
+
[],
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
callEncoder(
|
|
97
|
+
"writer",
|
|
98
|
+
ts.factory.createNewExpression(
|
|
99
|
+
props.context.importer.internal("ProtobufWriter"),
|
|
100
|
+
undefined,
|
|
101
|
+
[ts.factory.createIdentifier("sizer")],
|
|
102
|
+
),
|
|
103
|
+
),
|
|
104
|
+
ts.factory.createReturnStatement(
|
|
105
|
+
ts.factory.createCallExpression(
|
|
106
|
+
IdentifierFactory.access(WRITER(), "buffer"),
|
|
107
|
+
undefined,
|
|
108
|
+
undefined,
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
],
|
|
112
|
+
true,
|
|
113
|
+
),
|
|
114
|
+
),
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const write = (props: IProgrammerProps): ts.CallExpression => {
|
|
119
|
+
const functor: FunctionProgrammer = new FunctionProgrammer(
|
|
120
|
+
props.modulo.getText(),
|
|
121
|
+
);
|
|
122
|
+
const result: FeatureProgrammer.IDecomposed = decompose({
|
|
123
|
+
...props,
|
|
124
|
+
functor,
|
|
125
|
+
});
|
|
126
|
+
return FeatureProgrammer.writeDecomposed({
|
|
127
|
+
modulo: props.modulo,
|
|
128
|
+
functor,
|
|
129
|
+
result,
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const write_encoder = (props: {
|
|
134
|
+
context: ITypiaContext;
|
|
135
|
+
functor: FunctionProgrammer;
|
|
136
|
+
collection: MetadataCollection;
|
|
137
|
+
metadata: Metadata;
|
|
138
|
+
}): ts.ArrowFunction => {
|
|
139
|
+
const functors = props.collection
|
|
140
|
+
.objects()
|
|
141
|
+
.filter((obj) => ProtobufUtil.isStaticObject(obj))
|
|
142
|
+
.map((object) =>
|
|
143
|
+
StatementFactory.constant({
|
|
144
|
+
name: `${PREFIX}o${object.index}`,
|
|
145
|
+
value: write_object_function({
|
|
146
|
+
context: props.context,
|
|
147
|
+
functor: props.functor,
|
|
148
|
+
input: ts.factory.createIdentifier("input"),
|
|
149
|
+
object,
|
|
150
|
+
explore: {
|
|
151
|
+
source: "function",
|
|
152
|
+
from: "object",
|
|
153
|
+
tracable: false,
|
|
154
|
+
postfix: "",
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
const main: ts.Block = decode({
|
|
160
|
+
context: props.context,
|
|
161
|
+
functor: props.functor,
|
|
162
|
+
index: null,
|
|
163
|
+
input: ts.factory.createIdentifier("input"),
|
|
164
|
+
metadata: props.metadata,
|
|
165
|
+
explore: {
|
|
166
|
+
source: "top",
|
|
167
|
+
from: "top",
|
|
168
|
+
tracable: false,
|
|
169
|
+
postfix: "",
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
return ts.factory.createArrowFunction(
|
|
173
|
+
undefined,
|
|
174
|
+
undefined,
|
|
175
|
+
[
|
|
176
|
+
IdentifierFactory.parameter("writer"),
|
|
177
|
+
IdentifierFactory.parameter("input"),
|
|
178
|
+
],
|
|
179
|
+
TypeFactory.keyword("any"),
|
|
180
|
+
undefined,
|
|
181
|
+
ts.factory.createBlock(
|
|
182
|
+
[
|
|
183
|
+
...props.functor.declareUnions(),
|
|
184
|
+
...functors,
|
|
185
|
+
...IsProgrammer.write_function_statements(props),
|
|
186
|
+
...main.statements,
|
|
187
|
+
ts.factory.createReturnStatement(
|
|
188
|
+
ts.factory.createIdentifier("writer"),
|
|
189
|
+
),
|
|
190
|
+
],
|
|
191
|
+
true,
|
|
192
|
+
),
|
|
193
|
+
);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const write_object_function = (props: {
|
|
197
|
+
context: ITypiaContext;
|
|
198
|
+
functor: FunctionProgrammer;
|
|
199
|
+
input: ts.Expression;
|
|
200
|
+
object: MetadataObject;
|
|
201
|
+
explore: FeatureProgrammer.IExplore;
|
|
202
|
+
}): ts.ArrowFunction => {
|
|
203
|
+
let index: number = 1;
|
|
204
|
+
const body: ts.Statement[] = props.object.properties
|
|
205
|
+
.map((p) => {
|
|
206
|
+
const block = decode({
|
|
207
|
+
...props,
|
|
208
|
+
index,
|
|
209
|
+
input: IdentifierFactory.access(props.input, p.key.getSoleLiteral()!),
|
|
210
|
+
metadata: p.value,
|
|
211
|
+
});
|
|
212
|
+
index += ProtobufUtil.size(p.value);
|
|
213
|
+
return [
|
|
214
|
+
ts.factory.createExpressionStatement(
|
|
215
|
+
ts.factory.createIdentifier(
|
|
216
|
+
`// property "${p.key.getSoleLiteral()!}"`,
|
|
217
|
+
),
|
|
218
|
+
),
|
|
219
|
+
...block.statements,
|
|
220
|
+
];
|
|
221
|
+
})
|
|
222
|
+
.flat();
|
|
223
|
+
return ts.factory.createArrowFunction(
|
|
224
|
+
undefined,
|
|
225
|
+
undefined,
|
|
226
|
+
[IdentifierFactory.parameter("input")],
|
|
227
|
+
TypeFactory.keyword("any"),
|
|
228
|
+
undefined,
|
|
229
|
+
ts.factory.createBlock(body, true),
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/* -----------------------------------------------------------
|
|
234
|
+
DECODERS
|
|
235
|
+
----------------------------------------------------------- */
|
|
236
|
+
const decode = (props: {
|
|
237
|
+
context: ITypiaContext;
|
|
238
|
+
functor: FunctionProgrammer;
|
|
239
|
+
index: number | null;
|
|
240
|
+
input: ts.Expression;
|
|
241
|
+
metadata: Metadata;
|
|
242
|
+
explore: FeatureProgrammer.IExplore;
|
|
243
|
+
}): ts.Block => {
|
|
244
|
+
const wrapper: (block: ts.Block) => ts.Block =
|
|
245
|
+
props.metadata.isRequired() && props.metadata.nullable === false
|
|
246
|
+
? (block) => block
|
|
247
|
+
: props.metadata.isRequired() === false &&
|
|
248
|
+
props.metadata.nullable === true
|
|
249
|
+
? (block) =>
|
|
250
|
+
ts.factory.createBlock(
|
|
251
|
+
[
|
|
252
|
+
ts.factory.createIfStatement(
|
|
253
|
+
ts.factory.createLogicalAnd(
|
|
254
|
+
ts.factory.createStrictInequality(
|
|
255
|
+
ts.factory.createIdentifier("undefined"),
|
|
256
|
+
props.input,
|
|
257
|
+
),
|
|
258
|
+
ts.factory.createStrictInequality(
|
|
259
|
+
ts.factory.createNull(),
|
|
260
|
+
props.input,
|
|
261
|
+
),
|
|
262
|
+
),
|
|
263
|
+
block,
|
|
264
|
+
),
|
|
265
|
+
],
|
|
266
|
+
true,
|
|
267
|
+
)
|
|
268
|
+
: props.metadata.isRequired() === false
|
|
269
|
+
? (block) =>
|
|
270
|
+
ts.factory.createBlock(
|
|
271
|
+
[
|
|
272
|
+
ts.factory.createIfStatement(
|
|
273
|
+
ts.factory.createStrictInequality(
|
|
274
|
+
ts.factory.createIdentifier("undefined"),
|
|
275
|
+
props.input,
|
|
276
|
+
),
|
|
277
|
+
block,
|
|
278
|
+
),
|
|
279
|
+
],
|
|
280
|
+
true,
|
|
281
|
+
)
|
|
282
|
+
: (block) =>
|
|
283
|
+
ts.factory.createBlock(
|
|
284
|
+
[
|
|
285
|
+
ts.factory.createIfStatement(
|
|
286
|
+
ts.factory.createStrictInequality(
|
|
287
|
+
ts.factory.createNull(),
|
|
288
|
+
props.input,
|
|
289
|
+
),
|
|
290
|
+
block,
|
|
291
|
+
),
|
|
292
|
+
],
|
|
293
|
+
true,
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
// STARTS FROM ATOMIC TYPES
|
|
297
|
+
const unions: IUnion[] = [];
|
|
298
|
+
const numbers = ProtobufUtil.getNumbers(props.metadata);
|
|
299
|
+
const bigints = ProtobufUtil.getBigints(props.metadata);
|
|
300
|
+
|
|
301
|
+
for (const atom of ProtobufUtil.getAtomics(props.metadata))
|
|
302
|
+
if (atom === "bool")
|
|
303
|
+
unions.push({
|
|
304
|
+
type: "bool",
|
|
305
|
+
is: () =>
|
|
306
|
+
ts.factory.createStrictEquality(
|
|
307
|
+
ts.factory.createStringLiteral("boolean"),
|
|
308
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
309
|
+
),
|
|
310
|
+
value: (index) =>
|
|
311
|
+
decode_bool({
|
|
312
|
+
index,
|
|
313
|
+
input: props.input,
|
|
314
|
+
}),
|
|
315
|
+
});
|
|
316
|
+
else if (
|
|
317
|
+
atom === "int32" ||
|
|
318
|
+
atom === "uint32" ||
|
|
319
|
+
atom === "float" ||
|
|
320
|
+
atom === "double"
|
|
321
|
+
)
|
|
322
|
+
unions.push(
|
|
323
|
+
decode_number({
|
|
324
|
+
candidates: numbers,
|
|
325
|
+
type: atom,
|
|
326
|
+
input: props.input,
|
|
327
|
+
}),
|
|
328
|
+
);
|
|
329
|
+
else if (atom === "int64" || atom === "uint64")
|
|
330
|
+
if (numbers.some((n) => n === atom))
|
|
331
|
+
unions.push(
|
|
332
|
+
decode_number({
|
|
333
|
+
candidates: numbers,
|
|
334
|
+
type: atom,
|
|
335
|
+
input: props.input,
|
|
336
|
+
}),
|
|
337
|
+
);
|
|
338
|
+
else
|
|
339
|
+
unions.push(
|
|
340
|
+
decode_bigint({
|
|
341
|
+
candidates: bigints,
|
|
342
|
+
type: atom,
|
|
343
|
+
input: props.input,
|
|
344
|
+
}),
|
|
345
|
+
);
|
|
346
|
+
else if (atom === "string")
|
|
347
|
+
unions.push({
|
|
348
|
+
type: "string",
|
|
349
|
+
is: () =>
|
|
350
|
+
ts.factory.createStrictEquality(
|
|
351
|
+
ts.factory.createStringLiteral("string"),
|
|
352
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
353
|
+
),
|
|
354
|
+
value: (index) =>
|
|
355
|
+
decode_bytes({
|
|
356
|
+
method: "string",
|
|
357
|
+
index: index!,
|
|
358
|
+
input: props.input,
|
|
359
|
+
}),
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// CONSIDER BYTES
|
|
363
|
+
if (props.metadata.natives.length)
|
|
364
|
+
unions.push({
|
|
365
|
+
type: "bytes",
|
|
366
|
+
is: () => ExpressionFactory.isInstanceOf("Uint8Array", props.input),
|
|
367
|
+
value: (index) =>
|
|
368
|
+
decode_bytes({
|
|
369
|
+
method: "bytes",
|
|
370
|
+
index: index!,
|
|
371
|
+
input: props.input,
|
|
372
|
+
}),
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// CONSIDER ARRAYS
|
|
376
|
+
if (props.metadata.arrays.length)
|
|
377
|
+
unions.push({
|
|
378
|
+
type: "array",
|
|
379
|
+
is: () => ExpressionFactory.isArray(props.input),
|
|
380
|
+
value: (index) =>
|
|
381
|
+
decode_array({
|
|
382
|
+
...props,
|
|
383
|
+
array: props.metadata.arrays[0]!,
|
|
384
|
+
explore: {
|
|
385
|
+
...props.explore,
|
|
386
|
+
from: "array",
|
|
387
|
+
},
|
|
388
|
+
index: index!,
|
|
389
|
+
}),
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// CONSIDER MAPS
|
|
393
|
+
if (props.metadata.maps.length)
|
|
394
|
+
unions.push({
|
|
395
|
+
type: "map",
|
|
396
|
+
is: () => ExpressionFactory.isInstanceOf("Map", props.input),
|
|
397
|
+
value: (index) =>
|
|
398
|
+
decode_map({
|
|
399
|
+
...props,
|
|
400
|
+
index: index!,
|
|
401
|
+
entry: props.metadata.maps[0]!,
|
|
402
|
+
explore: {
|
|
403
|
+
...props.explore,
|
|
404
|
+
from: "array",
|
|
405
|
+
},
|
|
406
|
+
}),
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// CONSIDER OBJECTS
|
|
410
|
+
if (props.metadata.objects.length)
|
|
411
|
+
unions.push({
|
|
412
|
+
type: "object",
|
|
413
|
+
is: () =>
|
|
414
|
+
ExpressionFactory.isObject({
|
|
415
|
+
checkNull: true,
|
|
416
|
+
checkArray: false,
|
|
417
|
+
input: props.input,
|
|
418
|
+
}),
|
|
419
|
+
value: (index) =>
|
|
420
|
+
explore_objects({
|
|
421
|
+
...props,
|
|
422
|
+
level: 0,
|
|
423
|
+
index,
|
|
424
|
+
objects: props.metadata.objects,
|
|
425
|
+
explore: {
|
|
426
|
+
...props.explore,
|
|
427
|
+
from: "object",
|
|
428
|
+
},
|
|
429
|
+
}),
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// RETURNS
|
|
433
|
+
if (unions.length === 1) return wrapper(unions[0]!.value(props.index));
|
|
434
|
+
else
|
|
435
|
+
return wrapper(
|
|
436
|
+
iterate({
|
|
437
|
+
context: props.context,
|
|
438
|
+
functor: props.functor,
|
|
439
|
+
index: props.index,
|
|
440
|
+
unions,
|
|
441
|
+
expected: props.metadata.getName(),
|
|
442
|
+
input: props.input,
|
|
443
|
+
}),
|
|
444
|
+
);
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const iterate = (props: {
|
|
448
|
+
context: ITypiaContext;
|
|
449
|
+
functor: FunctionProgrammer;
|
|
450
|
+
index: number | null;
|
|
451
|
+
unions: IUnion[];
|
|
452
|
+
expected: string;
|
|
453
|
+
input: ts.Expression;
|
|
454
|
+
}) =>
|
|
455
|
+
ts.factory.createBlock(
|
|
456
|
+
[
|
|
457
|
+
props.unions
|
|
458
|
+
.map((u, i) =>
|
|
459
|
+
ts.factory.createIfStatement(
|
|
460
|
+
u.is(),
|
|
461
|
+
u.value(props.index ? props.index + i : null),
|
|
462
|
+
i === props.unions.length - 1
|
|
463
|
+
? create_throw_error(props)
|
|
464
|
+
: undefined,
|
|
465
|
+
),
|
|
466
|
+
)
|
|
467
|
+
.reverse()
|
|
468
|
+
.reduce((a, b) =>
|
|
469
|
+
ts.factory.createIfStatement(b.expression, b.thenStatement, a),
|
|
470
|
+
),
|
|
471
|
+
],
|
|
472
|
+
true,
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
const decode_map = (props: {
|
|
476
|
+
context: ITypiaContext;
|
|
477
|
+
functor: FunctionProgrammer;
|
|
478
|
+
index: number;
|
|
479
|
+
input: ts.Expression;
|
|
480
|
+
entry: Metadata.Entry;
|
|
481
|
+
explore: FeatureProgrammer.IExplore;
|
|
482
|
+
}): ts.Block => {
|
|
483
|
+
const each: ts.Statement[] = [
|
|
484
|
+
ts.factory.createExpressionStatement(
|
|
485
|
+
decode_tag({
|
|
486
|
+
wire: ProtobufWire.LEN,
|
|
487
|
+
index: props.index,
|
|
488
|
+
}),
|
|
489
|
+
),
|
|
490
|
+
ts.factory.createExpressionStatement(
|
|
491
|
+
ts.factory.createCallExpression(
|
|
492
|
+
IdentifierFactory.access(WRITER(), "fork"),
|
|
493
|
+
undefined,
|
|
494
|
+
undefined,
|
|
495
|
+
),
|
|
496
|
+
),
|
|
497
|
+
...decode({
|
|
498
|
+
...props,
|
|
499
|
+
index: 1,
|
|
500
|
+
input: ts.factory.createIdentifier("key"),
|
|
501
|
+
metadata: props.entry.key,
|
|
502
|
+
}).statements,
|
|
503
|
+
...decode({
|
|
504
|
+
...props,
|
|
505
|
+
index: 2,
|
|
506
|
+
input: ts.factory.createIdentifier("value"),
|
|
507
|
+
metadata: props.entry.value,
|
|
508
|
+
}).statements,
|
|
509
|
+
ts.factory.createExpressionStatement(
|
|
510
|
+
ts.factory.createCallExpression(
|
|
511
|
+
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
512
|
+
undefined,
|
|
513
|
+
undefined,
|
|
514
|
+
),
|
|
515
|
+
),
|
|
516
|
+
];
|
|
517
|
+
return ts.factory.createBlock(
|
|
518
|
+
[
|
|
519
|
+
ts.factory.createForOfStatement(
|
|
520
|
+
undefined,
|
|
521
|
+
StatementFactory.entry({
|
|
522
|
+
key: "key",
|
|
523
|
+
value: "value",
|
|
524
|
+
}),
|
|
525
|
+
props.input,
|
|
526
|
+
ts.factory.createBlock(each),
|
|
527
|
+
),
|
|
528
|
+
],
|
|
529
|
+
true,
|
|
530
|
+
);
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const decode_object = (props: {
|
|
534
|
+
context: ITypiaContext;
|
|
535
|
+
functor: FunctionProgrammer;
|
|
536
|
+
index: number | null;
|
|
537
|
+
input: ts.Expression;
|
|
538
|
+
object: MetadataObject;
|
|
539
|
+
explore: FeatureProgrammer.IExplore;
|
|
540
|
+
}): ts.Block => {
|
|
541
|
+
const top: MetadataProperty = props.object.properties[0]!;
|
|
542
|
+
if (top.key.isSoleLiteral() === false)
|
|
543
|
+
return decode_map({
|
|
544
|
+
...props,
|
|
545
|
+
index: props.index!,
|
|
546
|
+
input: ts.factory.createCallExpression(
|
|
547
|
+
ts.factory.createIdentifier("Object.entries"),
|
|
548
|
+
[],
|
|
549
|
+
[props.input],
|
|
550
|
+
),
|
|
551
|
+
entry: MetadataProperty.create({
|
|
552
|
+
...top,
|
|
553
|
+
key: (() => {
|
|
554
|
+
const key: Metadata = Metadata.initialize();
|
|
555
|
+
key.atomics.push(
|
|
556
|
+
MetadataAtomic.create({
|
|
557
|
+
type: "string",
|
|
558
|
+
tags: [],
|
|
559
|
+
}),
|
|
560
|
+
);
|
|
561
|
+
return key;
|
|
562
|
+
})(),
|
|
563
|
+
}),
|
|
564
|
+
});
|
|
565
|
+
return ts.factory.createBlock(
|
|
566
|
+
[
|
|
567
|
+
ts.factory.createIdentifier(
|
|
568
|
+
`//${props.index !== null ? ` ${props.index} -> ` : ""}${props.object.name}`,
|
|
569
|
+
),
|
|
570
|
+
...(props.index !== null
|
|
571
|
+
? [
|
|
572
|
+
decode_tag({
|
|
573
|
+
wire: ProtobufWire.LEN,
|
|
574
|
+
index: props.index,
|
|
575
|
+
}),
|
|
576
|
+
ts.factory.createCallExpression(
|
|
577
|
+
IdentifierFactory.access(WRITER(), "fork"),
|
|
578
|
+
undefined,
|
|
579
|
+
undefined,
|
|
580
|
+
),
|
|
581
|
+
]
|
|
582
|
+
: []),
|
|
583
|
+
ts.factory.createCallExpression(
|
|
584
|
+
ts.factory.createIdentifier(
|
|
585
|
+
props.functor.useLocal(`${PREFIX}o${props.object.index}`),
|
|
586
|
+
),
|
|
587
|
+
[],
|
|
588
|
+
[props.input],
|
|
589
|
+
),
|
|
590
|
+
...(props.index !== null
|
|
591
|
+
? [
|
|
592
|
+
ts.factory.createCallExpression(
|
|
593
|
+
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
594
|
+
undefined,
|
|
595
|
+
undefined,
|
|
596
|
+
),
|
|
597
|
+
]
|
|
598
|
+
: []),
|
|
599
|
+
].map((expr) => ts.factory.createExpressionStatement(expr)),
|
|
600
|
+
true,
|
|
601
|
+
);
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
const decode_array = (props: {
|
|
605
|
+
context: ITypiaContext;
|
|
606
|
+
functor: FunctionProgrammer;
|
|
607
|
+
index: number;
|
|
608
|
+
input: ts.Expression;
|
|
609
|
+
array: MetadataArray;
|
|
610
|
+
explore: FeatureProgrammer.IExplore;
|
|
611
|
+
}): ts.Block => {
|
|
612
|
+
const wire = get_standalone_wire(props.array.type.value);
|
|
613
|
+
const forLoop = (index: number | null) =>
|
|
614
|
+
ts.factory.createForOfStatement(
|
|
615
|
+
undefined,
|
|
616
|
+
ts.factory.createVariableDeclarationList(
|
|
617
|
+
[ts.factory.createVariableDeclaration("elem")],
|
|
618
|
+
ts.NodeFlags.Const,
|
|
619
|
+
),
|
|
620
|
+
props.input,
|
|
621
|
+
decode({
|
|
622
|
+
...props,
|
|
623
|
+
input: ts.factory.createIdentifier("elem"),
|
|
624
|
+
index,
|
|
625
|
+
metadata: props.array.type.value,
|
|
626
|
+
}),
|
|
627
|
+
);
|
|
628
|
+
const length = (block: ts.Block) =>
|
|
629
|
+
ts.factory.createBlock(
|
|
630
|
+
[
|
|
631
|
+
ts.factory.createIfStatement(
|
|
632
|
+
ts.factory.createStrictInequality(
|
|
633
|
+
ExpressionFactory.number(0),
|
|
634
|
+
IdentifierFactory.access(props.input, "length"),
|
|
635
|
+
),
|
|
636
|
+
block,
|
|
637
|
+
),
|
|
638
|
+
],
|
|
639
|
+
true,
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
if (wire === ProtobufWire.LEN)
|
|
643
|
+
return length(ts.factory.createBlock([forLoop(props.index)], true));
|
|
644
|
+
return length(
|
|
645
|
+
ts.factory.createBlock(
|
|
646
|
+
[
|
|
647
|
+
ts.factory.createExpressionStatement(
|
|
648
|
+
decode_tag({
|
|
649
|
+
wire: ProtobufWire.LEN,
|
|
650
|
+
index: props.index,
|
|
651
|
+
}),
|
|
652
|
+
),
|
|
653
|
+
ts.factory.createExpressionStatement(
|
|
654
|
+
ts.factory.createCallExpression(
|
|
655
|
+
IdentifierFactory.access(WRITER(), "fork"),
|
|
656
|
+
undefined,
|
|
657
|
+
undefined,
|
|
658
|
+
),
|
|
659
|
+
),
|
|
660
|
+
forLoop(null),
|
|
661
|
+
ts.factory.createExpressionStatement(
|
|
662
|
+
ts.factory.createCallExpression(
|
|
663
|
+
IdentifierFactory.access(WRITER(), "ldelim"),
|
|
664
|
+
undefined,
|
|
665
|
+
undefined,
|
|
666
|
+
),
|
|
667
|
+
),
|
|
668
|
+
],
|
|
669
|
+
true,
|
|
670
|
+
),
|
|
671
|
+
);
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
const decode_bool = (props: { index: number | null; input: ts.Expression }) =>
|
|
675
|
+
ts.factory.createBlock(
|
|
676
|
+
[
|
|
677
|
+
...(props.index !== null
|
|
678
|
+
? [
|
|
679
|
+
decode_tag({
|
|
680
|
+
wire: ProtobufWire.VARIANT,
|
|
681
|
+
index: props.index,
|
|
682
|
+
}),
|
|
683
|
+
]
|
|
684
|
+
: []),
|
|
685
|
+
ts.factory.createCallExpression(
|
|
686
|
+
IdentifierFactory.access(WRITER(), "bool"),
|
|
687
|
+
undefined,
|
|
688
|
+
[props.input],
|
|
689
|
+
),
|
|
690
|
+
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
691
|
+
true,
|
|
692
|
+
);
|
|
693
|
+
|
|
694
|
+
const decode_number = (props: {
|
|
695
|
+
candidates: ProtobufAtomic.Numeric[];
|
|
696
|
+
type: ProtobufAtomic.Numeric;
|
|
697
|
+
input: ts.Expression;
|
|
698
|
+
}): IUnion => ({
|
|
699
|
+
type: props.type,
|
|
700
|
+
is: () =>
|
|
701
|
+
props.candidates.length === 1
|
|
702
|
+
? ts.factory.createStrictEquality(
|
|
703
|
+
ts.factory.createStringLiteral("number"),
|
|
704
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
705
|
+
)
|
|
706
|
+
: ts.factory.createLogicalAnd(
|
|
707
|
+
ts.factory.createStrictEquality(
|
|
708
|
+
ts.factory.createStringLiteral("number"),
|
|
709
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
710
|
+
),
|
|
711
|
+
NumericRangeFactory.number(props.type, props.input),
|
|
712
|
+
),
|
|
713
|
+
value: (index) =>
|
|
714
|
+
ts.factory.createBlock(
|
|
715
|
+
[
|
|
716
|
+
...(index !== null
|
|
717
|
+
? [
|
|
718
|
+
decode_tag({
|
|
719
|
+
wire: get_numeric_wire(props.type),
|
|
720
|
+
index,
|
|
721
|
+
}),
|
|
722
|
+
]
|
|
723
|
+
: []),
|
|
724
|
+
ts.factory.createCallExpression(
|
|
725
|
+
IdentifierFactory.access(WRITER(), props.type),
|
|
726
|
+
undefined,
|
|
727
|
+
[props.input],
|
|
728
|
+
),
|
|
729
|
+
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
730
|
+
true,
|
|
731
|
+
),
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
const decode_bigint = (props: {
|
|
735
|
+
candidates: ProtobufAtomic.BigNumeric[];
|
|
736
|
+
type: ProtobufAtomic.BigNumeric;
|
|
737
|
+
input: ts.Expression;
|
|
738
|
+
}): IUnion => ({
|
|
739
|
+
type: props.type,
|
|
740
|
+
is: () =>
|
|
741
|
+
props.candidates.length === 1
|
|
742
|
+
? ts.factory.createStrictEquality(
|
|
743
|
+
ts.factory.createStringLiteral("bigint"),
|
|
744
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
745
|
+
)
|
|
746
|
+
: ts.factory.createLogicalAnd(
|
|
747
|
+
ts.factory.createStrictEquality(
|
|
748
|
+
ts.factory.createStringLiteral("bigint"),
|
|
749
|
+
ts.factory.createTypeOfExpression(props.input),
|
|
750
|
+
),
|
|
751
|
+
NumericRangeFactory.bigint(props.type, props.input),
|
|
752
|
+
),
|
|
753
|
+
value: (index) =>
|
|
754
|
+
ts.factory.createBlock(
|
|
755
|
+
[
|
|
756
|
+
...(index !== null
|
|
757
|
+
? [
|
|
758
|
+
decode_tag({
|
|
759
|
+
wire: ProtobufWire.VARIANT,
|
|
760
|
+
index,
|
|
761
|
+
}),
|
|
762
|
+
]
|
|
763
|
+
: []),
|
|
764
|
+
ts.factory.createCallExpression(
|
|
765
|
+
IdentifierFactory.access(WRITER(), props.type),
|
|
766
|
+
undefined,
|
|
767
|
+
[props.input],
|
|
768
|
+
),
|
|
769
|
+
].map((exp) => ts.factory.createExpressionStatement(exp)),
|
|
770
|
+
true,
|
|
771
|
+
),
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
const decode_bytes = (props: {
|
|
775
|
+
method: "bytes" | "string";
|
|
776
|
+
index: number;
|
|
777
|
+
input: ts.Expression;
|
|
778
|
+
}): ts.Block =>
|
|
779
|
+
ts.factory.createBlock(
|
|
780
|
+
[
|
|
781
|
+
decode_tag({
|
|
782
|
+
wire: ProtobufWire.LEN,
|
|
783
|
+
index: props.index,
|
|
784
|
+
}),
|
|
785
|
+
ts.factory.createCallExpression(
|
|
786
|
+
IdentifierFactory.access(WRITER(), props.method),
|
|
787
|
+
undefined,
|
|
788
|
+
[props.input],
|
|
789
|
+
),
|
|
790
|
+
].map((expr) => ts.factory.createExpressionStatement(expr)),
|
|
791
|
+
true,
|
|
792
|
+
);
|
|
793
|
+
|
|
794
|
+
const decode_tag = (props: {
|
|
795
|
+
wire: ProtobufWire;
|
|
796
|
+
index: number;
|
|
797
|
+
}): ts.CallExpression =>
|
|
798
|
+
ts.factory.createCallExpression(
|
|
799
|
+
IdentifierFactory.access(WRITER(), "uint32"),
|
|
800
|
+
undefined,
|
|
801
|
+
[ExpressionFactory.number((props.index << 3) | props.wire)],
|
|
802
|
+
);
|
|
803
|
+
|
|
804
|
+
const get_standalone_wire = (metadata: Metadata): ProtobufWire => {
|
|
805
|
+
if (
|
|
806
|
+
metadata.arrays.length ||
|
|
807
|
+
metadata.objects.length ||
|
|
808
|
+
metadata.maps.length ||
|
|
809
|
+
metadata.natives.length
|
|
810
|
+
)
|
|
811
|
+
return ProtobufWire.LEN;
|
|
812
|
+
|
|
813
|
+
const v = ProtobufUtil.getAtomics(metadata)[0]!;
|
|
814
|
+
if (v === "string") return ProtobufWire.LEN;
|
|
815
|
+
else if (
|
|
816
|
+
v === "bool" ||
|
|
817
|
+
v === "int32" ||
|
|
818
|
+
v === "uint32" ||
|
|
819
|
+
v === "int64" ||
|
|
820
|
+
v === "uint64"
|
|
821
|
+
)
|
|
822
|
+
return ProtobufWire.VARIANT;
|
|
823
|
+
else if (v === "float") return ProtobufWire.I32;
|
|
824
|
+
return ProtobufWire.I64;
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
const get_numeric_wire = (type: ProtobufAtomic.Numeric) =>
|
|
828
|
+
type === "double"
|
|
829
|
+
? ProtobufWire.I64
|
|
830
|
+
: type === "float"
|
|
831
|
+
? ProtobufWire.I32
|
|
832
|
+
: ProtobufWire.VARIANT;
|
|
833
|
+
|
|
834
|
+
/* -----------------------------------------------------------
|
|
835
|
+
EXPLORERS
|
|
836
|
+
----------------------------------------------------------- */
|
|
837
|
+
const explore_objects = (props: {
|
|
838
|
+
context: ITypiaContext;
|
|
839
|
+
functor: FunctionProgrammer;
|
|
840
|
+
level: number;
|
|
841
|
+
index: number | null;
|
|
842
|
+
input: ts.Expression;
|
|
843
|
+
objects: MetadataObject[];
|
|
844
|
+
explore: FeatureProgrammer.IExplore;
|
|
845
|
+
indexes?: Map<MetadataObject, number>;
|
|
846
|
+
}): ts.Block => {
|
|
847
|
+
if (props.objects.length === 1)
|
|
848
|
+
return decode_object({
|
|
849
|
+
context: props.context,
|
|
850
|
+
functor: props.functor,
|
|
851
|
+
index: props.indexes
|
|
852
|
+
? props.indexes.get(props.objects[0]!)!
|
|
853
|
+
: props.index,
|
|
854
|
+
input: props.input,
|
|
855
|
+
object: props.objects[0]!,
|
|
856
|
+
explore: props.explore,
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
const expected: string = `(${props.objects.map((t) => t.name).join(" | ")})`;
|
|
860
|
+
|
|
861
|
+
// POSSIBLE TO SPECIALIZE?
|
|
862
|
+
const specList: UnionPredicator.ISpecialized[] = UnionPredicator.object(
|
|
863
|
+
props.objects,
|
|
864
|
+
);
|
|
865
|
+
const indexes: Map<MetadataObject, number> =
|
|
866
|
+
props.indexes ??
|
|
867
|
+
new Map(props.objects.map((t, i) => [t, props.index! + i]));
|
|
868
|
+
|
|
869
|
+
if (specList.length === 0) {
|
|
870
|
+
const condition: ts.Expression = decode_union_object({
|
|
871
|
+
checker: (v) =>
|
|
872
|
+
IsProgrammer.decode_object({
|
|
873
|
+
context: props.context,
|
|
874
|
+
functor: props.functor,
|
|
875
|
+
object: v.object,
|
|
876
|
+
input: v.input,
|
|
877
|
+
explore: v.explore,
|
|
878
|
+
}),
|
|
879
|
+
decoder: (v) =>
|
|
880
|
+
ExpressionFactory.selfCall(
|
|
881
|
+
decode_object({
|
|
882
|
+
context: props.context,
|
|
883
|
+
functor: props.functor,
|
|
884
|
+
index: indexes.get(v.object)!,
|
|
885
|
+
input: v.input,
|
|
886
|
+
object: v.object,
|
|
887
|
+
explore: v.explore,
|
|
888
|
+
}),
|
|
889
|
+
),
|
|
890
|
+
success: (expr) => expr,
|
|
891
|
+
escaper: (v) =>
|
|
892
|
+
create_throw_error({
|
|
893
|
+
context: props.context,
|
|
894
|
+
functor: props.functor,
|
|
895
|
+
expected: v.expected,
|
|
896
|
+
input: v.input,
|
|
897
|
+
}),
|
|
898
|
+
input: props.input,
|
|
899
|
+
objects: props.objects,
|
|
900
|
+
explore: props.explore,
|
|
901
|
+
});
|
|
902
|
+
return StatementFactory.block(condition);
|
|
903
|
+
}
|
|
904
|
+
const remained: MetadataObject[] = props.objects.filter(
|
|
905
|
+
(t) => specList.find((s) => s.object === t) === undefined,
|
|
906
|
+
);
|
|
907
|
+
|
|
908
|
+
// DO SPECIALIZE
|
|
909
|
+
const condition: ts.IfStatement = specList
|
|
910
|
+
.filter((spec) => spec.property.key.getSoleLiteral() !== null)
|
|
911
|
+
.map((spec, i, array) => {
|
|
912
|
+
const key: string = spec.property.key.getSoleLiteral()!;
|
|
913
|
+
const accessor: ts.Expression = IdentifierFactory.access(
|
|
914
|
+
props.input,
|
|
915
|
+
key,
|
|
916
|
+
);
|
|
917
|
+
const pred: ts.Expression = spec.neighbour
|
|
918
|
+
? IsProgrammer.decode({
|
|
919
|
+
context: props.context,
|
|
920
|
+
functor: props.functor,
|
|
921
|
+
input: accessor,
|
|
922
|
+
metadata: spec.property.value,
|
|
923
|
+
explore: {
|
|
924
|
+
...props.explore,
|
|
925
|
+
tracable: false,
|
|
926
|
+
postfix: IdentifierFactory.postfix(key),
|
|
927
|
+
},
|
|
928
|
+
})
|
|
929
|
+
: ExpressionFactory.isRequired(accessor);
|
|
930
|
+
return ts.factory.createIfStatement(
|
|
931
|
+
pred,
|
|
932
|
+
ts.factory.createExpressionStatement(
|
|
933
|
+
ExpressionFactory.selfCall(
|
|
934
|
+
decode_object({
|
|
935
|
+
context: props.context,
|
|
936
|
+
functor: props.functor,
|
|
937
|
+
index: indexes.get(spec.object)!,
|
|
938
|
+
input: props.input,
|
|
939
|
+
object: spec.object,
|
|
940
|
+
explore: props.explore,
|
|
941
|
+
}),
|
|
942
|
+
),
|
|
943
|
+
),
|
|
944
|
+
i === array.length - 1
|
|
945
|
+
? remained.length
|
|
946
|
+
? ts.factory.createExpressionStatement(
|
|
947
|
+
ExpressionFactory.selfCall(
|
|
948
|
+
explore_objects({
|
|
949
|
+
context: props.context,
|
|
950
|
+
functor: props.functor,
|
|
951
|
+
level: props.level + 1,
|
|
952
|
+
index: props.index,
|
|
953
|
+
input: props.input,
|
|
954
|
+
objects: remained,
|
|
955
|
+
explore: props.explore,
|
|
956
|
+
indexes,
|
|
957
|
+
}),
|
|
958
|
+
),
|
|
959
|
+
)
|
|
960
|
+
: create_throw_error({
|
|
961
|
+
context: props.context,
|
|
962
|
+
functor: props.functor,
|
|
963
|
+
input: props.input,
|
|
964
|
+
expected,
|
|
965
|
+
})
|
|
966
|
+
: undefined,
|
|
967
|
+
);
|
|
968
|
+
})
|
|
969
|
+
.reverse()
|
|
970
|
+
.reduce((a, b) =>
|
|
971
|
+
ts.factory.createIfStatement(b.expression, b.thenStatement, a),
|
|
972
|
+
);
|
|
973
|
+
|
|
974
|
+
// RETURNS WITH CONDITIONS
|
|
975
|
+
return ts.factory.createBlock([condition], true);
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
/* -----------------------------------------------------------
|
|
979
|
+
CONFIGURATIONS
|
|
980
|
+
----------------------------------------------------------- */
|
|
981
|
+
const PREFIX = "$pe";
|
|
982
|
+
|
|
983
|
+
const create_throw_error = (props: {
|
|
984
|
+
context: ITypiaContext;
|
|
985
|
+
functor: FunctionProgrammer;
|
|
986
|
+
expected: string;
|
|
987
|
+
input: ts.Expression;
|
|
988
|
+
}) =>
|
|
989
|
+
ts.factory.createExpressionStatement(
|
|
990
|
+
ts.factory.createCallExpression(
|
|
991
|
+
props.context.importer.internal("throwTypeGuardError"),
|
|
992
|
+
[],
|
|
993
|
+
[
|
|
994
|
+
ts.factory.createObjectLiteralExpression(
|
|
995
|
+
[
|
|
996
|
+
ts.factory.createPropertyAssignment(
|
|
997
|
+
"method",
|
|
998
|
+
ts.factory.createStringLiteral(props.functor.method),
|
|
999
|
+
),
|
|
1000
|
+
ts.factory.createPropertyAssignment(
|
|
1001
|
+
"expected",
|
|
1002
|
+
ts.factory.createStringLiteral(props.expected),
|
|
1003
|
+
),
|
|
1004
|
+
ts.factory.createPropertyAssignment("value", props.input),
|
|
1005
|
+
],
|
|
1006
|
+
true,
|
|
1007
|
+
),
|
|
1008
|
+
],
|
|
1009
|
+
),
|
|
1010
|
+
);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
const WRITER = () => ts.factory.createIdentifier("writer");
|
|
1014
|
+
|
|
1015
|
+
interface IUnion {
|
|
1016
|
+
type: string;
|
|
1017
|
+
is: () => ts.Expression;
|
|
1018
|
+
value: (index: number | null) => ts.Block;
|
|
1019
|
+
}
|