Skip to content

Commit

Permalink
feat: support full expression in conditions builder(AutomaApp#1596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 committed Dec 21, 2023
1 parent 81a92a5 commit 8aaabfc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/stores/workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const defaultWorkflow = (data = null, options = {}) => {
settings: {
publicId: '',
blockDelay: 0,
saveLog: true,
saveLog: false,
debugMode: false,
restartTimes: 3,
notification: true,
Expand Down
12 changes: 11 additions & 1 deletion src/workflowEngine/templating/mustacheReplacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export function keyParser(key, data) {

function replacer(
str,
{ regex, tagLen, modifyPath, data, disableStringify = false }
{
data,
regex,
tagLen,
modifyPath,
checkExistence = false,
disableStringify = false,
}
) {
const replaceResult = {
list: {},
Expand Down Expand Up @@ -106,6 +113,8 @@ function replacer(
dataKey = dataKey.slice(1);
}

if (checkExistence) return objectPath.has(data[dataKey], path);

result = objectPath.get(data[dataKey], path);
if (typeof result === 'undefined') result = match;

Expand Down Expand Up @@ -144,6 +153,7 @@ export default function (str, refData, options = {}) {
tagLen: 1,
regex: /\[(.*?)\]/g,
...options,
checkExistence: false,
});
Object.assign(replacedList, list);

Expand Down
20 changes: 13 additions & 7 deletions src/workflowEngine/utils/testConditions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import cloneDeep from 'lodash.clonedeep';
import objectPath from 'object-path';
import { parseJSON } from '@/utils/helper';
import { conditionBuilder } from '@/utils/shared';
import renderString from '../templating/renderString';
import { keyParser } from '../templating/mustacheReplacer';

const isBoolStr = (str) => {
if (str === 'true') return true;
Expand Down Expand Up @@ -56,14 +54,22 @@ export default async function (conditionsArr, workflowData) {
const isInsideBrackets =
dataPath.startsWith('{{') && dataPath.endsWith('}}');

if (isInsideBrackets) {
dataPath = dataPath.slice(2, -2).trim();
if (!isInsideBrackets) {
dataPath = `{{${dataPath}}}`;
}

const parsedPath = keyParser(dataPath, workflowData.refData);
dataPath = `${parsedPath.dataKey}.${parsedPath.path}`;
let dataExists = await renderString(
dataPath,
workflowData.refData,
workflowData.isPopup,
{
checkExistence: true,
}
);
// It return string for some reason
dataExists = Boolean(parseJSON(dataExists.value, false));

return objectPath.has(workflowData.refData, dataPath);
return dataExists;
}

const copyData = cloneDeep(data);
Expand Down

0 comments on commit 8aaabfc

Please sign in to comment.