Skip to content

Commit c75e958

Browse files
Add fluent DSL equivalents for core task types and expand coverage (#1448)
Signed-off-by: Matheus André <matheusandr2@gmail.com>
1 parent 554aee6 commit c75e958

3 files changed

Lines changed: 652 additions & 1 deletion

File tree

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/UseBuilder.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
*/
1616
package io.serverlessworkflow.fluent.spec;
1717

18+
import io.serverlessworkflow.api.types.Error;
19+
import io.serverlessworkflow.api.types.ErrorDetails;
20+
import io.serverlessworkflow.api.types.ErrorTitle;
21+
import io.serverlessworkflow.api.types.ErrorType;
22+
import io.serverlessworkflow.api.types.UriTemplate;
1823
import io.serverlessworkflow.api.types.Use;
1924
import io.serverlessworkflow.api.types.UseAuthentications;
25+
import io.serverlessworkflow.api.types.UseErrors;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
2028
import java.util.List;
2129
import java.util.function.Consumer;
2230

@@ -47,9 +55,81 @@ public UseBuilder authentications(Consumer<UseAuthenticationsBuilder> authentica
4755
return this;
4856
}
4957

50-
// TODO: implement the remaining `use` attributes
58+
public UseBuilder errors(Consumer<UseErrorsBuilder> errorsConsumer) {
59+
final UseErrorsBuilder builder = new UseErrorsBuilder();
60+
errorsConsumer.accept(builder);
61+
final UseErrors built = builder.build();
62+
if (this.use.getErrors() == null) {
63+
this.use.setErrors(new UseErrors());
64+
}
65+
this.use.getErrors().getAdditionalProperties().putAll(built.getAdditionalProperties());
66+
return this;
67+
}
5168

5269
public Use build() {
5370
return use;
5471
}
72+
73+
public static final class UseErrorsBuilder {
74+
private final UseErrors useErrors;
75+
76+
UseErrorsBuilder() {
77+
this.useErrors = new UseErrors();
78+
}
79+
80+
public UseErrorsBuilder error(String name, Consumer<UseErrorBuilder> errorConsumer) {
81+
final UseErrorBuilder errorBuilder = new UseErrorBuilder();
82+
errorConsumer.accept(errorBuilder);
83+
this.useErrors.withAdditionalProperty(name, errorBuilder.build());
84+
return this;
85+
}
86+
87+
public UseErrors build() {
88+
return useErrors;
89+
}
90+
}
91+
92+
public static final class UseErrorBuilder {
93+
private final Error error;
94+
95+
UseErrorBuilder() {
96+
this.error = new Error();
97+
}
98+
99+
public UseErrorBuilder type(String expression) {
100+
ErrorType errorType = new ErrorType();
101+
try {
102+
errorType.withLiteralErrorType(new UriTemplate().withLiteralUri(new URI(expression)));
103+
} catch (URISyntaxException ex) {
104+
errorType.withExpressionErrorType(expression);
105+
}
106+
this.error.setType(errorType);
107+
return this;
108+
}
109+
110+
public UseErrorBuilder type(URI errorType) {
111+
this.error.setType(
112+
new ErrorType().withLiteralErrorType(new UriTemplate().withLiteralUri(errorType)));
113+
return this;
114+
}
115+
116+
public UseErrorBuilder status(int status) {
117+
this.error.setStatus(status);
118+
return this;
119+
}
120+
121+
public UseErrorBuilder title(String expression) {
122+
this.error.setTitle(new ErrorTitle().withExpressionErrorTitle(expression));
123+
return this;
124+
}
125+
126+
public UseErrorBuilder detail(String expression) {
127+
this.error.setDetail(new ErrorDetails().withExpressionErrorDetails(expression));
128+
return this;
129+
}
130+
131+
public Error build() {
132+
return error;
133+
}
134+
}
55135
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/dsl/UseSpec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.serverlessworkflow.fluent.spec.configurers.UseConfigurer;
2121
import java.util.LinkedList;
2222
import java.util.List;
23+
import java.util.function.Consumer;
2324

2425
public class UseSpec implements UseConfigurer {
2526

@@ -40,6 +41,11 @@ public UseSpec auth(String name, AuthenticationConfigurer auth) {
4041
return this;
4142
}
4243

44+
public UseSpec errors(Consumer<UseBuilder.UseErrorsBuilder> errorsConsumer) {
45+
steps.add(u -> u.errors(errorsConsumer));
46+
return this;
47+
}
48+
4349
@Override
4450
public void accept(UseBuilder useBuilder) {
4551
steps.forEach(step -> step.accept(useBuilder));

0 commit comments

Comments
 (0)