Skip to content

Commit 0eeee8a

Browse files
Add metadata support to TaskBaseBuilder and implement TaskMetadataBuilder (#1694)
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent 29f5927 commit 0eeee8a

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/TaskBaseBuilder.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,20 @@ public T timeout(Consumer<TimeoutBuilder> timeoutConsumer) {
124124
this.task.setTimeout(new TaskTimeout().withTaskTimeoutDefinition(timeoutBuilder.build()));
125125
return self();
126126
}
127+
128+
/**
129+
* Configures additional information about this task, such as descriptions meant for UI
130+
* visualization. Parallel to the `metadata` property in the Spec.
131+
*
132+
* @param metadataConsumer consumer used to populate the task metadata
133+
* @see <a
134+
* href="https://github.com/serverlessworkflow/specification/blob/main/dsl-reference.md#task">DSL
135+
* Reference - Task</a>
136+
*/
137+
public T metadata(Consumer<TaskMetadataBuilder> metadataConsumer) {
138+
final TaskMetadataBuilder metadataBuilder = new TaskMetadataBuilder();
139+
metadataConsumer.accept(metadataBuilder);
140+
this.task.setMetadata(metadataBuilder.build());
141+
return self();
142+
}
127143
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.fluent.spec;
17+
18+
import io.serverlessworkflow.api.types.TaskMetadata;
19+
20+
/**
21+
* Fluent builder for {@link TaskMetadata}, which holds additional information about a task (for
22+
* example, descriptions meant for UI visualization).
23+
*/
24+
public final class TaskMetadataBuilder {
25+
26+
public static final String DESCRIPTION = "description";
27+
28+
private final TaskMetadata metadata;
29+
30+
TaskMetadataBuilder() {
31+
this.metadata = new TaskMetadata();
32+
}
33+
34+
/**
35+
* Adds an arbitrary metadata entry.
36+
*
37+
* @param key metadata key
38+
* @param value metadata value
39+
*/
40+
public TaskMetadataBuilder put(final String key, final Object value) {
41+
this.metadata.withAdditionalProperty(key, value);
42+
return this;
43+
}
44+
45+
/**
46+
* Convenience method for the common {@value #DESCRIPTION} metadata entry.
47+
*
48+
* @param description human readable description of the task
49+
*/
50+
public TaskMetadataBuilder description(final String description) {
51+
return put(DESCRIPTION, description);
52+
}
53+
54+
public TaskMetadata build() {
55+
return this.metadata;
56+
}
57+
}

‎fluent/spec/src/test/java/io/serverlessworkflow/fluent/spec/WorkflowBuilderTest.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.junit.jupiter.api.Assertions.assertEquals;
3434
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3535
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertNull;
3637
import static org.junit.jupiter.api.Assertions.assertThrows;
3738
import static org.junit.jupiter.api.Assertions.assertTrue;
3839

@@ -57,6 +58,7 @@
5758
import io.serverlessworkflow.api.types.RunTaskConfiguration;
5859
import io.serverlessworkflow.api.types.SetTask;
5960
import io.serverlessworkflow.api.types.TaskItem;
61+
import io.serverlessworkflow.api.types.TaskMetadata;
6062
import io.serverlessworkflow.api.types.TryTask;
6163
import io.serverlessworkflow.api.types.TryTaskCatch;
6264
import io.serverlessworkflow.api.types.Use;
@@ -97,6 +99,40 @@ void testWorkflowDocumentExplicit() {
9799
assertEquals("1.2.3", doc.getVersion());
98100
}
99101

102+
@Test
103+
void testTaskMetadata() {
104+
Workflow wf =
105+
WorkflowBuilder.workflow("flowWithTaskMetadata")
106+
.tasks(
107+
d ->
108+
d.set(
109+
"described",
110+
s ->
111+
s.expr("$.value = 'test'")
112+
.metadata(
113+
m ->
114+
m.description("Exit when counter reaches 10")
115+
.put("exitConditionDescription", "counter == 10")
116+
.put("weight", 42)))
117+
.set("plain", s -> s.expr("$.other = 'value'")))
118+
.build();
119+
120+
List<TaskItem> items = wf.getDo();
121+
assertEquals(2, items.size(), "There should be two tasks");
122+
123+
SetTask described = items.get(0).getTask().getSetTask();
124+
TaskMetadata metadata = described.getMetadata();
125+
assertNotNull(metadata, "Task metadata should be present");
126+
Map<String, Object> props = metadata.getAdditionalProperties();
127+
assertEquals(3, props.size(), "Metadata should hold three entries");
128+
assertEquals("Exit when counter reaches 10", props.get(TaskMetadataBuilder.DESCRIPTION));
129+
assertEquals("counter == 10", props.get("exitConditionDescription"));
130+
assertEquals(42, props.get("weight"));
131+
132+
SetTask plain = items.get(1).getTask().getSetTask();
133+
assertNull(plain.getMetadata(), "Tasks without metadata() should not have metadata set");
134+
}
135+
100136
@Test
101137
void testUseAuthenticationsBasic() {
102138
Workflow wf =

0 commit comments

Comments
 (0)