Check and remove invalid label type when necessary. (#921)

* Check and remove invalid label type when necessary.

* Fix lint warning.
This commit is contained in:
Buddha Wang 2021-05-03 15:24:04 +08:00 коммит произвёл GitHub
Родитель 1f7a305d95
Коммит 77643b5342
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 35 добавлений и 7 удалений

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

@ -748,20 +748,44 @@ export class AssetService {
let shouldAssetsUpdate = false;
let updatedProject;
const { assets } = project;
const shouldSchemaUpdate = schema => constants.supportedLabelsSchemas.has(schema) && schema !== constants.labelsSchema;
if (_.isPlainObject(assets)) {
const assetService = new AssetService(project);
const assetMetadatas: IAssetMetadata[] = await Promise.all(Object.values(assets).map(async (asset) => await assetService.getAssetMetadata(asset)));
await Promise.all(assetMetadatas.map(async (assetMetadata) => {
if (_.isPlainObject(assetMetadata.labelData) && AssetService.shouldSchemaUpdate(assetMetadata.labelData?.$schema)) {
if (_.isPlainObject(assetMetadata.labelData)) {
let shouldSaveMetadata = false;
// Check and update $schema property.
if (AssetService.shouldSchemaUpdate(assetMetadata.labelData?.$schema)) {
shouldAssetsUpdate = true;
assetMetadata.labelData = { ...assetMetadata.labelData, "$schema": constants.labelsSchema };
shouldSaveMetadata = true;
}
// Check and remove labelType property.
let shouldUpdateLabels = false;
const labels = assetMetadata.labelData?.labels || [];
labels.forEach(label => {
if (AssetService.shouldRemoveLabelType(label)) {
delete label.labelType;
shouldUpdateLabels = true;
}
});
if (shouldUpdateLabels) {
assetMetadata.labelData = { ...assetMetadata.labelData, labels };
shouldSaveMetadata = true;
}
// Save back to storage.
if (shouldSaveMetadata) {
await assetService.save(assetMetadata);
}
}))
}
}));
const updatedAssets = { ...assets };
for (const [assetID, asset] of Object.entries(assets)) {
if (shouldSchemaUpdate(asset.schema)) {
if (AssetService.shouldSchemaUpdate(asset.schema)) {
updatedAssets[assetID] = { ...assets[assetID], schema: constants.labelsSchema };
}
}
@ -773,4 +797,8 @@ export class AssetService {
public static shouldSchemaUpdate = (schema: string): boolean => {
return constants.supportedLabelsSchemas.has(schema) && schema !== constants.labelsSchema;
}
public static shouldRemoveLabelType = (label: ILabel): boolean => {
return label.hasOwnProperty("labelType") && label.labelType === null;
}
}