@autorest/openapi-to-cadl
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
101 lines • 3.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateDecorators = exports.getEnumDecorators = exports.getPropertyDecorators = exports.getModelDecorators = void 0;
const discriminator_1 = require("./discriminator");
const schemas_1 = require("./schemas");
function getModelDecorators(model) {
var _a;
const decorators = [];
const paging = (_a = model.language.default.paging) !== null && _a !== void 0 ? _a : {};
if (paging.isPageable) {
decorators.push({
name: "pagedResult",
module: "@azure-tools/typespec-azure-core",
namespace: "Azure.Core",
});
}
const ownDiscriminator = (0, discriminator_1.getOwnDiscriminator)(model);
if (ownDiscriminator) {
decorators.push({
name: "discriminator",
arguments: [ownDiscriminator.serializedName],
});
}
if (model.language.default.isError) {
decorators.push({ name: "error" });
}
let resource = model.language.default.resource;
if (resource) {
if (resource.startsWith("/")) {
// Remove the leading
resource = resource.slice(1);
}
decorators.push({
name: "resource",
module: "@azure-tools/typespec-azure-core",
namespace: "Azure.Core",
arguments: [resource],
});
}
return decorators;
}
exports.getModelDecorators = getModelDecorators;
function getPropertyDecorators(property) {
var _a;
const decorators = [];
const paging = (_a = property.language.default.paging) !== null && _a !== void 0 ? _a : {};
if (property.readOnly) {
decorators.push({ name: "visibility", arguments: ["read"] });
}
if (paging.isNextLink) {
decorators.push({ name: "nextLink" });
}
if (paging.isValue) {
decorators.push({ name: "items" });
}
if (property.language.default.isResourceKey) {
decorators.push({
name: "key",
fixMe: [
"// FIXME: (resource-key-guessing) - Verify that this property is the resource key, if not please update the model with the right one",
],
});
}
if (property.serializedName !== property.language.default.name) {
decorators.push({
name: "projectedName",
arguments: ["json", property.serializedName],
});
}
return decorators;
}
exports.getPropertyDecorators = getPropertyDecorators;
function getEnumDecorators(enumeration) {
const decorators = [];
if ((0, schemas_1.isSealedChoiceSchema)(enumeration)) {
decorators.push({
name: "Azure.Core.fixed",
module: "@azure-tools/typespec-azure-core",
});
}
return decorators;
}
exports.getEnumDecorators = getEnumDecorators;
function generateDecorators(decorators = []) {
var _a;
const definitions = [];
for (const decorator of decorators !== null && decorators !== void 0 ? decorators : []) {
if (decorator.fixMe) {
definitions.push(decorator.fixMe.join(`\n`));
}
if (decorator.arguments) {
definitions.push(`@${decorator.name}(${(_a = decorator.arguments) === null || _a === void 0 ? void 0 : _a.map((a) => `"${a}"`).join(", ")})`);
}
else {
definitions.push(`@${decorator.name}`);
}
}
return definitions.join("\n");
}
exports.generateDecorators = generateDecorators;
//# sourceMappingURL=decorators.js.map