Skip to content

transition and functionref update #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
@@ -0,0 +1,78 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.api.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.serverlessworkflow.api.functions.FunctionRef;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FunctionRefDeserializer extends StdDeserializer<FunctionRef> {

private static final long serialVersionUID = 510l;
private static Logger logger = LoggerFactory.getLogger(FunctionRefDeserializer.class);

private WorkflowPropertySource context;

public FunctionRefDeserializer() {
this(FunctionRef.class);
}

public FunctionRefDeserializer(Class<?> vc) {
super(vc);
}

public FunctionRefDeserializer(WorkflowPropertySource context) {
this(FunctionRef.class);
this.context = context;
}

@Override
public FunctionRef deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {

ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode node = jp.getCodec().readTree(jp);

FunctionRef functionRef = new FunctionRef();

if (!node.isObject()) {
functionRef.setRefName(node.asText());
functionRef.setParameters(new HashMap<>());
return functionRef;
} else {
if(node.get("parameters") != null) {
functionRef.setParameters(mapper.treeToValue(node.get("parameters"), Map.class));
}

if(node.get("refName") != null) {
functionRef.setRefName(node.get("refName").asText());
}

return functionRef;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.api.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
import io.serverlessworkflow.api.produce.ProduceEvent;
import io.serverlessworkflow.api.transitions.Transition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class TransitionDeserializer extends StdDeserializer<Transition> {

private static final long serialVersionUID = 510l;
private static Logger logger = LoggerFactory.getLogger(TransitionDeserializer.class);

private WorkflowPropertySource context;

public TransitionDeserializer() {
this(Transition.class);
}

public TransitionDeserializer(Class<?> vc) {
super(vc);
}

public TransitionDeserializer(WorkflowPropertySource context) {
this(Transition.class);
this.context = context;
}

@Override
public Transition deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {

ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode node = jp.getCodec().readTree(jp);

Transition transition = new Transition();

if (!node.isObject()) {
transition.setProduceEvents(new ArrayList<>());
transition.setCompensate(false);
transition.setNextState(node.asText());
return transition;
} else {
if(node.get("produceEvents") != null) {
transition.setProduceEvents(Arrays.asList(mapper.treeToValue(node.get("produceEvents"), ProduceEvent[].class)) );
}

if(node.get("nextState") != null) {
transition.setNextState(node.get("nextState").asText());
}

if(node.get("compensate") != null) {
transition.setCompensate(node.get("compensate").asBoolean());
}

return transition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.serverlessworkflow.api.events.EventDefinition;
import io.serverlessworkflow.api.events.OnEvents;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.functions.FunctionRef;
import io.serverlessworkflow.api.interfaces.Extension;
import io.serverlessworkflow.api.interfaces.State;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
Expand All @@ -31,6 +32,7 @@
import io.serverlessworkflow.api.states.DefaultState;
import io.serverlessworkflow.api.states.OperationState;
import io.serverlessworkflow.api.states.ParallelState;
import io.serverlessworkflow.api.transitions.Transition;
import io.serverlessworkflow.api.workflow.Events;
import io.serverlessworkflow.api.workflow.Functions;
import io.serverlessworkflow.api.workflow.Retries;
Expand Down Expand Up @@ -69,6 +71,8 @@ private void addDefaultSerializers() {
addSerializer(new CallbackStateSerializer());
addSerializer(new StartDefinitionSerializer());
addSerializer(new EndDefinitionSerializer());
addSerializer(new TransitionSerializer());
addSerializer(new FunctionRefSerializer());
addSerializer(extensionSerializer);
}

Expand All @@ -93,7 +97,8 @@ private void addDefaultDeserializers() {
addDeserializer(End.class, new EndDefinitionDeserializer(workflowPropertySource));
addDeserializer(Extension.class, extensionDeserializer);
addDeserializer(FunctionDefinition.Type.class, new FunctionDefinitionTypeDeserializer(workflowPropertySource));

addDeserializer(Transition.class, new TransitionDeserializer(workflowPropertySource));
addDeserializer(FunctionRef.class, new FunctionRefDeserializer(workflowPropertySource));
}

public ExtensionSerializer getExtensionSerializer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.api.serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.serverlessworkflow.api.functions.FunctionRef;

import java.io.IOException;

public class FunctionRefSerializer extends StdSerializer<FunctionRef> {

public FunctionRefSerializer() {
this(FunctionRef.class);
}

protected FunctionRefSerializer(Class<FunctionRef> t) {
super(t);
}

@Override
public void serialize(FunctionRef functionRef,
JsonGenerator gen,
SerializerProvider provider) throws IOException {

if(functionRef != null) {
if((functionRef.getParameters() == null || functionRef.getParameters().isEmpty())
&& functionRef.getRefName() != null
&& functionRef.getRefName().length() > 0) {
gen.writeString(functionRef.getRefName());
} else {
gen.writeStartObject();

if(functionRef.getRefName() != null && functionRef.getRefName().length() > 0) {
gen.writeStringField("refName", functionRef.getRefName());
}

if (functionRef.getParameters() != null && !functionRef.getParameters().isEmpty()) {
gen.writeObjectField("parameters", functionRef.getParameters());
}


gen.writeEndObject();
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.api.serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.serverlessworkflow.api.produce.ProduceEvent;
import io.serverlessworkflow.api.transitions.Transition;

import java.io.IOException;

public class TransitionSerializer extends StdSerializer<Transition> {

public TransitionSerializer() {
this(Transition.class);
}

protected TransitionSerializer(Class<Transition> t) {
super(t);
}

@Override
public void serialize(Transition transition,
JsonGenerator gen,
SerializerProvider provider) throws IOException {

if(transition != null) {
if((transition.getProduceEvents() == null || transition.getProduceEvents().size() < 1)
&& !transition.isCompensate() && transition.getNextState() != null
&& transition.getNextState().length() > 0) {
gen.writeString(transition.getNextState());
} else {
gen.writeStartObject();

if (transition.getProduceEvents() != null && !transition.getProduceEvents().isEmpty()) {
gen.writeArrayFieldStart("produceEvents");
for (ProduceEvent produceEvent : transition.getProduceEvents()) {
gen.writeObject(produceEvent);
}
gen.writeEndArray();
}

if(transition.isCompensate()) {
gen.writeBooleanField("compensate", true);
}

if(transition.getNextState() != null && transition.getNextState().length() > 0) {
gen.writeStringField("nextState", transition.getNextState());
}

gen.writeEndObject();
}
}
}
}
Loading