Search code examples
cadence-workflow

How can I pass a complex objects as input when using Cadence CLI tool?


Here is a sample workflow and its input struct:

func MyWorkflow(ctx cadence.Context, input MyWorkflowParameters) error {
    ...
}

type MyWorkflowParameters struct {
    SomeString: string,
    SomeInteger: int32
} 

What’s the best way to pass the complex struct above as the input parameter to the Cadence CLI tool when starting or signaling a workflow?


Solution

  • The input parameter of the Cadence command-line tool accepts values in a few different formats depending on what your workflow expects. Here are examples for three cases:

    1. Single integers or strings:

    --input 12345
    --input “my-string"
    

    2. Complex objects:

    When the parameter is a struct as in your example, you need to pass a valid JSON encoded object as in the following example:

    --input '{"SomeString":"my-string","SomeInteger":12345}'

    3. Multiple parameters:

    If you have a workflow that expects multiple parameters, you need to pass a single space-delimited string where each part of the string corresponds to a particular parameter expected by the workflow. The example below shows how you can pass one integer, one string, and one struct parameter, consecutively:

    --input '12345 "second param" {"SomeString":"my-string","SomeInteger":12345}'
    

    On a related note, the recommended way to accept input parameters in a workflow is using a single struct parameter. Even though the syntactic sugar in JSON pollutes the CLI command a little bit, especially when all you need to pass is a single parameter, it pays off when you start passing more parameters to the workflow.