Skip to content

Commit

Permalink
Merge branch 'master' into loomClient
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan authored Sep 7, 2024
2 parents b2260f0 + 295a23e commit 63e3fe4
Show file tree
Hide file tree
Showing 45 changed files with 1,853 additions and 0 deletions.
31 changes: 31 additions & 0 deletions http-generator-sigma/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-parent</artifactId>
<version>2.8-RC1</version>
</parent>

<artifactId>avaje-http-sigma-generator</artifactId>

<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-generator-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-prisms</artifactId>
<version>${avaje.prisms.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
1 change: 1 addition & 0 deletions http-generator-sigma/src/etc/activate-shade-module
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove from module-info all content after: // SHADED:
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.avaje.http.generator.sigma;

import static io.avaje.http.generator.core.ProcessingContext.*;

import java.util.List;

import io.avaje.http.generator.core.*;
import io.avaje.http.generator.core.openapi.MediaType;

/** Write code to register Web route for a given controller method. */
class ControllerMethodWriter {

private final MethodReader method;
private final Append writer;
private final WebMethod webMethod;
private final boolean instrumentContext;
private final boolean customMethod;

ControllerMethodWriter(MethodReader method, Append writer) {
this.method = method;
this.writer = writer;
final var webM = method.webMethod();
this.webMethod = webM == CoreWebMethod.FILTER ? SigmaWebMethod.BEFORE : webM;
this.instrumentContext = method.instrumentContext();
customMethod = !(webMethod instanceof CoreWebMethod);
}

void write() {
final var segments = method.pathSegments();
final var fullPath = segments.fullPath();

writeMethod(fullPath);

final var params = writeParams(segments);
writer.append(" ");
if (!method.isVoid() && !customMethod) {
writer.append("var result = ");
}

if (instrumentContext) {
method.writeContext(writer, "ctx", "ctx");
}

writer.append("controller.");

writer.append(method.simpleName()).append("(");
for (var i = 0; i < params.size(); i++) {
if (i > 0) {
writer.append(", ");
}
final var param = params.get(i);
if (isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
writer.append("ex");
} else {
param.buildParamName(writer);
}
}

if (instrumentContext) {
writer.append(")");
}

writer.append(");").eol();
if (!method.isVoid() && !customMethod) {
writeContextReturn();
writer.eol();
}

writer.append(" }");
writer.append(");").eol().eol();
}

private void writeMethod(final String fullPath) {
if (method.isErrorMethod()) {
writer
.append(" router.exception(%s.class, (ex, ctx) -> {", method.exceptionShortName())
.eol();
} else {
var methodName = webMethod.name().toLowerCase().replace("_m", "M");
writer.append(" router.%s(\"%s\", ctx -> {", methodName, fullPath).eol();
}
if (!customMethod) {
int statusCode = method.statusCode();
if (statusCode > 0) {
writer.append(" ctx.status(%d);", statusCode).eol();
}
}
}

private List<MethodParam> writeParams(final PathSegments segments) {
final var matrixSegments = segments.matrixSegments();
for (final PathSegments.Segment matrixSegment : matrixSegments) {
matrixSegment.writeCreateSegment(writer, platform());
}

final var params = method.params();
for (final MethodParam param : params) {
if (!isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
param.writeCtxGet(writer, segments);
}
}
if (method.includeValidate()) {
for (final MethodParam param : params) {
param.writeValidate(writer);
}
}
return params;
}

private void writeContextReturn() {
var produces = method.produces();
boolean applicationJson =
produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces);
if (applicationJson || JsonBUtil.isJsonMimeType(produces)) {
if (applicationJson) {
writer.append(" ctx.json(result);");
} else {
writer.append(" ctx.contentType(\"%s\").result(result);", produces);
}
} else if (MediaType.TEXT_HTML.getValue().equalsIgnoreCase(produces)) {
writer.append(" ctx.html(result);");
} else if (MediaType.TEXT_PLAIN.getValue().equalsIgnoreCase(produces)) {
writer.append(" ctx.text(result);");
} else {
writer.append(" ctx.contentType(\"%s\").result(%s);", produces);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.avaje.http.generator.sigma;

import static io.avaje.http.generator.core.ProcessingContext.diAnnotation;

import java.io.IOException;

import io.avaje.http.generator.core.BaseControllerWriter;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.MethodReader;

/**
* Write Sigma specific Controller WebRoute handling adapter.
*/
class ControllerWriter extends BaseControllerWriter {

private static final String AT_GENERATED = "@Generated(\"avaje-sigma-generator\")";

ControllerWriter(ControllerReader reader) throws IOException {
super(reader);
reader.addImportType("io.avaje.sigma.HttpService");
reader.addImportType("io.avaje.sigma.Router");
}

void write() {
writePackage();
writeImports();
writeClassStart();
writeAddRoutes();
writeClassEnd();
}

private void writeAddRoutes() {
writer.append(" @Override").eol();
writer.append(" public void setup(Router router) {").eol().eol();


for (final MethodReader method : reader.methods()) {
if (method.isWebMethod()) {
writeForMethod(method);
}
}
writer.append(" }").eol().eol();
}

private void writeForMethod(MethodReader method) {
new ControllerMethodWriter(method, writer).write();
if (!reader.isDocHidden()) {
method.buildApiDocumentation();
}
}

private void writeClassStart() {
writer.append(AT_GENERATED).eol();
writer.append(diAnnotation()).eol();
writer
.append("public class ")
.append(shortName)
.append("$Route implements HttpService {")
.eol()
.eol();

var controllerName = "controller";
var controllerType = shortName;
writer.append(" private final %s %s;", controllerType, controllerName).eol();

if (reader.isIncludeValidator()) {
writer.append(" private final Validator validator;").eol();
}

if (instrumentContext) {
writer.append(" private final RequestContextResolver resolver;").eol();
}
writer.eol();

writer.append(" public %s$Route(%s %s", shortName, controllerType, controllerName);
if (reader.isIncludeValidator()) {
writer.append(", Validator validator");
}
if (instrumentContext) {
writer.append(", RequestContextResolver resolver");
}
writer.append(") {").eol();
writer.append(" this.%s = %s;", controllerName, controllerName).eol();
if (reader.isIncludeValidator()) {
writer.append(" this.validator = validator;").eol();
}
if (instrumentContext) {
writer.append(" this.resolver = resolver;").eol();
}
writer.append(" }").eol().eol();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package io.avaje.http.generator.sigma;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import javax.lang.model.element.Element;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.Constants;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.CustomWebMethod;
import io.avaje.http.generator.core.ParamType;
import io.avaje.http.generator.core.PlatformAdapter;
import io.avaje.http.generator.core.UType;

class SigmaAdapter implements PlatformAdapter {

static final String CONTEXT = "io.avaje.sigma.HttpContext";

@Override
public boolean isContextType(String rawType) {
return CONTEXT.equals(rawType);
}

@Override
public String platformVariable(String rawType) {
return "ctx";
}

@Override
public boolean isBodyMethodParam() {
return false;
}

@Override
public String bodyAsClass(UType type) {
if ("java.lang.String".equals(type.full())) {
return "ctx.body()";
} else {
return "ctx.bodyAsClass(" + type.mainType() + ".class)";
}
}

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

@Override
public void writeReadParameter(Append writer, ParamType paramType, String paramName) {
writer.append("ctx.%s(\"%s\")", paramType, paramName);
}

@Override
public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) {
writer.append("withDefault(ctx.%s(\"%s\"), \"%s\")", paramType, paramName, paramDefault);
}

@Override
public void writeReadMapParameter(Append writer, ParamType paramType) {

switch (paramType) {
case QUERYPARAM:
writer.append("ctx.queryParamMap()");
break;
case FORM:
case FORMPARAM:
writer.append("ctx.formParamMap()");
break;
default:
throw new UnsupportedOperationException(
"Only Query/Form Params have Map<String, List<String>> supported in Javalin");
}
}

@Override
public void writeReadCollectionParameter(Append writer, ParamType paramType, String paramName) {
switch (paramType) {
case QUERYPARAM:
writer.append("ctx.queryParams(\"%s\")", paramName);
break;
case HEADER:
writer.append("ctx.headers(\"%s\")", paramName);
break;
case FORMPARAM:
writer.append("ctx.formParams(\"%s\")", paramName);
break;
default:
throw new UnsupportedOperationException(
"Only MultiValue Form/Query Params are supported in Javalin");
}
}

@Override
public void writeReadCollectionParameter(
Append writer, ParamType paramType, String paramName, List<String> paramDefault) {

switch (paramType) {
case QUERYPARAM:
writer.append(
"withDefault(ctx.queryParams(\"%s\"), java.util.List.of(\"%s\"))",
paramName, String.join(",", paramDefault));
break;
case HEADER:
writer.append(
"withDefault(ctx.headers(\"%s\"), java.util.List.of(\"%s\"))",
paramName, String.join(",", paramDefault));
break;
case FORMPARAM:
writer.append(
"withDefault(ctx.formParams(\"%s\"), java.util.List.of(\"%s\"))",
paramName, String.join(",", paramDefault));
break;
default:
throw new UnsupportedOperationException(
"Only MultiValue Form/Header/Query Params are supported in Javalin");
}
}

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

@Override
public List<Function<Element, Optional<CustomWebMethod>>> customHandlers() {

return List.of();
}

@Override
public void controllerRoles(List<String> roles, ControllerReader controller) {

}

@Override
public void methodRoles(List<String> roles, ControllerReader controller) {

}
}
Loading

0 comments on commit 63e3fe4

Please sign in to comment.