Skip to content

BRANCH Step ​

The BRANCH step enables conditional logic in your flows. It allows the ESB to choose different execution paths based on the values currently in the pipeline.

Properties ​

PropertyTypeDescription
typestringMust be branch.
switchstring(Optional) The pipeline variable to evaluate for Switch Mode.
casesobjectA map where keys are values or conditions, and values are arrays of steps.

Mode 1: Switch Mode (Value Matching) ​

Use Switch Mode when you want to compare a single variable against multiple possible values. This is equivalent to a switch or match statement in code.

  • Specify the variable in the switch property (e.g., switch: "${status}").
  • In cases, the keys are the literal values to match.
yaml
- type: "branch"
  switch: "${paymentMethod}"
  cases:
    "credit_card":
      - type: "invoke"
        service: "payments:ProcessCreditCard"
    "paypal":
      - type: "invoke"
        service: "payments:ProcessPaypal"
    "$default":
      - type: "exit"
        message: "Unsupported payment method"

Mode 2: Expression Mode (Condition Matching) ​

Use Expression Mode for more complex logic involving multiple variables or numeric comparisons.

  • Leave the switch property empty.
  • In cases, the keys are conditional expressions.
  • The engine evaluates cases sequentially. The first case that evaluates to true is executed.

Supported Operators ​

  • Equality: ==, !=
  • Comparison: >, <, >=, <=

Example: Scoring Logic ​

yaml
- type: "branch"
  cases:
    "${score} >= 90":
      - type: "map"
        set:
          grade: "A"
    "${score} >= 80":
      - type: "map"
        set:
          grade: "B"
    "$default":
      - type: "map"
        set:
          grade: "F"

The $default Case ​

Both modes support a special $default case. This case is executed only if no other cases match. It is highly recommended to include a $default case to handle unexpected data.

Example: Expression Branching ​

Step Definition ​

yaml
- name: "EvaluateScore"
  type: "branch"
  cases:
    "${score} >= 90":
      - type: "map"
        set:
          grade: "Excellent"
    "${score} >= 60":
      - type: "map"
        set:
          grade: "Pass"
    "$default":
      - type: "map"
        set:
          grade: "Fail"

Data Flow Scenario: Passing Score ​

Initial Pipeline:

json
{
  "student": "Alice",
  "score": 85
}

Resulting Pipeline:

json
{
  "student": "Alice",
  "score": 85,
  "grade": "Pass"
}

Data Flow Scenario: Default Case ​

Initial Pipeline:

json
{
  "student": "Bob",
  "score": 45
}

Resulting Pipeline:

json
{
  "student": "Bob",
  "score": 45,
  "grade": "Fail"
}

Tips ​

  • Switch for Clarity: If you are just checking one variable, Switch Mode is cleaner and more readable.
  • Order Matters: In Expression Mode, the order of cases is critical. Put the most specific conditions first.
  • Empty Branch: If no case matches and there is no $default, the branch step simply does nothing and moves to the next step in the flow.

Released under the ISC License.