Skip to content

Commit

Permalink
fix(Compare Datasets Node): Support for dot notation in skip fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Apr 4, 2023
1 parent 5ff3dea commit 83e25c0
Show file tree
Hide file tree
Showing 3 changed files with 1,007 additions and 8 deletions.
36 changes: 36 additions & 0 deletions packages/nodes-base/nodes/CompareDatasets/CompareDatasets.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,42 @@ export class CompareDatasets implements INodeType {
description: 'Output contains all data (but structure more complex)',
},
],
displayOptions: {
show: {
'@version': [1, 2],
},
},
},
{
displayName: 'When There Are Differences',
name: 'resolve',
type: 'options',
default: 'includeBoth',
options: [
{
name: 'Use Input A Version',
value: 'preferInput1',
},
{
name: 'Use Input B Version',
value: 'preferInput2',
},
{
name: 'Use a Mix of Versions',
value: 'mix',
description: 'Output uses different inputs for different fields',
},
{
name: 'Include Both Versions',
value: 'includeBoth',
description: 'Output contains all data (but structure more complex)',
},
],
displayOptions: {
hide: {
'@version': [1, 2],
},
},
},
{
displayName: 'Fuzzy Compare',
Expand Down
82 changes: 74 additions & 8 deletions packages/nodes-base/nodes/CompareDatasets/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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 };
}
}
}
});
Expand Down Expand Up @@ -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[],
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 83e25c0

Please sign in to comment.