зеркало из
1
0
Форкнуть 0

Add pre-namer option to rename duplicate schema names

This commit is contained in:
David Wilson 2020-04-21 16:23:32 -07:00
Родитель 969a27f576
Коммит 4e463006b6
1 изменённых файлов: 54 добавлений и 5 удалений

Просмотреть файл

@ -32,6 +32,30 @@ function setNameAllowEmpty(thing: { language: Languages }, styler: Styler, defau
thing.language.default.name = styler(defaultValue && isUnassigned(thing.language.default.name) ? defaultValue : thing.language.default.name, true, overrides);
}
function deduplicateSchemaName(schema: Schema, schemaNames: Set<string>, session: Session<CodeModel>): void {
const maxDedupes = 1000;
if (schemaNames.has(schema.language.default.name)) {
const oldName = schema.language.default.name;
for (let i = 1; i <= maxDedupes; i++) {
const newName = `${oldName}AutoGenerated${(i === 1 ? "" : i)}`;
if (!schemaNames.has(newName)) {
schema.language.default.name = newName;
schemaNames.add(newName);
session.warning(`Deduplicating schema name: '${oldName}' -> '${newName}'`, ["PreNamer/DeduplicateName"]);
return;
}
}
session.error(`Attempted to deduplicate schema name '${schema.language.default.name}' more than ${maxDedupes} times and failed.`, ["PreNamer/DeduplicateName"])
}
// We haven't seen the name before, add it
schemaNames.add(schema.language.default.name);
}
function schemaNameComparer(s1: Schema, s2: Schema): number {
return s1.language.default.name.localeCompare(s2.language.default.name);
}
export class PreNamer {
codeModel: CodeModel
options: Dictionary<any> = {};
@ -90,17 +114,31 @@ export class PreNamer {
return this.codeModel;
}
const deduplicateSchemaNames = !!this.options['deduplicate-schema-names'];
// choice
for (const schema of values(this.codeModel.schemas.choices)) {
const choiceSchemaNames = new Set<string>();
for (const schema of values(this.codeModel.schemas.choices).toArray().sort(schemaNameComparer)) {
setName(schema, this.format.choice, `Enum${this.enum++}`, this.format.override);
if (deduplicateSchemaNames) {
deduplicateSchemaName(schema, choiceSchemaNames, this.session);
}
for (const choice of values(schema.choices)) {
setName(choice, this.format.choiceValue, '', this.format.override);
}
}
// sealed choice
for (const schema of values(this.codeModel.schemas.sealedChoices)) {
const sealedChoiceSchemaNames = new Set<string>();
for (const schema of values(this.codeModel.schemas.sealedChoices).toArray().sort(schemaNameComparer)) {
setName(schema, this.format.choice, `Enum${this.enum++}`, this.format.override);
if (deduplicateSchemaNames) {
deduplicateSchemaName(schema, sealedChoiceSchemaNames, this.session);
}
for (const choice of values(schema.choices)) {
setName(choice, this.format.choiceValue, '', this.format.override);
}
@ -177,16 +215,27 @@ export class PreNamer {
}
}
for (const schema of values(this.codeModel.schemas.objects)) {
const objectSchemaNames = new Set<string>();
for (const schema of values(this.codeModel.schemas.objects).toArray().sort(schemaNameComparer)) {
setName(schema, this.format.type, '', this.format.override);
if (deduplicateSchemaNames) {
deduplicateSchemaName(schema, objectSchemaNames, this.session);
}
for (const property of values(schema.properties)) {
setName(property, this.format.property, '', this.format.override);
}
}
for (const schema of values(this.codeModel.schemas.groups)) {
const groupSchemaNames = new Set<string>();
for (const schema of values(this.codeModel.schemas.groups).toArray().sort(schemaNameComparer)) {
setName(schema, this.format.type, '', this.format.override);
if (deduplicateSchemaNames) {
deduplicateSchemaName(schema, groupSchemaNames, this.session);
}
for (const property of values(schema.properties)) {
setName(property, this.format.property, '', this.format.override);
}