如何通过Cadence CLI传递复杂结构体作为工作流输入参数?
Great question! When using Cadence CLI to pass complex struct inputs (like your MyWorkflowParameters) to workflows or signals, the standard and most reliable approach is to serialize your struct into JSON format—Cadence CLI natively accepts JSON for complex type inputs. Let me walk you through the exact steps for both workflow initiation and signal delivery:
Starting a Workflow with Complex Input
First, convert your MyWorkflowParameters struct into a valid JSON object. Make sure the JSON field names match your struct's field names exactly (Go is case-sensitive, so SomeString and SomeInteger must stay in camel case).
For example, your struct:
type MyWorkflowParameters struct { SomeString string SomeInteger int32 }
Would translate to this JSON:
{"SomeString": "hello cadence", "SomeInteger": 42}
Now use the cadence workflow start command with the --input flag, wrapping your JSON in single quotes (to avoid shell parsing issues with double quotes):
cadence workflow start \ --domain your-domain-name \ --tasklist your-tasklist-name \ --workflow-type MyWorkflow \ --input '{"SomeString": "sample text", "SomeInteger": 123}'
If your JSON contains escaped characters (like inner double quotes), you can use a here-doc to make it cleaner:
cadence workflow start \ --domain your-domain-name \ --tasklist your-tasklist-name \ --workflow-type MyWorkflow \ --input "$(cat <<EOF {"SomeString": "text with \"escaped quotes\"", "SomeInteger": 456} EOF )"
Sending a Signal with Complex Input
The process is nearly identical for sending signals. First, define your signal's input as JSON (matching the struct your signal handler expects), then use the cadence workflow signal command:
cadence workflow signal \ --domain your-domain-name \ --workflow-id your-target-workflow-id \ --signal-name YourSignalName \ --input '{"SomeString": "signal payload", "SomeInteger": 789}'
Key Notes to Remember
- Case Sensitivity: Always match the exact case of your struct's field names in the JSON—Cadence relies on this to correctly deserialize the input into your Go struct.
- Nested Structs: If your input has nested complex types, just extend the JSON structure accordingly. For example:
{"SomeString": "test", "NestedParam": {"InnerField": "value", "InnerInt": 10}} - Type Accuracy: Ensure numeric values are not wrapped in quotes (e.g.,
SomeInteger: 123instead of"SomeInteger": "123"), so Cadence can parse them into the correct Go type.
内容的提问来源于stack exchange,提问作者Emrah Seker

