From 420e195f31ad32bcd3cef1312c1eb8dc6456d374 Mon Sep 17 00:00:00 2001 From: Alik Date: Thu, 11 Jul 2024 14:39:40 +0300 Subject: [PATCH] HCK-7067: fix Sonar reports (#20) --- .../helpers/getFieldChildren.js | 13 +++++- .../helpers/removeChildrenFromField.js | 2 +- .../transformJsonSchemaToDremelService.js | 5 +-- .../helpers/adaptJsonSchema/mapJsonSchema.js | 9 ++-- .../helpers/defineSchemaFieldsPath.js | 9 ++-- .../helpers/generateSchemaFieldsTree.js | 2 +- .../helpers/includeMetadataInJSONSchema.js | 7 +-- .../helpers/reverseFieldSchema.js | 24 +++++++---- .../services/JSONConvertService.js | 5 ++- .../services/fileReadWriteService.js | 34 ++++++--------- .../services/rawFileDataTransformService.js | 43 +++++++++++-------- 11 files changed, 85 insertions(+), 68 deletions(-) diff --git a/forward_engineering/helpers/getFieldChildren.js b/forward_engineering/helpers/getFieldChildren.js index 544f5fb..cde574a 100644 --- a/forward_engineering/helpers/getFieldChildren.js +++ b/forward_engineering/helpers/getFieldChildren.js @@ -1,11 +1,20 @@ const getChildren = field => { const { items, properties, patternProperties } = field; if (properties || patternProperties) { - return Object.assign({}, properties, patternProperties); + return { + ...properties, + ...patternProperties, + }; } if (items && Array.isArray(items)) { - return items.reduce((acc, item, i) => Object.assign({}, acc, { [`[${i}]`]: item }), {}); + return items.reduce( + (acc, item, i) => ({ + ...acc, + [`[${i}]`]: item, + }), + {}, + ); } return { element: items }; diff --git a/forward_engineering/helpers/removeChildrenFromField.js b/forward_engineering/helpers/removeChildrenFromField.js index d3b9e59..eae1bd1 100644 --- a/forward_engineering/helpers/removeChildrenFromField.js +++ b/forward_engineering/helpers/removeChildrenFromField.js @@ -1,5 +1,5 @@ const removeChildrenFromField = field => { - const newField = Object.assign({}, field); + const newField = { ...field }; delete newField.properties; delete newField.patternProperties; delete newField.items; diff --git a/forward_engineering/services/transformJsonSchemaToDremelService.js b/forward_engineering/services/transformJsonSchemaToDremelService.js index 9f0b3a3..edd331a 100644 --- a/forward_engineering/services/transformJsonSchemaToDremelService.js +++ b/forward_engineering/services/transformJsonSchemaToDremelService.js @@ -41,7 +41,7 @@ const transformFieldByType = type => field => { } }; -const setName = name => field => Object.assign({}, field, { name }); +const setName = name => field => ({ ...field, name }); const isDefinition = field => Boolean(field.$ref); const transformFields = @@ -51,8 +51,7 @@ const transformFields = const field = pipe([ fieldBody => (isDefinition(fieldBody) ? getFieldDefinition(fieldBody) : fieldBody), fieldBody => (fieldBody.physicalType ? removeChildrenFromField(fieldBody) : fieldBody), - fieldBody => - fieldBody.physicalType ? Object.assign({}, fieldBody, { logicalType: 'UTF8' }) : fieldBody, + fieldBody => (fieldBody.physicalType ? { ...fieldBody, logicalType: 'UTF8' } : fieldBody), ])(fieldBody); const fieldType = defineFieldType(field, initialParent); const stringifiedField = pipe([ diff --git a/reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema.js b/reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema.js index f0621a9..b334089 100644 --- a/reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema.js +++ b/reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema.js @@ -1,6 +1,6 @@ const { dependencies } = require('../../appDependencies'); -const add = (obj, properties) => Object.assign({}, obj, properties); +const add = (obj, properties) => ({ ...obj, ...properties }); const mapJsonSchema = (jsonSchema, callback) => { const _ = dependencies.lodash; @@ -25,9 +25,10 @@ const mapJsonSchema = (jsonSchema, callback) => { return jsonSchema; } - return Object.assign({}, jsonSchema, { + return { + ...jsonSchema, [propertyName]: mapper(jsonSchema[propertyName]), - }); + }; }, jsonSchema); }; if (!_.isPlainObject(jsonSchema)) { @@ -37,7 +38,7 @@ const mapJsonSchema = (jsonSchema, callback) => { const propertiesLike = ['properties', 'definitions', 'patternProperties']; const itemsLike = ['items', 'oneOf', 'allOf', 'anyOf', 'not']; - const copyJsonSchema = Object.assign({}, jsonSchema); + const copyJsonSchema = { ...jsonSchema }; const jsonSchemaWithNewProperties = applyTo( propertiesLike, copyJsonSchema, diff --git a/reverse_engineering/helpers/defineSchemaFieldsPath.js b/reverse_engineering/helpers/defineSchemaFieldsPath.js index c2e0bcc..7c0f6a7 100644 --- a/reverse_engineering/helpers/defineSchemaFieldsPath.js +++ b/reverse_engineering/helpers/defineSchemaFieldsPath.js @@ -3,10 +3,10 @@ const auxiliaryFieldProperties = ['num_children', 'parent']; const defineFieldPath = (fieldName, fieldBody, parentPath) => { const path = parentPath.concat([fieldName]); if (!fieldBody.fields) { - return Object.assign({}, fieldBody, { path }); + return { ...fieldBody, path }; } - return Object.assign({}, fieldBody, { fields: defineSchemaFieldsPath(fieldBody.fields, path) }); + return { ...fieldBody, fields: defineSchemaFieldsPath(fieldBody.fields, path) }; }; const defineSchemaFieldsPath = (schema, parentPath = []) => { @@ -15,7 +15,10 @@ const defineSchemaFieldsPath = (schema, parentPath = []) => { return acc; } - return Object.assign(acc, { [fieldName]: defineFieldPath(fieldName, fieldBody, parentPath) }); + return { + ...acc, + [fieldName]: defineFieldPath(fieldName, fieldBody, parentPath), + }; }, {}); }; diff --git a/reverse_engineering/helpers/generateSchemaFieldsTree.js b/reverse_engineering/helpers/generateSchemaFieldsTree.js index f300499..e520092 100644 --- a/reverse_engineering/helpers/generateSchemaFieldsTree.js +++ b/reverse_engineering/helpers/generateSchemaFieldsTree.js @@ -22,7 +22,7 @@ function generateSchemaFieldsTree(schemaElements) { schema = schema[schemaElement.name].fields; } else { - schema[schemaElement.name] = Object.assign({}, schemaElement); + schema[schemaElement.name] = { ...schemaElement }; } while (schema.parent && Object.keys(schema).length === schema.num_children) { diff --git a/reverse_engineering/helpers/includeMetadataInJSONSchema.js b/reverse_engineering/helpers/includeMetadataInJSONSchema.js index 1e2c8ea..9eef6db 100644 --- a/reverse_engineering/helpers/includeMetadataInJSONSchema.js +++ b/reverse_engineering/helpers/includeMetadataInJSONSchema.js @@ -5,7 +5,7 @@ const includeCreator = createdBy => JSONSchema => { return JSONSchema; } - return Object.assign({}, JSONSchema, { creator: createdBy }); + return { ...JSONSchema, creator: createdBy }; }; const includeExtra = keyValueMetadata => JSONSchema => { @@ -13,9 +13,10 @@ const includeExtra = keyValueMetadata => JSONSchema => { return JSONSchema; } - return Object.assign({}, JSONSchema, { + return { + ...JSONSchema, extra: keyValueMetadata.map(({ key, value }) => ({ extraKey: key, extraValue: value })), - }); + }; }; const includeMetadataInJSONSchema = metadata => JSONSchema => diff --git a/reverse_engineering/helpers/reverseFieldSchema.js b/reverse_engineering/helpers/reverseFieldSchema.js index 178a0eb..db16646 100644 --- a/reverse_engineering/helpers/reverseFieldSchema.js +++ b/reverse_engineering/helpers/reverseFieldSchema.js @@ -69,23 +69,29 @@ const reverseOriginalType = value => { const reverseProperty = (propertyName, value) => { switch (propertyName) { - case 'primitiveType': + case 'primitiveType': { return reverseType(value); - case 'encoding': + } + case 'encoding': { return reverseEncoding(value); - case 'repetitionType': + } + case 'repetitionType': { return { repetition: value.toLowerCase() }; - case 'compression': + } + case 'compression': { return reverseCompression(value); - case 'typeLength': + } + case 'typeLength': { return reverseTypeLength(value); - case 'originalType': + } + case 'originalType': { return reverseOriginalType(value); - case 'precision': + } + case 'precision': { return { precision: value }; - case 'scale': + } + case 'scale': { return { scale: value }; - default: { } } }; diff --git a/reverse_engineering/services/JSONConvertService.js b/reverse_engineering/services/JSONConvertService.js index 034d0a9..aa94b61 100644 --- a/reverse_engineering/services/JSONConvertService.js +++ b/reverse_engineering/services/JSONConvertService.js @@ -4,10 +4,11 @@ const defineGroupNestedFields = require('../helpers/defineGroupNestedFields'); const reverseGroupField = field => { const type = defineGroupType(field); - return Object.assign({}, reverseFieldSchema(field), { + return { + ...reverseFieldSchema(field), type, properties: convertFieldSchemasToJSON(defineGroupNestedFields(field, type)), - }); + }; }; const isGroupType = schema => schema.isNested; diff --git a/reverse_engineering/services/fileReadWriteService.js b/reverse_engineering/services/fileReadWriteService.js index cb8a164..a807caa 100644 --- a/reverse_engineering/services/fileReadWriteService.js +++ b/reverse_engineering/services/fileReadWriteService.js @@ -2,29 +2,19 @@ const parquet = require('parquetjs-lite'); const rawFileDataTransformService = require('./rawFileDataTransformService'); const { getRowGroups } = require('./rawFileDataTransformService'); -const getMetadataFromFile = filePath => - new Promise(async (resolve, reject) => { - try { - const reader = await parquet.ParquetReader.openFile(filePath); - reader.close(); - resolve(reader); - } catch (e) { - reject(e); - } - }); +const getMetadataFromFile = async filePath => { + const reader = await parquet.ParquetReader.openFile(filePath); + reader.close(); + return reader; +}; -const getRawMetadataFromFile = filePath => - new Promise(async (resolve, reject) => { - try { - const envelopeReader = await parquet.ParquetEnvelopeReader.openFile(filePath); - await envelopeReader.readHeader(); - const metadata = await envelopeReader.readFooter(); - await envelopeReader.close(); - resolve(metadata); - } catch (e) { - reject(e); - } - }); +const getRawMetadataFromFile = async filePath => { + const envelopeReader = await parquet.ParquetEnvelopeReader.openFile(filePath); + await envelopeReader.readHeader(); + const metadata = await envelopeReader.readFooter(); + await envelopeReader.close(); + return metadata; +}; const readParquetFile = async filePath => { try { diff --git a/reverse_engineering/services/rawFileDataTransformService.js b/reverse_engineering/services/rawFileDataTransformService.js index a8d2efb..330ceef 100644 --- a/reverse_engineering/services/rawFileDataTransformService.js +++ b/reverse_engineering/services/rawFileDataTransformService.js @@ -29,25 +29,32 @@ const transformEncoding = encodingArray => ({ const transformFieldProperty = (propertyName, value) => { switch (propertyName) { - case 'name': + case 'name': { return { name: value }; - case 'type': + } + case 'type': { return isNumber(value) ? transformType(value) : {}; - case 'repetition_type': + } + case 'repetition_type': { return isNumber(value) ? transformRepetitionType(value) : {}; - case 'converted_type': + } + case 'converted_type': { return isNumber(value) ? transformConvertedType(value) : {}; - case 'scale': + } + case 'scale': { return isNumber(value) ? { scale: value } : {}; - case 'precision': + } + case 'precision': { return isNumber(value) ? { precision: value } : {}; - case 'num_children': + } + case 'num_children': { return { num_children: value }; - case 'codec': + } + case 'codec': { return transformCodec(value); - case 'encodings': + } + case 'encodings': { return transformEncoding(value); - default: { } } }; @@ -66,26 +73,26 @@ const defineFieldAdditionalData = fieldsMetadata => fields => if (field.fields) { return Object.assign(acc, { - [name]: Object.assign({}, field, { + [name]: { + ...field, fields: defineFieldAdditionalData(fieldsMetadata)(field.fields), - }), + }, }); } const additionalData = getFieldAdditionalData(fieldsMetadata, field); const additionalFieldMeta = additionalData ? additionalData.meta_data : {}; return Object.assign(acc, { - [name]: Object.assign({}, field, transformField(additionalFieldMeta)), + [name]: { + ...field, + ...transformField(additionalFieldMeta), + }, }); }, {}); const getFieldsMetadata = rawMetadata => { const [firstRowGroup] = rawMetadata.row_groups; - if (!firstRowGroup || !firstRowGroup.columns) { - return []; - } - - return firstRowGroup.columns; + return firstRowGroup?.columns || []; }; const getRowGroups = (rawMetadata, schema) => {