Skip to content

Commit 58343da

Browse files
committed
Add consumer and JavaContextFunction shortcuts
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent e583216 commit 58343da

File tree

15 files changed

+1093
-55
lines changed

15 files changed

+1093
-55
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import io.serverlessworkflow.api.types.func.CallJava;
1919
import io.serverlessworkflow.api.types.func.CallTaskJava;
20+
import io.serverlessworkflow.api.types.func.JavaContextFunction;
2021
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
2122
import io.serverlessworkflow.fluent.func.spi.FuncTaskTransformations;
2223
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
24+
import java.util.function.Consumer;
2325
import java.util.function.Function;
2426

2527
public class FuncCallTaskBuilder extends TaskBaseBuilder<FuncCallTaskBuilder>
@@ -48,6 +50,31 @@ public <T, V> FuncCallTaskBuilder function(Function<T, V> function, Class<T> arg
4850
return this;
4951
}
5052

53+
public <T, V> FuncCallTaskBuilder function(JavaContextFunction<T, V> function) {
54+
return function(function, null);
55+
}
56+
57+
public <T, V> FuncCallTaskBuilder function(
58+
JavaContextFunction<T, V> function, Class<T> argClass) {
59+
this.callTaskJava = new CallTaskJava(CallJava.function(function, argClass));
60+
super.setTask(this.callTaskJava.getCallJava());
61+
return this;
62+
}
63+
64+
/** Accept a side-effect Consumer; engine should pass input through unchanged. */
65+
public <T> FuncCallTaskBuilder consumer(Consumer<T> consumer) {
66+
this.callTaskJava = new CallTaskJava(CallJava.consumer(consumer));
67+
super.setTask(this.callTaskJava.getCallJava());
68+
return this;
69+
}
70+
71+
/** Accept a Consumer with explicit input type hint. */
72+
public <T> FuncCallTaskBuilder consumer(Consumer<T> consumer, Class<T> argClass) {
73+
this.callTaskJava = new CallTaskJava(CallJava.consumer(consumer, argClass));
74+
super.setTask(this.callTaskJava.getCallJava());
75+
return this;
76+
}
77+
5178
public CallTaskJava build() {
5279
return this.callTaskJava;
5380
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
22+
public final class ConsumeStep<T> extends Step<ConsumeStep<T>, FuncCallTaskBuilder> {
23+
private final String name; // may be null
24+
private final Consumer<T> consumer;
25+
private final Class<T> argClass; // may be null
26+
27+
ConsumeStep(Consumer<T> consumer, Class<T> argClass) {
28+
this(null, consumer, argClass);
29+
}
30+
31+
ConsumeStep(String name, Consumer<T> consumer, Class<T> argClass) {
32+
this.name = name;
33+
this.consumer = consumer;
34+
this.argClass = argClass;
35+
}
36+
37+
@Override
38+
protected void configure(
39+
FuncTaskItemListBuilder list, java.util.function.Consumer<FuncCallTaskBuilder> post) {
40+
if (name == null) {
41+
list.callFn(
42+
cb -> {
43+
// prefer the typed consumer if your builder supports it; otherwise fallback:
44+
if (argClass != null) cb.consumer(consumer, argClass);
45+
else cb.consumer(consumer);
46+
post.accept(cb);
47+
});
48+
} else {
49+
list.callFn(
50+
name,
51+
cb -> {
52+
if (argClass != null) cb.consumer(consumer, argClass);
53+
else cb.consumer(consumer);
54+
post.accept(cb);
55+
});
56+
}
57+
}
58+
}
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.func.dsl;
17+
18+
import io.serverlessworkflow.impl.WorkflowContextData;
19+
20+
/**
21+
* Functions that expect a {@link WorkflowContextData} injection in runtime
22+
*
23+
* @param <T> The task payload input
24+
* @param <R> The task result output
25+
*/
26+
@FunctionalInterface
27+
public interface CtxBiFunction<T, R> {
28+
R apply(WorkflowContextData context, T payload);
29+
}

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,61 @@
1515
*/
1616
package io.serverlessworkflow.fluent.func.dsl;
1717

18+
import io.serverlessworkflow.api.types.func.JavaContextFunction;
1819
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
1920
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
2021
import java.util.function.Consumer;
2122
import java.util.function.Function;
2223

23-
// FuncCallStep
2424
public final class FuncCallStep<T, R> extends Step<FuncCallStep<T, R>, FuncCallTaskBuilder> {
25-
private final String name; // may be null
25+
26+
private final String name;
2627
private final Function<T, R> fn;
28+
private final JavaContextFunction<T, R> ctxFn;
2729
private final Class<T> argClass;
2830

31+
/** Function<T,R> variant (unnamed). */
2932
FuncCallStep(Function<T, R> fn, Class<T> argClass) {
3033
this(null, fn, argClass);
3134
}
3235

36+
/** Function<T,R> variant (named). */
3337
FuncCallStep(String name, Function<T, R> fn, Class<T> argClass) {
3438
this.name = name;
3539
this.fn = fn;
40+
this.ctxFn = null;
41+
this.argClass = argClass;
42+
}
43+
44+
/** JavaContextFunction<T,R> variant (unnamed). */
45+
FuncCallStep(JavaContextFunction<T, R> ctxFn, Class<T> argClass) {
46+
this(null, ctxFn, argClass);
47+
}
48+
49+
/** JavaContextFunction<T,R> variant (named). */
50+
FuncCallStep(String name, JavaContextFunction<T, R> ctxFn, Class<T> argClass) {
51+
this.name = name;
52+
this.fn = null;
53+
this.ctxFn = ctxFn;
3654
this.argClass = argClass;
3755
}
3856

3957
@Override
4058
protected void configure(FuncTaskItemListBuilder list, Consumer<FuncCallTaskBuilder> post) {
41-
if (name == null) {
42-
list.callFn(
43-
cb -> {
59+
final Consumer<FuncCallTaskBuilder> apply =
60+
cb -> {
61+
if (ctxFn != null) {
62+
cb.function(ctxFn, argClass);
63+
} else {
4464
cb.function(fn, argClass);
45-
post.accept(cb);
46-
});
65+
}
66+
post.accept(cb);
67+
};
68+
69+
if (name == null) {
70+
list.callFn(apply);
4771
} else {
48-
list.callFn(
49-
name,
50-
cb -> {
51-
cb.function(fn, argClass);
52-
post.accept(cb);
53-
});
72+
list.callFn(name, apply);
5473
}
5574
}
5675
}

0 commit comments

Comments
 (0)