Skip to content

Commit

Permalink
HCK-7067: fix Sonar reports (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlikRakhmonov authored Jul 11, 2024
1 parent cffa2fa commit 420e195
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 68 deletions.
13 changes: 11 additions & 2 deletions forward_engineering/helpers/getFieldChildren.js
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion forward_engineering/helpers/removeChildrenFromField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const removeChildrenFromField = field => {
const newField = Object.assign({}, field);
const newField = { ...field };
delete newField.properties;
delete newField.patternProperties;
delete newField.items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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([
Expand Down
9 changes: 5 additions & 4 deletions reverse_engineering/helpers/adaptJsonSchema/mapJsonSchema.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,9 +25,10 @@ const mapJsonSchema = (jsonSchema, callback) => {
return jsonSchema;
}

return Object.assign({}, jsonSchema, {
return {
...jsonSchema,
[propertyName]: mapper(jsonSchema[propertyName]),
});
};
}, jsonSchema);
};
if (!_.isPlainObject(jsonSchema)) {
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions reverse_engineering/helpers/defineSchemaFieldsPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []) => {
Expand All @@ -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),
};
}, {});
};

Expand Down
2 changes: 1 addition & 1 deletion reverse_engineering/helpers/generateSchemaFieldsTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions reverse_engineering/helpers/includeMetadataInJSONSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ const includeCreator = createdBy => JSONSchema => {
return JSONSchema;
}

return Object.assign({}, JSONSchema, { creator: createdBy });
return { ...JSONSchema, creator: createdBy };
};

const includeExtra = keyValueMetadata => JSONSchema => {
if (!keyValueMetadata || !Array.isArray(keyValueMetadata)) {
return JSONSchema;
}

return Object.assign({}, JSONSchema, {
return {
...JSONSchema,
extra: keyValueMetadata.map(({ key, value }) => ({ extraKey: key, extraValue: value })),
});
};
};

const includeMetadataInJSONSchema = metadata => JSONSchema =>
Expand Down
24 changes: 15 additions & 9 deletions reverse_engineering/helpers/reverseFieldSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions reverse_engineering/services/JSONConvertService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 12 additions & 22 deletions reverse_engineering/services/fileReadWriteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 25 additions & 18 deletions reverse_engineering/services/rawFileDataTransformService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
}
}
};
Expand All @@ -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) => {
Expand Down

0 comments on commit 420e195

Please sign in to comment.