switch/case:
The SwitchCase node is designed to facilitate decision-making processes within a workflow by implementing a switch/case selection mechanism. This node allows you to choose from multiple options based on a computed index, known as the selector. By providing a selector input, which is an integer, and multiple case value inputs, the node returns the value corresponding to the provided index. If the index is out of range, a default value is returned, ensuring that the workflow can handle unexpected or undefined cases gracefully. This functionality is particularly useful in scenarios where you need to dynamically select between different paths or outcomes based on varying conditions, enhancing the flexibility and robustness of your workflow.
switch/case Input Parameters:
select
The select parameter is an integer that determines which case value will be selected and returned by the node. It acts as an index to choose among the provided case values. The default value for select is 0, and it must be a non-negative integer. The value of select directly impacts the node's execution, as it dictates which case value is chosen. If the select value is within the range of available case indices, the corresponding case value is returned. If it is out of range, the node will attempt to return the default value if provided.
case_0
The case_0 parameter represents the first case value in the list of options. It can be of any data type, allowing for flexibility in the types of values you can handle. This parameter is marked as lazy, meaning it is only evaluated when needed, which can optimize performance in certain scenarios. The case_0 parameter is crucial as it serves as the initial option in the selection process, and its presence is required for the node to function correctly.
default
The default parameter is an optional input that provides a fallback value if the select index is out of range of the available case values. Like case_0, it can be of any data type and is also evaluated lazily. The default parameter ensures that the node can handle cases where the select index does not correspond to any defined case, thereby preventing errors and maintaining the continuity of the workflow.
switch/case Output Parameters:
result
The result parameter is the output of the SwitchCase node, representing the selected case value based on the select index. If the select index is valid and corresponds to a defined case value, the result will be that case value. If the select index is out of range, the result will be the default value if provided, or None if no default is specified. This output is crucial as it determines the path or outcome of the workflow based on the selection logic implemented by the node.
switch/case Usage Tips:
- Ensure that the
selectparameter is within the range of defined case indices to avoid unexpected results and to ensure the correct case value is returned. - Always provide a
defaultvalue to handle cases where theselectindex is out of range, which can prevent errors and ensure the workflow continues smoothly. - Utilize the lazy evaluation feature of the
case_0anddefaultparameters to optimize performance, especially in complex workflows where not all cases need to be evaluated.
switch/case Common Errors and Solutions:
Selector Out of Range
- Explanation: This error occurs when the
selectindex is greater than the number of defined case values. - Solution: Ensure that the
selectindex is within the range of available case indices or provide adefaultvalue to handle out-of-range selections.
Missing Default Value
- Explanation: If the
selectindex is out of range and nodefaultvalue is provided, the node will returnNone. - Solution: Always provide a
defaultvalue to ensure that the node can return a meaningful result even when theselectindex is out of range.
