Skip to content

Commit da2d5a5

Browse files
committed
Introduce enrich and enrichOutput FuncDSL methods
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent f95b082 commit da2d5a5

4 files changed

Lines changed: 403 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.WorkflowModel;
19+
20+
/**
21+
* A function that enriches a typed value by combining it with the root workflow input.
22+
*
23+
* <p>This is useful when you need to merge a task output or state value with the original workflow
24+
* input, where the value is typed and the root input is a {@link WorkflowModel}.
25+
*
26+
* @param <T> The type of the input value to be enriched
27+
* @param <R> The type of the enriched result
28+
*/
29+
@FunctionalInterface
30+
public interface EnrichWithModelBiFunction<T, R> {
31+
/**
32+
* Applies this function to enrich a typed value with the root workflow input.
33+
*
34+
* @param value the typed value to be enriched (for example a task output or state value)
35+
* @param rootInput the original workflow input as {@link WorkflowModel}
36+
* @return the enriched result combining both inputs
37+
*/
38+
R apply(T value, WorkflowModel rootInput);
39+
}

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

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.serverlessworkflow.fluent.spec.configurers.AuthenticationConfigurer;
3333
import io.serverlessworkflow.impl.TaskContextData;
3434
import io.serverlessworkflow.impl.WorkflowContextData;
35+
import io.serverlessworkflow.impl.WorkflowModel;
3536
import java.net.URI;
3637
import java.util.Collection;
3738
import java.util.List;
@@ -279,6 +280,176 @@ public static FuncPredicateEventConfigurer event(String type) {
279280
return OPS.event(type);
280281
}
281282

283+
/**
284+
* Create an input transformation that enriches the typed last state with the root workflow input
285+
* as WorkflowModel.
286+
*
287+
* <p>This is useful when you want to combine the last task output (with its actual type) with the
288+
* original workflow input as WorkflowModel.
289+
*
290+
* <p>Example usage:
291+
*
292+
* <pre>{@code
293+
* function("processData", (Long input) -> input + 5, Long.class),
294+
* function("combineData", (Long enrichedValue) -> enrichedValue, Long.class)
295+
* .inputFrom(enrich((lastState, rootInputModel) -> {
296+
* Long rootInput = rootInputModel.as(Long.class).orElse(0L);
297+
* return lastState + rootInput;
298+
* }, Long.class))
299+
* }</pre>
300+
*
301+
* @param fn the enrichment function that receives typed lastState and WorkflowModel rootInput
302+
* @param lastStateClass the class of the last state type
303+
* @param <T> the type of the last state
304+
* @param <R> the type of the enriched result
305+
* @return a JavaContextFunction that can be used with inputFrom
306+
*/
307+
public static <T, R> JavaContextFunction<T, R> enrich(
308+
EnrichWithModelBiFunction<T, R> fn, Class<T> lastStateClass) {
309+
return (lastState, workflowContext) -> {
310+
Objects.requireNonNull(lastStateClass, "lastStateClass must not be null");
311+
final WorkflowModel rootInput = workflowContext.instanceData().input();
312+
final WorkflowModel lastStateModel =
313+
workflowContext.definition().application().modelFactory().fromAny(lastState);
314+
final T typedLastState =
315+
lastStateModel
316+
.as(lastStateClass)
317+
.orElseThrow(
318+
() ->
319+
new IllegalArgumentException(
320+
"Cannot convert lastState of type "
321+
+ (lastState != null ? lastState.getClass().getName() : "null")
322+
+ " to "
323+
+ lastStateClass.getName()));
324+
return fn.apply(typedLastState, rootInput);
325+
};
326+
}
327+
328+
/**
329+
* Create an input transformation that uses only the root workflow input as WorkflowModel.
330+
*
331+
* <p>This is useful when you want to transform the task input based solely on the original
332+
* workflow input, ignoring the last state.
333+
*
334+
* <p>Example usage:
335+
*
336+
* <pre>{@code
337+
* function("processData", (Long input) -> input * 2, Long.class)
338+
* .inputFrom(enrich(rootInput -> rootInput.asNumber().orElseThrow()))
339+
* }</pre>
340+
*
341+
* @param fn the function that receives the root workflow input and returns the enriched result
342+
* @param <R> the type of the enriched result
343+
* @return a JavaContextFunction that can be used with inputFrom
344+
*/
345+
public static <R> JavaContextFunction<Object, R> enrich(Function<WorkflowModel, R> fn) {
346+
return (lastState, workflowContext) -> {
347+
final WorkflowModel rootInput = workflowContext.instanceData().input();
348+
return fn.apply(rootInput);
349+
};
350+
}
351+
352+
/**
353+
* Create an output transformation that enriches the typed task output with the root workflow
354+
* input as WorkflowModel.
355+
*
356+
* <p>This is useful when you want to combine the task output (with its actual type) with the
357+
* original workflow input as WorkflowModel.
358+
*
359+
* <p>Example usage:
360+
*
361+
* <pre>{@code
362+
* function("processData", (Long input) -> input + 5, Long.class)
363+
* .outputAs(FuncDSL.enrichOutput((taskOutput, rootInputModel) -> {
364+
* Long rootInput = rootInputModel.as(Long.class).orElse(0L);
365+
* return taskOutput + rootInput;
366+
* }, Long.class))
367+
* }</pre>
368+
*
369+
* @param fn the enrichment function that receives typed taskOutput and WorkflowModel rootInput
370+
* @param taskOutputClass the class of the task output type
371+
* @param <T> the type of the task output
372+
* @param <R> the type of the enriched result
373+
* @return a JavaContextFunction that can be used with outputAs
374+
*/
375+
public static <T, R> JavaContextFunction<T, R> enrichOutput(
376+
EnrichWithModelBiFunction<T, R> fn, Class<T> taskOutputClass) {
377+
return (taskOutput, workflowContext) -> {
378+
Objects.requireNonNull(taskOutputClass, "taskOutputClass must not be null");
379+
final WorkflowModel rootInput = workflowContext.instanceData().input();
380+
final T typedTaskOutput = convertTaskOutput(taskOutput, taskOutputClass);
381+
return fn.apply(typedTaskOutput, rootInput);
382+
};
383+
}
384+
385+
private static <T> T convertTaskOutput(Object taskOutput, Class<T> taskOutputClass) {
386+
if (taskOutput == null) {
387+
return null;
388+
}
389+
if (taskOutputClass.isInstance(taskOutput)) {
390+
return taskOutputClass.cast(taskOutput);
391+
}
392+
if (Number.class.isAssignableFrom(taskOutputClass) && taskOutput instanceof Number) {
393+
Number number = (Number) taskOutput;
394+
if (taskOutputClass == Long.class) {
395+
@SuppressWarnings("unchecked")
396+
T converted = (T) Long.valueOf(number.longValue());
397+
return converted;
398+
}
399+
if (taskOutputClass == Integer.class) {
400+
@SuppressWarnings("unchecked")
401+
T converted = (T) Integer.valueOf(number.intValue());
402+
return converted;
403+
}
404+
if (taskOutputClass == Short.class) {
405+
@SuppressWarnings("unchecked")
406+
T converted = (T) Short.valueOf(number.shortValue());
407+
return converted;
408+
}
409+
if (taskOutputClass == Byte.class) {
410+
@SuppressWarnings("unchecked")
411+
T converted = (T) Byte.valueOf(number.byteValue());
412+
return converted;
413+
}
414+
if (taskOutputClass == Double.class) {
415+
@SuppressWarnings("unchecked")
416+
T converted = (T) Double.valueOf(number.doubleValue());
417+
return converted;
418+
}
419+
if (taskOutputClass == Float.class) {
420+
@SuppressWarnings("unchecked")
421+
T converted = (T) Float.valueOf(number.floatValue());
422+
return converted;
423+
}
424+
}
425+
// Fallback to the original behavior for incompatible types.
426+
return taskOutputClass.cast(taskOutput);
427+
}
428+
429+
/**
430+
* Create an output transformation that uses only the root workflow input as WorkflowModel.
431+
*
432+
* <p>This is useful when you want to transform the task output based solely on the original
433+
* workflow input, ignoring the actual task output.
434+
*
435+
* <p>Example usage:
436+
*
437+
* <pre>{@code
438+
* function("processData", (Long input) -> input * 2, Long.class)
439+
* .outputAs(FuncDSL.enrichOutput(rootInput -> rootInput.asNumber().orElseThrow()))
440+
* }</pre>
441+
*
442+
* @param fn the function that receives the root workflow input and returns the enriched result
443+
* @param <R> the type of the enriched result
444+
* @return a JavaContextFunction that can be used with outputAs
445+
*/
446+
public static <R> JavaContextFunction<Object, R> enrichOutput(Function<WorkflowModel, R> fn) {
447+
return (taskOutput, workflowContext) -> {
448+
final WorkflowModel rootInput = workflowContext.instanceData().input();
449+
return fn.apply(rootInput);
450+
};
451+
}
452+
282453
/**
283454
* Create a {@link FuncCallStep} that calls a simple Java {@link Function} with explicit input
284455
* type.

impl/test/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
<artifactId>grpc-netty</artifactId>
9595
<scope>test</scope>
9696
</dependency>
97+
<dependency>
98+
<groupId>io.serverlessworkflow</groupId>
99+
<artifactId>serverlessworkflow-experimental-fluent-func</artifactId>
100+
<scope>test</scope>
101+
<version>${project.version}</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>io.serverlessworkflow</groupId>
105+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
106+
<scope>test</scope>
107+
<version>${project.version}</version>
108+
</dependency>
97109
</dependencies>
98110
<build>
99111
<resources>

0 commit comments

Comments
 (0)