IN PROGRESS: Deduplicate schema names
This commit is contained in:
Родитель
4e21a0912e
Коммит
c0e7bbc1be
|
@ -32,6 +32,22 @@ 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>): string {
|
||||
if (schemaNames.has(schema.language.default.name)) {
|
||||
const oldName = schema.language.default.name;
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const newName = `${oldName}_AUTOGENERATED${(i === 0 ? "" : i)}`;
|
||||
if (!schemaNames.has(newName)) {
|
||||
schemaNames.add(newName);
|
||||
session.warning(`Deduplicating schema name: '${oldName}' -> '${newName}'`, ["Prenamer/DeduplicateName"]);
|
||||
return newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return schema.language.default.name;
|
||||
}
|
||||
|
||||
export class PreNamer {
|
||||
codeModel: CodeModel
|
||||
options: Dictionary<any> = {};
|
||||
|
@ -90,6 +106,8 @@ export class PreNamer {
|
|||
return this.codeModel;
|
||||
}
|
||||
|
||||
const deduplicateSchemaNames = this.options['deduplicate-schema-names'] === true;
|
||||
|
||||
// choice
|
||||
for (const schema of values(this.codeModel.schemas.choices)) {
|
||||
setName(schema, this.format.choice, `Enum${this.enum++}`, this.format.override);
|
||||
|
@ -177,9 +195,23 @@ export class PreNamer {
|
|||
}
|
||||
}
|
||||
|
||||
const schemaNameSorter = function (s1: Schema, s2: Schema): number {
|
||||
return s1.language.default.name.localeCompare(s2.language.default.name);
|
||||
}
|
||||
|
||||
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(schemaNameSorter)) {
|
||||
setName(schema, this.format.type, '', this.format.override);
|
||||
if (objectSchemaNames.has(schema.language.default.name)) {
|
||||
// Only attempt to deduplicate schema names if requested,
|
||||
// otherwise let the checker find them
|
||||
if (deduplicateSchemaNames) {
|
||||
schema.language.default.name = deduplicateSchemaName(schema, objectSchemaNames, this.session);
|
||||
}
|
||||
}
|
||||
|
||||
objectSchemaNames.add(schema.language.default.name);
|
||||
|
||||
for (const property of values(schema.properties)) {
|
||||
setName(property, this.format.property, '', this.format.override);
|
||||
}
|
||||
|
|
|
@ -132,6 +132,11 @@ modelerfour:
|
|||
# defaults to true if not specified
|
||||
prenamer: true|false
|
||||
|
||||
# tells the pre-namer to rename any schemas with duplicate
|
||||
# names. Each will be suffixed with _AUTOGENERATED with an
|
||||
# additional numeric suffix for multiple duplicates
|
||||
deduplicate-schema-names: true|false
|
||||
|
||||
# does a check to see if names in schemas/enums/etc will collide
|
||||
# off by default
|
||||
resolve-schema-name-collisons: false|true
|
||||
|
|
Загрузка…
Ссылка в новой задаче