Skip to content

Commit

Permalink
HCK-5631: fix handle of multiple sample data for map properties (#127)
Browse files Browse the repository at this point in the history
* RE: fix handle of multiple sample data for map properties

* fix formatting
  • Loading branch information
serhii-filonenko authored May 2, 2024
1 parent e1333ce commit 1949801
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion reverse_engineering/helpers/filterComplexUdt.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ module.exports = (_) => {

return handlerRecords.some(record => record === undefined) ? [] : handlerRecords;
};

const handleMap = ({ properties, records }) => {
return Object.entries(properties).reduce((result, [propertyName, propertyValue]) => {
return {
...result,
[propertyName]: filterUdt(propertyValue?.properties, records?.[propertyName])
}
}, {});
};

const filterUdt = (properties, records) => {
if (!_.isPlainObject(records)) {
Expand All @@ -45,7 +54,7 @@ module.exports = (_) => {
} else if (type === 'map' && propertyValue.properties) {
return {
...records,
[recordName]: filterUdt(propertyValue.properties, recordValues),
[recordName]: handleMap({ properties: propertyValue.properties, records: recordValues }),
}
}

Expand Down
18 changes: 16 additions & 2 deletions reverse_engineering/typesHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = (_) => {
const keyData = (column.info || column.type.info)[0];
const valueData = (column.info || column.type.info)[1];
const handledKeyData = getColumnType(keyData);
const properties = getProperties(valueData, sample, udtHash);
const properties = getMapProperties({ valueData, sample, udtHash });
const keySubtype = handledKeyData.mode
? { keySubtype: handledKeyData.mode }
: {};
Expand Down Expand Up @@ -176,10 +176,24 @@ module.exports = (_) => {
const name = handledValueData.refName || defaultColumnName;

return {
[name]: _.omit(handledValueData, 'refName')
[name]: _.omit(handledValueData, 'refName'),
};
};

const getMapProperties = ({ valueData, sample, udtHash }) => {
const properties = getProperties(valueData, sample, udtHash);
const propertyPairs = Object.entries(properties);
const uniqueProperties = _.uniqWith(
propertyPairs,
([, propertyA], [, propertyB]) =>
propertyA.type === propertyB.type &&
propertyA.$ref === propertyB.$ref &&
propertyA.mode === propertyB.mode
);

return Object.fromEntries(uniqueProperties);
};

const getJsonType = (type) => {
switch (type) {
case "smallint":
Expand Down

0 comments on commit 1949801

Please sign in to comment.