|
32 | 32 | import io.serverlessworkflow.fluent.spec.configurers.AuthenticationConfigurer; |
33 | 33 | import io.serverlessworkflow.impl.TaskContextData; |
34 | 34 | import io.serverlessworkflow.impl.WorkflowContextData; |
| 35 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 36 | +import io.serverlessworkflow.impl.jackson.JsonUtils; |
35 | 37 | import java.net.URI; |
36 | 38 | import java.util.Collection; |
37 | 39 | import java.util.List; |
@@ -279,6 +281,123 @@ public static FuncPredicateEventConfigurer event(String type) { |
279 | 281 | return OPS.event(type); |
280 | 282 | } |
281 | 283 |
|
| 284 | + /** |
| 285 | + * Create an input transformation that enriches the typed last state with the root workflow input |
| 286 | + * as WorkflowModel. |
| 287 | + * |
| 288 | + * <p>This is useful when you want to combine the last task output (with its actual type) with the |
| 289 | + * original workflow input as WorkflowModel. |
| 290 | + * |
| 291 | + * <p>Example usage: |
| 292 | + * |
| 293 | + * <pre>{@code |
| 294 | + * function("processData", (Long input) -> input + 5, Long.class), |
| 295 | + * function("combineData", (Long enrichedValue) -> enrichedValue, Long.class) |
| 296 | + * .inputFrom( |
| 297 | + * FuncDSL.enrichInput((Long lastState, WorkflowModel rootInputModel) -> { |
| 298 | + * Long rootInput = rootInputModel.as(Long.class).orElse(0L); |
| 299 | + * return lastState + rootInput; |
| 300 | + * }), |
| 301 | + * Long.class) |
| 302 | + * }</pre> |
| 303 | + * |
| 304 | + * @param fn the enrichment function that receives typed lastState and WorkflowModel rootInput |
| 305 | + * @param <T> the type of the last state |
| 306 | + * @param <R> the type of the enriched result |
| 307 | + * @return a JavaContextFunction that can be used with inputFrom |
| 308 | + */ |
| 309 | + public static <T, R> JavaContextFunction<T, R> enrichInput(EnrichWithModelBiFunction<T, R> fn) { |
| 310 | + return (lastState, workflowContext) -> { |
| 311 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 312 | + return fn.apply(lastState, rootInput); |
| 313 | + }; |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * Create an input transformation that uses only the root workflow input as WorkflowModel. |
| 318 | + * |
| 319 | + * <p>This is useful when you want to transform the task input based solely on the original |
| 320 | + * workflow input, ignoring the last state. |
| 321 | + * |
| 322 | + * <p>Example usage: |
| 323 | + * |
| 324 | + * <pre>{@code |
| 325 | + * function("processData", (Long input) -> input * 2, Long.class) |
| 326 | + * .inputFrom(enrichInput(rootInput -> rootInput.asNumber().orElseThrow())) |
| 327 | + * }</pre> |
| 328 | + * |
| 329 | + * @param fn the function that receives the root workflow input and returns the enriched result |
| 330 | + * @param <R> the type of the enriched result |
| 331 | + * @return a JavaContextFunction that can be used with inputFrom |
| 332 | + */ |
| 333 | + public static <R> JavaContextFunction<Object, R> enrichInput(Function<WorkflowModel, R> fn) { |
| 334 | + return (lastState, workflowContext) -> { |
| 335 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 336 | + return fn.apply(rootInput); |
| 337 | + }; |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * Create an output transformation that enriches the typed task output with the root workflow |
| 342 | + * input as WorkflowModel. |
| 343 | + * |
| 344 | + * <p>This is useful when you want to combine the task output (converted to a specific type) with |
| 345 | + * the original workflow input as WorkflowModel. The task output will be converted to the |
| 346 | + * specified type using Jackson conversion before being passed to the enrichment function. |
| 347 | + * |
| 348 | + * <p>Example usage: |
| 349 | + * |
| 350 | + * <pre>{@code |
| 351 | + * // Task returns Long, but we want to work with it as Integer in the enrichment function |
| 352 | + * function("processData", (Long input) -> input + 5, Long.class) |
| 353 | + * .outputAs(FuncDSL.enrichOutput((taskOutput, rootInputModel) -> { |
| 354 | + * // taskOutput is converted from Long to Integer |
| 355 | + * Long rootInput = rootInputModel.as(Long.class).orElse(0L); |
| 356 | + * return taskOutput + rootInput.intValue(); |
| 357 | + * }, Integer.class)) |
| 358 | + * }</pre> |
| 359 | + * |
| 360 | + * @param fn the enrichment function that receives typed taskOutput and WorkflowModel rootInput |
| 361 | + * @param taskOutputClass the target type to convert the task output to before passing it to the |
| 362 | + * enrichment function |
| 363 | + * @param <T> the target type for the task output conversion |
| 364 | + * @param <R> the type of the enriched result |
| 365 | + * @return a JavaContextFunction that can be used with outputAs |
| 366 | + */ |
| 367 | + public static <T, R> JavaContextFunction<T, R> enrichOutput( |
| 368 | + EnrichWithModelBiFunction<T, R> fn, Class<T> taskOutputClass) { |
| 369 | + return (taskOutput, workflowContext) -> { |
| 370 | + Objects.requireNonNull(taskOutputClass, "taskOutputClass must not be null"); |
| 371 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 372 | + final T typedTaskOutput = JsonUtils.convertValue(taskOutput, taskOutputClass); |
| 373 | + return fn.apply(typedTaskOutput, rootInput); |
| 374 | + }; |
| 375 | + } |
| 376 | + |
| 377 | + /** |
| 378 | + * Create an output transformation that uses only the root workflow input as WorkflowModel. |
| 379 | + * |
| 380 | + * <p>This is useful when you want to transform the task output based solely on the original |
| 381 | + * workflow input, ignoring the actual task output. |
| 382 | + * |
| 383 | + * <p>Example usage: |
| 384 | + * |
| 385 | + * <pre>{@code |
| 386 | + * function("processData", (Long input) -> input * 2, Long.class) |
| 387 | + * .outputAs(FuncDSL.enrichOutput(rootInput -> rootInput.asNumber().orElseThrow())) |
| 388 | + * }</pre> |
| 389 | + * |
| 390 | + * @param fn the function that receives the root workflow input and returns the enriched result |
| 391 | + * @param <R> the type of the enriched result |
| 392 | + * @return a JavaContextFunction that can be used with outputAs |
| 393 | + */ |
| 394 | + public static <R> JavaContextFunction<Object, R> enrichOutput(Function<WorkflowModel, R> fn) { |
| 395 | + return (ignore, workflowContext) -> { |
| 396 | + final WorkflowModel rootInput = workflowContext.instanceData().input(); |
| 397 | + return fn.apply(rootInput); |
| 398 | + }; |
| 399 | + } |
| 400 | + |
282 | 401 | /** |
283 | 402 | * Create a {@link FuncCallStep} that calls a simple Java {@link Function} with explicit input |
284 | 403 | * type. |
|
0 commit comments