fix(Compare Datasets Node): Support for dot notation in skip fields

This commit is contained in:
Michael Kret
2023-04-04 14:52:53 +03:00
committed by GitHub
parent 5ff3dea7bb
commit 83e25c066a
3 changed files with 1007 additions and 8 deletions

View File

@@ -5,6 +5,8 @@ import get from 'lodash.get';
import intersection from 'lodash.intersection';
import isEmpty from 'lodash.isempty';
import omit from 'lodash.omit';
import unset from 'lodash.unset';
import { cloneDeep } from 'lodash';
import set from 'lodash.set';
import union from 'lodash.union';
import { fuzzyCompare } from '../../utils/utilities';
@@ -65,7 +67,7 @@ function compareItems(
const different: IDataObject = {};
const skipped: IDataObject = {};
differentKeys.forEach((key) => {
differentKeys.forEach((key, i) => {
const processNullishValue = processNullishValueFunction(options.nodeVersion as number);
switch (options.resolve) {
@@ -76,18 +78,63 @@ function compareItems(
different[key] = processNullishValue(item2.json[key]);
break;
default:
const input1 = processNullishValue(item1.json[key]);
const input2 = processNullishValue(item2.json[key]);
let input1 = processNullishValue(item1.json[key]);
let input2 = processNullishValue(item2.json[key]);
let [firstInputName, secondInputName] = ['input1', 'input2'];
if ((options.nodeVersion as number) >= 2) {
[firstInputName, secondInputName] = ['inputA', 'inputB'];
}
if (skipFields.includes(key)) {
skipped[key] = { [firstInputName]: input1, [secondInputName]: input2 };
} else {
if (
(options.nodeVersion as number) >= 2.1 &&
!options.disableDotNotation &&
!skipFields.some((field) => field === key)
) {
const skippedFieldsWithDotNotation = skipFields.filter(
(field) => field.startsWith(key) && field.includes('.'),
);
input1 = cloneDeep(input1);
input2 = cloneDeep(input2);
if (
skippedFieldsWithDotNotation.length &&
(typeof input1 !== 'object' || typeof input2 !== 'object')
) {
throw new Error(
`The field \'${key}\' in item ${i} is not an object. It is not possible to use dot notation.`,
);
}
if (skipped[key] === undefined && skippedFieldsWithDotNotation.length) {
skipped[key] = { [firstInputName]: {}, [secondInputName]: {} };
}
for (const skippedField of skippedFieldsWithDotNotation) {
const nestedField = skippedField.replace(`${key}.`, '');
set(
(skipped[key] as IDataObject)[firstInputName] as IDataObject,
nestedField,
get(input1, nestedField),
);
set(
(skipped[key] as IDataObject)[secondInputName] as IDataObject,
nestedField,
get(input2, nestedField),
);
unset(input1, nestedField);
unset(input2, nestedField);
}
different[key] = { [firstInputName]: input1, [secondInputName]: input2 };
} else {
if (skipFields.includes(key)) {
skipped[key] = { [firstInputName]: input1, [secondInputName]: input2 };
} else {
different[key] = { [firstInputName]: input1, [secondInputName]: input2 };
}
}
}
});
@@ -205,6 +252,15 @@ export function findMatches(
const multipleMatches = (options.multipleMatches as string) || 'first';
const skipFields = ((options.skipFields as string) || '').split(',').map((field) => field.trim());
if (disableDotNotation && skipFields.some((field) => field.includes('.'))) {
const fieldToSkip = skipFields.find((field) => field.includes('.'));
throw new Error(
`Dot notation is disabled, but field to skip comparing '${
fieldToSkip as string
}' contains dot`,
);
}
const filteredData = {
matched: [] as EntryMatches[],
unmatched1: [] as INodeExecutionData[],
@@ -265,8 +321,18 @@ export function findMatches(
let entryFromInput2 = match.json;
if (skipFields.length) {
entryFromInput1 = omit(entryFromInput1, skipFields);
entryFromInput2 = omit(entryFromInput2, skipFields);
if (disableDotNotation || !skipFields.some((field) => field.includes('.'))) {
entryFromInput1 = omit(entryFromInput1, skipFields);
entryFromInput2 = omit(entryFromInput2, skipFields);
} else {
entryFromInput1 = cloneDeep(entryFromInput1);
entryFromInput2 = cloneDeep(entryFromInput2);
skipFields.forEach((field) => {
unset(entryFromInput1, field);
unset(entryFromInput2, field);
});
}
}
let isItemsEqual = true;