Skip to content

Commit d5f28e1

Browse files
committed
Allowing passing WorkflowModel as function input parameter
Signed-off-by: fjtirado <[email protected]>
1 parent 1d2a238 commit d5f28e1

File tree

2 files changed

+32
-0
lines changed
  • experimental/lambda/src

2 files changed

+32
-0
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/func/JavaModel.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public Class<?> objectClass() {
9494

9595
@Override
9696
public <T> Optional<T> as(Class<T> clazz) {
97+
if (WorkflowModel.class.isAssignableFrom(clazz)) {
98+
return Optional.of(clazz.cast(this));
99+
}
97100
return object != null && clazz.isAssignableFrom(object.getClass())
98101
? Optional.of(clazz.cast(object))
99102
: Optional.empty();

experimental/lambda/src/test/java/io/serverless/workflow/impl/CallTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.serverlessworkflow.api.types.func.SwitchCaseFunction;
3434
import io.serverlessworkflow.impl.WorkflowApplication;
3535
import io.serverlessworkflow.impl.WorkflowDefinition;
36+
import io.serverlessworkflow.impl.WorkflowModel;
3637
import io.serverlessworkflow.impl.expressions.TaskMetadataKeys;
3738
import java.util.Collection;
3839
import java.util.List;
@@ -166,6 +167,30 @@ void testIf() throws InterruptedException, ExecutionException {
166167
}
167168
}
168169

170+
@Test
171+
void testIfWithModel() throws InterruptedException, ExecutionException {
172+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
173+
Workflow workflow =
174+
new Workflow()
175+
.withDocument(
176+
new Document().withNamespace("test").withName("testIf").withVersion("1.0"))
177+
.withDo(
178+
List.of(
179+
new TaskItem(
180+
"java",
181+
new Task()
182+
.withCallTask(
183+
new CallTaskJava(
184+
withPredicate(
185+
CallJava.function(
186+
CallTest::zeroWithModel, WorkflowModel.class),
187+
CallTest::isOdd))))));
188+
WorkflowDefinition definition = app.workflowDefinition(workflow);
189+
assertThat(definition.instance(3).start().get().asNumber().orElseThrow()).isEqualTo(0);
190+
assertThat(definition.instance(4).start().get().asNumber().orElseThrow()).isEqualTo(4);
191+
}
192+
}
193+
169194
private <T> CallJava withPredicate(CallJava call, Predicate<T> pred) {
170195
return (CallJava)
171196
call.withMetadata(
@@ -184,6 +209,10 @@ public static int zero(Integer value) {
184209
return 0;
185210
}
186211

212+
public static int zeroWithModel(WorkflowModel value) {
213+
return 0;
214+
}
215+
187216
public static Integer sum(Object model, Integer item) {
188217
return model instanceof Collection ? item : (Integer) model + item;
189218
}

0 commit comments

Comments
 (0)