Skip to content

Commit be736e3

Browse files
feat: optional taskName parameter to switchWhenOrElse, forEach, and o… (#1362)
* feat: optional taskName parameter to switchWhenOrElse, forEach, and other control flow methods Signed-off-by: Matheus André <matheusandr2@gmail.com> * Revert ForExecutor changes to previous implementation Signed-off-by: Matheus André <matheusandr2@gmail.com> --------- Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent bb7586b commit be736e3

2 files changed

Lines changed: 481 additions & 11 deletions

File tree

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java

Lines changed: 184 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,22 @@ public static FuncTaskConfigurer tryCatch(String name, Consumer<FuncTryTaskBuild
10981098
*/
10991099
public static <T> FuncTaskConfigurer switchWhen(
11001100
Predicate<T> pred, String thenTask, Class<T> predClass) {
1101-
return list -> list.switchCase(cases(caseOf(pred, predClass).then(thenTask)));
1101+
return switchWhen(null, pred, thenTask, predClass);
1102+
}
1103+
1104+
/**
1105+
* Named variant of {@link #switchWhen(Predicate, String, Class)}.
1106+
*
1107+
* @param taskName task name for the switch task
1108+
* @param pred predicate
1109+
* @param thenTask task name when predicate is true
1110+
* @param predClass predicate class
1111+
* @param <T> predicate input type
1112+
* @return list configurer
1113+
*/
1114+
public static <T> FuncTaskConfigurer switchWhen(
1115+
String taskName, Predicate<T> pred, String thenTask, Class<T> predClass) {
1116+
return list -> list.switchCase(taskName, cases(caseOf(pred, predClass).then(thenTask)));
11021117
}
11031118

11041119
/**
@@ -1117,7 +1132,20 @@ public static <T> FuncTaskConfigurer switchWhen(
11171132
* @return list configurer
11181133
*/
11191134
public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask) {
1120-
return list -> list.switchCase(sw -> sw.on(c -> c.when(jqExpression).then(thenTask)));
1135+
return switchWhen(null, jqExpression, thenTask);
1136+
}
1137+
1138+
/**
1139+
* Named variant of {@link #switchWhen(String, String)}.
1140+
*
1141+
* @param taskName task name for the switch task
1142+
* @param jqExpression JQ expression evaluated against the current task input
1143+
* @param thenTask task name to jump to when the expression evaluates truthy
1144+
* @return list configurer
1145+
*/
1146+
public static FuncTaskConfigurer switchWhen(
1147+
String taskName, String jqExpression, String thenTask) {
1148+
return list -> list.switchCase(taskName, sw -> sw.on(c -> c.when(jqExpression).then(thenTask)));
11211149
}
11221150

11231151
/**
@@ -1132,14 +1160,44 @@ public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask
11321160
*/
11331161
public static <T> FuncTaskConfigurer switchWhenOrElse(
11341162
Predicate<T> pred, String thenTask, FlowDirectiveEnum otherwise, Class<T> predClass) {
1163+
return switchWhenOrElse(null, pred, thenTask, otherwise, predClass);
1164+
}
1165+
1166+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1167+
SerializablePredicate<T> pred, String thenTask, FlowDirectiveEnum otherwise) {
1168+
return switchWhenOrElse(null, pred, thenTask, otherwise);
1169+
}
1170+
1171+
/**
1172+
* Named variant of {@link #switchWhenOrElse(Predicate, String, FlowDirectiveEnum, Class)}.
1173+
*
1174+
* @param taskName task name for the switch task
1175+
* @param pred predicate
1176+
* @param thenTask task name when predicate is true
1177+
* @param otherwise default flow directive when predicate is false
1178+
* @param predClass predicate class
1179+
* @param <T> predicate input type
1180+
* @return list configurer
1181+
*/
1182+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1183+
String taskName,
1184+
Predicate<T> pred,
1185+
String thenTask,
1186+
FlowDirectiveEnum otherwise,
1187+
Class<T> predClass) {
11351188
return list ->
11361189
list.switchCase(
1190+
taskName,
11371191
FuncDSL.cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwise)));
11381192
}
11391193

11401194
public static <T> FuncTaskConfigurer switchWhenOrElse(
1141-
SerializablePredicate<T> pred, String thenTask, FlowDirectiveEnum otherwise) {
1142-
return switchWhenOrElse(pred, thenTask, otherwise, ReflectionUtils.inferInputType(pred));
1195+
String taskName,
1196+
SerializablePredicate<T> pred,
1197+
String thenTask,
1198+
FlowDirectiveEnum otherwise) {
1199+
return switchWhenOrElse(
1200+
taskName, pred, thenTask, otherwise, ReflectionUtils.inferInputType(pred));
11431201
}
11441202

11451203
/**
@@ -1154,13 +1212,40 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
11541212
*/
11551213
public static <T> FuncTaskConfigurer switchWhenOrElse(
11561214
Predicate<T> pred, String thenTask, String otherwiseTask, Class<T> predClass) {
1157-
return list ->
1158-
list.switchCase(cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwiseTask)));
1215+
return switchWhenOrElse(null, pred, thenTask, otherwiseTask, predClass);
11591216
}
11601217

11611218
public static <T> FuncTaskConfigurer switchWhenOrElse(
11621219
SerializablePredicate<T> pred, String thenTask, String otherwiseTask) {
1163-
return switchWhenOrElse(pred, thenTask, otherwiseTask, ReflectionUtils.inferInputType(pred));
1220+
return switchWhenOrElse(null, pred, thenTask, otherwiseTask);
1221+
}
1222+
1223+
/**
1224+
* Named variant of {@link #switchWhenOrElse(Predicate, String, String, Class)}.
1225+
*
1226+
* @param taskName task name for the switch task
1227+
* @param pred predicate
1228+
* @param thenTask task name when predicate is true
1229+
* @param otherwiseTask task name when predicate is false
1230+
* @param predClass predicate class
1231+
* @param <T> predicate input type
1232+
* @return list configurer
1233+
*/
1234+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1235+
String taskName,
1236+
Predicate<T> pred,
1237+
String thenTask,
1238+
String otherwiseTask,
1239+
Class<T> predClass) {
1240+
return list ->
1241+
list.switchCase(
1242+
taskName, cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwiseTask)));
1243+
}
1244+
1245+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1246+
String taskName, SerializablePredicate<T> pred, String thenTask, String otherwiseTask) {
1247+
return switchWhenOrElse(
1248+
taskName, pred, thenTask, otherwiseTask, ReflectionUtils.inferInputType(pred));
11641249
}
11651250

11661251
/**
@@ -1180,13 +1265,28 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
11801265
*/
11811266
public static FuncTaskConfigurer switchWhenOrElse(
11821267
String jqExpression, String thenTask, FlowDirectiveEnum otherwise) {
1268+
return switchWhenOrElse(null, jqExpression, thenTask, otherwise);
1269+
}
1270+
1271+
/**
1272+
* Named variant of {@link #switchWhenOrElse(String, String, FlowDirectiveEnum)}.
1273+
*
1274+
* @param taskName task name for the switch task
1275+
* @param jqExpression JQ expression evaluated against the current task input
1276+
* @param thenTask task to jump to if the expression evaluates truthy
1277+
* @param otherwise default flow directive when the expression is falsy
1278+
* @return list configurer
1279+
*/
1280+
public static FuncTaskConfigurer switchWhenOrElse(
1281+
String taskName, String jqExpression, String thenTask, FlowDirectiveEnum otherwise) {
11831282

11841283
Objects.requireNonNull(jqExpression, "jqExpression");
11851284
Objects.requireNonNull(thenTask, "thenTask");
11861285
Objects.requireNonNull(otherwise, "otherwise");
11871286

11881287
return list ->
1189-
list.switchCase(sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwise));
1288+
list.switchCase(
1289+
taskName, sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwise));
11901290
}
11911291

11921292
/**
@@ -1206,13 +1306,28 @@ public static FuncTaskConfigurer switchWhenOrElse(
12061306
*/
12071307
public static FuncTaskConfigurer switchWhenOrElse(
12081308
String jqExpression, String thenTask, String otherwiseTask) {
1309+
return switchWhenOrElse(null, jqExpression, thenTask, otherwiseTask);
1310+
}
1311+
1312+
/**
1313+
* Named variant of {@link #switchWhenOrElse(String, String, String)}.
1314+
*
1315+
* @param taskName task name for the switch task
1316+
* @param jqExpression JQ expression evaluated against the current task input
1317+
* @param thenTask task name when truthy
1318+
* @param otherwiseTask task name when falsy
1319+
* @return list configurer
1320+
*/
1321+
public static FuncTaskConfigurer switchWhenOrElse(
1322+
String taskName, String jqExpression, String thenTask, String otherwiseTask) {
12091323

12101324
Objects.requireNonNull(jqExpression, "jqExpression");
12111325
Objects.requireNonNull(thenTask, "thenTask");
12121326
Objects.requireNonNull(otherwiseTask, "otherwiseTask");
12131327

12141328
return list ->
12151329
list.switchCase(
1330+
taskName,
12161331
sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwiseTask));
12171332
}
12181333

@@ -1226,23 +1341,53 @@ public static FuncTaskConfigurer switchWhenOrElse(
12261341
*/
12271342
public static <T, V> FuncTaskConfigurer forEach(
12281343
SerializableFunction<T, Collection<V>> collection, Consumer<FuncTaskItemListBuilder> body) {
1344+
return forEach(null, collection, body);
1345+
}
1346+
1347+
/**
1348+
* Named variant of {@link #forEach(SerializableFunction, Consumer)}.
1349+
*
1350+
* @param taskName task name for the for-loop task
1351+
* @param collection function that returns the collection to iterate
1352+
* @param body inner task list body
1353+
* @param <T> input type for the collection function
1354+
* @return list configurer
1355+
*/
1356+
public static <T, V> FuncTaskConfigurer forEach(
1357+
String taskName,
1358+
SerializableFunction<T, Collection<V>> collection,
1359+
Consumer<FuncTaskItemListBuilder> body) {
12291360
return list ->
12301361
list.forEach(
1362+
taskName,
12311363
j -> j.collection(collection, ReflectionUtils.inferInputType(collection)).tasks(body));
12321364
}
12331365

12341366
public static <T, V> FuncTaskConfigurer forEach(
12351367
SerializableFunction<T, Collection<V>> collection, LoopFunction<T, V, ?> function) {
1368+
return forEach(null, collection, function);
1369+
}
1370+
1371+
public static <T, V> FuncTaskConfigurer forEach(
1372+
String taskName,
1373+
SerializableFunction<T, Collection<V>> collection,
1374+
LoopFunction<T, V, ?> function) {
12361375
return list ->
12371376
list.forEach(
1377+
taskName,
12381378
j ->
12391379
j.collection(collection, ReflectionUtils.inferInputType(collection))
12401380
.tasks(function));
12411381
}
12421382

12431383
public static <T, V> FuncTaskConfigurer forEachItem(
12441384
SerializableFunction<T, Collection<V>> collection, Function<V, ?> function) {
1245-
return forEach(collection, ((t, v) -> function.apply((V) v)));
1385+
return forEachItem(null, collection, function);
1386+
}
1387+
1388+
public static <T, V> FuncTaskConfigurer forEachItem(
1389+
String taskName, SerializableFunction<T, Collection<V>> collection, Function<V, ?> function) {
1390+
return forEach(taskName, collection, ((t, v) -> function.apply((V) v)));
12461391
}
12471392

12481393
/**
@@ -1255,8 +1400,22 @@ public static <T, V> FuncTaskConfigurer forEachItem(
12551400
*/
12561401
public static <T, V> FuncTaskConfigurer forEach(
12571402
Collection<V> collection, Consumer<FuncTaskItemListBuilder> body) {
1403+
return forEach(null, collection, body);
1404+
}
1405+
1406+
/**
1407+
* Named variant of {@link #forEach(Collection, Consumer)}.
1408+
*
1409+
* @param taskName task name for the for-loop task
1410+
* @param collection static collection to iterate
1411+
* @param body inner task list body
1412+
* @param <T> ignored (kept for signature consistency)
1413+
* @return list configurer
1414+
*/
1415+
public static <T, V> FuncTaskConfigurer forEach(
1416+
String taskName, Collection<V> collection, Consumer<FuncTaskItemListBuilder> body) {
12581417
Function<T, Collection<V>> f = ctx -> collection;
1259-
return list -> list.forEach(j -> j.collection(f).tasks(body));
1418+
return list -> list.forEach(taskName, j -> j.collection(f).tasks(body));
12601419
}
12611420

12621421
/**
@@ -1269,7 +1428,21 @@ public static <T, V> FuncTaskConfigurer forEach(
12691428
*/
12701429
public static <T> FuncTaskConfigurer forEach(
12711430
List<T> collection, Consumer<FuncTaskItemListBuilder> body) {
1272-
return list -> list.forEach(j -> j.collection(ctx -> collection).tasks(body));
1431+
return forEach(null, collection, body);
1432+
}
1433+
1434+
/**
1435+
* Named variant of {@link #forEach(List, Consumer)}.
1436+
*
1437+
* @param taskName task name for the for-loop task
1438+
* @param collection list to iterate
1439+
* @param body inner task list body
1440+
* @param <T> element type
1441+
* @return list configurer
1442+
*/
1443+
public static <T> FuncTaskConfigurer forEach(
1444+
String taskName, List<T> collection, Consumer<FuncTaskItemListBuilder> body) {
1445+
return list -> list.forEach(taskName, j -> j.collection(ctx -> collection).tasks(body));
12731446
}
12741447

12751448
/**

0 commit comments

Comments
 (0)