Skip to content

Commit

Permalink
Auto-update dependencies and plugins (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
mellbot authored Apr 17, 2023
1 parent 69b2778 commit 132da5c
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ public final class ConjureAdapter {
private ConjureAdapter() {}

public static Endpoints adapt(UndertowService service, UndertowRuntime runtime) {
Set<EndpointHandler> handlers =
service.endpoints(runtime).stream()
.map(e -> fromConjureEndpoint(e, runtime))
.collect(Collectors.toSet());
Set<EndpointHandler> handlers = service.endpoints(runtime).stream()
.map(e -> fromConjureEndpoint(e, runtime))
.collect(Collectors.toSet());
return () -> handlers;
}

private static EndpointHandler fromConjureEndpoint(
Endpoint endpoint, UndertowRuntime conjureRuntime) {
private static EndpointHandler fromConjureEndpoint(Endpoint endpoint, UndertowRuntime conjureRuntime) {
return new EndpointHandler() {
@Override
public HttpMethod method() {
Expand All @@ -65,8 +63,7 @@ public HttpMethod method() {
case "PUT" -> HttpMethod.PUT;
case "POST" -> HttpMethod.POST;
case "DELETE" -> HttpMethod.DELETE;
default -> throw new IllegalStateException(
"Unsupported HTTP method " + endpoint.method());
default -> throw new IllegalStateException("Unsupported HTTP method " + endpoint.method());
};
}

Expand All @@ -78,10 +75,7 @@ public String route() {
@Override
public HttpHandler handler(EndpointRuntime runtime) {
return HandlerChain.of(BlockingHandler::new)
.then(
h ->
new ConjureExceptionHandler(
h, conjureRuntime.exceptionHandler()))
.then(h -> new ConjureExceptionHandler(h, conjureRuntime.exceptionHandler()))
.last(endpoint.handler());
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,17 @@
public final class AnnotationHelpers {

record EndpointAnnotation(
Class<? extends Annotation> annontationClass,
HttpMethod httpMethod,
Function<Annotation, String> pathFn) {}
Class<? extends Annotation> annontationClass, HttpMethod httpMethod, Function<Annotation, String> pathFn) {}

private static final Set<EndpointAnnotation> ENDPOINT_ANNOTATIONS =
ImmutableSet.of(
new EndpointAnnotation(
Http.Get.class, HttpMethod.GET, a -> ((Http.Get) a).value()),
new EndpointAnnotation(
Http.Post.class, HttpMethod.POST, a -> ((Http.Post) a).value()),
new EndpointAnnotation(
Http.Put.class, HttpMethod.PUT, a -> ((Http.Put) a).value()),
new EndpointAnnotation(
Http.Delete.class, HttpMethod.DELETE, a -> ((Http.Delete) a).value()));
private static final Set<EndpointAnnotation> ENDPOINT_ANNOTATIONS = ImmutableSet.of(
new EndpointAnnotation(Http.Get.class, HttpMethod.GET, a -> ((Http.Get) a).value()),
new EndpointAnnotation(Http.Post.class, HttpMethod.POST, a -> ((Http.Post) a).value()),
new EndpointAnnotation(Http.Put.class, HttpMethod.PUT, a -> ((Http.Put) a).value()),
new EndpointAnnotation(Http.Delete.class, HttpMethod.DELETE, a -> ((Http.Delete) a).value()));

private static Set<Class<? extends Annotation>> ENDPOINT_ANNOTATION_CLASSES =
ENDPOINT_ANNOTATIONS.stream()
.map(EndpointAnnotation::annontationClass)
.collect(Collectors.toSet());
private static Set<Class<? extends Annotation>> ENDPOINT_ANNOTATION_CLASSES = ENDPOINT_ANNOTATIONS.stream()
.map(EndpointAnnotation::annontationClass)
.collect(Collectors.toSet());

public static Set<Class<? extends Annotation>> endpointAnnotations() {
return ENDPOINT_ANNOTATION_CLASSES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,22 @@

public final class EndpointHandlerGenerator {

public static JavaFile generate(
ClassName className, Collection<EndpointHandlerDefinition> handlers) {
public static JavaFile generate(ClassName className, Collection<EndpointHandlerDefinition> handlers) {
ClassName resourceClassName = endpointsClassName(className);
TypeSpec resourceClass =
TypeSpec.classBuilder(resourceClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(ClassName.get(Endpoints.class))
.addField(className, "delegate", Modifier.PRIVATE, Modifier.FINAL)
.addMethod(
MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(className, "delegate")
.addStatement("this.$N = $N", "delegate", "delegate")
.build())
.addMethod(generateEndpointsMethod(className, handlers))
.addTypes(
handlers.stream()
.map(h -> generateEndpointHandlers(className, h))
.collect(Collectors.toList()))
.build();
TypeSpec resourceClass = TypeSpec.classBuilder(resourceClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(ClassName.get(Endpoints.class))
.addField(className, "delegate", Modifier.PRIVATE, Modifier.FINAL)
.addMethod(MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(className, "delegate")
.addStatement("this.$N = $N", "delegate", "delegate")
.build())
.addMethod(generateEndpointsMethod(className, handlers))
.addTypes(handlers.stream()
.map(h -> generateEndpointHandlers(className, h))
.collect(Collectors.toList()))
.build();
return JavaFile.builder(className.packageName(), resourceClass).build();
}

Expand All @@ -83,50 +79,44 @@ private static MethodSpec generateEndpointsMethod(
.build();
}

private static TypeSpec generateEndpointHandlers(
ClassName className, EndpointHandlerDefinition definition) {
private static TypeSpec generateEndpointHandlers(ClassName className, EndpointHandlerDefinition definition) {
return TypeSpec.classBuilder(endpointHandlerClassName(className, definition))
.addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC)
.addSuperinterface(ClassName.get(EndpointHandler.class))
.addField(className, "delegate", Modifier.PRIVATE, Modifier.FINAL)
.addMethod(
MethodSpec.constructorBuilder()
.addParameter(className, "delegate")
.addStatement("this.$N = $N", "delegate", "delegate")
.build())
.addMethod(
MethodSpec.methodBuilder("method")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ClassName.get(HttpMethod.class))
.addStatement(
"return $T.$N",
HttpMethod.class,
definition.httpMethod().toString())
.build())
.addMethod(
MethodSpec.methodBuilder("route")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ClassName.get(String.class))
.addStatement("return $S", definition.route())
.build())
.addMethod(
MethodSpec.methodBuilder("handler")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(EndpointRuntime.class, "runtime")
.returns(ClassName.get("io.undertow.server", "HttpHandler"))
.addCode(
CodeBlock.builder()
.beginControlFlow("return exchange ->")
.add(generateHttpHandler(definition))
.endControlFlow()
// because we're returning a lambda and want a
// terminating ";"
.addStatement("")
.build())
.addMethod(MethodSpec.constructorBuilder()
.addParameter(className, "delegate")
.addStatement("this.$N = $N", "delegate", "delegate")
.build())
.addMethod(MethodSpec.methodBuilder("method")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ClassName.get(HttpMethod.class))
.addStatement(
"return $T.$N",
HttpMethod.class,
definition.httpMethod().toString())
.build())
.addMethod(MethodSpec.methodBuilder("route")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(ClassName.get(String.class))
.addStatement("return $S", definition.route())
.build())
.addMethod(MethodSpec.methodBuilder("handler")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(EndpointRuntime.class, "runtime")
.returns(ClassName.get("io.undertow.server", "HttpHandler"))
.addCode(CodeBlock.builder()
.beginControlFlow("return exchange ->")
.add(generateHttpHandler(definition))
.endControlFlow()
// because we're returning a lambda and want a
// terminating ";"
.addStatement("")
.build())
.build())
.build();
}

Expand Down Expand Up @@ -175,22 +165,20 @@ private static CodeBlock generateHttpHandler(EndpointHandlerDefinition definitio
.filter(p -> p.type() == ParamType.BODY)
.findAny()
.ifPresentOrElse(
bodyParam ->
handler.add(
CodeBlock.builder()
.beginControlFlow(
"$N.getRequestReceiver().receiveFullString((bodyExchange, body_) ->",
"exchange")
.addStatement(
"$T $N = $N.serde().deserialize(new $T(body_), $T.class)",
bodyParam.className(),
bodyParam.argumentName(),
"runtime",
ByteRepr.class,
bodyParam.className())
.add(returnStatement)
.endControlFlow(")")
.build()),
bodyParam -> handler.add(CodeBlock.builder()
.beginControlFlow(
"$N.getRequestReceiver().receiveFullString((bodyExchange, body_) ->",
"exchange")
.addStatement(
"$T $N = $N.serde().deserialize(new $T(body_), $T.class)",
bodyParam.className(),
bodyParam.argumentName(),
"runtime",
ByteRepr.class,
bodyParam.className())
.add(returnStatement)
.endControlFlow(")")
.build()),
() -> handler.add(returnStatement));
return handler.build();
}
Expand All @@ -206,8 +194,7 @@ private static CodeBlock authBlock(String authParamName) {
"runtime",
"exchange")
.beginControlFlow("if ($N.isError())", authParamName)
.addStatement(
"$N.error($N.error().get(), $N)", "runtime", authParamName, "exchange")
.addStatement("$N.error($N.error().get(), $N)", "runtime", authParamName, "exchange")
.addStatement("return")
.endControlFlow()
.build();
Expand All @@ -220,16 +207,14 @@ private static CodeBlock paramBlock(ParameterDefinition param) {
case QUERY -> "queryParameter";
case HEADER -> "headerParameter";
case COOKIE -> "cookieParameter";
default -> throw new IllegalStateException(
"Processor invariant failed due to programmer error");
default -> throw new IllegalStateException("Processor invariant failed due to programmer error");
};
return CodeBlock.builder()
.addStatement(
"$T $N = $N.$N($S, $N)",
isOptional(param.className())
? param.className()
: ParameterizedTypeName.get(
ClassName.get(Optional.class), param.className()),
: ParameterizedTypeName.get(ClassName.get(Optional.class), param.className()),
param.argumentName(),
"runtime",
methodName,
Expand All @@ -241,17 +226,13 @@ private static CodeBlock paramBlock(ParameterDefinition param) {
private static CodeBlock argumentList(EndpointHandlerDefinition definition) {
return CodeBlock.join(
definition.parameters().stream()
.map(
param ->
switch (param.type()) {
case TOKEN -> CodeBlock.of(
"$N.unwrap()", param.argumentName());
case BODY -> CodeBlock.of("$N", param.argumentName());
default -> isOptional(param.className())
? CodeBlock.of("$N", param.argumentName())
: CodeBlock.of(
"$N.get()", param.argumentName());
})
.map(param -> switch (param.type()) {
case TOKEN -> CodeBlock.of("$N.unwrap()", param.argumentName());
case BODY -> CodeBlock.of("$N", param.argumentName());
default -> isOptional(param.className())
? CodeBlock.of("$N", param.argumentName())
: CodeBlock.of("$N.get()", param.argumentName());
})
.collect(Collectors.toList()),
", ");
}
Expand All @@ -265,10 +246,8 @@ private static ClassName endpointsClassName(ClassName className) {
return ClassName.get(className.packageName(), className.simpleName() + "Endpoints");
}

private static ClassName endpointHandlerClassName(
ClassName className, EndpointHandlerDefinition definition) {
return endpointsClassName(className)
.nestedClass(ucfirst(definition.methodName()) + "EndpointHandler");
private static ClassName endpointHandlerClassName(ClassName className, EndpointHandlerDefinition definition) {
return endpointsClassName(className).nestedClass(ucfirst(definition.methodName()) + "EndpointHandler");
}

private static String ucfirst(String str) {
Expand All @@ -285,7 +264,10 @@ public record EndpointHandlerDefinition(

public EndpointHandlerDefinition {
Preconditions.checkArgument(
parameters.stream().filter(p -> p.type().equals(ParamType.BODY)).count() <= 1,
parameters.stream()
.filter(p -> p.type().equals(ParamType.BODY))
.count()
<= 1,
"At most one body-type parameter allowed");
}

Expand All @@ -305,8 +287,7 @@ enum ReturnType {
}
}

public record ParameterDefinition(
String argumentName, String httpName, TypeName className, ParamType type) {
public record ParameterDefinition(String argumentName, String httpName, TypeName className, ParamType type) {
enum ParamType {
BODY,
PATH,
Expand Down
Loading

0 comments on commit 132da5c

Please sign in to comment.