Skip to content

Commit e583216

Browse files
committed
Review FuncDSL
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent 9351d0d commit e583216

File tree

13 files changed

+570
-444
lines changed

13 files changed

+570
-444
lines changed

experimental/fluent/func/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>io.serverlessworkflow</groupId>
2222
<artifactId>serverlessworkflow-experimental-types</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>io.serverlessworkflow</groupId>
26+
<artifactId>serverlessworkflow-impl-json</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>io.serverlessworkflow</groupId>
2630
<artifactId>serverlessworkflow-fluent-spec</artifactId>

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,28 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func;
1717

18+
import io.serverlessworkflow.api.types.Set;
19+
import io.serverlessworkflow.api.types.SetTask;
20+
import io.serverlessworkflow.api.types.func.MapSetTaskConfiguration;
1821
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
1922
import io.serverlessworkflow.fluent.spec.SetTaskBuilder;
23+
import java.util.Map;
2024

2125
public class FuncSetTaskBuilder extends SetTaskBuilder
2226
implements ConditionalTaskBuilder<FuncSetTaskBuilder> {
2327

24-
FuncSetTaskBuilder() {}
28+
private final SetTask task;
29+
30+
FuncSetTaskBuilder() {
31+
this.task = new SetTask();
32+
this.setTask(task);
33+
}
34+
35+
public FuncSetTaskBuilder expr(Map<String, Object> map) {
36+
if (this.task.getSet() == null) {
37+
this.task.setSet(new Set());
38+
}
39+
this.task.getSet().withSetTaskConfiguration(new MapSetTaskConfiguration(map));
40+
return this;
41+
}
2542
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.FuncEmitTaskBuilder;
19+
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
20+
import java.util.Objects;
21+
import java.util.function.Consumer;
22+
23+
/** Chainable emit step; applies FuncEmitSpec then queued export/when. */
24+
public final class EmitStep extends Step<EmitStep, FuncEmitTaskBuilder> {
25+
26+
private final String name; // nullable
27+
private final Consumer<FuncEmitTaskBuilder> cfg;
28+
29+
EmitStep(String name, Consumer<FuncEmitTaskBuilder> cfg) {
30+
this.name = name;
31+
this.cfg = Objects.requireNonNull(cfg, "cfg");
32+
}
33+
34+
@Override
35+
protected void configure(FuncTaskItemListBuilder list, Consumer<FuncEmitTaskBuilder> postApply) {
36+
if (name == null) {
37+
list.emit(
38+
e -> {
39+
cfg.accept(e);
40+
postApply.accept(e);
41+
});
42+
} else {
43+
list.emit(
44+
name,
45+
e -> {
46+
cfg.accept(e);
47+
postApply.accept(e);
48+
});
49+
}
50+
}
51+
}
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.func.dsl;
17+
18+
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
19+
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
20+
import java.util.function.Consumer;
21+
import java.util.function.Function;
22+
23+
// FuncCallStep
24+
public final class FuncCallStep<T, R> extends Step<FuncCallStep<T, R>, FuncCallTaskBuilder> {
25+
private final String name; // may be null
26+
private final Function<T, R> fn;
27+
private final Class<T> argClass;
28+
29+
FuncCallStep(Function<T, R> fn, Class<T> argClass) {
30+
this(null, fn, argClass);
31+
}
32+
33+
FuncCallStep(String name, Function<T, R> fn, Class<T> argClass) {
34+
this.name = name;
35+
this.fn = fn;
36+
this.argClass = argClass;
37+
}
38+
39+
@Override
40+
protected void configure(FuncTaskItemListBuilder list, Consumer<FuncCallTaskBuilder> post) {
41+
if (name == null) {
42+
list.callFn(
43+
cb -> {
44+
cb.function(fn, argClass);
45+
post.accept(cb);
46+
});
47+
} else {
48+
list.callFn(
49+
name,
50+
cb -> {
51+
cb.function(fn, argClass);
52+
post.accept(cb);
53+
});
54+
}
55+
}
56+
}

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

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import io.cloudevents.CloudEventData;
1919
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
2020
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
21-
import io.serverlessworkflow.fluent.func.FuncDoTaskBuilder;
2221
import io.serverlessworkflow.fluent.func.FuncEmitTaskBuilder;
2322
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
2423
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
@@ -28,6 +27,7 @@
2827
import io.serverlessworkflow.fluent.func.dsl.internal.CommonFuncOps;
2928
import java.util.Collection;
3029
import java.util.List;
30+
import java.util.Map;
3131
import java.util.Objects;
3232
import java.util.function.Consumer;
3333
import java.util.function.Function;
@@ -99,50 +99,90 @@ public static <T> Consumer<FuncEmitTaskBuilder> event(
9999
return OPS.event(type, function, clazz);
100100
}
101101

102+
/** Emit a JSON CloudEvent (PojoCloudEventData) from a POJO payload. */
103+
public static <T> Consumer<FuncEmitTaskBuilder> eventJson(String type, Class<T> clazz) {
104+
return b -> new FuncEmitSpec().type(type).jsonData(clazz).accept(b);
105+
}
106+
107+
public static <T> Consumer<FuncEmitTaskBuilder> eventBytes(
108+
String type, Function<T, byte[]> serializer, Class<T> clazz) {
109+
return b -> new FuncEmitSpec().type(type).bytesData(serializer, clazz).accept(b);
110+
}
111+
112+
public static Consumer<FuncEmitTaskBuilder> eventBytesUtf8(String type) {
113+
return b -> new FuncEmitSpec().type(type).bytesDataUtf8().accept(b);
114+
}
115+
102116
public static FuncPredicateEventConfigurer event(String type) {
103117
return OPS.event(type);
104118
}
105119

106-
public static <T, R> FuncTaskConfigurer function(Function<T, R> fn) {
120+
public static <T, R> FuncCallStep<T, R> function(Function<T, R> fn, Class<T> clazz) {
121+
return new FuncCallStep<>(fn, clazz);
122+
}
123+
124+
public static <T, R> FuncCallStep<T, R> function(Function<T, R> fn) {
107125
Class<T> clazz = ReflectionUtils.inferInputType(fn);
108-
return list -> list.callFn(f -> f.function(fn, clazz));
126+
return new FuncCallStep<>(fn, clazz);
109127
}
110128

111-
public static <T, R> FuncTaskConfigurer function(Function<T, R> fn, Class<T> clazz) {
112-
return list -> list.callFn(f -> f.function(fn, clazz));
129+
public static <T, R> FuncCallStep<T, R> function(String name, Function<T, R> fn) {
130+
Class<T> clazz = ReflectionUtils.inferInputType(fn);
131+
return new FuncCallStep<>(name, fn, clazz);
113132
}
114133

115-
// ------------------ tasks ---------------- //
116-
public static Consumer<FuncDoTaskBuilder> doTasks(FuncTaskConfigurer... steps) {
117-
final Consumer<FuncTaskItemListBuilder> tasks = tasks(steps);
118-
return d -> d.tasks(tasks);
134+
public static <T, R> FuncCallStep<T, R> function(String name, Function<T, R> fn, Class<T> clazz) {
135+
return new FuncCallStep<>(name, fn, clazz);
119136
}
120137

138+
// ------------------ tasks ---------------- //
139+
121140
public static Consumer<FuncTaskItemListBuilder> tasks(FuncTaskConfigurer... steps) {
122141
Objects.requireNonNull(steps, "Steps in a tasks are required");
123142
final List<FuncTaskConfigurer> snapshot = List.of(steps.clone());
124143
return list -> snapshot.forEach(s -> s.accept(list));
125144
}
126145

127-
public static FuncTaskConfigurer emit(Consumer<FuncEmitTaskBuilder> emitTask) {
128-
return list -> list.emit(emitTask);
146+
public static EmitStep emit(Consumer<FuncEmitTaskBuilder> cfg) {
147+
return new EmitStep(null, cfg);
148+
}
149+
150+
public static EmitStep emit(String name, Consumer<FuncEmitTaskBuilder> cfg) {
151+
return new EmitStep(name, cfg);
152+
}
153+
154+
public static <T> EmitStep emit(String type, Function<T, CloudEventData> fn) {
155+
// `event(type, fn)` is your Consumer<FuncEmitTaskBuilder> for EMIT
156+
return new EmitStep(null, event(type, fn));
157+
}
158+
159+
public static <T> EmitStep emit(String name, String type, Function<T, CloudEventData> fn) {
160+
return new EmitStep(name, event(type, fn));
161+
}
162+
163+
public static <T> EmitStep emit(
164+
String name, String type, Function<T, byte[]> serializer, Class<T> clazz) {
165+
return new EmitStep(name, eventBytes(type, serializer, clazz));
166+
}
167+
168+
public static <T> EmitStep emit(String type, Function<T, byte[]> serializer, Class<T> clazz) {
169+
return new EmitStep(null, eventBytes(type, serializer, clazz));
129170
}
130171

131-
public static <T> FuncTaskConfigurer emit(String type, Function<T, CloudEventData> fn) {
132-
return list -> list.emit(event(type, fn));
172+
public static <T> EmitStep emitJson(String type, Class<T> clazz) {
173+
return new EmitStep(null, eventJson(type, clazz));
133174
}
134175

135-
public static <T> FuncTaskConfigurer emit(
136-
String name, String type, Function<T, CloudEventData> fn) {
137-
return list -> list.emit(name, event(type, fn));
176+
public static <T> EmitStep emitJson(String name, String type, Class<T> clazz) {
177+
return new EmitStep(name, eventJson(type, clazz));
138178
}
139179

140-
public static FuncTaskConfigurer listen(FuncListenSpec listen) {
141-
return list -> list.listen(listen);
180+
public static ListenStep listen(FuncListenSpec spec) {
181+
return new ListenStep(null, spec);
142182
}
143183

144-
public static FuncTaskConfigurer listen(String name, FuncListenSpec listen) {
145-
return list -> list.listen(name, listen);
184+
public static ListenStep listen(String name, FuncListenSpec spec) {
185+
return new ListenStep(name, spec);
146186
}
147187

148188
public static FuncTaskConfigurer switchCase(
@@ -176,6 +216,12 @@ public static <T> FuncTaskConfigurer switchWhenOrElse(
176216
list.switchCase(FuncDSL.cases(caseOf(pred).then(thenTask), caseDefault(otherwise)));
177217
}
178218

219+
public static <T> FuncTaskConfigurer switchWhenOrElse(
220+
Predicate<T> pred, String thenTask, String otherwiseTask) {
221+
return list ->
222+
list.switchCase(FuncDSL.cases(caseOf(pred).then(thenTask), caseDefault(otherwiseTask)));
223+
}
224+
179225
public static <T> FuncTaskConfigurer forEach(
180226
Function<T, Collection<?>> collection, Consumer<FuncTaskItemListBuilder> body) {
181227
return list -> list.forEach(j -> j.collection(collection).tasks(body));
@@ -192,4 +238,12 @@ public static <T> FuncTaskConfigurer forEach(
192238
List<T> collection, Consumer<FuncTaskItemListBuilder> body) {
193239
return list -> list.forEach(j -> j.collection(ctx -> collection).tasks(body));
194240
}
241+
242+
public static FuncTaskConfigurer set(String expr) {
243+
return list -> list.set(expr);
244+
}
245+
246+
public static FuncTaskConfigurer set(Map<String, Object> map) {
247+
return list -> list.set(s -> s.expr(map));
248+
}
195249
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
package io.serverlessworkflow.fluent.func.dsl;
1717

1818
import io.cloudevents.CloudEventData;
19+
import io.cloudevents.core.data.BytesCloudEventData;
20+
import io.cloudevents.core.data.PojoCloudEventData;
1921
import io.serverlessworkflow.api.types.func.EventDataFunction;
2022
import io.serverlessworkflow.fluent.func.FuncEventPropertiesBuilder;
2123
import io.serverlessworkflow.fluent.spec.dsl.EventFilterSpec;
24+
import io.serverlessworkflow.impl.jackson.JsonUtils;
25+
import java.nio.charset.StandardCharsets;
2226
import java.util.ArrayList;
2327
import java.util.function.Function;
2428

@@ -36,9 +40,31 @@ public <T> SELF jsonData(Function<T, CloudEventData> function) {
3640
return JSON();
3741
}
3842

43+
/** Sets the event data and the contentType to `application/octet-stream` */
44+
public <T> SELF bytesData(Function<T, byte[]> serializer, Class<T> clazz) {
45+
addStep(e -> e.data(payload -> BytesCloudEventData.wrap(serializer.apply(payload)), clazz));
46+
return OCTET_STREAM();
47+
}
48+
49+
public SELF bytesDataUtf8() {
50+
return bytesData((String s) -> s.getBytes(StandardCharsets.UTF_8), String.class);
51+
}
52+
3953
/** Sets the event data and the contentType to `application/json` */
4054
public <T> SELF jsonData(Function<T, CloudEventData> function, Class<T> clazz) {
4155
addStep(e -> e.data(new EventDataFunction().withFunction(function, clazz)));
4256
return JSON();
4357
}
58+
59+
/** JSON with default mapper (PojoCloudEventData + application/json). */
60+
public <T> SELF jsonData(Class<T> clazz) {
61+
addStep(
62+
e ->
63+
e.data(
64+
payload ->
65+
PojoCloudEventData.wrap(
66+
payload, p -> JsonUtils.mapper().writeValueAsString(p).getBytes()),
67+
clazz));
68+
return JSON();
69+
}
4470
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.FuncListenTaskBuilder;
19+
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
20+
import java.util.function.Consumer;
21+
22+
/** Chainable listen step; applies FuncListenSpec then queued export/when. */
23+
public final class ListenStep extends Step<ListenStep, FuncListenTaskBuilder> {
24+
25+
private final String name; // nullable
26+
private final FuncListenSpec spec;
27+
28+
ListenStep(String name, FuncListenSpec spec) {
29+
this.name = name;
30+
this.spec = spec;
31+
}
32+
33+
@Override
34+
protected void configure(
35+
FuncTaskItemListBuilder list, Consumer<FuncListenTaskBuilder> postApply) {
36+
if (name == null) {
37+
list.listen(
38+
lb -> {
39+
spec.accept(lb);
40+
postApply.accept(lb);
41+
});
42+
} else {
43+
list.listen(
44+
name,
45+
lb -> {
46+
spec.accept(lb);
47+
postApply.accept(lb);
48+
});
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)