feat: support arrays in flows

This commit is contained in:
Ali BARIN
2024-07-29 11:33:36 +00:00
parent 02a872a376
commit 920a711c00
2 changed files with 33 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ export default function computeParameters(parameters, executionSteps) {
const computedValue = parts const computedValue = parts
.map((part) => { .map((part) => {
const isVariable = part.match(variableRegExp); const isVariable = part.match(variableRegExp);
if (isVariable) { if (isVariable) {
const stepIdAndKeyPath = part.replace(/{{step.|}}/g, ''); const stepIdAndKeyPath = part.replace(/{{step.|}}/g, '');
const [stepId, ...keyPaths] = stepIdAndKeyPath.split('.'); const [stepId, ...keyPaths] = stepIdAndKeyPath.split('.');
@@ -20,18 +21,33 @@ export default function computeParameters(parameters, executionSteps) {
}); });
const data = executionStep?.dataOut; const data = executionStep?.dataOut;
const dataValue = get(data, keyPath); const dataValue = get(data, keyPath);
// Covers both arrays and objects
if (typeof dataValue === 'object') {
return JSON.stringify(dataValue);
}
return dataValue; return dataValue;
} }
return part; return part;
}) }).join('');
.join('');
// 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 { return {
...result, ...result,
[key]: computedValue, [key]: computedValue,
}; };
} }
}
if (Array.isArray(value)) { if (Array.isArray(value)) {
return { return {

View File

@@ -19,7 +19,13 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => {
const value = joinBy('.', parentKey, index?.toString(), name); const value = joinBy('.', parentKey, index?.toString(), name);
if (Array.isArray(sampleValue)) { if (Array.isArray(sampleValue)) {
return sampleValue.flatMap((item, index) => const arrayItself = {
label,
value,
sampleValue: JSON.stringify(sampleValue),
};
const arrayItems = sampleValue.flatMap((item, index) =>
process({ process({
data: item, data: item,
parentKey: value, parentKey: value,
@@ -27,6 +33,9 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => {
parentLabel: label, parentLabel: label,
}), }),
); );
// TODO: remove spreading
return [arrayItself, ...arrayItems];
} }
if (typeof sampleValue === 'object' && sampleValue !== null) { if (typeof sampleValue === 'object' && sampleValue !== null) {
@@ -36,6 +45,7 @@ const process = ({ data, parentKey, index, parentLabel = '' }) => {
parentLabel: label, parentLabel: label,
}); });
} }
return [ return [
{ {
label, label,