import * as tmpl from 'riot-tmpl' ; import { INode , INodeExecutionData , INodeParameters , IRunExecutionData , IWorkflowDataProxyAdditionalKeys , NodeParameterValue , Workflow , WorkflowDataProxy , WorkflowExecuteMode , } from '.' ; tmpl . brackets . set ( '{{ }}' ) ; tmpl . tmpl . errorHandler = ( ) => { } ; export class Expression { workflow : Workflow ; constructor ( workflow ) { this . workflow = workflow ; } param { object } value returns { string } memberof convertObjectValueToString ( value ) { const typeName = Array . isArray ( value ) ? 'Array' : 'Object' ; return `[${ typeName }: ${ JSON . stringify ( value ) }]` ; } param { NodeParameterValue } parameterValue param { ( IRunExecutionData | null ) } runExecutionData param { number } runIndex param { number } itemIndex param { string } activeNodeName param { INodeExecutionData [ ] } connectionInputData param { boolean } returnObjectAsString returns { ( NodeParameterValue | INodeParameters | NodeParameterValue [ ] | INodeParameters [ ] ) } memberof resolveSimpleParameterValue ( parameterValue , siblingParameters , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , mode , additionalKeys , returnObjectAsString = false , selfData = { } ) { if ( typeof parameterValue !== 'string' || parameterValue . charAt ( 0 ) !== '=' ) { return parameterValue ; } parameterValue = parameterValue . substr ( 1 ) ; const dataProxy = new WorkflowDataProxy ( this . workflow , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , siblingParameters , mode , additionalKeys , - 1 , selfData ) ; const data = dataProxy . getDataProxy ( ) ; 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 , } ; data . document = { } ; data . constructor = { } ; try { 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' ) { if ( returnObjectAsString ) { return this . convertObjectValueToString ( returnValue ) ; } } return returnValue ; } catch ( e ) { throw new Error ( `Expression is not valid: ${ e . message }` ) ; } } param { INode } node param { ( string | undefined ) } parameterValue param { string } defaultValue returns { ( string | undefined ) } memberof getSimpleParameterValue ( node , parameterValue , mode , additionalKeys , defaultValue ? ) { if ( parameterValue === undefined ) { return defaultValue ; } const runIndex = 0 ; const itemIndex = 0 ; const connectionInputData = [ ] ; const runData = { resultData : { runData : { } , } , } ; return this . getParameterValue ( parameterValue , runData , runIndex , itemIndex , node . name , connectionInputData , mode , additionalKeys ) as boolean | number | string | undefined ; } param { INode } node param { ( NodeParameterValue | INodeParameters | NodeParameterValue [ ] | INodeParameters [ ] ) } parameterValue param { ( NodeParameterValue | INodeParameters | NodeParameterValue [ ] | INodeParameters [ ] | undefined ) } defaultValue returns { ( NodeParameterValue | INodeParameters | NodeParameterValue [ ] | INodeParameters [ ] | undefined ) } memberof getComplexParameterValue ( node , parameterValue , mode , additionalKeys , defaultValue = undefined , selfData = { } ) { if ( parameterValue === undefined ) { return defaultValue ; } const runIndex = 0 ; const itemIndex = 0 ; const connectionInputData = [ ] ; const runData = { resultData : { runData : { } , } , } ; const returnData = this . getParameterValue ( parameterValue , runData , runIndex , itemIndex , node . name , connectionInputData , mode , additionalKeys , false , selfData ) ; return this . getParameterValue ( returnData , runData , runIndex , itemIndex , node . name , connectionInputData , mode , additionalKeys , false , selfData ) ; } 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 returns { ( NodeParameterValue | INodeParameters | NodeParameterValue [ ] | INodeParameters [ ] ) } memberof getParameterValue ( parameterValue , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , mode , additionalKeys , returnObjectAsString = false , selfData = { } ) { const isComplexParameter = ( value ) => { return typeof value === 'object' ; } ; const resolveParameterValue = ( value , siblingParameters ) => { if ( isComplexParameter ( value ) ) { return this . getParameterValue ( value , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , mode , additionalKeys , returnObjectAsString , selfData ) ; } return this . resolveSimpleParameterValue ( value as NodeParameterValue , siblingParameters , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , mode , additionalKeys , returnObjectAsString , selfData ) ; } ; if ( ! isComplexParameter ( parameterValue ) ) { return this . resolveSimpleParameterValue ( parameterValue as NodeParameterValue , { } , runExecutionData , runIndex , itemIndex , activeNodeName , connectionInputData , mode , additionalKeys , returnObjectAsString , selfData ) ; } if ( Array . isArray ( parameterValue ) ) { const returnData = [ ] ; for ( const item of parameterValue ) { returnData . push ( resolveParameterValue ( item , { } ) ) ; } if ( returnObjectAsString && typeof returnData === 'object' ) { return this . convertObjectValueToString ( returnData ) ; } return returnData as NodeParameterValue [ ] | INodeParameters [ ] ; } if ( parameterValue === null || parameterValue === undefined ) { return parameterValue ; } const returnData = { } ; for ( const key of Object . keys ( parameterValue ) ) { returnData [ key ] = resolveParameterValue ( ( parameterValue as INodeParameters ) [ key ] , parameterValue as INodeParameters ) ; } if ( returnObjectAsString && typeof returnData === 'object' ) { return this . convertObjectValueToString ( returnData ) ; } return returnData ; } }