Skip to main content
Version: ACE 5

Switch

Overview

Switch is a step which allows execution of other steps by comparing an expression with one or many case conditions, as well as a defaulting case when there are no matches.

This is useful for redirecting logic depending on the contents of the flow document, and the step is very similar to the switch statement found in most programming languages.

Execution

When the switch step is executed, it will try to match the expression to one of the defined case conditions. Once the first case is matched, it (meaning the steps contained within the case) will be executed. If no case is matched, the default case will be executed instead.

Compared to the conventional switch statement found in a programming language, switch step does not allow fall-through. Which means after a case is matched and executed, the switch step is complete.

It is also possible to nest switch steps multiple levels.

Configuration

  • switchExpression - the expression (e.g {{age}}) which will be matched to case conditions
  • cases - array of switch cases, each case contains a condition and also a steps array
    • condition - case condition (e.g {{35}}, thirty-five) which will be matched to the switchExpression
    • steps - array of steps to sequentially execute if the case is matched
  • defaultCase - array of steps to sequentially execute if no cases match, can be empty

Sample step configuration

Sample step
name: ProcessPolicyType
description: "Process two different policy types"
config:
switchExpression: "{{policyType}}"
stepType: switch
cases:
- condition: "life"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "life policy type selected"}'
targetPath: result
stepType: jsonata
- condition: "{{customPolicyType}}"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "custom policy type was selected"}'
targetPath: result
stepType: jsonata
defaultCase:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "no case matched the input"}'
targetPath: result
stepType: jsonata

Examples

Switch step used in flow to match categories to user messages

GetMessageByCategory.yaml
tags: []
sampleInputSchema: ""
sampleData: {}
description: ""
steps:
- name: SwitchBetweenCategories
description: Process supported categories
config:
switchExpression: "{{category}}"
stepType: switch
cases:
- condition: house
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "house was selected"}'
targetPath: result
stepType: jsonata
- condition: "{{customCategory}}"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "custom category was selected"}'
targetPath: result
stepType: jsonata
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '"sorry, this category is currently not available"'
targetPath: result.error
stepType: jsonata
defaultCase:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "none of the available categories were selected"}'
targetPath: result
stepType: jsonata

This flow is capable of taking two inputs from the document - category and customCategory. The category property represents the category selected by the user, and the customCategory property represents a category which we will handle, but return an error message that it is not available.

The switch statement in this flow is capable of handling three cases, case 1: house, case 2: the case provided by {{customCategory}}, and case 3: the default (fallback) case when nothing matches. Below are the sample input documents and expected results.

Document and result for case 1

Input document
{
"category": "house",
"customCategory": "pet"
}
Result document
{
"result": {
"message": "house was selected"
}
}

Document and result for case 2

Input document
{
"category": "pet",
"customCategory": "pet"
}
Result document
{
"result": {
"message": "custom category was selected",
"error": "sorry, this category is currently not available"
}
}

Document and result for default case

Input document
{
"category": "car",
"customCategory": "pet"
}
Result document
{
"result": {
"message": "none of the available categories were selected"
}
}