2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
|
|
|
import * as tmpl from 'riot-tmpl';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
2020-09-12 03:16:07 -07:00
|
|
|
import {
|
|
|
|
INode,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeParameters,
|
|
|
|
IRunExecutionData,
|
2021-08-21 05:11:32 -07:00
|
|
|
IWorkflowDataProxyAdditionalKeys,
|
2020-09-12 03:16:07 -07:00
|
|
|
NodeParameterValue,
|
|
|
|
Workflow,
|
|
|
|
WorkflowDataProxy,
|
2021-01-29 00:31:40 -08:00
|
|
|
WorkflowExecuteMode,
|
2021-08-29 11:58:11 -07:00
|
|
|
} from '.';
|
2020-09-12 03:16:07 -07:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
// Set it to use double curly brackets instead of single ones
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
2020-09-12 03:16:07 -07:00
|
|
|
tmpl.brackets.set('{{ }}');
|
|
|
|
|
|
|
|
// Make sure that it does not always print an error when it could not resolve
|
|
|
|
// a variable
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
|
|
tmpl.tmpl.errorHandler = () => {};
|
2020-09-12 03:16:07 -07:00
|
|
|
|
|
|
|
export class Expression {
|
|
|
|
workflow: Workflow;
|
|
|
|
|
|
|
|
constructor(workflow: Workflow) {
|
|
|
|
this.workflow = workflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an object to a string in a way to make it clear that
|
|
|
|
* the value comes from an object
|
|
|
|
*
|
|
|
|
* @param {object} value
|
|
|
|
* @returns {string}
|
|
|
|
* @memberof Workflow
|
|
|
|
*/
|
|
|
|
convertObjectValueToString(value: object): string {
|
|
|
|
const typeName = Array.isArray(value) ? 'Array' : 'Object';
|
|
|
|
return `[${typeName}: ${JSON.stringify(value)}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the paramter value. If it is an expression it will execute it and
|
|
|
|
* return the result. For everything simply the supplied value will be returned.
|
|
|
|
*
|
|
|
|
* @param {NodeParameterValue} parameterValue
|
|
|
|
* @param {(IRunExecutionData | null)} runExecutionData
|
|
|
|
* @param {number} runIndex
|
|
|
|
* @param {number} itemIndex
|
|
|
|
* @param {string} activeNodeName
|
|
|
|
* @param {INodeExecutionData[]} connectionInputData
|
|
|
|
* @param {boolean} [returnObjectAsString=false]
|
|
|
|
* @returns {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[])}
|
|
|
|
* @memberof Workflow
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
resolveSimpleParameterValue(
|
|
|
|
parameterValue: NodeParameterValue,
|
|
|
|
siblingParameters: INodeParameters,
|
|
|
|
runExecutionData: IRunExecutionData | null,
|
|
|
|
runIndex: number,
|
|
|
|
itemIndex: number,
|
|
|
|
activeNodeName: string,
|
|
|
|
connectionInputData: INodeExecutionData[],
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
additionalKeys: IWorkflowDataProxyAdditionalKeys,
|
|
|
|
returnObjectAsString = false,
|
|
|
|
selfData = {},
|
|
|
|
): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] {
|
2020-09-12 03:16:07 -07:00
|
|
|
// Check if it is an expression
|
|
|
|
if (typeof parameterValue !== 'string' || parameterValue.charAt(0) !== '=') {
|
|
|
|
// Is no expression so return value
|
|
|
|
return parameterValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is an expression
|
|
|
|
|
|
|
|
// Remove the equal sign
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-09-12 03:16:07 -07:00
|
|
|
parameterValue = parameterValue.substr(1);
|
|
|
|
|
|
|
|
// Generate a data proxy which allows to query workflow data
|
2021-08-29 11:58:11 -07:00
|
|
|
const dataProxy = new WorkflowDataProxy(
|
|
|
|
this.workflow,
|
|
|
|
runExecutionData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
activeNodeName,
|
|
|
|
connectionInputData,
|
|
|
|
siblingParameters,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
-1,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
const data = dataProxy.getDataProxy();
|
|
|
|
|
2021-11-09 23:49:45 -08:00
|
|
|
// Support only a subset of process properties
|
|
|
|
// @ts-ignore
|
|
|
|
data.process = {
|
|
|
|
arch: process.arch,
|
|
|
|
env: process.env,
|
|
|
|
platform: process.platform,
|
|
|
|
pid: process.pid,
|
|
|
|
ppid: process.ppid,
|
|
|
|
release: process.release,
|
|
|
|
version: process.pid,
|
|
|
|
versions: process.versions,
|
|
|
|
};
|
|
|
|
|
2021-11-13 15:11:50 -08:00
|
|
|
// @ts-ignore
|
|
|
|
data.document = {};
|
2021-12-03 06:08:38 -08:00
|
|
|
// @ts-ignore
|
|
|
|
data.constructor = {};
|
2021-11-13 15:11:50 -08:00
|
|
|
|
2020-09-12 03:16:07 -07:00
|
|
|
// Execute the expression
|
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
2020-09-12 03:16:07 -07:00
|
|
|
const returnValue = tmpl.tmpl(parameterValue, data);
|
|
|
|
if (typeof returnValue === 'function') {
|
|
|
|
throw new Error('Expression resolved to a function. Please add "()"');
|
|
|
|
} else if (returnValue !== null && typeof returnValue === 'object') {
|
2021-08-29 11:58:11 -07:00
|
|
|
if (returnObjectAsString) {
|
2020-09-12 03:16:07 -07:00
|
|
|
return this.convertObjectValueToString(returnValue);
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
2020-09-12 03:16:07 -07:00
|
|
|
return returnValue;
|
|
|
|
} catch (e) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
|
2020-09-12 03:16:07 -07:00
|
|
|
throw new Error(`Expression is not valid: ${e.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves value of parameter. But does not work for workflow-data.
|
|
|
|
*
|
|
|
|
* @param {INode} node
|
|
|
|
* @param {(string | undefined)} parameterValue
|
|
|
|
* @param {string} [defaultValue]
|
|
|
|
* @returns {(string | undefined)}
|
|
|
|
* @memberof Workflow
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
getSimpleParameterValue(
|
|
|
|
node: INode,
|
|
|
|
parameterValue: string | boolean | undefined,
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
additionalKeys: IWorkflowDataProxyAdditionalKeys,
|
|
|
|
defaultValue?: boolean | number | string,
|
|
|
|
): boolean | number | string | undefined {
|
2020-09-12 03:16:07 -07:00
|
|
|
if (parameterValue === undefined) {
|
|
|
|
// Value is not set so return the default
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value of the node (can be an expression)
|
|
|
|
const runIndex = 0;
|
|
|
|
const itemIndex = 0;
|
|
|
|
const connectionInputData: INodeExecutionData[] = [];
|
|
|
|
const runData: IRunExecutionData = {
|
|
|
|
resultData: {
|
|
|
|
runData: {},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-09-12 03:16:07 -07:00
|
|
|
};
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
return this.getParameterValue(
|
|
|
|
parameterValue,
|
|
|
|
runData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
node.name,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
) as boolean | number | string | undefined;
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves value of complex parameter. But does not work for workflow-data.
|
|
|
|
*
|
|
|
|
* @param {INode} node
|
|
|
|
* @param {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[])} parameterValue
|
|
|
|
* @param {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | undefined)} [defaultValue]
|
|
|
|
* @returns {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | undefined)}
|
|
|
|
* @memberof Workflow
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
getComplexParameterValue(
|
|
|
|
node: INode,
|
|
|
|
parameterValue: NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[],
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
additionalKeys: IWorkflowDataProxyAdditionalKeys,
|
|
|
|
defaultValue:
|
|
|
|
| NodeParameterValue
|
|
|
|
| INodeParameters
|
|
|
|
| NodeParameterValue[]
|
|
|
|
| INodeParameters[]
|
|
|
|
| undefined = undefined,
|
|
|
|
selfData = {},
|
|
|
|
): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] | undefined {
|
2020-09-12 03:16:07 -07:00
|
|
|
if (parameterValue === undefined) {
|
|
|
|
// Value is not set so return the default
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value of the node (can be an expression)
|
|
|
|
const runIndex = 0;
|
|
|
|
const itemIndex = 0;
|
|
|
|
const connectionInputData: INodeExecutionData[] = [];
|
|
|
|
const runData: IRunExecutionData = {
|
|
|
|
resultData: {
|
|
|
|
runData: {},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-09-12 03:16:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Resolve the "outer" main values
|
2021-08-29 11:58:11 -07:00
|
|
|
const returnData = this.getParameterValue(
|
|
|
|
parameterValue,
|
|
|
|
runData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
node.name,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
false,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
|
|
|
|
// Resolve the "inner" values
|
2021-08-29 11:58:11 -07:00
|
|
|
return this.getParameterValue(
|
|
|
|
returnData,
|
|
|
|
runData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
node.name,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
false,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the resolved node parameter value. If it is an expression it will execute it and
|
|
|
|
* return the result. If the value to resolve is an array or object it will do the same
|
|
|
|
* for all of the items and values.
|
|
|
|
*
|
|
|
|
* @param {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[])} parameterValue
|
|
|
|
* @param {(IRunExecutionData | null)} runExecutionData
|
|
|
|
* @param {number} runIndex
|
|
|
|
* @param {number} itemIndex
|
|
|
|
* @param {string} activeNodeName
|
|
|
|
* @param {INodeExecutionData[]} connectionInputData
|
|
|
|
* @param {boolean} [returnObjectAsString=false]
|
|
|
|
* @returns {(NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[])}
|
|
|
|
* @memberof Workflow
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
getParameterValue(
|
|
|
|
parameterValue: NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[],
|
|
|
|
runExecutionData: IRunExecutionData | null,
|
|
|
|
runIndex: number,
|
|
|
|
itemIndex: number,
|
|
|
|
activeNodeName: string,
|
|
|
|
connectionInputData: INodeExecutionData[],
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
additionalKeys: IWorkflowDataProxyAdditionalKeys,
|
|
|
|
returnObjectAsString = false,
|
|
|
|
selfData = {},
|
|
|
|
): NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[] {
|
2020-09-12 03:16:07 -07:00
|
|
|
// Helper function which returns true when the parameter is a complex one or array
|
2021-08-29 11:58:11 -07:00
|
|
|
const isComplexParameter = (
|
|
|
|
value: NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[],
|
|
|
|
) => {
|
2020-09-12 03:16:07 -07:00
|
|
|
return typeof value === 'object';
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper function which resolves a parameter value depending on if it is simply or not
|
2021-08-29 11:58:11 -07:00
|
|
|
const resolveParameterValue = (
|
|
|
|
value: NodeParameterValue | INodeParameters | NodeParameterValue[] | INodeParameters[],
|
|
|
|
siblingParameters: INodeParameters,
|
|
|
|
) => {
|
2020-09-12 03:16:07 -07:00
|
|
|
if (isComplexParameter(value)) {
|
2021-08-29 11:58:11 -07:00
|
|
|
return this.getParameterValue(
|
|
|
|
value,
|
|
|
|
runExecutionData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
activeNodeName,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
returnObjectAsString,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
return this.resolveSimpleParameterValue(
|
|
|
|
value as NodeParameterValue,
|
|
|
|
siblingParameters,
|
|
|
|
runExecutionData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
activeNodeName,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
returnObjectAsString,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check if it value is a simple one that we can get it resolved directly
|
|
|
|
if (!isComplexParameter(parameterValue)) {
|
2021-08-29 11:58:11 -07:00
|
|
|
return this.resolveSimpleParameterValue(
|
|
|
|
parameterValue as NodeParameterValue,
|
|
|
|
{},
|
|
|
|
runExecutionData,
|
|
|
|
runIndex,
|
|
|
|
itemIndex,
|
|
|
|
activeNodeName,
|
|
|
|
connectionInputData,
|
|
|
|
mode,
|
|
|
|
additionalKeys,
|
|
|
|
returnObjectAsString,
|
|
|
|
selfData,
|
|
|
|
);
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The parameter value is complex so resolve depending on type
|
|
|
|
|
|
|
|
if (Array.isArray(parameterValue)) {
|
|
|
|
// Data is an array
|
|
|
|
const returnData = [];
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-09-12 03:16:07 -07:00
|
|
|
for (const item of parameterValue) {
|
2021-05-14 16:16:48 -07:00
|
|
|
returnData.push(resolveParameterValue(item, {}));
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
if (returnObjectAsString && typeof returnData === 'object') {
|
2020-09-12 03:16:07 -07:00
|
|
|
return this.convertObjectValueToString(returnData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData as NodeParameterValue[] | INodeParameters[];
|
2021-08-29 11:58:11 -07:00
|
|
|
}
|
|
|
|
if (parameterValue === null || parameterValue === undefined) {
|
2020-09-23 04:20:50 -07:00
|
|
|
return parameterValue;
|
2021-08-29 11:58:11 -07:00
|
|
|
}
|
|
|
|
// Data is an object
|
|
|
|
const returnData: INodeParameters = {};
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const key of Object.keys(parameterValue)) {
|
|
|
|
returnData[key] = resolveParameterValue(
|
|
|
|
(parameterValue as INodeParameters)[key],
|
|
|
|
parameterValue as INodeParameters,
|
|
|
|
);
|
|
|
|
}
|
2020-09-12 03:16:07 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
if (returnObjectAsString && typeof returnData === 'object') {
|
|
|
|
return this.convertObjectValueToString(returnData);
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
return returnData;
|
2020-09-12 03:16:07 -07:00
|
|
|
}
|
|
|
|
}
|