Skip to content

Commit ce8afcb

Browse files
author
Tihomir Surdilovic
committed
transition and functionref update
Signed-off-by: Tihomir Surdilovic <[email protected]>
1 parent b24101a commit ce8afcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+619
-271
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
*/
17+
package io.serverlessworkflow.api.deserializers;
18+
19+
import com.fasterxml.jackson.core.JsonParser;
20+
import com.fasterxml.jackson.databind.DeserializationContext;
21+
import com.fasterxml.jackson.databind.JsonNode;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
24+
import io.serverlessworkflow.api.functions.FunctionRef;
25+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import java.io.IOException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
33+
public class FunctionRefDeserializer extends StdDeserializer<FunctionRef> {
34+
35+
private static final long serialVersionUID = 510l;
36+
private static Logger logger = LoggerFactory.getLogger(FunctionRefDeserializer.class);
37+
38+
private WorkflowPropertySource context;
39+
40+
public FunctionRefDeserializer() {
41+
this(FunctionRef.class);
42+
}
43+
44+
public FunctionRefDeserializer(Class<?> vc) {
45+
super(vc);
46+
}
47+
48+
public FunctionRefDeserializer(WorkflowPropertySource context) {
49+
this(FunctionRef.class);
50+
this.context = context;
51+
}
52+
53+
@Override
54+
public FunctionRef deserialize(JsonParser jp,
55+
DeserializationContext ctxt) throws IOException {
56+
57+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
58+
JsonNode node = jp.getCodec().readTree(jp);
59+
60+
FunctionRef functionRef = new FunctionRef();
61+
62+
if (!node.isObject()) {
63+
functionRef.setRefName(node.asText());
64+
functionRef.setParameters(new HashMap<>());
65+
return functionRef;
66+
} else {
67+
if(node.get("parameters") != null) {
68+
functionRef.setParameters(mapper.treeToValue(node.get("parameters"), Map.class));
69+
}
70+
71+
if(node.get("refName") != null) {
72+
functionRef.setRefName(node.get("refName").asText());
73+
}
74+
75+
return functionRef;
76+
}
77+
}
78+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
*/
17+
package io.serverlessworkflow.api.deserializers;
18+
19+
import com.fasterxml.jackson.core.JsonParser;
20+
import com.fasterxml.jackson.databind.DeserializationContext;
21+
import com.fasterxml.jackson.databind.JsonNode;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
24+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
25+
import io.serverlessworkflow.api.produce.ProduceEvent;
26+
import io.serverlessworkflow.api.transitions.Transition;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
34+
public class TransitionDeserializer extends StdDeserializer<Transition> {
35+
36+
private static final long serialVersionUID = 510l;
37+
private static Logger logger = LoggerFactory.getLogger(TransitionDeserializer.class);
38+
39+
private WorkflowPropertySource context;
40+
41+
public TransitionDeserializer() {
42+
this(Transition.class);
43+
}
44+
45+
public TransitionDeserializer(Class<?> vc) {
46+
super(vc);
47+
}
48+
49+
public TransitionDeserializer(WorkflowPropertySource context) {
50+
this(Transition.class);
51+
this.context = context;
52+
}
53+
54+
@Override
55+
public Transition deserialize(JsonParser jp,
56+
DeserializationContext ctxt) throws IOException {
57+
58+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
59+
JsonNode node = jp.getCodec().readTree(jp);
60+
61+
Transition transition = new Transition();
62+
63+
if (!node.isObject()) {
64+
transition.setProduceEvents(new ArrayList<>());
65+
transition.setCompensate(false);
66+
transition.setNextState(node.asText());
67+
return transition;
68+
} else {
69+
if(node.get("produceEvents") != null) {
70+
transition.setProduceEvents(Arrays.asList(mapper.treeToValue(node.get("produceEvents"), ProduceEvent[].class)) );
71+
}
72+
73+
if(node.get("nextState") != null) {
74+
transition.setNextState(node.get("nextState").asText());
75+
}
76+
77+
if(node.get("compensate") != null) {
78+
transition.setCompensate(node.get("compensate").asBoolean());
79+
}
80+
81+
return transition;
82+
}
83+
}
84+
}

api/src/main/java/io/serverlessworkflow/api/mapper/WorkflowModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.serverlessworkflow.api.events.EventDefinition;
2323
import io.serverlessworkflow.api.events.OnEvents;
2424
import io.serverlessworkflow.api.functions.FunctionDefinition;
25+
import io.serverlessworkflow.api.functions.FunctionRef;
2526
import io.serverlessworkflow.api.interfaces.Extension;
2627
import io.serverlessworkflow.api.interfaces.State;
2728
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
@@ -31,6 +32,7 @@
3132
import io.serverlessworkflow.api.states.DefaultState;
3233
import io.serverlessworkflow.api.states.OperationState;
3334
import io.serverlessworkflow.api.states.ParallelState;
35+
import io.serverlessworkflow.api.transitions.Transition;
3436
import io.serverlessworkflow.api.workflow.Events;
3537
import io.serverlessworkflow.api.workflow.Functions;
3638
import io.serverlessworkflow.api.workflow.Retries;
@@ -69,6 +71,8 @@ private void addDefaultSerializers() {
6971
addSerializer(new CallbackStateSerializer());
7072
addSerializer(new StartDefinitionSerializer());
7173
addSerializer(new EndDefinitionSerializer());
74+
addSerializer(new TransitionSerializer());
75+
addSerializer(new FunctionRefSerializer());
7276
addSerializer(extensionSerializer);
7377
}
7478

@@ -93,7 +97,8 @@ private void addDefaultDeserializers() {
9397
addDeserializer(End.class, new EndDefinitionDeserializer(workflowPropertySource));
9498
addDeserializer(Extension.class, extensionDeserializer);
9599
addDeserializer(FunctionDefinition.Type.class, new FunctionDefinitionTypeDeserializer(workflowPropertySource));
96-
100+
addDeserializer(Transition.class, new TransitionDeserializer(workflowPropertySource));
101+
addDeserializer(FunctionRef.class, new FunctionRefDeserializer(workflowPropertySource));
97102
}
98103

99104
public ExtensionSerializer getExtensionSerializer() {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
*/
17+
package io.serverlessworkflow.api.serializers;
18+
19+
import com.fasterxml.jackson.core.JsonGenerator;
20+
import com.fasterxml.jackson.databind.SerializerProvider;
21+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
22+
import io.serverlessworkflow.api.functions.FunctionRef;
23+
24+
import java.io.IOException;
25+
26+
public class FunctionRefSerializer extends StdSerializer<FunctionRef> {
27+
28+
public FunctionRefSerializer() {
29+
this(FunctionRef.class);
30+
}
31+
32+
protected FunctionRefSerializer(Class<FunctionRef> t) {
33+
super(t);
34+
}
35+
36+
@Override
37+
public void serialize(FunctionRef functionRef,
38+
JsonGenerator gen,
39+
SerializerProvider provider) throws IOException {
40+
41+
if(functionRef != null) {
42+
if((functionRef.getParameters() == null || functionRef.getParameters().isEmpty())
43+
&& functionRef.getRefName() != null
44+
&& functionRef.getRefName().length() > 0) {
45+
gen.writeString(functionRef.getRefName());
46+
} else {
47+
gen.writeStartObject();
48+
49+
if(functionRef.getRefName() != null && functionRef.getRefName().length() > 0) {
50+
gen.writeStringField("refName", functionRef.getRefName());
51+
}
52+
53+
if (functionRef.getParameters() != null && !functionRef.getParameters().isEmpty()) {
54+
gen.writeObjectField("parameters", functionRef.getParameters());
55+
}
56+
57+
58+
gen.writeEndObject();
59+
}
60+
}
61+
}
62+
}
63+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
*/
17+
package io.serverlessworkflow.api.serializers;
18+
19+
import com.fasterxml.jackson.core.JsonGenerator;
20+
import com.fasterxml.jackson.databind.SerializerProvider;
21+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
22+
import io.serverlessworkflow.api.produce.ProduceEvent;
23+
import io.serverlessworkflow.api.transitions.Transition;
24+
25+
import java.io.IOException;
26+
27+
public class TransitionSerializer extends StdSerializer<Transition> {
28+
29+
public TransitionSerializer() {
30+
this(Transition.class);
31+
}
32+
33+
protected TransitionSerializer(Class<Transition> t) {
34+
super(t);
35+
}
36+
37+
@Override
38+
public void serialize(Transition transition,
39+
JsonGenerator gen,
40+
SerializerProvider provider) throws IOException {
41+
42+
if(transition != null) {
43+
if((transition.getProduceEvents() == null || transition.getProduceEvents().size() < 1)
44+
&& !transition.isCompensate() && transition.getNextState() != null
45+
&& transition.getNextState().length() > 0) {
46+
gen.writeString(transition.getNextState());
47+
} else {
48+
gen.writeStartObject();
49+
50+
if (transition.getProduceEvents() != null && !transition.getProduceEvents().isEmpty()) {
51+
gen.writeArrayFieldStart("produceEvents");
52+
for (ProduceEvent produceEvent : transition.getProduceEvents()) {
53+
gen.writeObject(produceEvent);
54+
}
55+
gen.writeEndArray();
56+
}
57+
58+
if(transition.isCompensate()) {
59+
gen.writeBooleanField("compensate", true);
60+
}
61+
62+
if(transition.getNextState() != null && transition.getNextState().length() > 0) {
63+
gen.writeStringField("nextState", transition.getNextState());
64+
}
65+
66+
gen.writeEndObject();
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)