Skip to content

Commit

Permalink
Refactor rest data panache to reuse the same implementation for lists
Browse files Browse the repository at this point in the history
At the moment, the list implementator and the list hal implementation duplicate almost the same code. This pull request addresses this issue, so in the future we can include changes in the list implementator for both json and hal resources.
  • Loading branch information
Sgitario committed Nov 11, 2022
1 parent a402597 commit df2f0f5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import javax.ws.rs.core.UriInfo;

import io.quarkus.deployment.Capabilities;
import io.quarkus.gizmo.AnnotatedElement;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.FieldDescriptor;
import io.quarkus.gizmo.MethodCreator;
Expand All @@ -28,7 +30,7 @@
import io.quarkus.rest.data.panache.deployment.utils.UniImplementor;
import io.smallrye.mutiny.Uni;

public final class ListMethodImplementor extends StandardMethodImplementor {
public class ListMethodImplementor extends StandardMethodImplementor {

private static final String METHOD_NAME = "list";

Expand All @@ -38,14 +40,11 @@ public final class ListMethodImplementor extends StandardMethodImplementor {

private static final String REL = "list";

private final PaginationImplementor paginationImplementor;

private final PaginationImplementor paginationImplementor = new PaginationImplementor();
private final SortImplementor sortImplementor = new SortImplementor();

public ListMethodImplementor(Capabilities capabilities) {
super(capabilities);

this.paginationImplementor = new PaginationImplementor();
}

/**
Expand Down Expand Up @@ -131,18 +130,37 @@ protected String getResourceMethodName() {
return RESOURCE_METHOD_NAME;
}

protected String getMethodName() {
return METHOD_NAME;
}

@Override
protected void addProducesJsonAnnotation(AnnotatedElement element, ResourceProperties properties) {
super.addProducesAnnotation(element, APPLICATION_JSON);
}

protected void returnValueWithLinks(BytecodeCreator creator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, ResultHandle value, ResultHandle links) {
creator.returnValue(responseImplementor.ok(creator, value, links));
}

protected void returnValue(BytecodeCreator creator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, ResultHandle value) {
creator.returnValue(responseImplementor.ok(creator, value));
}

private void implementPaged(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
// Method parameters: sort strings, page index, page size, uri info
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(getMethodName(), classCreator,
isNotReactivePanache() ? ofType(Response.class) : ofType(Uni.class, resourceMetadata.getEntityType()),
List.class, int.class, int.class, UriInfo.class);
methodCreator.setParameterNames(new String[] { "sort", "page", "size", "uriInfo" });

// Add method annotations
addGetAnnotation(methodCreator);
addPathAnnotation(methodCreator, resourceProperties.getPath(RESOURCE_METHOD_NAME));
addProducesAnnotation(methodCreator, APPLICATION_JSON);
addProducesJsonAnnotation(methodCreator, resourceProperties);
addLinksAnnotation(methodCreator, resourceMetadata.getEntityType(), REL);
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, resourceMetadata.getEntityType(), true);
Expand Down Expand Up @@ -177,7 +195,7 @@ private void implementPaged(ClassCreator classCreator, ResourceMetadata resource
resource, page, sort);

// Return response
tryBlock.returnValue(responseImplementor.ok(tryBlock, entities, links));
returnValueWithLinks(tryBlock, resourceMetadata, resourceProperties, entities, links);
tryBlock.close();
} else {
ResultHandle uniPageCount = methodCreator.invokeVirtualMethod(
Expand All @@ -194,7 +212,8 @@ private void implementPaged(ClassCreator classCreator, ResourceMetadata resource
Sort.class),
resource, page, sort);
body.returnValue(UniImplementor.map(body, uniEntities, EXCEPTION_MESSAGE,
(listBody, list) -> listBody.returnValue(responseImplementor.ok(listBody, list, links))));
(listBody, list) -> returnValueWithLinks(listBody, resourceMetadata, resourceProperties, list,
links)));
}));
}

Expand All @@ -203,15 +222,15 @@ private void implementPaged(ClassCreator classCreator, ResourceMetadata resource

private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, FieldDescriptor resourceFieldDescriptor) {
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(getMethodName(), classCreator,
isNotReactivePanache() ? ofType(Response.class) : ofType(Uni.class, resourceMetadata.getEntityType()),
List.class);
methodCreator.setParameterNames(new String[] { "sort" });

// Add method annotations
addGetAnnotation(methodCreator);
addPathAnnotation(methodCreator, resourceProperties.getPath(RESOURCE_METHOD_NAME));
addProducesAnnotation(methodCreator, APPLICATION_JSON);
addProducesJsonAnnotation(methodCreator, resourceProperties);
addLinksAnnotation(methodCreator, resourceMetadata.getEntityType(), REL);
addMethodAnnotations(methodCreator, resourceProperties.getMethodAnnotations(RESOURCE_METHOD_NAME));
addOpenApiResponseAnnotation(methodCreator, Response.Status.OK, resourceMetadata.getEntityType(), true);
Expand All @@ -228,7 +247,7 @@ private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resou
ofMethod(resourceMetadata.getResourceClass(), RESOURCE_METHOD_NAME,
List.class, Page.class, Sort.class),
resource, tryBlock.loadNull(), sort);
tryBlock.returnValue(responseImplementor.ok(tryBlock, entities));
returnValue(tryBlock, resourceMetadata, resourceProperties, entities);
tryBlock.close();
} else {
ResultHandle uniEntities = methodCreator.invokeVirtualMethod(
Expand All @@ -237,7 +256,7 @@ private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resou
resource, methodCreator.loadNull(), sort);

methodCreator.returnValue(UniImplementor.map(methodCreator, uniEntities, EXCEPTION_MESSAGE,
(body, entities) -> body.returnValue(responseImplementor.ok(body, entities))));
(body, entities) -> returnValue(body, resourceMetadata, resourceProperties, entities)));
}

methodCreator.close();
Expand Down

This file was deleted.

Loading

0 comments on commit df2f0f5

Please sign in to comment.