|
|
@@ -68,14 +68,25 @@ function selectInput(value, path) {
|
|
|
return smartbotic.utils.getFieldValue(value, path);
|
|
|
}
|
|
|
|
|
|
+function producedSomething(value) {
|
|
|
+ if (value === undefined || value === null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (Array.isArray(value) && value.length === 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
async function execute(config, input, context) {
|
|
|
const mode = config.mode || 'append';
|
|
|
const first = selectInput(input ? input.input1 : undefined, config.path1);
|
|
|
const second = selectInput(input ? input.input2 : undefined, config.path2);
|
|
|
|
|
|
if (mode === 'choose-first') {
|
|
|
- const chosen = first !== undefined && first !== null ? first : second;
|
|
|
- smartbotic.log.info('Merge: choose-first took ' + (first !== undefined && first !== null ? 'input1' : 'input2'));
|
|
|
+ const firstHasSomething = producedSomething(first);
|
|
|
+ const chosen = firstHasSomething ? first : (producedSomething(second) ? second : first);
|
|
|
+ smartbotic.log.info('Merge: choose-first took ' + (firstHasSomething ? 'input1' : 'input2'));
|
|
|
return {
|
|
|
merged: chosen === undefined ? null : chosen,
|
|
|
count: Array.isArray(chosen) ? chosen.length : (chosen === undefined || chosen === null ? 0 : 1)
|
|
|
@@ -93,6 +104,9 @@ async function execute(config, input, context) {
|
|
|
|
|
|
for (const item of asArray(first)) {
|
|
|
if (item === null || typeof item !== 'object') continue;
|
|
|
+ if (item[key] === undefined || item[key] === null) {
|
|
|
+ throw new Error('Merge: an item in input1 has no "' + key + '" to combine on');
|
|
|
+ }
|
|
|
const id = String(item[key]);
|
|
|
byKey[id] = JSON.parse(JSON.stringify(item));
|
|
|
order.push(id);
|
|
|
@@ -100,6 +114,9 @@ async function execute(config, input, context) {
|
|
|
|
|
|
for (const item of asArray(second)) {
|
|
|
if (item === null || typeof item !== 'object') continue;
|
|
|
+ if (item[key] === undefined || item[key] === null) {
|
|
|
+ throw new Error('Merge: an item in input2 has no "' + key + '" to combine on');
|
|
|
+ }
|
|
|
const id = String(item[key]);
|
|
|
if (byKey[id] === undefined) {
|
|
|
byKey[id] = JSON.parse(JSON.stringify(item));
|