Switch
The switch
component: A conditional branching component.
It matches the case expressions in sequence. If a match is found, it stops and forwards the message to the corresponding route chain. If no match is found, it forwards to the default Default
chain.
# Configuration
Field | Type | Description | Default Value |
---|---|---|---|
cases | List of Case | List of conditions | None |
Case:
Field | Type | Description | Default Value |
---|---|---|---|
case | string | Conditional expression | None |
then | string | Routing relationship | None |
The case expression can use the following variables:
- Access the message ID through the
id
variable. - Access the message timestamp through the
ts
variable. - Access the original content of the message through the
data
variable. - Access the message body through the
msg
variable. If the message's dataType is JSON, you can access msg fields with themsg.XX
method. For example:msg.temperature > 50;
- Access message metadata through the
metadata
variable. For example:metadata.customerName
- Access the message type through the
type
variable. - Access the data type through the
dataType
variable.
Expression examples:
msg.temperature > 50
msg.temperature > 50 && metadata.customerName == 'rulego'
upper(metadata.customerName[:4]) == 'GO'
replace(toJSON(msg),'name','productName')
For more information on expr expression syntax, refer to: expr (opens new window)
# Relation Type
- If a
case
is matched, the message is sent to the chain corresponding to thethen
value. - Failure: If execution fails, the message is sent to the
Failure
chain.
# Execution Result
This component does not change the content of msg
, metadata
, and msgType
.
# Configuration Example
{
"ruleChain": {
"id": "bW1lMC97oYih",
"name": "测试条件分支",
"debugMode": true,
"root": true
},
"metadata": {
"endpoints": [],
"nodes": [
{
"id": "node_2",
"additionalInfo": {
"description": "",
"layoutX": 480,
"layoutY": 280
},
"type": "switch",
"name": "条件分支",
"debugMode": false,
"configuration": {
"cases": [
{
"case": "msg.temperature>=20 && msg.temperature<=50",
"then": "Case1"
},
{
"case": "msg.temperature>50",
"then": "Case2"
}
]
}
},
{
"id": "node_4",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 160
},
"type": "jsTransform",
"name": "case1",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Case1'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
},
{
"id": "node_5",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 280
},
"type": "jsTransform",
"name": "case2",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Case2'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
},
{
"id": "node_6",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 380
},
"type": "jsTransform",
"name": "default",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Default'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
}
],
"connections": [
{
"fromId": "node_2",
"toId": "node_4",
"type": "Case1"
},
{
"fromId": "node_2",
"toId": "node_5",
"type": "Case2"
},
{
"fromId": "node_2",
"toId": "node_6",
"type": "Default"
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/23, 10:13:01