Skip to content

Commit

Permalink
Merge pull request #515 from avaje/feature/jex-handler-methods
Browse files Browse the repository at this point in the history
[Jex] Modify generated code for Jex controllers to use handler methods
  • Loading branch information
SentryMan authored Nov 25, 2024
2 parents 4705147 + 069e185 commit 620d73d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ControllerMethodWriter {
private final MethodReader method;
private final Append writer;
private final WebMethod webMethod;
private boolean instrumentContext;
private final boolean instrumentContext;

ControllerMethodWriter(MethodReader method, Append writer) {
this.method = method;
Expand All @@ -23,16 +23,37 @@ class ControllerMethodWriter {
this.instrumentContext = method.instrumentContext();
}

void write(boolean requestScoped) {
void writeRouting() {
final PathSegments segments = method.pathSegments();
final String fullPath = segments.fullPath();
writer.append(" routing.%s(\"%s\", this::_%s)", webMethod.name().toLowerCase(), fullPath, method.simpleName());
List<String> roles = method.roles();
if (!roles.isEmpty()) {
writer.append(".withRoles(");
for (int i = 0; i < roles.size(); i++) {
if (i > 0) {
writer.append(", ");
}
writer.append(Util.shortName(roles.get(i), true));
}
writer.append(")");
}
writer.append(";").eol();
}

writer.append(" routing.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol();
void writeHandler(boolean requestScoped) {
writer.append(" private void _%s(Context ctx) {", method.simpleName()).eol();
write(requestScoped);
writer.append(" }").eol().eol();
}

private void write(boolean requestScoped) {
int statusCode = method.statusCode();
if (statusCode > 0) {
writer.append(" ctx.status(%d);", statusCode).eol();
writer.append(" ctx.status(%d);", statusCode).eol();
}

final PathSegments segments = method.pathSegments();
List<PathSegments.Segment> matrixSegments = segments.matrixSegments();
for (PathSegments.Segment matrixSegment : matrixSegments) {
matrixSegment.writeCreateSegment(writer, platform());
Expand All @@ -47,7 +68,7 @@ void write(boolean requestScoped) {
param.writeValidate(writer);
}
}
writer.append(" ");
writer.append(" ");
if (!method.isVoid()) {
writeContextReturn();
}
Expand All @@ -74,19 +95,6 @@ void write(boolean requestScoped) {
writer.append(")");
}
writer.append(";").eol();
writer.append(" }");

List<String> roles = method.roles();
if (!roles.isEmpty()) {
writer.append(").withRoles(");
for (int i = 0; i < roles.size(); i++) {
if (i > 0) {
writer.append(", ");
}
writer.append(Util.shortName(roles.get(i), true));
}
}
writer.append(");").eol().eol();
}

private void writeContextReturn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
class ControllerWriter extends BaseControllerWriter {

private static final String AT_GENERATED = "@Generated(\"avaje-jex-generator\")";
private static final String API_CONTEXT = "io.avaje.jex.Context";
private static final String API_ROUTING = "io.avaje.jex.Routing";
private static final String API_ROUTING_SERVICE = "io.avaje.jex.Routing.Service";

ControllerWriter(ControllerReader reader) throws IOException {
super(reader);
reader.addImportType(API_CONTEXT);
reader.addImportType(API_ROUTING);
reader.addImportType(API_ROUTING_SERVICE);
}
Expand All @@ -25,27 +27,36 @@ void write() {
writeImports();
writeClassStart();
writeAddRoutes();
writeHandlers();
writeClassEnd();
}

private void writeAddRoutes() {
writer.append(" @Override").eol();
writer.append(" public void add(Routing routing) {").eol().eol();
writer.append(" public void add(Routing routing) {").eol();
for (MethodReader method : reader.methods()) {
if (method.isWebMethod()) {
writeForMethod(method);
writeRouting(method);
}
}
writer.append(" }").eol().eol();
}

private void writeForMethod(MethodReader method) {
new ControllerMethodWriter(method, writer).write(isRequestScoped());
if (!reader.isDocHidden()) {
method.buildApiDocumentation();
private void writeHandlers() {
for (MethodReader method : reader.methods()) {
if (method.isWebMethod()) {
new ControllerMethodWriter(method, writer).writeHandler(isRequestScoped());
if (!reader.isDocHidden()) {
method.buildApiDocumentation();
}
}
}
}

private void writeRouting(MethodReader method) {
new ControllerMethodWriter(method, writer).writeRouting();
}

private void writeClassStart() {
writer.append(AT_GENERATED).eol();
writer.append(diAnnotation()).eol();
Expand All @@ -62,7 +73,7 @@ private void writeClassStart() {
if (reader.isIncludeValidator()) {
writer.append(" private final Validator validator;").eol();
}

if (instrumentContext) {
writer.append(" private final RequestContextResolver resolver;").eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String bodyAsClass(UType uType) {

@Override
public String indent() {
return " ";
return " ";
}

@Override
Expand Down Expand Up @@ -85,7 +85,7 @@ public void writeReadCollectionParameter(
}
writer.append("withDefault(ctx.queryParams(\"%s\"), java.util.List.of(\"%s\"))", paramName, String.join(",", paramDefault));
}

@Override
public void writeAcceptLanguage(Append writer) {
writer.append("ctx.header(\"%s\")", Constants.ACCEPT_LANGUAGE);
Expand Down

0 comments on commit 620d73d

Please sign in to comment.