@azure-tools/typespec-ts 0.15.0 → 0.16.0-alpha.20230918.1
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 +10 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +14 -17
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +3 -3
- package/dist/src/lib.js.map +1 -1
- package/dist/src/modular/buildClassicalClient.d.ts +2 -3
- package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
- package/dist/src/modular/buildClassicalClient.js +26 -12
- package/dist/src/modular/buildClassicalClient.js.map +1 -1
- package/dist/src/modular/buildClientContext.d.ts +3 -3
- package/dist/src/modular/buildClientContext.d.ts.map +1 -1
- package/dist/src/modular/buildClientContext.js +5 -4
- package/dist/src/modular/buildClientContext.js.map +1 -1
- package/dist/src/modular/buildCodeModel.d.ts +3 -2
- package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
- package/dist/src/modular/buildCodeModel.js +163 -69
- package/dist/src/modular/buildCodeModel.js.map +1 -1
- package/dist/src/modular/buildOperations.d.ts +2 -2
- package/dist/src/modular/buildOperations.d.ts.map +1 -1
- package/dist/src/modular/buildOperations.js +15 -8
- package/dist/src/modular/buildOperations.js.map +1 -1
- package/dist/src/modular/buildProjectFiles.js +2 -2
- package/dist/src/modular/buildProjectFiles.js.map +1 -1
- package/dist/src/modular/buildRootIndex.d.ts +4 -4
- package/dist/src/modular/buildRootIndex.d.ts.map +1 -1
- package/dist/src/modular/buildRootIndex.js +12 -5
- package/dist/src/modular/buildRootIndex.js.map +1 -1
- package/dist/src/modular/buildSubpathIndex.d.ts +2 -2
- package/dist/src/modular/buildSubpathIndex.d.ts.map +1 -1
- package/dist/src/modular/buildSubpathIndex.js +7 -4
- package/dist/src/modular/buildSubpathIndex.js.map +1 -1
- package/dist/src/modular/emitModels.d.ts +3 -3
- package/dist/src/modular/emitModels.d.ts.map +1 -1
- package/dist/src/modular/emitModels.js +28 -18
- package/dist/src/modular/emitModels.js.map +1 -1
- package/dist/src/modular/helpers/clientHelpers.js +1 -1
- package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts +1 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.js +167 -88
- package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.d.ts +2 -2
- package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.js +13 -7
- package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
- package/dist/src/modular/modularCodeModel.d.ts +11 -0
- package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
- package/dist/src/transform/transformHelperFunctionDetails.js +3 -4
- package/dist/src/transform/transformHelperFunctionDetails.js.map +1 -1
- package/dist/src/transform/transformParameters.d.ts +0 -10
- package/dist/src/transform/transformParameters.d.ts.map +1 -1
- package/dist/src/transform/transformParameters.js +7 -51
- package/dist/src/transform/transformParameters.js.map +1 -1
- package/dist/src/transform/transformResponses.js +3 -4
- package/dist/src/transform/transformResponses.js.map +1 -1
- package/dist/src/transform/transformSchemas.d.ts.map +1 -1
- package/dist/src/transform/transformSchemas.js +4 -8
- package/dist/src/transform/transformSchemas.js.map +1 -1
- package/dist/src/transform/transformTelemetryInfo.d.ts.map +1 -1
- package/dist/src/transform/transformTelemetryInfo.js +3 -10
- package/dist/src/transform/transformTelemetryInfo.js.map +1 -1
- package/dist/src/transform/transfromRLCOptions.d.ts.map +1 -1
- package/dist/src/transform/transfromRLCOptions.js +48 -9
- package/dist/src/transform/transfromRLCOptions.js.map +1 -1
- package/dist/src/utils/modelUtils.d.ts +2 -1
- package/dist/src/utils/modelUtils.d.ts.map +1 -1
- package/dist/src/utils/modelUtils.js +83 -73
- package/dist/src/utils/modelUtils.js.map +1 -1
- package/dist/src/utils/operationUtil.d.ts +15 -1
- package/dist/src/utils/operationUtil.d.ts.map +1 -1
- package/dist/src/utils/operationUtil.js +82 -3
- package/dist/src/utils/operationUtil.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -11
- package/src/index.ts +24 -51
- package/src/lib.ts +3 -3
- package/src/modular/buildClassicalClient.ts +29 -16
- package/src/modular/buildClientContext.ts +8 -9
- package/src/modular/buildCodeModel.ts +212 -75
- package/src/modular/buildOperations.ts +16 -11
- package/src/modular/buildProjectFiles.ts +2 -2
- package/src/modular/buildRootIndex.ts +19 -12
- package/src/modular/buildSubpathIndex.ts +10 -8
- package/src/modular/emitModels.ts +35 -24
- package/src/modular/helpers/clientHelpers.ts +1 -1
- package/src/modular/helpers/operationHelpers.ts +256 -127
- package/src/modular/helpers/typeHelpers.ts +19 -7
- package/src/modular/modularCodeModel.ts +11 -0
- package/src/transform/transformHelperFunctionDetails.ts +10 -4
- package/src/transform/transformParameters.ts +15 -57
- package/src/transform/transformResponses.ts +2 -4
- package/src/transform/transformSchemas.ts +5 -9
- package/src/transform/transformTelemetryInfo.ts +4 -18
- package/src/transform/transfromRLCOptions.ts +67 -9
- package/src/utils/modelUtils.ts +81 -72
- package/src/utils/operationUtil.ts +121 -4
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { getPagedResult, isFixed } from "@azure-tools/typespec-azure-core";
|
|
2
|
-
import { getDoc, getFriendlyName, getMaxLength, getMaxValue, getMinLength, getMinValue, getPattern, getSummary, getVisibility, ignoreDiagnostics, isErrorModel, isNeverType, getEffectiveModelType, getDiscriminator, isKey, isStringType, getPropertyType, isNumericType,
|
|
3
|
-
import { getAuthentication, getHeaderFieldName,
|
|
2
|
+
import { getDoc, getFriendlyName, getMaxLength, getMaxValue, getMinLength, getMinValue, getPattern, getSummary, getVisibility, ignoreDiagnostics, isErrorModel, isNeverType, getEffectiveModelType, getDiscriminator, isKey, isStringType, getPropertyType, isNumericType, getMinItems, getMaxItems, listServices, getProjectedName, isNullType, getEncode, isTemplateDeclarationOrInstance, getFormat } from "@typespec/compiler";
|
|
3
|
+
import { getAuthentication, getHeaderFieldName, getPathParamName, getQueryParamName, getServers, isStatusCode, isHeader, getHttpOperation } from "@typespec/http";
|
|
4
4
|
import { getAddedOnVersions } from "@typespec/versioning";
|
|
5
|
-
import { listClients, listOperationGroups, listOperationsInOperationGroup, isApiVersion, getDefaultApiVersion, getClientNamespaceString,
|
|
5
|
+
import { listClients, listOperationGroups, listOperationsInOperationGroup, isApiVersion, getDefaultApiVersion, getClientNamespaceString, getSdkUnion, getAllModels, getSdkSimpleType } from "@azure-tools/typespec-client-generator-core";
|
|
6
6
|
import { getResourceOperation } from "@typespec/rest";
|
|
7
|
-
import {
|
|
8
|
-
import { getEnrichedDefaultApiVersion } from "../utils/modelUtils.js";
|
|
7
|
+
import { getEnrichedDefaultApiVersion, isAzureCoreErrorType } from "../utils/modelUtils.js";
|
|
9
8
|
import { camelToSnakeCase, toCamelCase } from "../utils/casingUtils.js";
|
|
10
|
-
import { getClientName } from "@azure-tools/rlc-common";
|
|
11
|
-
import { getOperationGroupName, getOperationName } from "../utils/operationUtil.js";
|
|
9
|
+
import { getClientName, NameType, normalizeName } from "@azure-tools/rlc-common";
|
|
10
|
+
import { getOperationGroupName, getOperationName, isIgnoredHeaderParam } from "../utils/operationUtil.js";
|
|
12
11
|
let CASING = "snake";
|
|
13
12
|
function applyCasing(name, options = { casing: "snake" }) {
|
|
14
13
|
if (options.casing === "camel") {
|
|
@@ -28,7 +27,8 @@ function isSimpleType(program, type) {
|
|
|
28
27
|
getMaxValue,
|
|
29
28
|
getMinLength,
|
|
30
29
|
getMaxLength,
|
|
31
|
-
getPattern
|
|
30
|
+
getPattern,
|
|
31
|
+
getEncode
|
|
32
32
|
];
|
|
33
33
|
for (const func of funcs) {
|
|
34
34
|
if (func(program, type)) {
|
|
@@ -50,34 +50,37 @@ function isLro(_program, operation) {
|
|
|
50
50
|
}
|
|
51
51
|
return false;
|
|
52
52
|
}
|
|
53
|
-
function
|
|
53
|
+
function isDiscriminator(context, type, propertyName) {
|
|
54
|
+
const discriminator = getDiscriminator(context.program, type);
|
|
55
|
+
if (discriminator && discriminator.propertyName === propertyName) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function handleDiscriminator(context, type) {
|
|
54
61
|
const discriminator = getDiscriminator(context.program, type);
|
|
55
62
|
if (discriminator) {
|
|
56
|
-
|
|
63
|
+
const discriminatorValues = [];
|
|
57
64
|
for (const childModel of type.derivedModels) {
|
|
58
65
|
const modelType = getType(context, childModel);
|
|
59
66
|
for (const property of modelType.properties) {
|
|
60
67
|
if (property.restApiName === discriminator.propertyName) {
|
|
61
68
|
modelType.discriminatorValue = property.type.value;
|
|
62
|
-
|
|
63
|
-
model.discriminatedSubtypes[property.type.value] = modelType;
|
|
64
|
-
discriminatorProperty = property;
|
|
69
|
+
discriminatorValues.push(modelType.discriminatorValue);
|
|
65
70
|
}
|
|
66
71
|
}
|
|
67
72
|
}
|
|
68
|
-
// it is not included in properties of
|
|
69
|
-
if (
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
const propertyCopy = {
|
|
73
|
-
...discriminatorProperty,
|
|
73
|
+
// it is not included in properties of typespec but needed by python codegen
|
|
74
|
+
if (discriminatorValues.length > 0) {
|
|
75
|
+
const discriminatorInfo = {
|
|
76
|
+
description: `the discriminator possible values ${discriminatorValues.join(", ")}`,
|
|
74
77
|
isPolymorphic: true,
|
|
75
|
-
|
|
78
|
+
isDiscriminator: true
|
|
76
79
|
};
|
|
77
|
-
|
|
78
|
-
model.properties.push(propertyCopy);
|
|
80
|
+
return discriminatorInfo;
|
|
79
81
|
}
|
|
80
82
|
}
|
|
83
|
+
return undefined;
|
|
81
84
|
}
|
|
82
85
|
function getEffectiveSchemaType(program, type) {
|
|
83
86
|
var _a;
|
|
@@ -125,14 +128,18 @@ function processModelProperties(context, newValue, model) {
|
|
|
125
128
|
if (newValue.properties === undefined || newValue.properties === null) {
|
|
126
129
|
newValue.properties = [];
|
|
127
130
|
}
|
|
128
|
-
|
|
131
|
+
let newProperty = emitProperty(context, property);
|
|
132
|
+
if (isDiscriminator(context, model, property.name)) {
|
|
133
|
+
newProperty = { ...newProperty, ...handleDiscriminator(context, model) };
|
|
134
|
+
}
|
|
135
|
+
newValue.properties.push(newProperty);
|
|
129
136
|
}
|
|
130
137
|
// need to do discriminator outside `emitModel` to avoid infinite recursion
|
|
131
|
-
handleDiscriminator(context, model, newValue);
|
|
138
|
+
// handleDiscriminator(context, model, newValue);
|
|
132
139
|
}
|
|
133
140
|
function getType(context, type, options = {}) {
|
|
134
141
|
// don't cache simple type(string, int, etc) since decorators may change the result
|
|
135
|
-
const enableCache = !isSimpleType(context.program, type)
|
|
142
|
+
const enableCache = !isSimpleType(context.program, type);
|
|
136
143
|
const effectiveModel = !options.disableEffectiveModel &&
|
|
137
144
|
(type.kind === "Model" || type.kind === "Union")
|
|
138
145
|
? getEffectiveSchemaType(context.program, type)
|
|
@@ -151,6 +158,9 @@ function getType(context, type, options = {}) {
|
|
|
151
158
|
else {
|
|
152
159
|
newValue = emitType(context, type);
|
|
153
160
|
}
|
|
161
|
+
if (type.kind === "ModelProperty" || type.kind === "Scalar") {
|
|
162
|
+
newValue = applyEncoding(context.program, type, newValue);
|
|
163
|
+
}
|
|
154
164
|
if (enableCache) {
|
|
155
165
|
typesMap.set(effectiveModel, newValue);
|
|
156
166
|
if (type.kind === "Union") {
|
|
@@ -187,11 +197,14 @@ function emitParamBase(program, parameter) {
|
|
|
187
197
|
let name;
|
|
188
198
|
let description = "";
|
|
189
199
|
let addedOn;
|
|
200
|
+
let format;
|
|
190
201
|
if (parameter.kind === "ModelProperty") {
|
|
202
|
+
const newParameter = applyEncoding(program, parameter, parameter);
|
|
191
203
|
optional = parameter.optional;
|
|
192
204
|
name = parameter.name;
|
|
193
205
|
description = getDocStr(program, parameter);
|
|
194
206
|
addedOn = getAddedOnVersion(program, parameter);
|
|
207
|
+
format = newParameter.format;
|
|
195
208
|
}
|
|
196
209
|
else {
|
|
197
210
|
optional = false;
|
|
@@ -202,7 +215,8 @@ function emitParamBase(program, parameter) {
|
|
|
202
215
|
description,
|
|
203
216
|
addedOn,
|
|
204
217
|
clientName: applyCasing(name, { casing: CASING }),
|
|
205
|
-
inOverload: false
|
|
218
|
+
inOverload: false,
|
|
219
|
+
format
|
|
206
220
|
};
|
|
207
221
|
}
|
|
208
222
|
function getBodyType(program, route) {
|
|
@@ -266,6 +280,7 @@ function emitBodyParameter(context, httpOperation) {
|
|
|
266
280
|
};
|
|
267
281
|
}
|
|
268
282
|
function emitParameter(context, parameter, implementation) {
|
|
283
|
+
var _a;
|
|
269
284
|
const base = emitParamBase(context.program, parameter.param);
|
|
270
285
|
let type = getType(context, parameter.param.type);
|
|
271
286
|
let clientDefaultValue = undefined;
|
|
@@ -279,9 +294,10 @@ function emitParameter(context, parameter, implementation) {
|
|
|
279
294
|
const paramMap = {
|
|
280
295
|
restApiName: parameter.name,
|
|
281
296
|
location: parameter.type,
|
|
282
|
-
type: type,
|
|
297
|
+
type: base.format ? { ...type, format: base.format } : type,
|
|
283
298
|
implementation: implementation,
|
|
284
|
-
skipUrlEncoding: parameter.type === "endpointPath"
|
|
299
|
+
skipUrlEncoding: parameter.type === "endpointPath",
|
|
300
|
+
format: (_a = parameter.format) !== null && _a !== void 0 ? _a : base.format
|
|
285
301
|
};
|
|
286
302
|
if (paramMap.type.type === "constant") {
|
|
287
303
|
clientDefaultValue = paramMap.type.value;
|
|
@@ -352,19 +368,6 @@ function emitResponseHeaders(context, headers) {
|
|
|
352
368
|
}
|
|
353
369
|
return retval;
|
|
354
370
|
}
|
|
355
|
-
function isAzureCoreErrorType(t) {
|
|
356
|
-
var _a;
|
|
357
|
-
if ((t === null || t === void 0 ? void 0 : t.kind) !== "Model" ||
|
|
358
|
-
!["Error", "ErrorResponse", "InnerError"].includes(t.name))
|
|
359
|
-
return false;
|
|
360
|
-
const namespaces = ".Azure.Core.Foundations".split(".");
|
|
361
|
-
while (namespaces.length > 0 &&
|
|
362
|
-
((t === null || t === void 0 ? void 0 : t.kind) === "Model" || (t === null || t === void 0 ? void 0 : t.kind) === "Namespace") &&
|
|
363
|
-
((_a = t.namespace) === null || _a === void 0 ? void 0 : _a.name) === namespaces.pop()) {
|
|
364
|
-
t = t.namespace;
|
|
365
|
-
}
|
|
366
|
-
return namespaces.length == 0;
|
|
367
|
-
}
|
|
368
371
|
function emitResponse(context, response, innerResponse) {
|
|
369
372
|
var _a, _b;
|
|
370
373
|
let type = undefined;
|
|
@@ -468,6 +471,9 @@ function emitBasicOperation(context, operation, operationGroupName, rlcModels) {
|
|
|
468
471
|
op.path === sourceRoutePath);
|
|
469
472
|
});
|
|
470
473
|
for (const param of httpOperation.parameters.parameters) {
|
|
474
|
+
if (isIgnoredHeaderParam(param)) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
471
477
|
const emittedParam = emitParameter(context, param, "Method");
|
|
472
478
|
if (isApiVersion(context, param) && apiVersionParam === undefined) {
|
|
473
479
|
apiVersionParam = emittedParam;
|
|
@@ -482,7 +488,7 @@ function emitBasicOperation(context, operation, operationGroupName, rlcModels) {
|
|
|
482
488
|
for (const innerResponse of response.responses) {
|
|
483
489
|
const emittedResponse = emitResponse(context, response, innerResponse);
|
|
484
490
|
if (isErrorModel(context.program, response.type)) {
|
|
485
|
-
// * is valid status code in
|
|
491
|
+
// * is valid status code in typespec but invalid for autorest.python
|
|
486
492
|
if (response.statusCode === "*") {
|
|
487
493
|
exceptions.push(emittedResponse);
|
|
488
494
|
}
|
|
@@ -532,7 +538,7 @@ function emitBasicOperation(context, operation, operationGroupName, rlcModels) {
|
|
|
532
538
|
};
|
|
533
539
|
}
|
|
534
540
|
function isReadOnly(program, type) {
|
|
535
|
-
// https://microsoft.github.io/
|
|
541
|
+
// https://microsoft.github.io/typespec/standard-library/http/operations#automatic-visibility
|
|
536
542
|
// Only "read" should be readOnly
|
|
537
543
|
const visibility = getVisibility(program, type);
|
|
538
544
|
if (visibility) {
|
|
@@ -543,7 +549,8 @@ function isReadOnly(program, type) {
|
|
|
543
549
|
}
|
|
544
550
|
}
|
|
545
551
|
function emitProperty(context, property) {
|
|
546
|
-
var _a, _b;
|
|
552
|
+
var _a, _b, _c;
|
|
553
|
+
const newProperty = applyEncoding(context.program, property, property);
|
|
547
554
|
let clientDefaultValue = undefined;
|
|
548
555
|
const propertyDefaultKind = (_a = property.default) === null || _a === void 0 ? void 0 : _a.kind;
|
|
549
556
|
if (property.default &&
|
|
@@ -552,21 +559,26 @@ function emitProperty(context, property) {
|
|
|
552
559
|
propertyDefaultKind === "Boolean")) {
|
|
553
560
|
clientDefaultValue = property.default.value;
|
|
554
561
|
}
|
|
562
|
+
if (propertyDefaultKind === "EnumMember") {
|
|
563
|
+
clientDefaultValue = (_b = property.default.value) !== null && _b !== void 0 ? _b : property.default.name;
|
|
564
|
+
}
|
|
555
565
|
// const [clientName, jsonName] = getPropertyNames(context, property);
|
|
556
566
|
const clientName = property.name;
|
|
557
|
-
const jsonName = (
|
|
567
|
+
const jsonName = (_c = getProjectedName(context.program, property, "json")) !== null && _c !== void 0 ? _c : property.name;
|
|
558
568
|
if (property.model) {
|
|
559
569
|
getType(context, property.model);
|
|
560
570
|
}
|
|
571
|
+
const type = getType(context, property.type);
|
|
561
572
|
return {
|
|
562
573
|
clientName: applyCasing(clientName, { casing: CASING }),
|
|
563
574
|
restApiName: jsonName,
|
|
564
|
-
type:
|
|
575
|
+
type: newProperty.format ? { ...type, format: newProperty.format } : type,
|
|
565
576
|
optional: property.optional,
|
|
566
577
|
description: getDocStr(context.program, property),
|
|
567
578
|
addedOn: getAddedOnVersion(context.program, property),
|
|
568
579
|
readonly: isReadOnly(context.program, property) || isKey(context.program, property),
|
|
569
|
-
clientDefaultValue: clientDefaultValue
|
|
580
|
+
clientDefaultValue: clientDefaultValue,
|
|
581
|
+
format: newProperty.format
|
|
570
582
|
};
|
|
571
583
|
}
|
|
572
584
|
function getName(program, type) {
|
|
@@ -589,6 +601,7 @@ function getName(program, type) {
|
|
|
589
601
|
}
|
|
590
602
|
}
|
|
591
603
|
function emitModel(context, type) {
|
|
604
|
+
var _a, _b;
|
|
592
605
|
// Now we know it's a defined model
|
|
593
606
|
const properties = [];
|
|
594
607
|
let baseModel = undefined;
|
|
@@ -596,9 +609,27 @@ function emitModel(context, type) {
|
|
|
596
609
|
baseModel = getType(context, type.baseModel);
|
|
597
610
|
}
|
|
598
611
|
const effectiveName = getEffectiveSchemaType(context.program, type).name;
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
612
|
+
const overridedModelName = (_b = (_a = getProjectedName(context.program, type, "javascript")) !== null && _a !== void 0 ? _a : getProjectedName(context.program, type, "client")) !== null && _b !== void 0 ? _b : getFriendlyName(context.program, type);
|
|
613
|
+
let modelName = overridedModelName !== null && overridedModelName !== void 0 ? overridedModelName : (effectiveName ? effectiveName : getName(context.program, type));
|
|
614
|
+
if (!overridedModelName &&
|
|
615
|
+
type.templateMapper &&
|
|
616
|
+
type.templateMapper.args &&
|
|
617
|
+
type.templateMapper.args.length > 0 &&
|
|
618
|
+
getPagedResult(context.program, type)) {
|
|
619
|
+
modelName =
|
|
620
|
+
type.templateMapper.args
|
|
621
|
+
.map((it) => {
|
|
622
|
+
switch (it.kind) {
|
|
623
|
+
case "Model":
|
|
624
|
+
return it.name;
|
|
625
|
+
case "String":
|
|
626
|
+
return it.value;
|
|
627
|
+
default:
|
|
628
|
+
return "";
|
|
629
|
+
}
|
|
630
|
+
})
|
|
631
|
+
.join("") + "List";
|
|
632
|
+
}
|
|
602
633
|
return {
|
|
603
634
|
type: "model",
|
|
604
635
|
name: modelName,
|
|
@@ -690,9 +721,11 @@ function emitCredentialUnion(cred_types) {
|
|
|
690
721
|
return result;
|
|
691
722
|
}
|
|
692
723
|
function emitStdScalar(program, scalar) {
|
|
724
|
+
var _a, _b;
|
|
725
|
+
const newScalar = applyEncoding(program, scalar, scalar);
|
|
693
726
|
switch (scalar.name) {
|
|
694
727
|
case "bytes":
|
|
695
|
-
return { type: "byte-array", format:
|
|
728
|
+
return { type: "byte-array", format: newScalar.format };
|
|
696
729
|
case "int8":
|
|
697
730
|
case "int16":
|
|
698
731
|
case "int32":
|
|
@@ -714,31 +747,55 @@ function emitStdScalar(program, scalar) {
|
|
|
714
747
|
case "boolean":
|
|
715
748
|
return { type: "boolean" };
|
|
716
749
|
case "plainDate":
|
|
717
|
-
return { type: "date" };
|
|
750
|
+
return { type: "datetime", format: (_a = newScalar.format) !== null && _a !== void 0 ? _a : "date" };
|
|
718
751
|
case "utcDateTime":
|
|
719
|
-
return { type: "datetime", format:
|
|
752
|
+
return { type: "datetime", format: newScalar.format };
|
|
720
753
|
case "plainTime":
|
|
721
|
-
return { type: "time" };
|
|
754
|
+
return { type: "datetime", format: (_b = newScalar.format) !== null && _b !== void 0 ? _b : "time" };
|
|
755
|
+
case "offsetDateTime":
|
|
756
|
+
return { type: "string" };
|
|
722
757
|
case "duration":
|
|
723
|
-
return { type: "duration" };
|
|
758
|
+
return { type: "duration", format: newScalar.format };
|
|
724
759
|
case "numeric":
|
|
725
760
|
return {}; // Waiting on design for more precise type https://github.com/microsoft/cadl/issues/1260
|
|
726
761
|
default:
|
|
727
762
|
return {};
|
|
728
763
|
}
|
|
729
764
|
}
|
|
765
|
+
function applyEncoding(program, typespecType, target = {}) {
|
|
766
|
+
const encodeData = getEncode(program, typespecType);
|
|
767
|
+
const formatData = getFormat(program, typespecType);
|
|
768
|
+
formatData;
|
|
769
|
+
if (encodeData) {
|
|
770
|
+
const newTarget = { ...target };
|
|
771
|
+
const newType = emitScalar(program, encodeData.type);
|
|
772
|
+
// newTarget["type"] = newType["type"];
|
|
773
|
+
// If the target already has a format it takes priority. (e.g. int32)
|
|
774
|
+
newTarget["format"] = mergeFormatAndEncoding(newTarget.format, encodeData.encoding, newType["format"]);
|
|
775
|
+
return newTarget;
|
|
776
|
+
}
|
|
777
|
+
return target;
|
|
778
|
+
}
|
|
779
|
+
function mergeFormatAndEncoding(format, encoding, encodeAsFormat) {
|
|
780
|
+
switch (format) {
|
|
781
|
+
case undefined:
|
|
782
|
+
return encodeAsFormat !== null && encodeAsFormat !== void 0 ? encodeAsFormat : encoding;
|
|
783
|
+
case "date-time":
|
|
784
|
+
return encoding;
|
|
785
|
+
case "duration":
|
|
786
|
+
default:
|
|
787
|
+
return encodeAsFormat !== null && encodeAsFormat !== void 0 ? encodeAsFormat : encoding;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
730
790
|
function applyIntrinsicDecorators(program, type, result) {
|
|
731
|
-
|
|
791
|
+
let newResult = { ...result };
|
|
732
792
|
const docStr = getDoc(program, type);
|
|
733
793
|
const isString = isStringType(program, getPropertyType(type));
|
|
734
794
|
const isNumeric = isNumericType(program, getPropertyType(type));
|
|
735
795
|
if (!result.description && docStr) {
|
|
736
796
|
newResult.description = docStr;
|
|
737
797
|
}
|
|
738
|
-
|
|
739
|
-
if (isString && !result.format && formatStr) {
|
|
740
|
-
newResult.format = formatStr;
|
|
741
|
-
}
|
|
798
|
+
newResult = applyEncoding(program, type, newResult);
|
|
742
799
|
const pattern = getPattern(program, type);
|
|
743
800
|
if (isString && !result.pattern && pattern) {
|
|
744
801
|
newResult.pattern = pattern;
|
|
@@ -771,7 +828,8 @@ function applyIntrinsicDecorators(program, type, result) {
|
|
|
771
828
|
}
|
|
772
829
|
function emitScalar(program, scalar) {
|
|
773
830
|
let result = {};
|
|
774
|
-
|
|
831
|
+
const isStd = program.checker.isStdType(scalar);
|
|
832
|
+
if (isStd) {
|
|
775
833
|
result = emitStdScalar(program, scalar);
|
|
776
834
|
}
|
|
777
835
|
else if (scalar.baseScalar) {
|
|
@@ -799,7 +857,7 @@ function emitListOrDict(context, type) {
|
|
|
799
857
|
}
|
|
800
858
|
return undefined;
|
|
801
859
|
}
|
|
802
|
-
function
|
|
860
|
+
function mapTypeSpecType(context, type) {
|
|
803
861
|
switch (type.kind) {
|
|
804
862
|
case "Number":
|
|
805
863
|
return constantType(type.value, intOrFloat(type.value));
|
|
@@ -893,7 +951,7 @@ function emitType(context, type) {
|
|
|
893
951
|
if (type.kind === "CredentialTypeUnion") {
|
|
894
952
|
return emitCredentialUnion(type);
|
|
895
953
|
}
|
|
896
|
-
const builtinType =
|
|
954
|
+
const builtinType = mapTypeSpecType(context, type);
|
|
897
955
|
if (builtinType !== undefined) {
|
|
898
956
|
// add in description elements for types derived from primitive types (SecureString, etc.)
|
|
899
957
|
const doc = getDoc(context.program, type);
|
|
@@ -944,8 +1002,32 @@ function emitOperationGroups(context, client, rlcModels) {
|
|
|
944
1002
|
operations: clientOperations
|
|
945
1003
|
});
|
|
946
1004
|
}
|
|
1005
|
+
resolveConflictIfExist(operationGroups);
|
|
947
1006
|
return operationGroups;
|
|
948
1007
|
}
|
|
1008
|
+
function resolveConflictIfExist(operationGroups) {
|
|
1009
|
+
if (operationGroups.length < 2) {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
const nameSet = new Set();
|
|
1013
|
+
const hasConflict = operationGroups.some((g) => g.operations.some((op) => {
|
|
1014
|
+
if (nameSet.has(op.name)) {
|
|
1015
|
+
return true;
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
nameSet.add(op.name);
|
|
1019
|
+
return false;
|
|
1020
|
+
}
|
|
1021
|
+
}));
|
|
1022
|
+
if (!hasConflict) {
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
// Append operation group prefix
|
|
1026
|
+
operationGroups.forEach((g) => g.operations.forEach((op) => {
|
|
1027
|
+
op.oriName = op.name;
|
|
1028
|
+
op.name = `${g.propertyName}_${op.name}`;
|
|
1029
|
+
}));
|
|
1030
|
+
}
|
|
949
1031
|
function getServerHelper(program, namespace) {
|
|
950
1032
|
const servers = getServers(program, namespace);
|
|
951
1033
|
if (servers === undefined) {
|
|
@@ -1097,7 +1179,8 @@ function emitClients(context, namespace, rlcModelsMap) {
|
|
|
1097
1179
|
operationGroups: emitOperationGroups(context, client, rlcModels),
|
|
1098
1180
|
url: server ? server.url : "",
|
|
1099
1181
|
apiVersions: [],
|
|
1100
|
-
rlcClientName: rlcModels ? getClientName(rlcModels) : client.name
|
|
1182
|
+
rlcClientName: rlcModels ? getClientName(rlcModels) : client.name,
|
|
1183
|
+
subfolder: ""
|
|
1101
1184
|
};
|
|
1102
1185
|
const emittedApiVersionParam = getApiVersionParameter(context);
|
|
1103
1186
|
if (emittedApiVersionParam) {
|
|
@@ -1125,19 +1208,22 @@ function getNamespaces(context) {
|
|
|
1125
1208
|
}
|
|
1126
1209
|
return namespaces;
|
|
1127
1210
|
}
|
|
1128
|
-
export function emitCodeModel(
|
|
1129
|
-
var _a, _b;
|
|
1211
|
+
export function emitCodeModel(dpgContext, rlcModelsMap, modularSourcesRoot, project, options = { casing: "snake" }) {
|
|
1212
|
+
var _a, _b, _c;
|
|
1130
1213
|
CASING = (_a = options.casing) !== null && _a !== void 0 ? _a : CASING;
|
|
1131
|
-
const dpgContext = createSdkContext(context);
|
|
1132
1214
|
const clientNamespaceString = (_b = getClientNamespaceString(dpgContext)) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
|
1133
1215
|
// Get types
|
|
1134
1216
|
const codeModel = {
|
|
1135
|
-
options:
|
|
1217
|
+
options: (_c = dpgContext.rlcOptions) !== null && _c !== void 0 ? _c : {},
|
|
1218
|
+
modularOptions: { sourceRoot: modularSourcesRoot },
|
|
1136
1219
|
namespace: clientNamespaceString,
|
|
1137
1220
|
subnamespaceToClients: {},
|
|
1138
1221
|
clients: [],
|
|
1139
|
-
types: []
|
|
1222
|
+
types: [],
|
|
1223
|
+
project
|
|
1140
1224
|
};
|
|
1225
|
+
typesMap.clear();
|
|
1226
|
+
simpleTypesMap.clear();
|
|
1141
1227
|
const allModels = getAllModels(dpgContext);
|
|
1142
1228
|
for (const model of allModels) {
|
|
1143
1229
|
getType(dpgContext, model);
|
|
@@ -1145,9 +1231,17 @@ export function emitCodeModel(context, rlcModelsMap, options = { casing: "snake"
|
|
|
1145
1231
|
for (const namespace of getNamespaces(dpgContext)) {
|
|
1146
1232
|
if (namespace === clientNamespaceString) {
|
|
1147
1233
|
codeModel.clients = emitClients(dpgContext, namespace, rlcModelsMap);
|
|
1234
|
+
codeModel.clients.length > 1 &&
|
|
1235
|
+
codeModel.clients.map((client) => {
|
|
1236
|
+
client["subfolder"] = normalizeName(client.name.replace("Client", ""), NameType.File);
|
|
1237
|
+
});
|
|
1148
1238
|
}
|
|
1149
1239
|
else {
|
|
1150
1240
|
codeModel["subnamespaceToClients"][namespace] = emitClients(dpgContext, namespace, rlcModelsMap);
|
|
1241
|
+
codeModel["subnamespaceToClients"][namespace].length > 1 &&
|
|
1242
|
+
codeModel["subnamespaceToClients"][namespace].map((client) => {
|
|
1243
|
+
client["subfolder"] = normalizeName(client.name, NameType.File);
|
|
1244
|
+
});
|
|
1151
1245
|
}
|
|
1152
1246
|
}
|
|
1153
1247
|
codeModel["types"] = [
|