Skip to content

[sigma-generator] add jstachio support #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package io.avaje.http.generator.sigma;

import static io.avaje.http.generator.core.ProcessingContext.*;
import static io.avaje.http.generator.core.ProcessingContext.isAssignable2Interface;
import static io.avaje.http.generator.core.ProcessingContext.logError;
import static io.avaje.http.generator.core.ProcessingContext.platform;

import java.util.List;

import io.avaje.http.generator.core.*;
import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.CoreWebMethod;
import io.avaje.http.generator.core.JsonBUtil;
import io.avaje.http.generator.core.MethodParam;
import io.avaje.http.generator.core.MethodReader;
import io.avaje.http.generator.core.PathSegments;
import io.avaje.http.generator.core.ProcessingContext;
import io.avaje.http.generator.core.WebMethod;
import io.avaje.http.generator.core.openapi.MediaType;

/** Write code to register Web route for a given controller method. */
Expand All @@ -15,13 +24,15 @@ class ControllerMethodWriter {
private final WebMethod webMethod;
private final boolean instrumentContext;
private final boolean isFilter;
private boolean useJstachio;

ControllerMethodWriter(MethodReader method, Append writer) {
this.method = method;
this.writer = writer;
this.webMethod = method.webMethod();
this.instrumentContext = method.instrumentContext();
this.isFilter = webMethod == CoreWebMethod.FILTER;
this.useJstachio = ProcessingContext.isJstacheTemplate(method.returnType());
if (isFilter) {
validateFilter();
}
Expand Down Expand Up @@ -122,6 +133,11 @@ private List<MethodParam> writeParams(final PathSegments segments) {

private void writeContextReturn() {
var produces = method.produces();

if (useJstachio && produces == null) {
produces = MediaType.TEXT_HTML.getValue();
}

boolean applicationJson =
produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces);
if (applicationJson || JsonBUtil.isJsonMimeType(produces)) {
Expand All @@ -130,6 +146,9 @@ private void writeContextReturn() {
} else {
writer.append(" ctx.contentType(\"%s\").result(result);", produces);
}
} else if (useJstachio) {
var renderer = ProcessingContext.jstacheRenderer(method.returnType());
writer.append(" ctx.contentType(\"%s\").result(%s(result));", produces, renderer);
} else if (MediaType.TEXT_HTML.getValue().equalsIgnoreCase(produces)) {
writer.append(" ctx.html(result);");
} else if (MediaType.TEXT_PLAIN.getValue().equalsIgnoreCase(produces)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test-sigma/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,23 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>


<dependency>
<groupId>io.jstach</groupId>
<artifactId>jstachio</artifactId>
<version>1.3.6</version>
</dependency>


<!-- java annotation processors -->

<dependency>
<groupId>io.jstach</groupId>
<artifactId>jstachio-apt</artifactId>
<version>1.3.6</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.example.myapp.web.jstache;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import io.jstach.jstache.JStache;
import io.jstach.jstache.JStacheConfig;
import io.jstach.jstache.JStacheLambda;
import io.jstach.jstache.JStacheType;

@Controller("/jstache")
public class JstacheController {

@Get("/hello")
public HelloWorldZeroDependency hello() {
Person rick = new Person("Rick", LocalDate.now().minusYears(70));
Person morty = new Person("Morty", LocalDate.now().minusYears(14));
Person beth = new Person("Beth", LocalDate.now().minusYears(35));
Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
return new HelloWorldZeroDependency("Hello alien", List.of(rick, morty, beth, jerry));
}

@Get("/helloRuntime")
public HelloWorld helloRuntime() {
Person rick = new Person("Rick", LocalDate.now().minusYears(70));
Person morty = new Person("Morty", LocalDate.now().minusYears(14));
Person beth = new Person("Beth", LocalDate.now().minusYears(35));
Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
return new HelloWorld("Hello alien", List.of(rick, morty, beth, jerry));
}

/*
* Annotate the root model with an inline mustache template
*/
@JStacheConfig(type = JStacheType.STACHE)
@JStache(
template =
"""
{{#people}}
{{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
{{#-last}}
That is all for now!
{{/-last}}
{{/people}}
""")
public record HelloWorldZeroDependency(String message, List<Person> people) implements AgeLambdaSupport {}

public record Person(String name, LocalDate birthday) {}

public record AgeInfo(long age, String date) {}

public interface AgeLambdaSupport {
@JStacheLambda
default AgeInfo ageInfo(Person person) {
long age = ChronoUnit.YEARS.between(person.birthday(), LocalDate.now());
String date = person.birthday().format(DateTimeFormatter.ISO_DATE);
return new AgeInfo(age, date);
}
}

@JStache(
template =
"""
{{#people}}
{{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
{{#-last}}
That is all for now!
{{/-last}}
{{/people}}
""")
public record HelloWorld(String message, List<Person> people) implements AgeLambdaSupport {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public void runAnnotationProcessor() throws Exception {
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AdisableDirectWrites=true"),
List.of(
"--release=" + Integer.getInteger("java.specification.version"),
"-AdisableDirectWrites=true"),
null,
files);
task.setProcessors(List.of(new SigmaProcessor()));
Expand All @@ -75,7 +77,7 @@ void runAnnotationProcessorJakarta() throws Exception {
null,
null,
List.of(
"--release=11",
"--release=" + Integer.getInteger("java.specification.version"),
"-AuseJavax=false",
"-AuseSingleton=true",
"-AdisableDirectWrites=true"),
Expand Down