From 920a711c009b7bba367835b1b9336bdaf88ae807 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 29 Jul 2024 11:33:36 +0000 Subject: [PATCH] feat: support arrays in flows --- .../backend/src/helpers/compute-parameters.js | 28 +++++++++++++++---- .../web/src/components/PowerInput/data.js | 12 +++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/helpers/compute-parameters.js b/packages/backend/src/helpers/compute-parameters.js index d29252b6..8c527942 100644 --- a/packages/backend/src/helpers/compute-parameters.js +++ b/packages/backend/src/helpers/compute-parameters.js @@ -11,6 +11,7 @@ export default function computeParameters(parameters, executionSteps) { const computedValue = parts .map((part) => { const isVariable = part.match(variableRegExp); + if (isVariable) { const stepIdAndKeyPath = part.replace(/{{step.|}}/g, ''); const [stepId, ...keyPaths] = stepIdAndKeyPath.split('.'); @@ -20,17 +21,32 @@ export default function computeParameters(parameters, executionSteps) { }); const data = executionStep?.dataOut; const dataValue = get(data, keyPath); + + // Covers both arrays and objects + if (typeof dataValue === 'object') { + return JSON.stringify(dataValue); + } + return dataValue; } return part; - }) - .join(''); + }).join(''); - return { - ...result, - [key]: computedValue, - }; + // challenge the input to see if it is stringifies object or array + try { + const parsedValue = JSON.parse(computedValue); + + return { + ...result, + [key]: parsedValue, + }; + } catch (error) { + return { + ...result, + [key]: computedValue, + }; + } } if (Array.isArray(value)) { diff --git a/packages/web/src/components/PowerInput/data.js b/packages/web/src/components/PowerInput/data.js index 3aec60bb..df9ea7df 100644 --- a/packages/web/src/components/PowerInput/data.js +++ b/packages/web/src/components/PowerInput/data.js @@ -19,7 +19,13 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => { const value = joinBy('.', parentKey, index?.toString(), name); if (Array.isArray(sampleValue)) { - return sampleValue.flatMap((item, index) => + const arrayItself = { + label, + value, + sampleValue: JSON.stringify(sampleValue), + }; + + const arrayItems = sampleValue.flatMap((item, index) => process({ data: item, parentKey: value, @@ -27,6 +33,9 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => { parentLabel: label, }), ); + + // TODO: remove spreading + return [arrayItself, ...arrayItems]; } if (typeof sampleValue === 'object' && sampleValue !== null) { @@ -36,6 +45,7 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => { parentLabel: label, }); } + return [ { label,