Skip to content

Commit e1a4f4b

Browse files
committed
Adding lambda support
1 parent 1362e62 commit e1a4f4b

File tree

5 files changed

+117
-6
lines changed

5 files changed

+117
-6
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/JavaExpressionFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.serverlessworkflow.impl.WorkflowModelFactory;
2323
import java.util.function.BiFunction;
2424
import java.util.function.BiPredicate;
25+
import java.util.function.Function;
2526

2627
public class JavaExpressionFactory implements ExpressionFactory {
2728

@@ -42,15 +43,17 @@ public Expression buildExpression(String expression) {
4243

4344
@Override
4445
public WorkflowFilter buildFilter(String expr, Object value) {
45-
if (value instanceof BiPredicate pred) {
46+
if (value instanceof Function func) {
47+
return (w, t, n) -> modelFactory.fromAny(func.apply(n.asJavaObject()));
48+
} else if (value instanceof BiPredicate pred) {
4649
return (w, t, n) -> modelFactory.from(pred.test(w, t));
4750
} else if (value instanceof BiFunction func) {
4851
return (w, t, n) -> modelFactory.fromAny(func.apply(w, t));
4952
} else if (value instanceof WorkflowFilter filter) {
5053
return filter;
54+
} else {
55+
return (w, t, n) -> modelFactory.fromAny(value);
5156
}
52-
throw new UnsupportedOperationException(
53-
"Unsupported value " + value + "+ of type " + value.getClass());
5457
}
5558

5659
@Override

experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/JavaModelFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public class JavaModelFactory implements WorkflowModelFactory {
2727

2828
@Override
2929
public WorkflowModel combine(Map<String, WorkflowModel> workflowVariables) {
30-
// TODO Auto-generated method stub
31-
return null;
30+
return new JavaModel(workflowVariables);
3231
}
3332

3433
@Override
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverless.workflow.impl;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import io.serverlessworkflow.api.types.Document;
21+
import io.serverlessworkflow.api.types.Output;
22+
import io.serverlessworkflow.api.types.OutputAsExpresssions;
23+
import io.serverlessworkflow.api.types.Set;
24+
import io.serverlessworkflow.api.types.SetTask;
25+
import io.serverlessworkflow.api.types.SetTaskConfiguration;
26+
import io.serverlessworkflow.api.types.Task;
27+
import io.serverlessworkflow.api.types.TaskItem;
28+
import io.serverlessworkflow.api.types.Workflow;
29+
import io.serverlessworkflow.impl.WorkflowApplication;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.concurrent.ExecutionException;
33+
import org.junit.jupiter.api.Test;
34+
35+
class LambdaTest {
36+
37+
@Test
38+
void testSimpleExpression() throws InterruptedException, ExecutionException {
39+
40+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
41+
Workflow workflow =
42+
new Workflow()
43+
.withDocument(
44+
new Document().withNamespace("test").withName("test").withVersion("1.0"))
45+
.withDo(
46+
List.of(
47+
new TaskItem(
48+
"javierito",
49+
new Task()
50+
.withSetTask(
51+
new SetTask()
52+
.withSet(
53+
new Set()
54+
.withSetTaskConfiguration(
55+
new SetTaskConfiguration()
56+
.withAdditionalProperty("name", "Francisco")))
57+
.withOutput(
58+
new Output()
59+
.withAs(
60+
new OutputAsExpresssions()
61+
.withFunction(this::addJavierito)))))));
62+
assertThat(
63+
app.workflowDefinition(workflow)
64+
.instance(Map.of())
65+
.start()
66+
.get()
67+
.asMap()
68+
.map(m -> m.get("name"))
69+
.orElseThrow())
70+
.isEqualTo("Francisco Javierito");
71+
}
72+
}
73+
74+
private Map<String, Object> addJavierito(Map<String, Object> map) {
75+
return Map.of("name", map.get("name") + " Javierito");
76+
}
77+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api.types;
17+
18+
import java.util.function.Function;
19+
20+
public class OutputAsExpresssions extends OutputAs {
21+
22+
public <T, V> OutputAs withFunction(Function<T, V> value) {
23+
setObject(value);
24+
return this;
25+
}
26+
}

impl/core/src/main/java/io/serverlessworkflow/impl/executors/ForkExecutor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.serverlessworkflow.impl.executors.RegularTaskExecutor.RegularTaskExecutorBuilder;
2727
import io.serverlessworkflow.impl.resources.ResourceLoader;
2828
import java.util.HashMap;
29+
import java.util.LinkedHashMap;
2930
import java.util.Map;
3031
import java.util.Map.Entry;
3132
import java.util.Optional;
@@ -108,6 +109,11 @@ private WorkflowModel combine(WorkflowContext context, Map<String, TaskContext>
108109
.application()
109110
.modelFactory()
110111
.combine(
111-
sortedStream.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().output())));
112+
sortedStream.collect(
113+
Collectors.toMap(
114+
Entry::getKey,
115+
e -> e.getValue().output(),
116+
(x, y) -> y,
117+
LinkedHashMap::new)));
112118
}
113119
}

0 commit comments

Comments
 (0)