Skip to content

Commit 4ee7a89

Browse files
author
Tihomir Surdilovic
committed
update to function definition types
Signed-off-by: Tihomir Surdilovic <[email protected]>
1 parent 62a20d8 commit 4ee7a89

File tree

7 files changed

+170
-5
lines changed

7 files changed

+170
-5
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.deser.std.StdDeserializer;
22+
import io.serverlessworkflow.api.functions.FunctionDefinition;
23+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.io.IOException;
28+
29+
public class FunctionDefinitionTypeDeserializer extends StdDeserializer<FunctionDefinition.Type> {
30+
31+
private static final long serialVersionUID = 510l;
32+
private static Logger logger = LoggerFactory.getLogger(FunctionDefinitionTypeDeserializer.class);
33+
34+
private WorkflowPropertySource context;
35+
36+
public FunctionDefinitionTypeDeserializer() {
37+
this(FunctionDefinition.Type.class);
38+
}
39+
40+
public FunctionDefinitionTypeDeserializer(WorkflowPropertySource context) {
41+
this(FunctionDefinition.Type.class);
42+
this.context = context;
43+
}
44+
45+
public FunctionDefinitionTypeDeserializer(Class<?> vc) {
46+
super(vc);
47+
}
48+
49+
@Override
50+
public FunctionDefinition.Type deserialize(JsonParser jp,
51+
DeserializationContext ctxt) throws IOException {
52+
53+
String value = jp.getText();
54+
if (context != null) {
55+
try {
56+
String result = context.getPropertySource().getProperty(value);
57+
58+
if (result != null) {
59+
return FunctionDefinition.Type.fromValue(result);
60+
} else {
61+
return FunctionDefinition.Type.fromValue(jp.getText());
62+
}
63+
} catch (Exception e) {
64+
logger.info("Exception trying to evaluate property: {}", e.getMessage());
65+
return FunctionDefinition.Type.fromValue(jp.getText());
66+
}
67+
} else {
68+
return FunctionDefinition.Type.fromValue(jp.getText());
69+
}
70+
}
71+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.serverlessworkflow.api.end.End;
2222
import io.serverlessworkflow.api.events.EventDefinition;
2323
import io.serverlessworkflow.api.events.OnEvents;
24+
import io.serverlessworkflow.api.functions.FunctionDefinition;
2425
import io.serverlessworkflow.api.interfaces.Extension;
2526
import io.serverlessworkflow.api.interfaces.State;
2627
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
@@ -91,6 +92,7 @@ private void addDefaultDeserializers() {
9192
addDeserializer(Start.class, new StartDefinitionDeserializer(workflowPropertySource));
9293
addDeserializer(End.class, new EndDefinitionDeserializer(workflowPropertySource));
9394
addDeserializer(Extension.class, extensionDeserializer);
95+
addDeserializer(FunctionDefinition.Type.class, new FunctionDefinitionTypeDeserializer(workflowPropertySource));
9496

9597
}
9698

api/src/main/resources/schema/functions/functiondef.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@
99
},
1010
"operation": {
1111
"type": "string",
12-
"description": "Combination of the function/service OpenAPI definition URI and the operationID of the operation that needs to be invoked, separated by a '#'. For example 'https://petstore.swagger.io/v2/swagger.json#getPetById'",
12+
"description": "If type `rest`, combination of the function/service OpenAPI definition URI and the operationID of the operation that needs to be invoked, separated by a '#'. If type is `expression` defines the workflow expression.",
1313
"minLength": 1
1414
},
15+
"type": {
16+
"type": "string",
17+
"description": "Defines the function type. Is either `rest` or `expression`. Default is `rest`",
18+
"enum": [
19+
"rest",
20+
"expression"
21+
],
22+
"default": "rest"
23+
},
1524
"metadata": {
1625
"$ref": "../metadata/metadata.json"
1726
}

api/src/main/resources/schema/transitions/transition.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
"type": "object",
33
"javaType": "io.serverlessworkflow.api.transitions.Transition",
44
"properties": {
5-
"expression": {
6-
"type": "string",
7-
"description": "Common Expression Language (CEL) expression. Must evaluate to true for the transition to be valid"
8-
},
95
"produceEvents": {
106
"type": "array",
117
"description": "Array of events to be produced",

api/src/test/java/io/serverlessworkflow/api/test/MarkupToWorkflowTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
package io.serverlessworkflow.api.test;
1818

1919
import io.serverlessworkflow.api.Workflow;
20+
import io.serverlessworkflow.api.functions.FunctionDefinition;
2021
import io.serverlessworkflow.api.interfaces.State;
2122
import io.serverlessworkflow.api.states.EventState;
2223
import io.serverlessworkflow.api.states.OperationState;
2324
import io.serverlessworkflow.api.test.utils.WorkflowTestUtils;
25+
import io.serverlessworkflow.api.workflow.Functions;
26+
import jdk.dynalink.Operation;
2427
import org.junit.jupiter.params.ParameterizedTest;
2528
import org.junit.jupiter.params.provider.ValueSource;
2629

30+
import java.util.List;
31+
2732
import static org.junit.jupiter.api.Assertions.*;
2833

2934
public class MarkupToWorkflowTest {
@@ -117,4 +122,31 @@ public void testSpecFreatureCompensation(String workflowLocation) {
117122

118123
assertTrue(operationState.isUsedForCompensation());
119124
}
125+
126+
@ParameterizedTest
127+
@ValueSource(strings = {"/features/functiontypes.json", "/features/functiontypes.yml"})
128+
public void testFunctionTypes(String workflowLocation) {
129+
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));
130+
131+
assertNotNull(workflow);
132+
assertNotNull(workflow.getId());
133+
assertNotNull(workflow.getName());
134+
assertNotNull(workflow.getStates());
135+
136+
assertNotNull(workflow.getStates());
137+
assertTrue(workflow.getStates().size() == 1);
138+
139+
State state = workflow.getStates().get(0);
140+
assertTrue(state instanceof OperationState);
141+
142+
List<FunctionDefinition> functionDefs = workflow.getFunctions().getFunctionDefs();
143+
assertNotNull(functionDefs);
144+
assertTrue(functionDefs.size() == 2);
145+
146+
FunctionDefinition restFunc = functionDefs.get(0);
147+
assertEquals(restFunc.getType(), FunctionDefinition.Type.REST);
148+
149+
FunctionDefinition restFunc2 = functionDefs.get(1);
150+
assertEquals(restFunc2.getType(), FunctionDefinition.Type.EXPRESSION);
151+
}
120152
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"id": "functiontypes",
3+
"version": "1.0",
4+
"name": "Function Types Workflow",
5+
"description": "Determine if applicant request is valid",
6+
"functions": [
7+
{
8+
"name": "restFunction",
9+
"operation": "http://myapis.org/applicationapi.json#emailRejection"
10+
},
11+
{
12+
"name": "expressionFunction",
13+
"operation": "$.my.data",
14+
"type" : "expression"
15+
}
16+
],
17+
"states":[
18+
{
19+
"name":"CheckFunctions",
20+
"type":"operation",
21+
"start": true,
22+
"actions":[
23+
{
24+
"functionRef": {
25+
"refName": "restFunction",
26+
"parameters": {
27+
"data": "{{ fn(expressionFunction) }}"
28+
}
29+
}
30+
}
31+
],
32+
"end": true
33+
}
34+
]
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
id: functiontypes
2+
version: '1.0'
3+
name: Function Types Workflow
4+
description: Determine if applicant request is valid
5+
functions:
6+
- name: restFunction
7+
operation: http://myapis.org/applicationapi.json#emailRejection
8+
- name: expressionFunction
9+
operation: "$.my.data"
10+
type: expression
11+
states:
12+
- name: CheckFunctions
13+
type: operation
14+
start: true
15+
actions:
16+
- functionRef:
17+
refName: restFunction
18+
parameters:
19+
data: "{{ fn(expressionFunction) }}"
20+
end: true

0 commit comments

Comments
 (0)