Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility class for writing Serverless Workflow definitions to various outputs and formats.
Expand All @@ -32,6 +34,8 @@
*/
public class WorkflowWriter {

private static final Logger logger = LoggerFactory.getLogger(WorkflowWriter.class);

/**
* Writes a {@link Workflow} to the given {@link OutputStream} in the specified format.
*
Expand Down Expand Up @@ -95,7 +99,9 @@ public static void writeWorkflow(Path output, Workflow workflow, WorkflowFormat
*/
public static String workflowAsString(Workflow workflow, WorkflowFormat format)
throws IOException {
return format.mapper().writeValueAsString(workflow);
String result = format.mapper().writeValueAsString(workflow);
logger.trace("Workflow is {}", result);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.serverlessworkflow.fluent.func;

import io.serverlessworkflow.api.types.CallTask;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.api.types.func.CallTaskJava;
import io.serverlessworkflow.api.types.func.ContextFunction;
import io.serverlessworkflow.api.types.func.FilterFunction;
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
Expand All @@ -29,12 +29,9 @@ public class FuncCallTaskBuilder extends TaskBaseBuilder<FuncCallTaskBuilder>
implements FuncTaskTransformations<FuncCallTaskBuilder>,
ConditionalTaskBuilder<FuncCallTaskBuilder> {

private CallTaskJava callTaskJava;
private CallTask callTaskJava;

FuncCallTaskBuilder() {
callTaskJava = new CallTaskJava(new CallJava() {});
super.setTask(callTaskJava.getCallJava());
}
FuncCallTaskBuilder() {}

@Override
protected FuncCallTaskBuilder self() {
Expand All @@ -51,8 +48,9 @@ public <T, V> FuncCallTaskBuilder function(Function<T, V> function, Class<T> arg

public <T, V> FuncCallTaskBuilder function(
Function<T, V> function, Class<T> argClass, Class<V> returnClass) {
this.callTaskJava = new CallTaskJava(CallJava.function(function, argClass, returnClass));
super.setTask(this.callTaskJava.getCallJava());
this.callTaskJava =
new CallTask().withCallFunction(CallJava.function(function, argClass, returnClass));
super.setTask(this.callTaskJava.getCallFunction());
return this;
}

Expand All @@ -66,8 +64,9 @@ public <T, V> FuncCallTaskBuilder function(ContextFunction<T, V> function, Class

public <T, V> FuncCallTaskBuilder function(
ContextFunction<T, V> function, Class<T> argClass, Class<V> returnClass) {
this.callTaskJava = new CallTaskJava(CallJava.function(function, argClass, returnClass));
super.setTask(this.callTaskJava.getCallJava());
this.callTaskJava =
new CallTask().withCallFunction(CallJava.function(function, argClass, returnClass));
super.setTask(this.callTaskJava.getCallFunction());
return this;
}

Expand All @@ -81,26 +80,27 @@ public <T, V> FuncCallTaskBuilder function(FilterFunction<T, V> function, Class<

public <T, V> FuncCallTaskBuilder function(
FilterFunction<T, V> function, Class<T> argClass, Class<V> outputClass) {
this.callTaskJava = new CallTaskJava(CallJava.function(function, argClass, outputClass));
super.setTask(this.callTaskJava.getCallJava());
this.callTaskJava =
new CallTask().withCallFunction(CallJava.function(function, argClass, outputClass));
super.setTask(this.callTaskJava.getCallFunction());
return this;
}

/** Accept a side-effect Consumer; engine should pass input through unchanged. */
public <T> FuncCallTaskBuilder consumer(Consumer<T> consumer) {
this.callTaskJava = new CallTaskJava(CallJava.consumer(consumer));
super.setTask(this.callTaskJava.getCallJava());
this.callTaskJava = new CallTask().withCallFunction(CallJava.consumer(consumer));
super.setTask(this.callTaskJava.getCallFunction());
return this;
}

/** Accept a Consumer with explicit input type hint. */
public <T> FuncCallTaskBuilder consumer(Consumer<T> consumer, Class<T> argClass) {
this.callTaskJava = new CallTaskJava(CallJava.consumer(consumer, argClass));
super.setTask(this.callTaskJava.getCallJava());
this.callTaskJava = new CallTask().withCallFunction(CallJava.consumer(consumer, argClass));
super.setTask(this.callTaskJava.getCallFunction());
return this;
}

public CallTaskJava build() {
public CallTask build() {
return this.callTaskJava;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package io.serverlessworkflow.fluent.func;

import io.serverlessworkflow.api.types.CallTask;
import io.serverlessworkflow.api.types.ForTask;
import io.serverlessworkflow.api.types.ForTaskConfiguration;
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.api.types.func.CallTaskJava;
import io.serverlessworkflow.api.types.func.ForTaskFunction;
import io.serverlessworkflow.api.types.func.LoopFunction;
import io.serverlessworkflow.api.types.func.LoopPredicate;
Expand All @@ -39,14 +40,16 @@ public class FuncForTaskBuilder extends TaskBaseBuilder<FuncForTaskBuilder>
ConditionalTaskBuilder<FuncForTaskBuilder>,
ForEachTaskFluent<FuncForTaskBuilder, FuncTaskItemListBuilder> {

private final ForTask forTask;
private final ForTaskFunction forTaskFunction;
private final List<TaskItem> items;

FuncForTaskBuilder() {
this.forTaskFunction = new ForTaskFunction();
this.forTaskFunction.withFor(new ForTaskConfiguration());
this.forTask = new ForTask();
this.forTaskFunction = new ForTaskFunction(forTask);
this.forTask.withFor(new ForTaskConfiguration());
this.items = new ArrayList<>();
super.setTask(forTaskFunction);
super.setTask(forTask);
}

@Override
Expand Down Expand Up @@ -84,9 +87,9 @@ public <T, V, R> FuncForTaskBuilder tasks(String name, LoopFunction<T, V, R> fun
name,
new Task()
.withCallTask(
new CallTaskJava(
CallJava.loopFunction(
function, this.forTaskFunction.getFor().getEach())))));
new CallTask()
.withCallFunction(
CallJava.loopFunction(function, this.forTask.getFor().getEach())))));
return this;
}

Expand All @@ -96,25 +99,25 @@ public <T, V, R> FuncForTaskBuilder tasks(LoopFunction<T, V, R> function) {

@Override
public FuncForTaskBuilder each(String each) {
this.forTaskFunction.getFor().withEach(each);
this.forTask.getFor().withEach(each);
return this;
}

@Override
public FuncForTaskBuilder in(String in) {
this.forTaskFunction.getFor().withIn(in);
this.forTask.getFor().withIn(in);
return this;
}

@Override
public FuncForTaskBuilder at(String at) {
this.forTaskFunction.getFor().withAt(at);
this.forTask.getFor().withAt(at);
return this;
}

@Override
public FuncForTaskBuilder whileC(String expression) {
this.forTaskFunction.setWhile(expression);
this.forTask.setWhile(expression);
return this;
}

Expand All @@ -125,8 +128,8 @@ public FuncForTaskBuilder tasks(Consumer<FuncTaskItemListBuilder> consumer) {
return this;
}

public ForTaskFunction build() {
this.forTaskFunction.setDo(this.items);
return this.forTaskFunction;
public ForTask build() {
this.forTask.setDo(this.items);
return this.forTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package io.serverlessworkflow.fluent.func;

import io.serverlessworkflow.api.types.CallTask;
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskItem;
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.api.types.func.CallTaskJava;
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
import io.serverlessworkflow.fluent.func.spi.FuncTaskTransformations;
import io.serverlessworkflow.fluent.spec.AbstractForkTaskBuilder;
Expand Down Expand Up @@ -61,7 +61,8 @@ public <T, V> FuncForkTaskBuilder branch(
this.defaultBranchName(name, this.currentOffset()),
new Task()
.withCallTask(
new CallTaskJava(CallJava.function(function, argParam, returnClass)))));
new CallTask()
.withCallFunction(CallJava.function(function, argParam, returnClass)))));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.serverlessworkflow.fluent.spec.WaitTaskBuilder;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class FuncTaskItemListBuilder extends BaseTaskItemListBuilder<FuncTaskItemListBuilder>
Expand Down Expand Up @@ -78,6 +79,10 @@ public FuncTaskItemListBuilder set(String name, String expr) {
return this.set(name, s -> s.expr(expr));
}

public FuncTaskItemListBuilder set(String name, Map<String, Object> map) {
return this.set(name, s -> s.expr(map));
}

@Override
public FuncTaskItemListBuilder emit(String name, Consumer<FuncEmitTaskBuilder> itemsConfigurer) {
name = this.defaultNameAndRequireConfig(name, itemsConfigurer, TYPE_EMIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ public static <T, V> FuncTaskConfigurer forEach(
}

public static <T, V> FuncTaskConfigurer forEachItem(
SerializableFunction<T, Collection<V>> collection, Function<V, ?> function) {
SerializableFunction<T, Collection<V>> collection, SerializableFunction<V, ?> function) {
return forEachItem(null, collection, function);
}

Expand Down
36 changes: 36 additions & 0 deletions experimental/fluent/jackson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-fluent</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-fluent-serialization-jackson</artifactId>
<name>Serverless Workflow :: Experimental :: Fluent :: Serialization:: Jackson</name>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-serialization</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-json</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.api.types.func;
package io.serverlessworkflow.fluent.func.serialization.jackson;

import java.util.Optional;
import com.fasterxml.jackson.databind.util.StdConverter;
import java.util.Map;

public abstract class CallAbstractJavaFunction<T, V> extends CallJava<T> {
public abstract class AbstractMapValueTransformer<T> extends StdConverter<T, T> {

private static final long serialVersionUID = 1L;

private final Optional<Class<V>> outputClass;

protected CallAbstractJavaFunction() {
this(Optional.empty(), Optional.empty());
@Override
public T convert(T value) {
for (Map.Entry<String, Object> entry : map(value).entrySet()) {
GlobalConverterRegistry.get()
.findConverter(entry.getKey())
.ifPresent(c -> entry.setValue(c.apply(entry.getValue())));
}
return value;
}

protected CallAbstractJavaFunction(
Optional<Class<T>> inputClass, Optional<Class<V>> outputClass) {
super(inputClass);
this.outputClass = outputClass;
}

public Optional<Class<V>> outputClass() {
return outputClass;
}
protected abstract Map<String, Object> map(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.func.serialization.jackson;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(converter = CallJavaDeserializer.class)
public abstract class CallFunctionMixIn {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.func.serialization.jackson;

import com.fasterxml.jackson.databind.util.StdConverter;
import io.serverlessworkflow.api.types.CallFunction;
import io.serverlessworkflow.api.types.func.CallJava;

public class CallJavaDeserializer extends StdConverter<CallFunction, CallFunction> {

@Override
public CallFunction convert(CallFunction value) {
return value.getCall().equals(CallJava.JAVA_CALL_KEY)
? CallJava.fromCallFunction(value)
: value;
}
}
Loading
Loading