This topic describes the basics and examples of choice steps, as well as related conditional expressions.
Parameters
Choice steps allow execution of different steps in a workflow based on conditions, similar to switch-case
in programming languages. A choice step contains multiple choices and a default. Each choice contains a conditional expression, several steps, and goto instructions. The default contains only several steps and goto instructions. When the workflow proceeds to a choice step, the system evaluates whether the conditional expressions return True
in the defined sequence.
If
True
is returned, the steps and then goto instructions defined in the corresponding choice are executed.If no choice returns
True
, the steps and goto instructions defined in the default are executed.If no default is defined, the choice step ends.
A choice step contains the following attributes:
type: Required. The step type. A value of choice specifies a choice step.
name: Required. The step name.
choices: Required. Multiple choices of the array type. Each element corresponds to a choice.
condition: Required. The conditional expression. Conditional expressions reference step inputs based on JSONPath (
$.key
).steps: Optional. The multiple serial steps defined for a choice.
goto: Optional. The name of the target step, which must be parallel to the choice step.
default: Required. The default.
steps: Optional. The multiple serial steps defined for the default.
goto: Optional. The name of the target step, which must be parallel to the current choice step.
end: Optional. Specifies whether to proceed with the subsequent steps after the current step ends.
inputMappings: Optional. The input mappings.
outputMappings: Optional. The output mappings. The
$local
of this step indicates the execution result of the choice branch.NoteIf no output mappings are specified,
$local
is used as the output of this step by default.
Examples
The following sample workflow defines a choice step.
If the value of
status
in the input isready
, thepass1
,pass3
andfinal
steps of the first choice are executed in sequence.If the value of
status
in the input isfailed
, the goto instructions of the second choice are executed, the choice step ends, and thefinal
step is executed.If the value of
status
in the input is neitherready
norfailed
, the default is executed. In other words, thepass2
andfinal
steps are executed.
version: v1
type: flow
steps:
- type: choice
name: mychoice
choices:
- condition: $.status == "ready"
# choice with steps
steps:
- type: pass
name: pass1
- condition: $.status == "failed"
# choice with goto
goto: final
default:
# choice with both steps and goto
steps:
- type: pass
name: pass2
goto: final
- type: pass
name: pass3
- type: pass
name: final
Conditional expressions
A conditional expression consists of the following operations and variables:
Comparison operations:
>
>=
<
<=
==
!=
. They are applicable to strings and numbers.Logical operations:
||
&&
.String constants: A string constant is enclosed in double quotation marks (") or grave accents (`). Examples: "foobar" or `foobar`.
Numeric constants:
1
12.5
.Boolean constant:
true
orfalse
.Prefix:
!
-
.Contain:
in
, which is used to determine whether an array contains a value or whether an object contains a key value.
The following example shows the execution results of steps for different conditional expressions.
{
"a": 1,
"b": {
"b1": true,
"b2": "ready"
},
"c": [1, 2, 3],
"d": 1,
"e": 1,
"f": {
"f1": false,
"f2": "inprogress"
}
}
Conditional expression | Result |
| true |
| false |
| true |
| true |
| true |
| true |
| true |
| true |
| false |
| true |
| true |
| false |
| true |
| true |
| false |
| true |
| true |
| true |