@azure-tools/typespec-ts 0.33.0 → 0.34.0
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/CHANGELOG.md +37 -0
- package/dist/src/framework/declaration.js.map +1 -1
- package/dist/src/framework/dependency.d.ts +25 -0
- package/dist/src/framework/dependency.d.ts.map +1 -1
- package/dist/src/framework/hooks/binder.d.ts +1 -1
- package/dist/src/framework/hooks/binder.d.ts.map +1 -1
- package/dist/src/framework/hooks/binder.js +16 -12
- package/dist/src/framework/hooks/binder.js.map +1 -1
- package/dist/src/framework/hooks/sdkTypes.d.ts +4 -2
- package/dist/src/framework/hooks/sdkTypes.d.ts.map +1 -1
- package/dist/src/framework/hooks/sdkTypes.js +43 -8
- package/dist/src/framework/hooks/sdkTypes.js.map +1 -1
- package/dist/src/framework/refkey.js +1 -1
- package/dist/src/framework/refkey.js.map +1 -1
- package/dist/src/framework/sample.js +1 -1
- package/dist/src/framework/sample.js.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +81 -24
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib.d.ts +37 -3
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +26 -1
- package/dist/src/lib.js.map +1 -1
- package/dist/src/modular/buildClassicalClient.d.ts +2 -2
- package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
- package/dist/src/modular/buildClassicalClient.js +25 -61
- package/dist/src/modular/buildClassicalClient.js.map +1 -1
- package/dist/src/modular/buildClassicalOperationGroups.d.ts.map +1 -1
- package/dist/src/modular/buildClassicalOperationGroups.js +0 -7
- package/dist/src/modular/buildClassicalOperationGroups.js.map +1 -1
- package/dist/src/modular/buildClientContext.d.ts +7 -3
- package/dist/src/modular/buildClientContext.d.ts.map +1 -1
- package/dist/src/modular/buildClientContext.js +58 -38
- package/dist/src/modular/buildClientContext.js.map +1 -1
- package/dist/src/modular/buildCodeModel.d.ts +18 -0
- package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
- package/dist/src/modular/buildCodeModel.js +95 -42
- package/dist/src/modular/buildCodeModel.js.map +1 -1
- package/dist/src/modular/buildOperations.d.ts +3 -4
- package/dist/src/modular/buildOperations.d.ts.map +1 -1
- package/dist/src/modular/buildOperations.js +24 -45
- package/dist/src/modular/buildOperations.js.map +1 -1
- package/dist/src/modular/buildProjectFiles.d.ts.map +1 -1
- package/dist/src/modular/buildProjectFiles.js +3 -1
- package/dist/src/modular/buildProjectFiles.js.map +1 -1
- package/dist/src/modular/buildRestorePoller.js +1 -1
- package/dist/src/modular/buildRestorePoller.js.map +1 -1
- package/dist/src/modular/buildRootIndex.d.ts +5 -0
- package/dist/src/modular/buildRootIndex.d.ts.map +1 -1
- package/dist/src/modular/buildRootIndex.js +62 -17
- package/dist/src/modular/buildRootIndex.js.map +1 -1
- package/dist/src/modular/buildSubpathIndex.d.ts +1 -1
- package/dist/src/modular/buildSubpathIndex.d.ts.map +1 -1
- package/dist/src/modular/buildSubpathIndex.js +11 -10
- package/dist/src/modular/buildSubpathIndex.js.map +1 -1
- package/dist/src/modular/emitModels.d.ts +13 -28
- package/dist/src/modular/emitModels.d.ts.map +1 -1
- package/dist/src/modular/emitModels.js +435 -240
- package/dist/src/modular/emitModels.js.map +1 -1
- package/dist/src/modular/emitModelsOptions.d.ts +26 -0
- package/dist/src/modular/emitModelsOptions.d.ts.map +1 -0
- package/dist/src/modular/emitModelsOptions.js +106 -0
- package/dist/src/modular/emitModelsOptions.js.map +1 -0
- package/dist/src/modular/emitSamples.d.ts +7 -0
- package/dist/src/modular/emitSamples.d.ts.map +1 -0
- package/dist/src/modular/emitSamples.js +354 -0
- package/dist/src/modular/emitSamples.js.map +1 -0
- package/dist/src/modular/external-dependencies.d.ts +7 -0
- package/dist/src/modular/external-dependencies.d.ts.map +1 -1
- package/dist/src/modular/external-dependencies.js +58 -1
- package/dist/src/modular/external-dependencies.js.map +1 -1
- package/dist/src/modular/helpers/classicalOperationHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/classicalOperationHelpers.js +2 -2
- package/dist/src/modular/helpers/classicalOperationHelpers.js.map +1 -1
- package/dist/src/modular/helpers/clientHelpers.d.ts +13 -6
- package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/clientHelpers.js +117 -48
- package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
- package/dist/src/modular/helpers/namingHelpers.d.ts +3 -2
- package/dist/src/modular/helpers/namingHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/namingHelpers.js +3 -0
- package/dist/src/modular/helpers/namingHelpers.js.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts +9 -11
- package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.js +255 -408
- package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.d.ts +12 -0
- package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.js +30 -6
- package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
- package/dist/src/modular/modularCodeModel.d.ts +2 -1
- package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
- package/dist/src/modular/serialization/buildDeserializerFunction.d.ts +5 -0
- package/dist/src/modular/serialization/buildDeserializerFunction.d.ts.map +1 -0
- package/dist/src/modular/serialization/buildDeserializerFunction.js +327 -0
- package/dist/src/modular/serialization/buildDeserializerFunction.js.map +1 -0
- package/dist/src/modular/serialization/buildSerializerFunction.d.ts +4 -3
- package/dist/src/modular/serialization/buildSerializerFunction.d.ts.map +1 -1
- package/dist/src/modular/serialization/buildSerializerFunction.js +285 -102
- package/dist/src/modular/serialization/buildSerializerFunction.js.map +1 -1
- package/dist/src/modular/serialization/serializeUtils.d.ts +15 -0
- package/dist/src/modular/serialization/serializeUtils.d.ts.map +1 -0
- package/dist/src/modular/serialization/serializeUtils.js +102 -0
- package/dist/src/modular/serialization/serializeUtils.js.map +1 -0
- package/dist/src/modular/type-expressions/get-credential-expression.d.ts +3 -0
- package/dist/src/modular/type-expressions/get-credential-expression.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/get-credential-expression.js +17 -0
- package/dist/src/modular/type-expressions/get-credential-expression.js.map +1 -0
- package/dist/src/modular/type-expressions/get-enum-expression.d.ts +6 -0
- package/dist/src/modular/type-expressions/get-enum-expression.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/get-enum-expression.js +18 -0
- package/dist/src/modular/type-expressions/get-enum-expression.js.map +1 -0
- package/dist/src/modular/type-expressions/get-model-expression.d.ts +9 -0
- package/dist/src/modular/type-expressions/get-model-expression.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/get-model-expression.js +42 -0
- package/dist/src/modular/type-expressions/get-model-expression.js.map +1 -0
- package/dist/src/modular/type-expressions/get-type-expression.d.ts +8 -0
- package/dist/src/modular/type-expressions/get-type-expression.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/get-type-expression.js +94 -0
- package/dist/src/modular/type-expressions/get-type-expression.js.map +1 -0
- package/dist/src/modular/type-expressions/get-union-expression.d.ts +5 -0
- package/dist/src/modular/type-expressions/get-union-expression.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/get-union-expression.js +13 -0
- package/dist/src/modular/type-expressions/get-union-expression.js.map +1 -0
- package/dist/src/modular/type-expressions/utils.d.ts +5 -0
- package/dist/src/modular/type-expressions/utils.d.ts.map +1 -0
- package/dist/src/modular/type-expressions/utils.js +9 -0
- package/dist/src/modular/type-expressions/utils.js.map +1 -0
- package/dist/src/transform/transform.js +1 -1
- package/dist/src/transform/transform.js.map +1 -1
- package/dist/src/transform/transformApiVersionInfo.js +1 -1
- package/dist/src/transform/transformApiVersionInfo.js.map +1 -1
- package/dist/src/transform/transformParameters.js +1 -1
- package/dist/src/transform/transformParameters.js.map +1 -1
- package/dist/src/transform/transformSchemas.d.ts.map +1 -1
- package/dist/src/transform/transformSchemas.js +3 -3
- package/dist/src/transform/transformSchemas.js.map +1 -1
- package/dist/src/utils/clientUtils.d.ts.map +1 -1
- package/dist/src/utils/clientUtils.js +1 -4
- package/dist/src/utils/clientUtils.js.map +1 -1
- package/dist/src/utils/credentialUtils.d.ts +5 -0
- package/dist/src/utils/credentialUtils.d.ts.map +1 -1
- package/dist/src/utils/credentialUtils.js +36 -4
- package/dist/src/utils/credentialUtils.js.map +1 -1
- package/dist/src/utils/modelUtils.d.ts.map +1 -1
- package/dist/src/utils/modelUtils.js +28 -43
- package/dist/src/utils/modelUtils.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +26 -26
- package/src/framework/declaration.ts +0 -1
- package/src/framework/dependency.ts +25 -0
- package/src/framework/hooks/binder.ts +19 -14
- package/src/framework/hooks/sdkTypes.ts +53 -13
- package/src/framework/refkey.ts +1 -1
- package/src/framework/sample.ts +1 -1
- package/src/index.ts +96 -25
- package/src/lib.ts +26 -3
- package/src/modular/buildClassicalClient.ts +30 -97
- package/src/modular/buildClassicalOperationGroups.ts +0 -14
- package/src/modular/buildClientContext.ts +90 -50
- package/src/modular/buildCodeModel.ts +118 -47
- package/src/modular/buildOperations.ts +41 -76
- package/src/modular/buildProjectFiles.ts +4 -2
- package/src/modular/buildRestorePoller.ts +1 -1
- package/src/modular/buildRootIndex.ts +75 -35
- package/src/modular/buildSubpathIndex.ts +13 -11
- package/src/modular/emitModels.ts +588 -326
- package/src/modular/emitModelsOptions.ts +161 -0
- package/src/modular/emitSamples.ts +481 -0
- package/src/modular/external-dependencies.ts +59 -1
- package/src/modular/helpers/classicalOperationHelpers.ts +6 -2
- package/src/modular/helpers/clientHelpers.ts +182 -76
- package/src/modular/helpers/namingHelpers.ts +14 -2
- package/src/modular/helpers/operationHelpers.ts +356 -638
- package/src/modular/helpers/typeHelpers.ts +38 -6
- package/src/modular/modularCodeModel.ts +3 -0
- package/src/modular/serialization/buildDeserializerFunction.ts +492 -0
- package/src/modular/serialization/buildSerializerFunction.ts +434 -141
- package/src/modular/serialization/serializeUtils.ts +143 -0
- package/src/modular/type-expressions/get-credential-expression.ts +18 -0
- package/src/modular/type-expressions/get-enum-expression.ts +28 -0
- package/src/modular/type-expressions/get-model-expression.ts +63 -0
- package/src/modular/type-expressions/get-type-expression.ts +111 -0
- package/src/modular/type-expressions/get-union-expression.ts +20 -0
- package/src/modular/type-expressions/utils.ts +15 -0
- package/src/transform/transform.ts +2 -2
- package/src/transform/transformApiVersionInfo.ts +2 -2
- package/src/transform/transformParameters.ts +1 -1
- package/src/transform/transformSchemas.ts +9 -8
- package/src/utils/clientUtils.ts +2 -12
- package/src/utils/credentialUtils.ts +43 -3
- package/src/utils/modelUtils.ts +32 -41
- package/static/static-helpers/serialization/serializers.ts +60 -0
- package/dist/src/modular/buildSerializeUtils.d.ts +0 -14
- package/dist/src/modular/buildSerializeUtils.d.ts.map +0 -1
- package/dist/src/modular/buildSerializeUtils.js +0 -534
- package/dist/src/modular/buildSerializeUtils.js.map +0 -1
- package/src/modular/buildSerializeUtils.ts +0 -749
|
@@ -1,144 +1,81 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Type as ModularType } from "../modularCodeModel.js";
|
|
3
|
-
import { getRequestModelMapping } from "../helpers/operationHelpers.js";
|
|
4
|
-
|
|
5
|
-
import { Imports as RuntimeImports } from "@azure-tools/rlc-common";
|
|
6
|
-
import { UsageFlags } from "@typespec/compiler";
|
|
1
|
+
import { FunctionDeclarationStructure, StructureKind } from "ts-morph";
|
|
7
2
|
import {
|
|
3
|
+
SdkArrayType,
|
|
4
|
+
SdkDictionaryType,
|
|
8
5
|
SdkModelType,
|
|
9
|
-
SdkType
|
|
6
|
+
SdkType,
|
|
7
|
+
SdkUnionType,
|
|
8
|
+
UsageFlags
|
|
10
9
|
} from "@azure-tools/typespec-client-generator-core";
|
|
10
|
+
import { toCamelCase, toPascalCase } from "../../utils/casingUtils.js";
|
|
11
|
+
|
|
12
|
+
import { SdkContext } from "../../utils/interfaces.js";
|
|
13
|
+
import { getRequestModelMapping } from "../helpers/operationHelpers.js";
|
|
14
|
+
import { getType } from "../buildCodeModel.js";
|
|
15
|
+
import { normalizeModelName } from "../emitModels.js";
|
|
16
|
+
import { NameType } from "@azure-tools/rlc-common";
|
|
17
|
+
import { isAzureCoreErrorType } from "../../utils/modelUtils.js";
|
|
18
|
+
import {
|
|
19
|
+
isDiscriminatedUnion,
|
|
20
|
+
isSupportedSerializeType,
|
|
21
|
+
ModelSerializeOptions
|
|
22
|
+
} from "./serializeUtils.js";
|
|
11
23
|
|
|
12
|
-
function getTcgcType(type: ModularType): SdkType {
|
|
13
|
-
if (type.tcgcType?.kind === "nullable") {
|
|
14
|
-
return type.tcgcType.type!;
|
|
15
|
-
}
|
|
16
|
-
return type.tcgcType!;
|
|
17
|
-
}
|
|
18
24
|
export function buildModelSerializer(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
) {
|
|
25
|
+
context: SdkContext,
|
|
26
|
+
type: SdkType,
|
|
27
|
+
skipDiscriminatedUnion = false,
|
|
28
|
+
nameOnly: boolean = false
|
|
29
|
+
): FunctionDeclarationStructure | undefined | string {
|
|
30
|
+
// const modelTcgcType = getTcgcType(type) as SdkModelType;
|
|
31
|
+
if (!isSupportedSerializeType(type)) {
|
|
27
32
|
return undefined;
|
|
28
33
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
if (type.kind === "model" || type.kind === "union" || type.kind === "enum") {
|
|
35
|
+
if (
|
|
36
|
+
!type.usage ||
|
|
37
|
+
(type.usage !== undefined &&
|
|
38
|
+
(type.usage & UsageFlags.Input) !== UsageFlags.Input)
|
|
39
|
+
) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (!type.name) {
|
|
43
|
+
// TODO: https://github.com/Azure/typespec-azure/issues/1713 and https://github.com/microsoft/typespec/issues/4815
|
|
44
|
+
// throw new Error(`NYI Serialization of anonymous types`);
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (isAzureCoreErrorType(context.program, type.__raw!)) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
32
50
|
}
|
|
33
51
|
|
|
34
|
-
let serializerName = `${toCamelCase(type.name)}Serializer`;
|
|
35
|
-
|
|
36
|
-
const output: string[] = [];
|
|
37
|
-
|
|
38
52
|
if (
|
|
39
53
|
!isDiscriminatedUnion(type) &&
|
|
40
|
-
type.
|
|
41
|
-
type.
|
|
54
|
+
type.kind === "model" &&
|
|
55
|
+
type.discriminatorProperty
|
|
42
56
|
) {
|
|
43
|
-
return buildPolymorphicSerializer(type);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (isDiscriminatedUnion(type)) {
|
|
47
|
-
const discriminatedTgcType = getTcgcType(type) as SdkModelType;
|
|
48
|
-
const cases: string[] = [];
|
|
49
|
-
const baseSerializerName = `${toCamelCase(
|
|
50
|
-
discriminatedTgcType.name
|
|
51
|
-
)}Serializer`;
|
|
52
|
-
for (const key in discriminatedTgcType.discriminatedSubtypes) {
|
|
53
|
-
const subType = discriminatedTgcType.discriminatedSubtypes[key]!;
|
|
54
|
-
const discriminatedValue = subType.discriminatorValue!;
|
|
55
|
-
const union = subType.discriminatedSubtypes ? "Union" : "";
|
|
56
|
-
const subTypeName = `${toPascalCase(subType.name)}${union}`;
|
|
57
|
-
const subtypeSerializerName = toCamelCase(`${subTypeName}Serializer`);
|
|
58
|
-
|
|
59
|
-
cases.push(`
|
|
60
|
-
case "${discriminatedValue}":
|
|
61
|
-
return ${subtypeSerializerName}(item as ${subTypeName});
|
|
62
|
-
`);
|
|
63
|
-
}
|
|
64
|
-
output.push(`
|
|
65
|
-
export function ${toCamelCase(type.name)}Serializer(item: ${toPascalCase(
|
|
66
|
-
type.name
|
|
67
|
-
)}) {
|
|
68
|
-
switch (item.${type.discriminator}) {
|
|
69
|
-
${cases.join("\n")}
|
|
70
|
-
default:
|
|
71
|
-
return ${baseSerializerName}(item);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
`);
|
|
75
|
-
|
|
76
|
-
serializerName = baseSerializerName;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (type.type === "model" || type.type === "dict") {
|
|
80
|
-
const nullabilityPrefix = "";
|
|
81
|
-
// getPropertySerializationPrefix({
|
|
82
|
-
// clientName: "item",
|
|
83
|
-
// type
|
|
84
|
-
// });
|
|
85
|
-
|
|
86
|
-
// This is only handling the compatibility mode, will need to update when we handle additionalProperties property.
|
|
87
|
-
const additionalPropertiesSpread = hasAdditionalProperties(type.tcgcType)
|
|
88
|
-
? "...item,"
|
|
89
|
-
: "";
|
|
90
|
-
|
|
91
|
-
const { propertiesStr, directAssignment } = getRequestModelMapping(
|
|
92
|
-
type,
|
|
93
|
-
"item",
|
|
94
|
-
runtimeImports
|
|
95
|
-
);
|
|
96
|
-
const propertiesSerialization = propertiesStr.filter((p) => p.trim());
|
|
97
|
-
|
|
98
|
-
// don't emit a serializer if there is nothing to serialize
|
|
99
|
-
if (propertiesSerialization.length || additionalPropertiesSpread) {
|
|
100
|
-
const spreadSerialized = directAssignment ? "..." : "";
|
|
101
|
-
const fnBody = `{
|
|
102
|
-
${additionalPropertiesSpread}
|
|
103
|
-
${nullabilityPrefix} ${spreadSerialized} ${propertiesSerialization.join(
|
|
104
|
-
",\n"
|
|
105
|
-
)}
|
|
106
|
-
}`;
|
|
107
|
-
output.push(`
|
|
108
|
-
export function ${serializerName}(item: ${toPascalCase(
|
|
109
|
-
type.name
|
|
110
|
-
)}): Record<string, unknown> {
|
|
111
|
-
return ${fnBody}
|
|
112
|
-
}
|
|
113
|
-
`);
|
|
114
|
-
} else {
|
|
115
|
-
output.push(`
|
|
116
|
-
export function ${serializerName}(item: ${toPascalCase(type.name)}) {
|
|
117
|
-
return item as any;
|
|
118
|
-
}
|
|
119
|
-
`);
|
|
120
|
-
}
|
|
121
|
-
} else if (type.type === "enum") {
|
|
122
|
-
output.push(`
|
|
123
|
-
export function ${serializerName}(item: ${toPascalCase(type.name)}) {
|
|
124
|
-
return item;
|
|
125
|
-
}
|
|
126
|
-
`);
|
|
57
|
+
return buildPolymorphicSerializer(context, type, nameOnly);
|
|
127
58
|
}
|
|
128
59
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
function isDiscriminatedUnion(
|
|
133
|
-
type: ModularType
|
|
134
|
-
): type is ModularType & { tcgcType: SdkModelType } {
|
|
135
|
-
const { tcgcType } = type;
|
|
60
|
+
if (isDiscriminatedUnion(type) && !skipDiscriminatedUnion) {
|
|
61
|
+
return buildDiscriminatedUnionSerializer(context, type, nameOnly);
|
|
62
|
+
}
|
|
136
63
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
64
|
+
switch (type.kind) {
|
|
65
|
+
case "model":
|
|
66
|
+
return buildModelTypeSerializer(context, type, {
|
|
67
|
+
nameOnly,
|
|
68
|
+
skipDiscriminatedUnionSuffix: skipDiscriminatedUnion
|
|
69
|
+
});
|
|
70
|
+
case "union": // for non-discriminated union, we just return whatever we get
|
|
71
|
+
return buildUnionSerializer(context, type, nameOnly);
|
|
72
|
+
case "dict":
|
|
73
|
+
return buildDictTypeSerializer(context, type, nameOnly);
|
|
74
|
+
case "array":
|
|
75
|
+
return buildArrayTypeSerializer(context, type, nameOnly);
|
|
76
|
+
default:
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
142
79
|
}
|
|
143
80
|
|
|
144
81
|
function hasAdditionalProperties(type: SdkType | undefined) {
|
|
@@ -157,38 +94,394 @@ function hasAdditionalProperties(type: SdkType | undefined) {
|
|
|
157
94
|
return false;
|
|
158
95
|
}
|
|
159
96
|
|
|
160
|
-
function buildPolymorphicSerializer(
|
|
161
|
-
|
|
97
|
+
function buildPolymorphicSerializer(
|
|
98
|
+
context: SdkContext,
|
|
99
|
+
type: SdkModelType,
|
|
100
|
+
nameOnly: boolean
|
|
101
|
+
): string;
|
|
102
|
+
function buildPolymorphicSerializer(
|
|
103
|
+
context: SdkContext,
|
|
104
|
+
type: SdkModelType
|
|
105
|
+
): FunctionDeclarationStructure | undefined;
|
|
106
|
+
function buildPolymorphicSerializer(
|
|
107
|
+
context: SdkContext,
|
|
108
|
+
type: SdkModelType,
|
|
109
|
+
nameOnly = false
|
|
110
|
+
): FunctionDeclarationStructure | undefined | string {
|
|
111
|
+
if (!type.name) {
|
|
112
|
+
throw new Error(`NYI Serialization of anonymous types`);
|
|
113
|
+
}
|
|
114
|
+
const serializeFunctionName = `${normalizeModelName(
|
|
115
|
+
context,
|
|
116
|
+
type,
|
|
117
|
+
NameType.Operation
|
|
118
|
+
)}Serializer`;
|
|
119
|
+
if (nameOnly) {
|
|
120
|
+
return serializeFunctionName;
|
|
121
|
+
}
|
|
122
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
123
|
+
kind: StructureKind.Function,
|
|
124
|
+
name: serializeFunctionName,
|
|
125
|
+
isExported: true,
|
|
126
|
+
parameters: [
|
|
127
|
+
{
|
|
128
|
+
name: "item",
|
|
129
|
+
type: normalizeModelName(context, type)
|
|
130
|
+
}
|
|
131
|
+
],
|
|
132
|
+
returnType: "any",
|
|
133
|
+
statements: []
|
|
134
|
+
};
|
|
135
|
+
if (!type.discriminatorProperty) {
|
|
162
136
|
return;
|
|
163
137
|
}
|
|
164
|
-
const
|
|
138
|
+
const statements: string[] = [];
|
|
165
139
|
|
|
166
|
-
const subTypes = type.
|
|
140
|
+
const subTypes = type.discriminatedSubtypes;
|
|
141
|
+
if (!subTypes) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
167
144
|
|
|
168
145
|
const cases: string[] = [];
|
|
169
|
-
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
146
|
+
Object.keys(subTypes).forEach((discriminatedValue) => {
|
|
147
|
+
const subType = subTypes[discriminatedValue];
|
|
148
|
+
if (
|
|
149
|
+
!subType?.usage ||
|
|
150
|
+
(subType?.usage !== undefined &&
|
|
151
|
+
(subType.usage & UsageFlags.Input) !== UsageFlags.Input)
|
|
152
|
+
) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const union = subType?.discriminatedSubtypes ? "Union" : "";
|
|
156
|
+
if (!subType || subType?.name) {
|
|
157
|
+
throw new Error(`NYI Serialization of anonymous types`);
|
|
158
|
+
}
|
|
159
|
+
const subTypeName = `${toPascalCase(subType.name)}${union}`;
|
|
173
160
|
const subtypeSerializerName = toCamelCase(`${subTypeName}Serializer`);
|
|
174
161
|
|
|
175
162
|
cases.push(`
|
|
176
163
|
case "${discriminatedValue}":
|
|
177
164
|
return ${subtypeSerializerName}(item as ${subTypeName});
|
|
178
165
|
`);
|
|
179
|
-
}
|
|
166
|
+
});
|
|
180
167
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
type.name!
|
|
184
|
-
)}) {
|
|
185
|
-
switch (item.${type.discriminator}) {
|
|
168
|
+
statements.push(`
|
|
169
|
+
switch (item.${type.discriminatorProperty.name}) {
|
|
186
170
|
${cases.join("\n")}
|
|
187
171
|
default:
|
|
188
172
|
return item;
|
|
189
173
|
}
|
|
174
|
+
`);
|
|
175
|
+
serializerFunction.statements = statements.join("\n");
|
|
176
|
+
return serializerFunction;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function buildDiscriminatedUnionSerializer(
|
|
180
|
+
context: SdkContext,
|
|
181
|
+
type: SdkModelType,
|
|
182
|
+
nameOnly: boolean
|
|
183
|
+
): string;
|
|
184
|
+
function buildDiscriminatedUnionSerializer(
|
|
185
|
+
context: SdkContext,
|
|
186
|
+
type: SdkModelType
|
|
187
|
+
): FunctionDeclarationStructure | undefined;
|
|
188
|
+
function buildDiscriminatedUnionSerializer(
|
|
189
|
+
context: SdkContext,
|
|
190
|
+
type: SdkModelType,
|
|
191
|
+
nameOnly = false
|
|
192
|
+
): FunctionDeclarationStructure | undefined | string {
|
|
193
|
+
if (!type.name) {
|
|
194
|
+
throw new Error(`NYI Serialization of anonymous types`);
|
|
195
|
+
}
|
|
196
|
+
const cases: string[] = [];
|
|
197
|
+
const output: string[] = [];
|
|
198
|
+
const serializeFunctionName = `${normalizeModelName(
|
|
199
|
+
context,
|
|
200
|
+
type,
|
|
201
|
+
NameType.Operation
|
|
202
|
+
)}Serializer`;
|
|
203
|
+
if (nameOnly) {
|
|
204
|
+
return serializeFunctionName;
|
|
205
|
+
}
|
|
206
|
+
const baseSerializerName = `${normalizeModelName(
|
|
207
|
+
context,
|
|
208
|
+
type,
|
|
209
|
+
NameType.Operation,
|
|
210
|
+
true
|
|
211
|
+
)}Serializer`;
|
|
212
|
+
for (const key in type.discriminatedSubtypes) {
|
|
213
|
+
const subType = type.discriminatedSubtypes[key]!;
|
|
214
|
+
if (
|
|
215
|
+
!subType.usage ||
|
|
216
|
+
(subType.usage !== undefined &&
|
|
217
|
+
(subType.usage & UsageFlags.Input) !== UsageFlags.Input)
|
|
218
|
+
) {
|
|
219
|
+
continue;
|
|
190
220
|
}
|
|
221
|
+
const discriminatedValue = subType.discriminatorValue!;
|
|
222
|
+
const union = subType.discriminatedSubtypes ? "Union" : "";
|
|
223
|
+
const subTypeName = `${toPascalCase(subType.name)}${union}`;
|
|
224
|
+
const subtypeSerializerName = toCamelCase(`${subTypeName}Serializer`);
|
|
225
|
+
|
|
226
|
+
cases.push(`
|
|
227
|
+
case "${discriminatedValue}":
|
|
228
|
+
return ${subtypeSerializerName}(item as ${subTypeName});
|
|
191
229
|
`);
|
|
230
|
+
}
|
|
231
|
+
output.push(`
|
|
232
|
+
switch (item.${type.discriminatorProperty?.name}) {
|
|
233
|
+
${cases.join("\n")}
|
|
234
|
+
default:
|
|
235
|
+
return ${baseSerializerName}(item);
|
|
236
|
+
}
|
|
237
|
+
`);
|
|
238
|
+
|
|
239
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
240
|
+
kind: StructureKind.Function,
|
|
241
|
+
name: serializeFunctionName,
|
|
242
|
+
isExported: true,
|
|
243
|
+
parameters: [
|
|
244
|
+
{
|
|
245
|
+
name: "item",
|
|
246
|
+
type: normalizeModelName(context, type)
|
|
247
|
+
}
|
|
248
|
+
],
|
|
249
|
+
returnType: "any",
|
|
250
|
+
statements: output.join("\n")
|
|
251
|
+
};
|
|
252
|
+
return serializerFunction;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function buildUnionSerializer(
|
|
256
|
+
context: SdkContext,
|
|
257
|
+
type: SdkUnionType,
|
|
258
|
+
nameOnly: boolean
|
|
259
|
+
): string;
|
|
260
|
+
function buildUnionSerializer(
|
|
261
|
+
context: SdkContext,
|
|
262
|
+
type: SdkUnionType
|
|
263
|
+
): FunctionDeclarationStructure;
|
|
264
|
+
function buildUnionSerializer(
|
|
265
|
+
context: SdkContext,
|
|
266
|
+
type: SdkUnionType,
|
|
267
|
+
nameOnly = false
|
|
268
|
+
): FunctionDeclarationStructure | string {
|
|
269
|
+
if (!type.name) {
|
|
270
|
+
throw new Error(`NYI Serialization of anonymous types`);
|
|
271
|
+
}
|
|
272
|
+
const serializerFunctionName = `${normalizeModelName(
|
|
273
|
+
context,
|
|
274
|
+
type,
|
|
275
|
+
NameType.Operation
|
|
276
|
+
)}Serializer`;
|
|
277
|
+
if (nameOnly) {
|
|
278
|
+
return serializerFunctionName;
|
|
279
|
+
}
|
|
280
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
281
|
+
kind: StructureKind.Function,
|
|
282
|
+
name: `${normalizeModelName(context, type, NameType.Operation)}Serializer`,
|
|
283
|
+
isExported: true,
|
|
284
|
+
parameters: [
|
|
285
|
+
{
|
|
286
|
+
name: "item",
|
|
287
|
+
type: normalizeModelName(context, type)
|
|
288
|
+
}
|
|
289
|
+
],
|
|
290
|
+
returnType: "any",
|
|
291
|
+
statements: ["return item;"]
|
|
292
|
+
};
|
|
293
|
+
return serializerFunction;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function buildModelTypeSerializer(
|
|
297
|
+
context: SdkContext,
|
|
298
|
+
type: SdkModelType,
|
|
299
|
+
options: ModelSerializeOptions = {
|
|
300
|
+
nameOnly: false,
|
|
301
|
+
skipDiscriminatedUnionSuffix: false
|
|
302
|
+
}
|
|
303
|
+
): FunctionDeclarationStructure | string {
|
|
304
|
+
if (!type.name) {
|
|
305
|
+
throw new Error(`NYI Deserialization of anonymous types`);
|
|
306
|
+
}
|
|
307
|
+
const serializerFunctionName = `${normalizeModelName(
|
|
308
|
+
context,
|
|
309
|
+
type,
|
|
310
|
+
NameType.Operation,
|
|
311
|
+
options.skipDiscriminatedUnionSuffix
|
|
312
|
+
)}Serializer`;
|
|
313
|
+
if (options.nameOnly) {
|
|
314
|
+
return serializerFunctionName;
|
|
315
|
+
}
|
|
316
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
317
|
+
kind: StructureKind.Function,
|
|
318
|
+
name: serializerFunctionName,
|
|
319
|
+
isExported: true,
|
|
320
|
+
parameters: [
|
|
321
|
+
{
|
|
322
|
+
name: "item",
|
|
323
|
+
type: normalizeModelName(
|
|
324
|
+
context,
|
|
325
|
+
type,
|
|
326
|
+
NameType.Interface,
|
|
327
|
+
options.skipDiscriminatedUnionSuffix
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
],
|
|
331
|
+
returnType: "any",
|
|
332
|
+
statements: ["return item;"]
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// This is only handling the compatibility mode, will need to update when we handle additionalProperties property.
|
|
336
|
+
const additionalPropertiesSpread = hasAdditionalProperties(type)
|
|
337
|
+
? "...item"
|
|
338
|
+
: "";
|
|
339
|
+
|
|
340
|
+
const { directAssignment, propertiesStr } = getRequestModelMapping(
|
|
341
|
+
context,
|
|
342
|
+
getType(context, type.__raw!),
|
|
343
|
+
"item"
|
|
344
|
+
);
|
|
345
|
+
if (additionalPropertiesSpread) {
|
|
346
|
+
propertiesStr.unshift(additionalPropertiesSpread);
|
|
347
|
+
}
|
|
348
|
+
const serializeContent =
|
|
349
|
+
directAssignment === true
|
|
350
|
+
? propertiesStr.join(",")
|
|
351
|
+
: `{${propertiesStr.join(",")}}`;
|
|
352
|
+
|
|
353
|
+
const output = [];
|
|
354
|
+
|
|
355
|
+
// don't emit a serializer if there is nothing to serialize
|
|
356
|
+
if (propertiesStr.length) {
|
|
357
|
+
output.push(`
|
|
358
|
+
return ${serializeContent}
|
|
359
|
+
`);
|
|
360
|
+
} else {
|
|
361
|
+
output.push(`
|
|
362
|
+
return item;
|
|
363
|
+
`);
|
|
364
|
+
}
|
|
365
|
+
serializerFunction.statements = output;
|
|
366
|
+
return serializerFunction;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function buildDictTypeSerializer(
|
|
370
|
+
context: SdkContext,
|
|
371
|
+
type: SdkDictionaryType,
|
|
372
|
+
nameOnly: boolean
|
|
373
|
+
): string;
|
|
374
|
+
function buildDictTypeSerializer(
|
|
375
|
+
context: SdkContext,
|
|
376
|
+
type: SdkDictionaryType
|
|
377
|
+
): FunctionDeclarationStructure | undefined;
|
|
378
|
+
function buildDictTypeSerializer(
|
|
379
|
+
context: SdkContext,
|
|
380
|
+
type: SdkDictionaryType,
|
|
381
|
+
nameOnly = false
|
|
382
|
+
): FunctionDeclarationStructure | undefined | string {
|
|
383
|
+
const valueSerializer = buildModelSerializer(
|
|
384
|
+
context,
|
|
385
|
+
type.valueType,
|
|
386
|
+
false,
|
|
387
|
+
true
|
|
388
|
+
);
|
|
389
|
+
if (!valueSerializer) {
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
if (!isSupportedSerializeType(type.valueType)) {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (typeof valueSerializer !== "string") {
|
|
397
|
+
return undefined;
|
|
398
|
+
}
|
|
399
|
+
const valueTypeName = toCamelCase(
|
|
400
|
+
valueSerializer ? valueSerializer.replace("Serializer", "") : ""
|
|
401
|
+
);
|
|
402
|
+
const serializerFunctionName = `${valueTypeName}RecordSerializer`;
|
|
403
|
+
if (nameOnly) {
|
|
404
|
+
return serializerFunctionName;
|
|
405
|
+
}
|
|
406
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
407
|
+
kind: StructureKind.Function,
|
|
408
|
+
name: serializerFunctionName,
|
|
409
|
+
isExported: true,
|
|
410
|
+
parameters: [
|
|
411
|
+
{
|
|
412
|
+
name: "item",
|
|
413
|
+
type: `Record<string, ${normalizeModelName(context, type.valueType as any) ?? "any"}>`
|
|
414
|
+
}
|
|
415
|
+
],
|
|
416
|
+
returnType: "Record<string, any>",
|
|
417
|
+
statements: [
|
|
418
|
+
`
|
|
419
|
+
const result: Record<string, any> = {};
|
|
420
|
+
Object.keys(item).map((key) => {
|
|
421
|
+
result[key] = !item[key]? item[key]: ${valueSerializer}(item[key])
|
|
422
|
+
});
|
|
423
|
+
return result;
|
|
424
|
+
`
|
|
425
|
+
]
|
|
426
|
+
};
|
|
427
|
+
return serializerFunction;
|
|
428
|
+
}
|
|
192
429
|
|
|
193
|
-
|
|
430
|
+
function buildArrayTypeSerializer(
|
|
431
|
+
context: SdkContext,
|
|
432
|
+
type: SdkArrayType,
|
|
433
|
+
nameOnly: boolean
|
|
434
|
+
): string;
|
|
435
|
+
function buildArrayTypeSerializer(
|
|
436
|
+
context: SdkContext,
|
|
437
|
+
type: SdkArrayType
|
|
438
|
+
): FunctionDeclarationStructure | undefined;
|
|
439
|
+
function buildArrayTypeSerializer(
|
|
440
|
+
context: SdkContext,
|
|
441
|
+
type: SdkArrayType,
|
|
442
|
+
nameOnly = false
|
|
443
|
+
): FunctionDeclarationStructure | undefined | string {
|
|
444
|
+
const valueSerializer = buildModelSerializer(
|
|
445
|
+
context,
|
|
446
|
+
type.valueType,
|
|
447
|
+
false,
|
|
448
|
+
true
|
|
449
|
+
);
|
|
450
|
+
if (!valueSerializer) {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
if (!isSupportedSerializeType(type.valueType)) {
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (typeof valueSerializer !== "string") {
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
const valueTypeName = toCamelCase(
|
|
461
|
+
valueSerializer ? valueSerializer.replace("Serializer", "") : ""
|
|
462
|
+
);
|
|
463
|
+
const serializerFunctionName = `${valueTypeName}ArraySerializer`;
|
|
464
|
+
if (nameOnly) {
|
|
465
|
+
return serializerFunctionName;
|
|
466
|
+
}
|
|
467
|
+
const serializerFunction: FunctionDeclarationStructure = {
|
|
468
|
+
kind: StructureKind.Function,
|
|
469
|
+
name: serializerFunctionName,
|
|
470
|
+
isExported: true,
|
|
471
|
+
parameters: [
|
|
472
|
+
{
|
|
473
|
+
name: "result",
|
|
474
|
+
type: `Array<${normalizeModelName(context, type.valueType as any) ?? "any"}>`
|
|
475
|
+
}
|
|
476
|
+
],
|
|
477
|
+
returnType: "any[]",
|
|
478
|
+
statements: [
|
|
479
|
+
`
|
|
480
|
+
return result.map((item) => {
|
|
481
|
+
return ${valueSerializer}(item)
|
|
482
|
+
});
|
|
483
|
+
`
|
|
484
|
+
]
|
|
485
|
+
};
|
|
486
|
+
return serializerFunction;
|
|
194
487
|
}
|