This topic describes the basics and examples of choice steps, and related conditional expressions.
Overview
Choice steps allow execution of different steps in a flow, 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 flow 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. The value choice indicates that the step is 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 JSON paths (
$.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 current choice step.
- condition: Required. The conditional expression. Conditional expressions reference step inputs based on JSON paths (
- 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.Note If no output mappings are specified,$local
is used as the output of this step by default.
Examples
The following sample flow 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 (`), for example, "foobar" or `foobar`.
- Numeric constants:
1
12.5
. - Boolean constants:
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 |
$.a==1 | true |
$.a==2 | false |
$.a>0 | true |
0<$.a | true |
$.a>=1 | true |
$.a!=2 | true |
$.b.b1 | true |
$.b.b1==true | true |
$.b.b1==false | false |
$.b.b2=="ready" | true |
$.b.b2==`ready` | true |
$.b.b2=="inprogress" | false |
$.a==1 && $.b.b1 | true |
$.a==1 || $.b.b1 | true |
$.a==2 && $.b.b1 | false |
$.a==2 || $.b.b1 | true |
$.c[0]==1 | true |
$.c[0]==$.a | true |