Repository navigation
FuncDSL: Add optional taskName parameter to switchWhenOrElse, forEach, and other control flow methods #1355
Description
Description
Several FuncDSL methods that create workflow tasks are missing the optional taskName parameter, forcing developers to either:
- Use auto-generated task names (like
switch-2,for-1) which are not descriptive - Rely on guessing task names when using the
.then("taskName")directive
This makes workflows harder to read and maintain, especially when navigating between tasks.
Affected Methods
The following FuncDSL methods should accept an optional taskName parameter:
1. switchWhenOrElse
Current signature:
public static <T> FuncTaskConfigurer switchWhenOrElse(
Predicate<T> condition,
String whenTaskName,
String orElseTaskName,
Class<T> inputClass)Missing: Name for the switch task itself
Issue: When using .then("nextTask") after a switch, you need to guess the auto-generated name like switch-2
Proposed signature:
public static <T> FuncTaskConfigurer switchWhenOrElse(
String taskName, // ← Add this
Predicate<T> condition,
String whenTaskName,
String orElseTaskName,
Class<T> inputClass)2. forEach (function-based variant)
Current signature:
public static <T> FuncTaskConfigurer forEach(
Function<T, Collection<?>> collection,
Consumer<FuncTaskItemListBuilder> body)Missing: Name for the for-loop task
Issue: Auto-generated names like for-1, for-2 are not descriptive
Proposed signature:
public static <T> FuncTaskConfigurer forEach(
String taskName, // ← Add this
Function<T, Collection<?>> collection,
Consumer<FuncTaskItemListBuilder> body)3. Other potentially affected methods
Similar issues may exist in:
switchvariants- Other control flow constructs that generate tasks
Example Impact
Current workflow (hard to navigate):
workflow("example")
.tasks(
function("getData", () -> loadData()),
// What's the name of this switch task? "switch-2"? "switch-1"?
switchWhenOrElse(
(Data data) -> data.isValid(),
"processValid",
"handleInvalid",
Data.class),
// Have to guess the switch task name here
consume("processValid", (Data data) -> process(data))
.then("switch-2"), // ← Guessing!
consume("handleInvalid", (Data data) -> handle(data))
)
.build();With proposed improvement:
workflow("example")
.tasks(
function("getData", () -> loadData()),
switchWhenOrElse(
"validateData", // ← Clear, descriptive name
(Data data) -> data.isValid(),
"processValid",
"handleInvalid",
Data.class),
consume("processValid", (Data data) -> process(data))
.then("validateData"), // ← No guessing!
consume("handleInvalid", (Data data) -> handle(data))
)
.build();Current Workaround
Use the builder-style API which requires more verbose code:
list -> list.switchTask("validateData", s -> s
.when((Data data) -> data.isValid(), "processValid")
.orElse("handleInvalid"))Requested Enhancement
Add optional taskName parameter as the first parameter to these methods, with overloaded versions for backward compatibility:
// New signature (preferred)
public static <T> FuncTaskConfigurer switchWhenOrElse(
String taskName,
Predicate<T> condition,
String whenTaskName,
String orElseTaskName,
Class<T> inputClass)
// Keep existing signature for backward compatibility
public static <T> FuncTaskConfigurer switchWhenOrElse(
Predicate<T> condition,
String whenTaskName,
String orElseTaskName,
Class<T> inputClass)Benefits
- Better readability: Descriptive task names make workflows self-documenting
- Easier navigation: No guessing task names when using
.then() - Better debugging: Clear task names in traces and logs
- Consistent API: Aligns with
function("name", ...)andconsume("name", ...)patterns
Files Affected
experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java
Activity
matheusandre1 commentedon Apr 30, 2026
after reviewing #1237,
can you assign me...
ricardozanini commentedon Apr 30, 2026
@matheusandre1 go ahead