Skip to content

Default case in switchΒ #923

@matthias-pichler

Description

@matthias-pichler

What would you like to be added:

Currently the DSL states for the switch case that: when

A runtime expression used to determine whether or not the case matches.
If not set, the case will be matched by default if no other case match.
Note that there can be only one default case, all others MUST set a condition.

This wording implies that the default case does not follow ordering because it is only matched by default if no other case match

therefore these two would be equivalent:

document:
  dsl: 1.0.0-alpha1
  namespace: test
  name: sample-workflow
  version: 0.1.0
do:
  - processOrder:
      switch:
        - case1:
            when: .orderType == "electronic"
            then: processElectronicOrder
        - case2:
            when: .orderType == "physical"
            then: processPhysicalOrder
        - default:
            then: handleUnknownOrderType

and

document:
  dsl: 1.0.0-alpha1
  namespace: test
  name: sample-workflow
  version: 0.1.0
do:
  - processOrder:
      switch:
        - case1:
            when: .orderType == "electronic"
            then: processElectronicOrder
        - default:
            then: handleUnknownOrderType
        - case2:
            when: .orderType == "physical"
            then: processPhysicalOrder

I think this is a bit ambiguous and would change the wording to something like:

If when is not specified it always matches.

This would then mean that the only logical place to put a default case would be at the end. Interested in what you folks think.

Note that there can be only one default case, all others MUST set a condition.

This is currently not validated by the schema. I think it could be It is doable with contains. This schema ensures only one case has no when

type: object
required: [ switch ]
unevaluatedProperties: false
properties:
  switch:
    type: array
    minItems: 1
    contains:
      type: object
      minProperties: 1
      maxProperties: 1
      additionalProperties:
        type: object
        properties:
          when: false
    minContains: 0
    maxContains: 1
    items:
      type: object
      minProperties: 1
      maxProperties: 1
      title: SwitchItem
      additionalProperties:
        type: object
        title: SwitchCase
        required: [ then ]
        properties:
          when:
            type: string
            description: A runtime expression used to determine whether or not the case matches.
          then:
            $ref: '#/$defs/flowDirective'
            description: The flow directive to execute when the case matches.

The DSL does not specify what happens when no case matches. Does it fall through to the next task? (I don't think this should be the intended behavior) Or which error is raised?

And my last question: would it make sense to enforce that the default case is named default?

Why is this needed:

Metadata

Metadata

Labels

area: specChanges in the Specificationchange: documentationImprovements or additions to documentation. It won't impact a version change.change: fixSomething isn't working. Impacts in a minor version change.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions