Skip to content

Commit dccb303

Browse files
committed
Add Class<T> args to switch predicates
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 13eda2a commit dccb303

File tree

2 files changed

+12
-56
lines changed
  • experimental/fluent/func/src

2 files changed

+12
-56
lines changed

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

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,13 @@ public static FuncTaskConfigurer switchCase(String taskName, SwitchCaseConfigure
623623
*
624624
* @param pred predicate
625625
* @param thenTask task name when predicate is true
626+
* @param predClass predicate class
626627
* @param <T> predicate input type
627628
* @return list configurer
628629
*/
629-
public static <T> FuncTaskConfigurer switchWhen(Predicate<T> pred, String thenTask) {
630-
return list -> list.switchCase(cases(caseOf(pred).then(thenTask)));
630+
public static <T> FuncTaskConfigurer switchWhen(
631+
Predicate<T> pred, String thenTask, Class<T> predClass) {
632+
return list -> list.switchCase(cases(caseOf(pred, predClass).then(thenTask)));
631633
}
632634

633635
public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask) {
@@ -640,13 +642,15 @@ public static FuncTaskConfigurer switchWhen(String jqExpression, String thenTask
640642
* @param pred predicate
641643
* @param thenTask task name when predicate is true
642644
* @param otherwise default flow directive when predicate is false
645+
* @param predClass predicate class
643646
* @param <T> predicate input type
644647
* @return list configurer
645648
*/
646649
public static <T> FuncTaskConfigurer switchWhenOrElse(
647-
Predicate<T> pred, String thenTask, FlowDirectiveEnum otherwise) {
650+
Predicate<T> pred, String thenTask, FlowDirectiveEnum otherwise, Class<T> predClass) {
648651
return list ->
649-
list.switchCase(FuncDSL.cases(caseOf(pred).then(thenTask), caseDefault(otherwise)));
652+
list.switchCase(
653+
FuncDSL.cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwise)));
650654
}
651655

652656
/**
@@ -655,12 +659,14 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
655659
* @param pred predicate
656660
* @param thenTask task name when predicate is true
657661
* @param otherwiseTask task name when predicate is false
662+
* @param predClass predicate class
658663
* @param <T> predicate input type
659664
* @return list configurer
660665
*/
661666
public static <T> FuncTaskConfigurer switchWhenOrElse(
662-
Predicate<T> pred, String thenTask, String otherwiseTask) {
663-
return list -> list.switchCase(cases(caseOf(pred).then(thenTask), caseDefault(otherwiseTask)));
667+
Predicate<T> pred, String thenTask, String otherwiseTask, Class<T> predClass) {
668+
return list ->
669+
list.switchCase(cases(caseOf(pred, predClass).then(thenTask), caseDefault(otherwiseTask)));
664670
}
665671

666672
/**
@@ -765,35 +771,4 @@ public static FuncTaskConfigurer set(String expr) {
765771
public static FuncTaskConfigurer set(Map<String, Object> map) {
766772
return list -> list.set(s -> s.expr(map));
767773
}
768-
769-
// ----- JQ helpers for outputAs / inputFrom ----- //
770-
771-
/**
772-
* JQ: pick the first element if the value is an array, otherwise return the value as-is. Does not
773-
* stringify. Useful when downstream expects non-string JSON.
774-
*
775-
* <pre>(first(.[]? // empty) // .)</pre>
776-
*/
777-
public static String selectFirst() {
778-
return "(first(.[]? // empty) // .)";
779-
}
780-
781-
/**
782-
* JQ: stringify the value only if it is not already a string.
783-
*
784-
* <pre>(if type==\"string\" then . else tostring end)</pre>
785-
*/
786-
public static String stringifyIfNeeded() {
787-
return "(if type==\"string\" then . else tostring end)";
788-
}
789-
790-
/**
791-
* JQ: common “normalize to string”: - if array -> take first element - if null/empty -> coerce to
792-
* empty string - if not a string -> tostring
793-
*
794-
* <pre>(first(.[]? // empty) // . // \"\") | (if type==\"string\" then . else tostring end)</pre>
795-
*/
796-
public static String selectFirstStringify() {
797-
return "(first(.[]? // empty) // . // \"\") | (if type==\"string\" then . else tostring end)";
798-
}
799774
}

experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.event;
2020
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function;
2121
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.listen;
22-
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.toAny;
2322
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.toOne;
2423
import static org.junit.jupiter.api.Assertions.*;
2524

@@ -177,24 +176,6 @@ void mixed_chaining_order_and_exports() {
177176
assertNotNull(t2.getListenTask().getExport(), "listen step should carry export");
178177
}
179178

180-
@Test
181-
void step_chaining_with_jq_and_java_mix() {
182-
Workflow wf =
183-
FuncWorkflowBuilder.workflow("mix")
184-
.tasks(
185-
listen("L", toAny("a", "b"))
186-
.inputFrom(FuncDSL.selectFirst())
187-
.outputAs(FuncDSL.stringifyIfNeeded())
188-
.when(". != null"))
189-
.build();
190-
191-
Task t = wf.getDo().get(0).getTask();
192-
assertNotNull(t.getListenTask());
193-
assertEquals(". != null", t.getListenTask().getIf());
194-
assertNotNull(t.getListenTask().getInput());
195-
assertNotNull(t.getListenTask().getOutput());
196-
}
197-
198179
@Test
199180
void switchWhenOrElse_jq_to_taskName() {
200181
Workflow wf =

0 commit comments

Comments
 (0)