@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
84 lines • 7.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schema_1 = require("../common/code-model/schema");
const dictionary_1 = require("../common/dictionary");
const model_state_1 = require("../common/model-state");
const process_code_model_1 = require("../common/process-code-model");
const text_manipulation_1 = require("../common/text-manipulation");
const dotnet = require("../csharp/code-dom/mscorlib");
const schema_resolver_1 = require("../csharp/schema/schema-resolver");
async function process(service) {
return process_code_model_1.processCodeModel(nameStuffRight, service);
}
exports.process = process;
async function nameStuffRight(codeModel, service) {
const resolver = new schema_resolver_1.SchemaDefinitionResolver();
// set the namespace for the service
const serviceNamespace = await service.GetValue('namespace') || 'Sample.API';
const clientName = text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(codeModel.details.default.name)));
// set c# client details (name)
codeModel.details.csharp = Object.assign({}, codeModel.details.default, { name: clientName, namespace: serviceNamespace, fullname: `${serviceNamespace}.${clientName}` });
for (const { key: schemaName, value: schema } of dictionary_1.items(codeModel.schemas)) {
const details = schema.details.default;
// object types.
if (schema.type === schema_1.JsonType.Object) {
schema.details.csharp = Object.assign({}, details, { interfaceName: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(['I', ...text_manipulation_1.deconstruct(details.name)])), name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.name))), namespace: text_manipulation_1.pascalCase([serviceNamespace, '.', `Models`]), fullname: `${text_manipulation_1.pascalCase([serviceNamespace, '.', `Models`])}.${text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.name)))}` });
}
else if (schema.type === schema_1.JsonType.String && schema.details.default.enum) {
// oh, it's an enum type
schema.details.csharp = Object.assign({}, details, { interfaceName: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(['I', ...text_manipulation_1.deconstruct(details.name)])), name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.enum.name))), namespace: text_manipulation_1.pascalCase([serviceNamespace, '.', `Support`]), fullname: `${text_manipulation_1.pascalCase([serviceNamespace, '.', `Support`])}.${text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.enum.name)))}`, enum: Object.assign({}, schema.details.default.enum, { name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.enum.name))), values: schema.details.default.enum.values.map(each => {
return Object.assign({}, each, { name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(each.name))) });
}) }) });
}
else {
schema.details.csharp = Object.assign({}, details, { interfaceName: '<INVALID>', name: schemaName, namespace: '<INVALID>', fullname: '<INVALID>' });
}
for (const { key: propertyName, value: propertySchema } of dictionary_1.items(schema.properties)) {
const propertyDetails = propertySchema.details.default;
const className = schema.details.csharp.name;
let pname = text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(propertyDetails.name)));
if (pname === className) {
pname = pname + "Property";
}
if (pname === 'default') {
pname = '@default';
}
propertySchema.details.csharp = Object.assign({}, propertyDetails, { name: pname // and so are the propertyNmaes
});
}
// fix enum names
if (schema.details.default.enum) {
schema.details.csharp.enum = Object.assign({}, schema.details.default.enum, { name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(schema.details.default.enum.name))) });
// and the value names themselves
for (const value of dictionary_1.values(schema.details.csharp.enum.values)) {
value.name = text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(value.name)));
}
}
}
for (const operation of dictionary_1.values(codeModel.http.operations)) {
const details = operation.details.default;
operation.details.csharp = Object.assign({}, details, { name: text_manipulation_1.pascalCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(details.name))) });
// parameters are camelCased.
for (const parameter of dictionary_1.values(operation.parameters)) {
const parameterDetails = parameter.details.default;
let propName = text_manipulation_1.camelCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(parameterDetails.name)));
if (propName === 'default') {
propName = '@default';
}
parameter.details.csharp = Object.assign({}, parameterDetails, { name: propName });
}
for (const { key: responseCode, value: responses } of dictionary_1.items(operation.responses_new)) {
// per responseCode
for (const response of dictionary_1.values(responses)) {
const scTd = response.schema ? resolver.resolveTypeDeclaration(response.schema, true, new model_state_1.ModelState(service, codeModel, `?`, ['schemas', response.schema.details.default.name])) : undefined;
const genericParameters = scTd ? `<${scTd.declaration}>` : ``;
const code = (dotnet.System.Net.HttpStatusCode[response.responseCode].value || '').replace('System.Net.HttpStatusCode', '') || response.responseCode;
response.details.csharp = Object.assign({}, response.details.default, { type: `Response${genericParameters}`, name: (dictionary_1.length(responses) <= 1) ?
text_manipulation_1.camelCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(`on ${code}`))) : // the common type (or the only one.)
text_manipulation_1.camelCase(text_manipulation_1.fixLeadingNumber(text_manipulation_1.deconstruct(`on ${code} ${response.mimeTypes[0]}`))) });
}
}
}
return codeModel;
}
//# sourceMappingURL=namer.js.map