Skip to content

Commit d8aad6f

Browse files
committed
Add gRPC support to the fluent SDK with new DSL
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent 2af31a0 commit d8aad6f

10 files changed

Lines changed: 334 additions & 1 deletion

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.func;
17+
18+
import io.serverlessworkflow.api.types.CallGRPC;
19+
import io.serverlessworkflow.api.types.GRPCArguments;
20+
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
21+
import io.serverlessworkflow.fluent.func.spi.FuncTaskTransformations;
22+
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
23+
import io.serverlessworkflow.fluent.spec.spi.CallGrpcTaskFluent;
24+
25+
public class FuncCallGrpcTaskBuilder extends TaskBaseBuilder<FuncCallGrpcTaskBuilder>
26+
implements CallGrpcTaskFluent<FuncCallGrpcTaskBuilder>,
27+
FuncTaskTransformations<FuncCallGrpcTaskBuilder>,
28+
ConditionalTaskBuilder<FuncCallGrpcTaskBuilder> {
29+
30+
FuncCallGrpcTaskBuilder() {
31+
final CallGRPC callGRPC = new CallGRPC();
32+
callGRPC.setWith(new GRPCArguments());
33+
super.setTask(callGRPC);
34+
}
35+
36+
@Override
37+
public FuncCallGrpcTaskBuilder self() {
38+
return this;
39+
}
40+
}

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncDoTaskBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public FuncDoTaskBuilder openapi(
104104
return this;
105105
}
106106

107+
@Override
108+
public FuncDoTaskBuilder grpc(String name, Consumer<FuncCallGrpcTaskBuilder> itemsConfigurer) {
109+
this.listBuilder().grpc(name, itemsConfigurer);
110+
return this;
111+
}
112+
107113
@Override
108114
public FuncDoTaskBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
109115
this.listBuilder().workflow(name, itemsConfigurer);

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncTaskItemListBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func;
1717

18+
import io.serverlessworkflow.api.types.CallGRPC;
1819
import io.serverlessworkflow.api.types.CallHTTP;
1920
import io.serverlessworkflow.api.types.CallOpenAPI;
2021
import io.serverlessworkflow.api.types.CallTask;
@@ -168,6 +169,23 @@ public FuncTaskItemListBuilder openapi(
168169
return this.addTaskItem(new TaskItem(name, task));
169170
}
170171

172+
@Override
173+
public FuncTaskItemListBuilder grpc(
174+
String name, Consumer<FuncCallGrpcTaskBuilder> itemsConfigurer) {
175+
name = this.defaultNameAndRequireConfig(name, itemsConfigurer, TYPE_GRPC);
176+
177+
final FuncCallGrpcTaskBuilder grpcTaskBuilder = new FuncCallGrpcTaskBuilder();
178+
itemsConfigurer.accept(grpcTaskBuilder);
179+
180+
final CallGRPC callGRPC = grpcTaskBuilder.build();
181+
final CallTask callTask = new CallTask();
182+
callTask.setCallGRPC(callGRPC);
183+
final Task task = new Task();
184+
task.setCallTask(callTask);
185+
186+
return this.addTaskItem(new TaskItem(name, task));
187+
}
188+
171189
@Override
172190
public FuncTaskItemListBuilder workflow(
173191
String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
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.func.configurers;
17+
18+
import io.serverlessworkflow.fluent.func.FuncCallGrpcTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface FuncCallGrpcConfigurer extends Consumer<FuncCallGrpcTaskBuilder> {}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.func.dsl;
17+
18+
import io.serverlessworkflow.fluent.func.FuncCallGrpcTaskBuilder;
19+
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
20+
import io.serverlessworkflow.fluent.spec.configurers.AuthenticationConfigurer;
21+
import io.serverlessworkflow.fluent.spec.spi.CallGrpcTaskFluent;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.function.Consumer;
25+
26+
public class FuncCallGrpcStep extends Step<FuncCallGrpcStep, FuncCallGrpcTaskBuilder> {
27+
28+
private final List<Consumer<CallGrpcTaskFluent<?>>> steps = new ArrayList<>();
29+
30+
private String name;
31+
32+
public FuncCallGrpcStep(String name) {
33+
this.name = name;
34+
}
35+
36+
public FuncCallGrpcStep() {}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public FuncCallGrpcStep proto(String uri) {
43+
steps.add(b -> b.proto(uri));
44+
return this;
45+
}
46+
47+
public FuncCallGrpcStep proto(String uri, AuthenticationConfigurer authenticationConfigurer) {
48+
steps.add(b -> b.proto(uri, authenticationConfigurer));
49+
return this;
50+
}
51+
52+
public FuncCallGrpcStep service(String name, String host) {
53+
steps.add(b -> b.service(name, host));
54+
return this;
55+
}
56+
57+
public FuncCallGrpcStep service(String name, String host, int port) {
58+
steps.add(b -> b.service(name, host, port));
59+
return this;
60+
}
61+
62+
public FuncCallGrpcStep method(String method) {
63+
steps.add(b -> b.method(method));
64+
return this;
65+
}
66+
67+
public FuncCallGrpcStep argument(String name, Object value) {
68+
steps.add(b -> b.argument(name, value));
69+
return this;
70+
}
71+
72+
public FuncCallGrpcStep arguments(java.util.Map<String, Object> arguments) {
73+
steps.add(b -> b.arguments(arguments));
74+
return this;
75+
}
76+
77+
public FuncCallGrpcStep authentication(AuthenticationConfigurer authenticationConfigurer) {
78+
steps.add(b -> b.authentication(authenticationConfigurer));
79+
return this;
80+
}
81+
82+
@Override
83+
protected void configure(FuncTaskItemListBuilder list, Consumer<FuncCallGrpcTaskBuilder> post) {
84+
list.grpc(
85+
name,
86+
builder -> {
87+
for (Consumer<CallGrpcTaskFluent<?>> c : steps) {
88+
c.accept(builder);
89+
}
90+
post.accept(builder);
91+
});
92+
}
93+
}

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

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
3434
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
3535
import io.serverlessworkflow.fluent.func.FuncTryTaskBuilder;
36+
import io.serverlessworkflow.fluent.func.configurers.FuncCallGrpcConfigurer;
3637
import io.serverlessworkflow.fluent.func.configurers.FuncCallHttpConfigurer;
3738
import io.serverlessworkflow.fluent.func.configurers.FuncCallOpenAPIConfigurer;
3839
import io.serverlessworkflow.fluent.func.configurers.FuncTaskConfigurer;
@@ -1947,7 +1948,78 @@ public static FuncTaskConfigurer call(String name, FuncCallOpenAPIConfigurer con
19471948
}
19481949

19491950
/**
1950-
* Create a new OpenAPI specification to be used with {@link #call(FuncCallOpenAPIStep)}.
1951+
* gRPC call using a fluent {@link FuncCallGrpcStep}.
1952+
*
1953+
* <p>This overload creates an unnamed gRPC call task.
1954+
*
1955+
* <pre>{@code
1956+
* tasks(FuncDSL.call(FuncDSL.grpc()
1957+
* .proto("proto/greeter.proto")
1958+
* .service("Greeter", "localhost")
1959+
* .method("SayHello")
1960+
* .argument("name", "World")));
1961+
* }</pre>
1962+
*
1963+
* @param spec fluent gRPC spec built via {@link #grpc()}
1964+
* @return a {@link FuncTaskConfigurer} that adds a gRPC call task
1965+
*/
1966+
public static FuncTaskConfigurer call(FuncCallGrpcStep spec) {
1967+
return call(null, spec);
1968+
}
1969+
1970+
/**
1971+
* gRPC call using a fluent {@link FuncCallGrpcStep} with an explicit task name.
1972+
*
1973+
* <pre>{@code
1974+
* tasks(
1975+
* FuncDSL.call(
1976+
* "greet",
1977+
* FuncDSL.grpc()
1978+
* .proto("proto/greeter.proto")
1979+
* .service("Greeter", "localhost")
1980+
* .method("SayHello")
1981+
* .argument("name", "World"))
1982+
* );
1983+
* }</pre>
1984+
*
1985+
* @param name task name, or {@code null} for an anonymous task
1986+
* @param spec fluent gRPC spec built via {@link #grpc()}
1987+
* @return a {@link FuncTaskConfigurer} that adds a named gRPC call task
1988+
*/
1989+
public static FuncTaskConfigurer call(String name, FuncCallGrpcStep spec) {
1990+
Objects.requireNonNull(spec, "spec");
1991+
spec.setName(name);
1992+
return spec;
1993+
}
1994+
1995+
/**
1996+
* Low-level gRPC call entrypoint using a {@link FuncCallGrpcConfigurer}.
1997+
*
1998+
* <p>This overload creates an unnamed gRPC call task.
1999+
*
2000+
* @param configurer configurer that mutates the underlying gRPC call builder
2001+
* @return a {@link FuncTaskConfigurer} that adds a gRPC call task
2002+
*/
2003+
public static FuncTaskConfigurer call(FuncCallGrpcConfigurer configurer) {
2004+
return call(null, configurer);
2005+
}
2006+
2007+
/**
2008+
* Low-level gRPC call entrypoint using a {@link FuncCallGrpcConfigurer}.
2009+
*
2010+
* <p>This overload allows assigning an explicit task name.
2011+
*
2012+
* @param name task name, or {@code null} for an anonymous task
2013+
* @param configurer configurer that mutates the underlying gRPC call builder
2014+
* @return a {@link FuncTaskConfigurer} that adds a gRPC call task
2015+
*/
2016+
public static FuncTaskConfigurer call(String name, FuncCallGrpcConfigurer configurer) {
2017+
Objects.requireNonNull(configurer, "configurer");
2018+
return list -> list.grpc(name, configurer);
2019+
}
2020+
2021+
/**
2022+
* Create a new gRPC specification to be used with {@link #call(FuncCallGrpcStep)}.
19512023
*
19522024
* <p>Typical usage:
19532025
*
@@ -2001,6 +2073,37 @@ public static FuncCallHttpStep http(String name) {
20012073
return new FuncCallHttpStep(name);
20022074
}
20032075

2076+
/**
2077+
* Create a new, empty gRPC specification to be used with {@link #call(FuncCallGrpcStep)}.
2078+
*
2079+
* <p>Typical usage:
2080+
*
2081+
* <pre>{@code
2082+
* FuncDSL.call(
2083+
* grpc()
2084+
* .proto("proto/greeter.proto")
2085+
* .service("Greeter", "localhost")
2086+
* .method("SayHello")
2087+
* .argument("name", "World"))
2088+
* );
2089+
* }</pre>
2090+
*
2091+
* @return a new {@link FuncCallGrpcStep}
2092+
*/
2093+
public static FuncCallGrpcStep grpc() {
2094+
return new FuncCallGrpcStep();
2095+
}
2096+
2097+
/**
2098+
* Named variant of {@link #grpc()}.
2099+
*
2100+
* @param name task name to be used when the spec is attached via {@link #call(FuncCallGrpcStep)}
2101+
* @return a new named {@link FuncCallGrpcStep}
2102+
*/
2103+
public static FuncCallGrpcStep grpc(String name) {
2104+
return new FuncCallGrpcStep(name);
2105+
}
2106+
20042107
/**
20052108
* Create a new HTTP specification preconfigured with an endpoint expression and authentication.
20062109
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.func.spi;
17+
18+
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
19+
import java.util.function.Consumer;
20+
21+
public interface CallGrpcFluent<SELF extends TaskBaseBuilder<SELF>, LIST> {
22+
23+
LIST grpc(String name, Consumer<SELF> itemsConfigurer);
24+
25+
default LIST grpc(Consumer<SELF> itemsConfigurer) {
26+
return this.grpc(null, itemsConfigurer);
27+
}
28+
}

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/spi/FuncDoFluent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func.spi;
1717

18+
import io.serverlessworkflow.fluent.func.FuncCallGrpcTaskBuilder;
1819
import io.serverlessworkflow.fluent.func.FuncCallHttpTaskBuilder;
1920
import io.serverlessworkflow.fluent.func.FuncCallOpenAPITaskBuilder;
2021
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
@@ -52,6 +53,7 @@ public interface FuncDoFluent<SELF extends FuncDoFluent<SELF>>
5253
CallFnFluent<FuncCallTaskBuilder, SELF>,
5354
CallHttpFluent<FuncCallHttpTaskBuilder, SELF>,
5455
CallOpenAPIFluent<FuncCallOpenAPITaskBuilder, SELF>,
56+
CallGrpcFluent<FuncCallGrpcTaskBuilder, SELF>,
5557
WorkflowFluent<WorkflowTaskBuilder, SELF> {
5658

5759
default SELF subflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {

0 commit comments

Comments
 (0)