Skip to content

Commit 218f9e3

Browse files
feat: support to subflows on the Java DSL (#1213)
* feat: support to subflows on the Java DSL Signed-off-by: Matheus André <92062874+matheusandre1@users.noreply.github.com> * test and ajust in classes Signed-off-by: Matheus André Galvão Da Silva <matheusandr2@gmail.com> * remove test Signed-off-by: Matheus André Galvão Da Silva <matheusandr2@gmail.com> --------- Signed-off-by: Matheus André <92062874+matheusandre1@users.noreply.github.com> Signed-off-by: Matheus André Galvão Da Silva <matheusandr2@gmail.com>
1 parent 566abd3 commit 218f9e3

15 files changed

Lines changed: 484 additions & 38 deletions

File tree

experimental/lambda/src/test/java/io/serverless/workflow/impl/executors/func/FuncDSLDataFlowTransformationHelpersTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.serverlessworkflow.impl.WorkflowApplication;
2626
import io.serverlessworkflow.impl.WorkflowContextData;
2727
import io.serverlessworkflow.impl.WorkflowDefinition;
28+
import io.serverlessworkflow.impl.WorkflowInstance;
2829
import io.serverlessworkflow.impl.WorkflowModel;
2930
import org.assertj.core.api.SoftAssertions;
3031
import org.junit.jupiter.api.Test;
@@ -160,4 +161,44 @@ void test_input_with_inputFrom_fluent_way() {
160161

161162
softly.assertAll();
162163
}
164+
165+
@Test
166+
void test_input_with_exportAs() {
167+
168+
SoftAssertions softly = new SoftAssertions();
169+
170+
Workflow workflow =
171+
FuncWorkflowBuilder.workflow("enrichExportWithInputTest")
172+
.tasks(
173+
function(
174+
"add5",
175+
(Long input) -> {
176+
softly.assertThat(input).isEqualTo(10L);
177+
return input + 5;
178+
},
179+
Long.class)
180+
.exportAs(
181+
(Long object,
182+
WorkflowContextData workflowContext,
183+
TaskContextData taskContextData) -> {
184+
Long taskOutput = output(taskContextData, Long.class);
185+
softly.assertThat(taskOutput).isEqualTo(15L);
186+
Long input = input(workflowContext, Long.class);
187+
softly.assertThat(input).isEqualTo(10L);
188+
return input + taskOutput;
189+
}))
190+
.build();
191+
192+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
193+
WorkflowDefinition def = app.workflowDefinition(workflow);
194+
195+
WorkflowInstance instance = def.instance(10L);
196+
instance.start().join();
197+
Number number = instance.context().asNumber().orElseThrow();
198+
199+
softly.assertThat(number.longValue()).isEqualTo(25L);
200+
}
201+
202+
softly.assertAll();
203+
}
163204
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/BaseTaskItemListBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public abstract class BaseTaskItemListBuilder<SELF extends BaseTaskItemListBuild
4444
protected final String TYPE_TRY = "try";
4545
protected final String TYPE_HTTP = "http";
4646
protected final String TYPE_OPENAPI = "openapi";
47+
protected final String TYPE_WORKFLOW = "workflow";
4748

4849
private final List<TaskItem> list;
4950
private final int offset;

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/DoTaskBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@ public DoTaskBuilder openapi(String name, Consumer<CallOpenAPITaskBuilder> items
9797
this.listBuilder().openapi(name, itemsConfigurer);
9898
return this;
9999
}
100+
101+
@Override
102+
public DoTaskBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
103+
this.listBuilder().workflow(name, itemsConfigurer);
104+
return this;
105+
}
100106
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/TaskItemListBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,17 @@ public TaskItemListBuilder openapi(
144144

145145
return addTaskItem(new TaskItem(name, task));
146146
}
147+
148+
@Override
149+
public TaskItemListBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
150+
name = defaultNameAndRequireConfig(name, itemsConfigurer, TYPE_WORKFLOW);
151+
152+
final WorkflowTaskBuilder workflowTaskBuilder = new WorkflowTaskBuilder();
153+
itemsConfigurer.accept(workflowTaskBuilder);
154+
155+
final Task task = new Task();
156+
task.setRunTask(workflowTaskBuilder.build());
157+
158+
return addTaskItem(new TaskItem(name, task));
159+
}
147160
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.fluent.spec;
17+
18+
import io.serverlessworkflow.api.types.RunTask;
19+
import io.serverlessworkflow.api.types.RunTaskConfigurationUnion;
20+
import io.serverlessworkflow.api.types.RunWorkflow;
21+
import io.serverlessworkflow.api.types.SubflowConfiguration;
22+
import io.serverlessworkflow.fluent.spec.spi.WorkflowTaskFluent;
23+
24+
public class WorkflowTaskBuilder extends TaskBaseBuilder<WorkflowTaskBuilder>
25+
implements WorkflowTaskFluent<WorkflowTaskBuilder> {
26+
27+
private final RunTask task;
28+
private final RunWorkflow configuration;
29+
private final SubflowConfiguration workflow;
30+
31+
WorkflowTaskBuilder() {
32+
this.task = new RunTask();
33+
this.configuration = new RunWorkflow();
34+
this.workflow = new SubflowConfiguration();
35+
this.configuration.setWorkflow(this.workflow);
36+
this.task.setRun(new RunTaskConfigurationUnion().withRunWorkflow(this.configuration));
37+
this.setTask(task);
38+
}
39+
40+
@Override
41+
public WorkflowTaskBuilder self() {
42+
return this;
43+
}
44+
45+
public RunWorkflow config() {
46+
return this.configuration;
47+
}
48+
49+
public SubflowConfiguration workflow() {
50+
return this.workflow;
51+
}
52+
53+
public RunTask build() {
54+
return this.task;
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.fluent.spec.configurers;
17+
18+
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface WorkflowConfigurer extends Consumer<WorkflowTaskBuilder> {}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/dsl/DSL.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import io.serverlessworkflow.fluent.spec.configurers.TasksConfigurer;
3838
import io.serverlessworkflow.fluent.spec.configurers.TryCatchConfigurer;
3939
import io.serverlessworkflow.fluent.spec.configurers.TryConfigurer;
40+
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
4041
import io.serverlessworkflow.types.Errors;
4142
import java.net.URI;
4243
import java.util.List;
@@ -105,6 +106,18 @@ public static CallOpenAPISpec openapi() {
105106
return new CallOpenAPISpec();
106107
}
107108

109+
public static WorkflowSpec workflow(String namespace, String name, String version) {
110+
return new WorkflowSpec().namespace(namespace).name(name).version(version);
111+
}
112+
113+
public static WorkflowSpec workflow(String namespace, String name) {
114+
return new WorkflowSpec().namespace(namespace).name(name);
115+
}
116+
117+
public static WorkflowSpec workflow() {
118+
return new WorkflowSpec();
119+
}
120+
108121
/**
109122
* Convenience for defining a {@code use} block with a single secret.
110123
*
@@ -640,6 +653,10 @@ public static TasksConfigurer call(CallOpenAPIConfigurer configurer) {
640653
return list -> list.openapi(configurer);
641654
}
642655

656+
public static TasksConfigurer workflow(WorkflowConfigurer configurer) {
657+
return list -> list.workflow(configurer);
658+
}
659+
643660
/**
644661
* Create a {@link TasksConfigurer} that adds an OpenAPI call task with an explicit name.
645662
*
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.fluent.spec.dsl;
17+
18+
import io.serverlessworkflow.api.types.RunTaskConfiguration;
19+
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
20+
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.function.Consumer;
25+
26+
public final class WorkflowSpec implements WorkflowConfigurer {
27+
28+
private final List<Consumer<WorkflowTaskBuilder>> steps = new ArrayList<>();
29+
30+
public WorkflowSpec namespace(String namespace) {
31+
steps.add(b -> b.namespace(namespace));
32+
return this;
33+
}
34+
35+
public WorkflowSpec name(String name) {
36+
steps.add(b -> b.name(name));
37+
return this;
38+
}
39+
40+
public WorkflowSpec version(String version) {
41+
steps.add(b -> b.version(version));
42+
return this;
43+
}
44+
45+
public WorkflowSpec input(Map<String, Object> input) {
46+
steps.add(b -> b.input(input));
47+
return this;
48+
}
49+
50+
public WorkflowSpec input(String key, Object value) {
51+
steps.add(b -> b.input(key, value));
52+
return this;
53+
}
54+
55+
public WorkflowSpec await(boolean await) {
56+
steps.add(b -> b.await(await));
57+
return this;
58+
}
59+
60+
public WorkflowSpec returnType(RunTaskConfiguration.ProcessReturnType returnType) {
61+
steps.add(b -> b.returnType(returnType));
62+
return this;
63+
}
64+
65+
@Override
66+
public void accept(WorkflowTaskBuilder builder) {
67+
for (var s : steps) {
68+
s.accept(builder);
69+
}
70+
}
71+
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/DoFluent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.serverlessworkflow.fluent.spec.SwitchTaskBuilder;
2727
import io.serverlessworkflow.fluent.spec.TaskItemListBuilder;
2828
import io.serverlessworkflow.fluent.spec.TryTaskBuilder;
29+
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
2930

3031
/**
3132
* Documents the exposed fluent `do` DSL.
@@ -44,4 +45,5 @@ public interface DoFluent<T>
4445
ForkFluent<ForkTaskBuilder, T>,
4546
ListenFluent<ListenTaskBuilder, T>,
4647
RaiseFluent<RaiseTaskBuilder, T>,
47-
CallOpenAPIFluent<CallOpenAPITaskBuilder, T> {}
48+
CallOpenAPIFluent<CallOpenAPITaskBuilder, T>,
49+
WorkflowFluent<WorkflowTaskBuilder, T> {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.fluent.spec.spi;
17+
18+
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
19+
import java.util.UUID;
20+
import java.util.function.Consumer;
21+
22+
public interface WorkflowFluent<SELF extends TaskBaseBuilder<SELF>, LIST> {
23+
24+
LIST workflow(String name, Consumer<SELF> itemsConfigurer);
25+
26+
default LIST workflow(Consumer<SELF> itemsConfigurer) {
27+
return this.workflow(UUID.randomUUID().toString(), itemsConfigurer);
28+
}
29+
}

0 commit comments

Comments
 (0)