Skip to content

Commit 16e25fa

Browse files
author
Tihomir Surdilovic
committed
Functions and event definitions reference capability
Signed-off-by: Tihomir Surdilovic <[email protected]>
1 parent f657537 commit 16e25fa

File tree

19 files changed

+623
-47
lines changed

19 files changed

+623
-47
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
25+
import io.serverlessworkflow.api.events.EventDefinition;
26+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
27+
import io.serverlessworkflow.api.utils.Utils;
28+
import io.serverlessworkflow.api.workflow.Events;
29+
import org.json.JSONObject;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
37+
public class EventsDeserializer extends StdDeserializer<Events> {
38+
39+
private static final long serialVersionUID = 510l;
40+
private static Logger logger = LoggerFactory.getLogger(EventsDeserializer.class);
41+
42+
private WorkflowPropertySource context;
43+
44+
public EventsDeserializer() {
45+
this(Events.class);
46+
}
47+
48+
public EventsDeserializer(Class<?> vc) {
49+
super(vc);
50+
}
51+
52+
public EventsDeserializer(WorkflowPropertySource context) {
53+
this(Events.class);
54+
this.context = context;
55+
}
56+
57+
@Override
58+
public Events deserialize(JsonParser jp,
59+
DeserializationContext ctxt) throws IOException {
60+
61+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
62+
JsonNode node = jp.getCodec().readTree(jp);
63+
64+
Events events = new Events();
65+
List<EventDefinition> eventDefs = new ArrayList<>();
66+
67+
if (node.isArray()) {
68+
for (final JsonNode nodeEle : node) {
69+
eventDefs.add(mapper.treeToValue(nodeEle, EventDefinition.class));
70+
}
71+
} else {
72+
String eventsFileDef = node.asText();
73+
String eventsFileSrc = Utils.getResourceFileAsString(eventsFileDef);
74+
JsonNode eventsRefNode;
75+
ObjectMapper jsonWriter = new ObjectMapper();
76+
if (eventsFileSrc != null && eventsFileSrc.trim().length() > 0) {
77+
// if its a yaml def convert to json first
78+
if (!eventsFileSrc.trim().startsWith("{")) {
79+
// convert yaml to json to validate
80+
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
81+
Object obj = yamlReader.readValue(eventsFileSrc, Object.class);
82+
83+
eventsRefNode = jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
84+
} else {
85+
eventsRefNode = jsonWriter.readTree(new JSONObject(eventsFileSrc).toString());
86+
}
87+
88+
JsonNode refEvents = eventsRefNode.get("events");
89+
if (refEvents != null) {
90+
for (final JsonNode nodeEle : refEvents) {
91+
eventDefs.add(mapper.treeToValue(nodeEle, EventDefinition.class));
92+
}
93+
} else {
94+
logger.error("Unable to find event definitions in reference file: {}", eventsFileSrc);
95+
}
96+
97+
} else {
98+
logger.error("Unable to load event defs reference file: {}", eventsFileSrc);
99+
}
100+
101+
}
102+
events.setEventDefs(eventDefs);
103+
return events;
104+
105+
}
106+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
25+
import io.serverlessworkflow.api.functions.FunctionDefinition;
26+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
27+
import io.serverlessworkflow.api.utils.Utils;
28+
import io.serverlessworkflow.api.workflow.Functions;
29+
import org.json.JSONObject;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
37+
public class FunctionsDeserializer extends StdDeserializer<Functions> {
38+
39+
private static final long serialVersionUID = 510l;
40+
private static Logger logger = LoggerFactory.getLogger(FunctionsDeserializer.class);
41+
42+
private WorkflowPropertySource context;
43+
44+
public FunctionsDeserializer() {
45+
this(Functions.class);
46+
}
47+
48+
public FunctionsDeserializer(Class<?> vc) {
49+
super(vc);
50+
}
51+
52+
public FunctionsDeserializer(WorkflowPropertySource context) {
53+
this(Functions.class);
54+
this.context = context;
55+
}
56+
57+
@Override
58+
public Functions deserialize(JsonParser jp,
59+
DeserializationContext ctxt) throws IOException {
60+
61+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
62+
JsonNode node = jp.getCodec().readTree(jp);
63+
64+
Functions functions = new Functions();
65+
List<FunctionDefinition> functionDefs = new ArrayList<>();
66+
if (node.isArray()) {
67+
for (final JsonNode nodeEle : node) {
68+
functionDefs.add(mapper.treeToValue(nodeEle, FunctionDefinition.class));
69+
}
70+
} else {
71+
String functionsFileDef = node.asText();
72+
String functionsFileSrc = Utils.getResourceFileAsString(functionsFileDef);
73+
JsonNode functionsRefNode;
74+
ObjectMapper jsonWriter = new ObjectMapper();
75+
if (functionsFileSrc != null && functionsFileSrc.trim().length() > 0) {
76+
// if its a yaml def convert to json first
77+
if (!functionsFileSrc.trim().startsWith("{")) {
78+
// convert yaml to json to validate
79+
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
80+
Object obj = yamlReader.readValue(functionsFileSrc, Object.class);
81+
82+
functionsRefNode = jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
83+
} else {
84+
functionsRefNode = jsonWriter.readTree(new JSONObject(functionsFileSrc).toString());
85+
}
86+
87+
JsonNode refFunctions = functionsRefNode.get("functions");
88+
if (refFunctions != null) {
89+
for (final JsonNode nodeEle : refFunctions) {
90+
functionDefs.add(mapper.treeToValue(nodeEle, FunctionDefinition.class));
91+
}
92+
} else {
93+
logger.error("Unable to find function definitions in reference file: {}", functionsFileSrc);
94+
}
95+
96+
} else {
97+
logger.error("Unable to load function defs reference file: {}", functionsFileSrc);
98+
}
99+
100+
}
101+
functions.setFunctionDefs(functionDefs);
102+
return functions;
103+
104+
}
105+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.serverlessworkflow.api.states.DefaultState;
2929
import io.serverlessworkflow.api.states.OperationState;
3030
import io.serverlessworkflow.api.states.ParallelState;
31+
import io.serverlessworkflow.api.workflow.Events;
32+
import io.serverlessworkflow.api.workflow.Functions;
3133

3234
public class WorkflowModule extends SimpleModule {
3335

@@ -78,6 +80,8 @@ private void addDefaultDeserializers() {
7880
addDeserializer(EventDefinition.Kind.class, new EventDefinitionKindDeserializer(workflowPropertySource));
7981
addDeserializer(ParallelState.CompletionType.class, new ParallelStateCompletionTypeDeserializer(workflowPropertySource));
8082
addDeserializer(Schedule.DirectInvoke.class, new ScheduleDirectInvokeDeserializer(workflowPropertySource));
83+
addDeserializer(Functions.class, new FunctionsDeserializer(workflowPropertySource));
84+
addDeserializer(Events.class, new EventsDeserializer(workflowPropertySource));
8185
addDeserializer(Extension.class, extensionDeserializer);
8286

8387
}

api/src/main/java/io/serverlessworkflow/api/serializers/WorkflowSerializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public void serialize(Workflow workflow,
8989
workflow.getMetadata());
9090
}
9191

92-
if (workflow.getEvents() != null && !workflow.getEvents().isEmpty()) {
92+
if (workflow.getEvents() != null && !workflow.getEvents().getEventDefs().isEmpty()) {
9393
gen.writeArrayFieldStart("events");
94-
for (EventDefinition eventDefinition : workflow.getEvents()) {
94+
for (EventDefinition eventDefinition : workflow.getEvents().getEventDefs()) {
9595
gen.writeObject(eventDefinition);
9696
}
9797
gen.writeEndArray();
@@ -100,9 +100,9 @@ public void serialize(Workflow workflow,
100100
gen.writeEndArray();
101101
}
102102

103-
if (workflow.getFunctions() != null && !workflow.getFunctions().isEmpty()) {
103+
if (workflow.getFunctions() != null && !workflow.getFunctions().getFunctionDefs().isEmpty()) {
104104
gen.writeArrayFieldStart("functions");
105-
for (FunctionDefinition function : workflow.getFunctions()) {
105+
for (FunctionDefinition function : workflow.getFunctions().getFunctionDefs()) {
106106
gen.writeObject(function);
107107
}
108108
gen.writeEndArray();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.utils;
18+
19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.util.stream.Collectors;
24+
25+
public class Utils {
26+
public static String getResourceFileAsString(String fileName) throws IOException {
27+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
28+
try (InputStream is = classLoader.getResourceAsStream(fileName)) {
29+
if (is == null) return null;
30+
try (InputStreamReader isr = new InputStreamReader(is);
31+
BufferedReader reader = new BufferedReader(isr)) {
32+
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
33+
}
34+
}
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.workflow;
18+
19+
import io.serverlessworkflow.api.events.EventDefinition;
20+
21+
import java.util.List;
22+
23+
public class Events {
24+
private String refValue;
25+
private List<EventDefinition> eventDefs;
26+
27+
public Events() {}
28+
29+
public Events(List<EventDefinition> eventDefs) {
30+
this.eventDefs = eventDefs;
31+
}
32+
33+
public Events(String refValue) {
34+
this.refValue = refValue;
35+
}
36+
37+
public String getRefValue() {
38+
return refValue;
39+
}
40+
41+
public void setRefValue(String refValue) {
42+
this.refValue = refValue;
43+
}
44+
45+
public List<EventDefinition> getEventDefs() {
46+
return eventDefs;
47+
}
48+
49+
public void setEventDefs(List<EventDefinition> eventDefs) {
50+
this.eventDefs = eventDefs;
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.workflow;
18+
19+
import io.serverlessworkflow.api.functions.FunctionDefinition;
20+
21+
import java.util.List;
22+
23+
public class Functions {
24+
private String refValue;
25+
private List<FunctionDefinition> functionDefs;
26+
27+
public Functions() {}
28+
29+
public Functions(List<FunctionDefinition> functionDefs) {
30+
this.functionDefs = functionDefs;
31+
}
32+
33+
public Functions(String refValue) {
34+
this.refValue = refValue;
35+
}
36+
37+
public String getRefValue() {
38+
return refValue;
39+
}
40+
41+
public void setRefValue(String refValue) {
42+
this.refValue = refValue;
43+
}
44+
45+
public List<FunctionDefinition> getFunctionDefs() {
46+
return functionDefs;
47+
}
48+
49+
public void setFunctionDefs(List<FunctionDefinition> functionDefs) {
50+
this.functionDefs = functionDefs;
51+
}
52+
}

0 commit comments

Comments
 (0)