Skip to content

Commit 6a56b89

Browse files
committed
[Fix #636] Initial refactor to separate Jackson/JQ
Signed-off-by: fjtirado <[email protected]>
1 parent 6b9394b commit 6a56b89

Some content is hidden

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

53 files changed

+1135
-422
lines changed

impl/core/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<artifactId>serverlessworkflow-impl-core</artifactId>
99
<name>Serverless Workflow :: Impl :: Core</name>
1010
<dependencies>
11-
<dependency>
11+
<dependency>
1212
<groupId>io.serverlessworkflow</groupId>
13-
<artifactId>serverlessworkflow-api</artifactId>
13+
<artifactId>serverlessworkflow-types</artifactId>
1414
<version>${project.version}</version>
1515
</dependency>
1616
<dependency>
@@ -58,5 +58,11 @@
5858
<artifactId>logback-classic</artifactId>
5959
<scope>test</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>io.serverlessworkflow</groupId>
63+
<artifactId>serverlessworkflow-api</artifactId>
64+
<version>${project.version}</version>
65+
</dependency>
66+
6167
</dependencies>
6268
</project>

impl/core/src/main/java/io/serverlessworkflow/impl/TaskContext.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
1918
import io.serverlessworkflow.api.types.TaskBase;
2019
import io.serverlessworkflow.impl.executors.TransitionInfo;
2120
import java.time.Instant;
@@ -25,22 +24,22 @@
2524

2625
public class TaskContext {
2726

28-
private final JsonNode rawInput;
27+
private final WorkflowModel rawInput;
2928
private final TaskBase task;
3029
private final WorkflowPosition position;
3130
private final Instant startedAt;
3231
private final String taskName;
3332
private final Map<String, Object> contextVariables;
3433
private final Optional<TaskContext> parentContext;
3534

36-
private JsonNode input;
37-
private JsonNode output;
38-
private JsonNode rawOutput;
35+
private WorkflowModel input;
36+
private WorkflowModel output;
37+
private WorkflowModel rawOutput;
3938
private Instant completedAt;
4039
private TransitionInfo transition;
4140

4241
public TaskContext(
43-
JsonNode input,
42+
WorkflowModel input,
4443
WorkflowPosition position,
4544
Optional<TaskContext> parentContext,
4645
String taskName,
@@ -49,15 +48,15 @@ public TaskContext(
4948
}
5049

5150
private TaskContext(
52-
JsonNode rawInput,
51+
WorkflowModel rawInput,
5352
Optional<TaskContext> parentContext,
5453
String taskName,
5554
TaskBase task,
5655
WorkflowPosition position,
5756
Instant startedAt,
58-
JsonNode input,
59-
JsonNode output,
60-
JsonNode rawOutput) {
57+
WorkflowModel input,
58+
WorkflowModel output,
59+
WorkflowModel rawOutput) {
6160
this.rawInput = rawInput;
6261
this.parentContext = parentContext;
6362
this.taskName = taskName;
@@ -76,40 +75,40 @@ public TaskContext copy() {
7675
rawInput, parentContext, taskName, task, position, startedAt, input, output, rawOutput);
7776
}
7877

79-
public void input(JsonNode input) {
78+
public void input(WorkflowModel input) {
8079
this.input = input;
8180
this.rawOutput = input;
8281
this.output = input;
8382
}
8483

85-
public JsonNode input() {
84+
public WorkflowModel input() {
8685
return input;
8786
}
8887

89-
public JsonNode rawInput() {
88+
public WorkflowModel rawInput() {
9089
return rawInput;
9190
}
9291

9392
public TaskBase task() {
9493
return task;
9594
}
9695

97-
public TaskContext rawOutput(JsonNode output) {
96+
public TaskContext rawOutput(WorkflowModel output) {
9897
this.rawOutput = output;
9998
this.output = output;
10099
return this;
101100
}
102101

103-
public JsonNode rawOutput() {
102+
public WorkflowModel rawOutput() {
104103
return rawOutput;
105104
}
106105

107-
public TaskContext output(JsonNode output) {
106+
public TaskContext output(WorkflowModel output) {
108107
this.output = output;
109108
return this;
110109
}
111110

112-
public JsonNode output() {
111+
public WorkflowModel output() {
113112
return output;
114113
}
115114

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public WorkflowPositionFactory positionFactory() {
202202
return positionFactory;
203203
}
204204

205+
public WorkflowModelFactory modelFactory() {
206+
return exprFactory.modelFactory();
207+
}
208+
205209
public RuntimeDescriptorFactory runtimeDescriptorFactory() {
206210
return runtimeDescriptorFactory;
207211
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowContext.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
19-
2018
public class WorkflowContext {
2119
private final WorkflowDefinition definition;
2220
private final WorkflowInstance instance;
23-
private JsonNode context;
21+
private WorkflowModel context;
2422

2523
WorkflowContext(WorkflowDefinition definition, WorkflowInstance instance) {
2624
this.definition = definition;
@@ -31,11 +29,11 @@ public WorkflowInstance instance() {
3129
return instance;
3230
}
3331

34-
public JsonNode context() {
32+
public WorkflowModel context() {
3533
return context;
3634
}
3735

38-
public void context(JsonNode context) {
36+
public void context(WorkflowModel context) {
3937
this.context = context;
4038
}
4139

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.serverlessworkflow.api.types.Workflow;
2323
import io.serverlessworkflow.impl.executors.TaskExecutor;
2424
import io.serverlessworkflow.impl.executors.TaskExecutorHelper;
25-
import io.serverlessworkflow.impl.json.JsonUtils;
2625
import io.serverlessworkflow.impl.jsonschema.SchemaValidator;
2726
import io.serverlessworkflow.impl.resources.ResourceLoader;
2827
import java.nio.file.Path;
@@ -47,13 +46,13 @@ private WorkflowDefinition(
4746
Input input = workflow.getInput();
4847
this.inputSchemaValidator =
4948
getSchemaValidator(application.validatorFactory(), resourceLoader, input.getSchema());
50-
this.inputFilter = buildWorkflowFilter(application.expressionFactory(), input.getFrom());
49+
this.inputFilter = buildWorkflowFilter(application, input.getFrom());
5150
}
5251
if (workflow.getOutput() != null) {
5352
Output output = workflow.getOutput();
5453
this.outputSchemaValidator =
5554
getSchemaValidator(application.validatorFactory(), resourceLoader, output.getSchema());
56-
this.outputFilter = buildWorkflowFilter(application.expressionFactory(), output.getAs());
55+
this.outputFilter = buildWorkflowFilter(application, output.getAs());
5756
}
5857
this.taskExecutor =
5958
TaskExecutorHelper.createExecutorList(
@@ -74,7 +73,7 @@ static WorkflowDefinition of(WorkflowApplication application, Workflow workflow,
7473
}
7574

7675
public WorkflowInstance instance(Object input) {
77-
return new WorkflowInstance(this, JsonUtils.fromValue(input));
76+
return new WorkflowInstance(this, application.modelFactory().fromAny(input));
7877
}
7978

8079
Optional<SchemaValidator> inputSchemaValidator() {

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
19-
2018
@FunctionalInterface
2119
public interface WorkflowFilter {
22-
JsonNode apply(WorkflowContext workflow, TaskContext task, JsonNode node);
20+
WorkflowModel apply(WorkflowContext workflow, TaskContext task, WorkflowModel node);
2321
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowInstance.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
1918
import io.serverlessworkflow.impl.executors.TaskExecutorHelper;
20-
import io.serverlessworkflow.impl.json.JsonUtils;
2119
import java.time.Instant;
2220
import java.util.Optional;
2321
import java.util.concurrent.CompletableFuture;
@@ -26,24 +24,24 @@
2624
public class WorkflowInstance {
2725
private final AtomicReference<WorkflowStatus> status;
2826
private final String id;
29-
private final JsonNode input;
27+
private final WorkflowModel input;
3028

3129
private WorkflowContext workflowContext;
3230
private WorkflowDefinition definition;
3331
private Instant startedAt;
3432
private Instant completedAt;
35-
private volatile JsonNode output;
36-
private CompletableFuture<JsonNode> completableFuture;
33+
private volatile WorkflowModel output;
34+
private CompletableFuture<WorkflowModel> completableFuture;
3735

38-
WorkflowInstance(WorkflowDefinition definition, JsonNode input) {
36+
WorkflowInstance(WorkflowDefinition definition, WorkflowModel input) {
3937
this.id = definition.application().idFactory().get();
4038
this.input = input;
4139
this.definition = definition;
4240
this.status = new AtomicReference<>(WorkflowStatus.PENDING);
4341
definition.inputSchemaValidator().ifPresent(v -> v.validate(input));
4442
}
4543

46-
public CompletableFuture<JsonNode> start() {
44+
public CompletableFuture<WorkflowModel> start() {
4745
this.startedAt = Instant.now();
4846
this.workflowContext = new WorkflowContext(definition, this);
4947
this.status.set(WorkflowStatus.RUNNING);
@@ -60,7 +58,7 @@ public CompletableFuture<JsonNode> start() {
6058
return completableFuture;
6159
}
6260

63-
private JsonNode whenCompleted(JsonNode node) {
61+
private WorkflowModel whenCompleted(WorkflowModel node) {
6462
output =
6563
workflowContext
6664
.definition()
@@ -85,7 +83,7 @@ public Instant completedAt() {
8583
return completedAt;
8684
}
8785

88-
public JsonNode input() {
86+
public WorkflowModel input() {
8987
return input;
9088
}
9189

@@ -97,11 +95,16 @@ public void status(WorkflowStatus state) {
9795
this.status.set(state);
9896
}
9997

100-
public Object output() {
101-
return JsonUtils.toJavaValue(outputAsJsonNode());
98+
public WorkflowModel output() {
99+
return output;
102100
}
103101

104-
public JsonNode outputAsJsonNode() {
105-
return output;
102+
public <T> T outputAs(Class<T> clazz) {
103+
return output
104+
.as(clazz)
105+
.orElseThrow(
106+
() ->
107+
new IllegalArgumentException(
108+
"Output " + output + " cannot be converted to class " + clazz));
106109
}
107110
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
package io.serverlessworkflow.impl;
17+
18+
import io.cloudevents.CloudEventData;
19+
import java.time.OffsetDateTime;
20+
import java.util.Collection;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.function.BiConsumer;
24+
25+
public interface WorkflowModel {
26+
27+
void forEach(BiConsumer<String, WorkflowModel> consumer);
28+
29+
Optional<Boolean> asBoolean();
30+
31+
Collection<WorkflowModel> asCollection();
32+
33+
Optional<String> asText();
34+
35+
Optional<OffsetDateTime> asDate();
36+
37+
Optional<Number> asNumber();
38+
39+
Optional<CloudEventData> asCloudEventData();
40+
41+
Optional<Map<String, Object>> asMap();
42+
43+
Object asJavaObject();
44+
45+
Object asIs();
46+
47+
Class<?> objectClass();
48+
49+
<T> Optional<T> as(Class<T> clazz);
50+
}

0 commit comments

Comments
 (0)