feat(web): Add DAG variables to web flow (#1981)

Co-authored-by: Fangyin Cheng <staneyffer@gmail.com>
Co-authored-by: 谨欣 <echo.cmy@antgroup.com>
Co-authored-by: yanzhiyong <932374019@qq.com>
Co-authored-by: 严志勇 <yanzhiyong@tiansuixiansheng.com>
This commit is contained in:
Dreammy23
2024-09-10 09:39:40 +08:00
committed by GitHub
parent fe29f977f3
commit 746e4fda37
23 changed files with 767 additions and 156 deletions

View File

@@ -1,4 +1,4 @@
import { IFlowData, IFlowDataNode, IFlowNode } from '@/types/flow';
import { IFlowData, IFlowDataNode, IFlowNode, IVariableItem } from '@/types/flow';
import { Node } from 'reactflow';
export const getUniqueNodeId = (nodeData: IFlowNode, nodes: Node[]) => {
@@ -140,3 +140,57 @@ export const convertKeysToCamelCase = (obj: Record<string, any>): Record<string,
return convert(obj);
};
function escapeVariable(value: string, enableEscape: boolean): string {
if (!enableEscape) {
return value;
}
return value.replace(/@/g, '\\@').replace(/#/g, '\\#').replace(/%/g, '\\%').replace(/:/g, '\\:');
}
export function buildVariableString(variableDict: IVariableItem): string {
const scopeSig = '@';
const sysCodeSig = '#';
const userSig = '%';
const kvSig = ':';
const enableEscape = true;
const specialChars = new Set([scopeSig, sysCodeSig, userSig, kvSig]);
const newVariableDict: Partial<IVariableItem> = {
key: variableDict.key || '',
name: variableDict.name || '',
scope: variableDict.scope || '',
scope_key: variableDict.scope_key || '',
sys_code: variableDict.sys_code || '',
user_name: variableDict.user_name || '',
};
// Check for special characters in values
for (const [key, value] of Object.entries(newVariableDict)) {
if (value && [...specialChars].some(char => (value as string).includes(char))) {
if (enableEscape) {
newVariableDict[key] = escapeVariable(value as string, enableEscape);
} else {
throw new Error(
`${key} contains special characters, error value: ${value}, special characters: ${[...specialChars].join(', ')}`,
);
}
}
}
const { key, name, scope, scope_key, sys_code, user_name } = newVariableDict;
let variableStr = `${key}`;
if (name) variableStr += `${kvSig}${name}`;
if (scope || scope_key) {
variableStr += `${scopeSig}${scope}`;
if (scope_key) {
variableStr += `${kvSig}${scope_key}`;
}
}
if (sys_code) variableStr += `${sysCodeSig}${sys_code}`;
if (user_name) variableStr += `${userSig}${user_name}`;
return `\${${variableStr}}`;
}