switch
Execute different instructions based on a variable’s value
Properties
switch
An object that accepts the following properties.
switch.variable
Name of the variable whose value needs to be compared. A SWML variable reference in variable must resolve to a variable
name.
switch.case
Case_params object of key-mapped values to array of SWML Methods to execute.
switch.default
Array of SWML Methods to execute if no cases match.
case_params
The case_params object serves as a dictionary where each key is a string identifier, and
the associated value is an array of SWML Method objects.
switch.case.{value}
Each key is a value to match, and its value is the array of SWML Methods to execute when the variable’s value matches the key.
Before the lookup, the variable’s value is converted to a string: strings are used as is, booleans become true or false,
and numbers and other JSON values use their compact JSON text. For example, the number 5 matches the key '5', as in the
second example below.
When no key matches and there’s no default, no branch runs. SWML reports a relay_script_method_no_matching_condition
warning, sets the script_error variable, and execution continues with the next method while the call is still active.
Variables
Set by the method:
- script_error: (out) Set when
switchreports a warning: nocasekey matches and there’s nodefault,variableisn’t a valid variable name after substitution, orcaseis missing. Holds the warning details as a JSON string, including the warning code (for example,relay_script_method_no_matching_condition) and a message. See Errors for the warning codes.
Examples
See Also
- Variables and Expressions: Complete reference for SWML variables, scopes, and using variables in conditional logic
- cond: Alternative conditional logic method