Skip to content

Commit 6eae99a

Browse files
committed
feat: optional taskName parameter to switchWhenOrElse, forEach, and other control flow methods
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent ecc28e8 commit 6eae99a

3 files changed

Lines changed: 491 additions & 16 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
@@ -942,7 +942,22 @@ public static FuncTaskConfigurer tryCatch(String name, Consumer<FuncTryTaskBuild
942942
*/
943943
public static <T> FuncTaskConfigurer switchWhen(
944944
Predicate<T> pred, String thenTask, Class<T> predClass) {
945-
return list -> list.switchCase(cases(caseOf(pred, predClass).then(thenTask)));
945+
return switchWhen(null, pred, thenTask, predClass);
946+
}
947+
948+
/**
949+
* Named variant of {@link #switchWhen(Predicate, String, Class)}.
950+
*
951+
* @param taskName task name for the switch task
952+
* @param pred predicate
953+
* @param thenTask task name when predicate is true
954+
* @param predClass predicate class
955+
* @param <T> predicate input type
956+
* @return list configurer
957+
*/
958+
public static <T> FuncTaskConfigurer switchWhen(
959+
String taskName, Predicate<T> pred, String thenTask, Class<T> predClass) {
960+
return list -> list.switchCase(taskName, cases(caseOf(pred, predClass).then(thenTask)));
946961
}
947962

948963
/**
@@ -961,7 +976,20 @@ public static <T> FuncTaskConfigurer switchWhen(
961976
* @return list configurer
962977
*/
963978
public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask) {
964-
return list -> list.switchCase(sw -> sw.on(c -> c.when(jqExpression).then(thenTask)));
979+
return switchWhen(null, jqExpression, thenTask);
980+
}
981+
982+
/**
983+
* Named variant of {@link #switchWhen(String, String)}.
984+
*
985+
* @param taskName task name for the switch task
986+
* @param jqExpression JQ expression evaluated against the current task input
987+
* @param thenTask task name to jump to when the expression evaluates truthy
988+
* @return list configurer
989+
*/
990+
public static FuncTaskConfigurer switchWhen(
991+
String taskName, String jqExpression, String thenTask) {
992+
return list -> list.switchCase(taskName, sw -> sw.on(c -> c.when(jqExpression).then(thenTask)));
965993
}
966994

967995
/**
@@ -976,14 +1004,44 @@ public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask
9761004
*/
9771005
public static <T> FuncTaskConfigurer switchWhenOrElse(
9781006
Predicate<T> pred, String thenTask, FlowDirectiveEnum otherwise, Class<T> predClass) {
1007+
return switchWhenOrElse(null, pred, thenTask, otherwise, predClass);
1008+
}
1009+
1010+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1011+
SerializablePredicate<T> pred, String thenTask, FlowDirectiveEnum otherwise) {
1012+
return switchWhenOrElse(null, pred, thenTask, otherwise);
1013+
}
1014+
1015+
/**
1016+
* Named variant of {@link #switchWhenOrElse(Predicate, String, FlowDirectiveEnum, Class)}.
1017+
*
1018+
* @param taskName task name for the switch task
1019+
* @param pred predicate
1020+
* @param thenTask task name when predicate is true
1021+
* @param otherwise default flow directive when predicate is false
1022+
* @param predClass predicate class
1023+
* @param <T> predicate input type
1024+
* @return list configurer
1025+
*/
1026+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1027+
String taskName,
1028+
Predicate<T> pred,
1029+
String thenTask,
1030+
FlowDirectiveEnum otherwise,
1031+
Class<T> predClass) {
9791032
return list ->
9801033
list.switchCase(
1034+
taskName,
9811035
FuncDSL.cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwise)));
9821036
}
9831037

9841038
public static <T> FuncTaskConfigurer switchWhenOrElse(
985-
SerializablePredicate<T> pred, String thenTask, FlowDirectiveEnum otherwise) {
986-
return switchWhenOrElse(pred, thenTask, otherwise, ReflectionUtils.inferInputType(pred));
1039+
String taskName,
1040+
SerializablePredicate<T> pred,
1041+
String thenTask,
1042+
FlowDirectiveEnum otherwise) {
1043+
return switchWhenOrElse(
1044+
taskName, pred, thenTask, otherwise, ReflectionUtils.inferInputType(pred));
9871045
}
9881046

9891047
/**
@@ -998,13 +1056,40 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
9981056
*/
9991057
public static <T> FuncTaskConfigurer switchWhenOrElse(
10001058
Predicate<T> pred, String thenTask, String otherwiseTask, Class<T> predClass) {
1001-
return list ->
1002-
list.switchCase(cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwiseTask)));
1059+
return switchWhenOrElse(null, pred, thenTask, otherwiseTask, predClass);
10031060
}
10041061

10051062
public static <T> FuncTaskConfigurer switchWhenOrElse(
10061063
SerializablePredicate<T> pred, String thenTask, String otherwiseTask) {
1007-
return switchWhenOrElse(pred, thenTask, otherwiseTask, ReflectionUtils.inferInputType(pred));
1064+
return switchWhenOrElse(null, pred, thenTask, otherwiseTask);
1065+
}
1066+
1067+
/**
1068+
* Named variant of {@link #switchWhenOrElse(Predicate, String, String, Class)}.
1069+
*
1070+
* @param taskName task name for the switch task
1071+
* @param pred predicate
1072+
* @param thenTask task name when predicate is true
1073+
* @param otherwiseTask task name when predicate is false
1074+
* @param predClass predicate class
1075+
* @param <T> predicate input type
1076+
* @return list configurer
1077+
*/
1078+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1079+
String taskName,
1080+
Predicate<T> pred,
1081+
String thenTask,
1082+
String otherwiseTask,
1083+
Class<T> predClass) {
1084+
return list ->
1085+
list.switchCase(
1086+
taskName, cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwiseTask)));
1087+
}
1088+
1089+
public static <T> FuncTaskConfigurer switchWhenOrElse(
1090+
String taskName, SerializablePredicate<T> pred, String thenTask, String otherwiseTask) {
1091+
return switchWhenOrElse(
1092+
taskName, pred, thenTask, otherwiseTask, ReflectionUtils.inferInputType(pred));
10081093
}
10091094

10101095
/**
@@ -1024,13 +1109,28 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
10241109
*/
10251110
public static FuncTaskConfigurer switchWhenOrElse(
10261111
String jqExpression, String thenTask, FlowDirectiveEnum otherwise) {
1112+
return switchWhenOrElse(null, jqExpression, thenTask, otherwise);
1113+
}
1114+
1115+
/**
1116+
* Named variant of {@link #switchWhenOrElse(String, String, FlowDirectiveEnum)}.
1117+
*
1118+
* @param taskName task name for the switch task
1119+
* @param jqExpression JQ expression evaluated against the current task input
1120+
* @param thenTask task to jump to if the expression evaluates truthy
1121+
* @param otherwise default flow directive when the expression is falsy
1122+
* @return list configurer
1123+
*/
1124+
public static FuncTaskConfigurer switchWhenOrElse(
1125+
String taskName, String jqExpression, String thenTask, FlowDirectiveEnum otherwise) {
10271126

10281127
Objects.requireNonNull(jqExpression, "jqExpression");
10291128
Objects.requireNonNull(thenTask, "thenTask");
10301129
Objects.requireNonNull(otherwise, "otherwise");
10311130

10321131
return list ->
1033-
list.switchCase(sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwise));
1132+
list.switchCase(
1133+
taskName, sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwise));
10341134
}
10351135

10361136
/**
@@ -1050,13 +1150,28 @@ public static FuncTaskConfigurer switchWhenOrElse(
10501150
*/
10511151
public static FuncTaskConfigurer switchWhenOrElse(
10521152
String jqExpression, String thenTask, String otherwiseTask) {
1153+
return switchWhenOrElse(null, jqExpression, thenTask, otherwiseTask);
1154+
}
1155+
1156+
/**
1157+
* Named variant of {@link #switchWhenOrElse(String, String, String)}.
1158+
*
1159+
* @param taskName task name for the switch task
1160+
* @param jqExpression JQ expression evaluated against the current task input
1161+
* @param thenTask task name when truthy
1162+
* @param otherwiseTask task name when falsy
1163+
* @return list configurer
1164+
*/
1165+
public static FuncTaskConfigurer switchWhenOrElse(
1166+
String taskName, String jqExpression, String thenTask, String otherwiseTask) {
10531167

10541168
Objects.requireNonNull(jqExpression, "jqExpression");
10551169
Objects.requireNonNull(thenTask, "thenTask");
10561170
Objects.requireNonNull(otherwiseTask, "otherwiseTask");
10571171

10581172
return list ->
10591173
list.switchCase(
1174+
taskName,
10601175
sw -> sw.on(c -> c.when(jqExpression).then(thenTask)).onDefault(otherwiseTask));
10611176
}
10621177

@@ -1070,23 +1185,53 @@ public static FuncTaskConfigurer switchWhenOrElse(
10701185
*/
10711186
public static <T, V> FuncTaskConfigurer forEach(
10721187
SerializableFunction<T, Collection<V>> collection, Consumer<FuncTaskItemListBuilder> body) {
1188+
return forEach(null, collection, body);
1189+
}
1190+
1191+
/**
1192+
* Named variant of {@link #forEach(SerializableFunction, Consumer)}.
1193+
*
1194+
* @param taskName task name for the for-loop task
1195+
* @param collection function that returns the collection to iterate
1196+
* @param body inner task list body
1197+
* @param <T> input type for the collection function
1198+
* @return list configurer
1199+
*/
1200+
public static <T, V> FuncTaskConfigurer forEach(
1201+
String taskName,
1202+
SerializableFunction<T, Collection<V>> collection,
1203+
Consumer<FuncTaskItemListBuilder> body) {
10731204
return list ->
10741205
list.forEach(
1206+
taskName,
10751207
j -> j.collection(collection, ReflectionUtils.inferInputType(collection)).tasks(body));
10761208
}
10771209

10781210
public static <T, V> FuncTaskConfigurer forEach(
10791211
SerializableFunction<T, Collection<V>> collection, LoopFunction<T, V, ?> function) {
1212+
return forEach(null, collection, function);
1213+
}
1214+
1215+
public static <T, V> FuncTaskConfigurer forEach(
1216+
String taskName,
1217+
SerializableFunction<T, Collection<V>> collection,
1218+
LoopFunction<T, V, ?> function) {
10801219
return list ->
10811220
list.forEach(
1221+
taskName,
10821222
j ->
10831223
j.collection(collection, ReflectionUtils.inferInputType(collection))
10841224
.tasks(function));
10851225
}
10861226

10871227
public static <T, V> FuncTaskConfigurer forEachItem(
10881228
SerializableFunction<T, Collection<V>> collection, Function<V, ?> function) {
1089-
return forEach(collection, ((t, v) -> function.apply((V) v)));
1229+
return forEachItem(null, collection, function);
1230+
}
1231+
1232+
public static <T, V> FuncTaskConfigurer forEachItem(
1233+
String taskName, SerializableFunction<T, Collection<V>> collection, Function<V, ?> function) {
1234+
return forEach(taskName, collection, ((t, v) -> function.apply((V) v)));
10901235
}
10911236

10921237
/**
@@ -1099,8 +1244,22 @@ public static <T, V> FuncTaskConfigurer forEachItem(
10991244
*/
11001245
public static <T, V> FuncTaskConfigurer forEach(
11011246
Collection<V> collection, Consumer<FuncTaskItemListBuilder> body) {
1247+
return forEach(null, collection, body);
1248+
}
1249+
1250+
/**
1251+
* Named variant of {@link #forEach(Collection, Consumer)}.
1252+
*
1253+
* @param taskName task name for the for-loop task
1254+
* @param collection static collection to iterate
1255+
* @param body inner task list body
1256+
* @param <T> ignored (kept for signature consistency)
1257+
* @return list configurer
1258+
*/
1259+
public static <T, V> FuncTaskConfigurer forEach(
1260+
String taskName, Collection<V> collection, Consumer<FuncTaskItemListBuilder> body) {
11021261
Function<T, Collection<V>> f = ctx -> collection;
1103-
return list -> list.forEach(j -> j.collection(f).tasks(body));
1262+
return list -> list.forEach(taskName, j -> j.collection(f).tasks(body));
11041263
}
11051264

11061265
/**
@@ -1113,7 +1272,21 @@ public static <T, V> FuncTaskConfigurer forEach(
11131272
*/
11141273
public static <T> FuncTaskConfigurer forEach(
11151274
List<T> collection, Consumer<FuncTaskItemListBuilder> body) {
1116-
return list -> list.forEach(j -> j.collection(ctx -> collection).tasks(body));
1275+
return forEach(null, collection, body);
1276+
}
1277+
1278+
/**
1279+
* Named variant of {@link #forEach(List, Consumer)}.
1280+
*
1281+
* @param taskName task name for the for-loop task
1282+
* @param collection list to iterate
1283+
* @param body inner task list body
1284+
* @param <T> element type
1285+
* @return list configurer
1286+
*/
1287+
public static <T> FuncTaskConfigurer forEach(
1288+
String taskName, List<T> collection, Consumer<FuncTaskItemListBuilder> body) {
1289+
return list -> list.forEach(taskName, j -> j.collection(ctx -> collection).tasks(body));
11171290
}
11181291

11191292
/**

0 commit comments

Comments
 (0)