Skip to content

Commit 45f7496

Browse files
committed
[sigma] add jstachio support
almost forgot about this one
1 parent 12f5079 commit 45f7496

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

http-generator-sigma/src/main/java/io/avaje/http/generator/sigma/ControllerMethodWriter.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package io.avaje.http.generator.sigma;
22

3-
import static io.avaje.http.generator.core.ProcessingContext.*;
3+
import static io.avaje.http.generator.core.ProcessingContext.isAssignable2Interface;
4+
import static io.avaje.http.generator.core.ProcessingContext.logError;
5+
import static io.avaje.http.generator.core.ProcessingContext.platform;
46

57
import java.util.List;
68

7-
import io.avaje.http.generator.core.*;
9+
import io.avaje.http.generator.core.Append;
10+
import io.avaje.http.generator.core.CoreWebMethod;
11+
import io.avaje.http.generator.core.JsonBUtil;
12+
import io.avaje.http.generator.core.MethodParam;
13+
import io.avaje.http.generator.core.MethodReader;
14+
import io.avaje.http.generator.core.PathSegments;
15+
import io.avaje.http.generator.core.ProcessingContext;
16+
import io.avaje.http.generator.core.WebMethod;
817
import io.avaje.http.generator.core.openapi.MediaType;
918

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

1929
ControllerMethodWriter(MethodReader method, Append writer) {
2030
this.method = method;
2131
this.writer = writer;
2232
this.webMethod = method.webMethod();
2333
this.instrumentContext = method.instrumentContext();
2434
this.isFilter = webMethod == CoreWebMethod.FILTER;
35+
this.useJstachio = ProcessingContext.isJstacheTemplate(method.returnType());
2536
if (isFilter) {
2637
validateFilter();
2738
}
@@ -122,6 +133,11 @@ private List<MethodParam> writeParams(final PathSegments segments) {
122133

123134
private void writeContextReturn() {
124135
var produces = method.produces();
136+
137+
if (useJstachio && produces == null) {
138+
produces = MediaType.TEXT_HTML.getValue();
139+
}
140+
125141
boolean applicationJson =
126142
produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces);
127143
if (applicationJson || JsonBUtil.isJsonMimeType(produces)) {
@@ -130,6 +146,9 @@ private void writeContextReturn() {
130146
} else {
131147
writer.append(" ctx.contentType(\"%s\").result(result);", produces);
132148
}
149+
} else if (useJstachio) {
150+
var renderer = ProcessingContext.jstacheRenderer(method.returnType());
151+
writer.append(" ctx.contentType(\"%s\").result(%s(result));", produces, renderer);
133152
} else if (MediaType.TEXT_HTML.getValue().equalsIgnoreCase(produces)) {
134153
writer.append(" ctx.html(result);");
135154
} else if (MediaType.TEXT_PLAIN.getValue().equalsIgnoreCase(produces)) {

tests/test-sigma/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,23 @@
4848
<artifactId>swagger-annotations</artifactId>
4949
<version>${swagger.version}</version>
5050
</dependency>
51+
52+
53+
<dependency>
54+
<groupId>io.jstach</groupId>
55+
<artifactId>jstachio</artifactId>
56+
<version>1.3.6</version>
57+
</dependency>
58+
5159

5260
<!-- java annotation processors -->
61+
62+
<dependency>
63+
<groupId>io.jstach</groupId>
64+
<artifactId>jstachio-apt</artifactId>
65+
<version>1.3.6</version>
66+
</dependency>
67+
5368
<dependency>
5469
<groupId>io.avaje</groupId>
5570
<artifactId>avaje-inject-generator</artifactId>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.example.myapp.web.jstache;
2+
3+
import java.time.LocalDate;
4+
import java.time.format.DateTimeFormatter;
5+
import java.time.temporal.ChronoUnit;
6+
import java.util.List;
7+
8+
import io.avaje.http.api.Controller;
9+
import io.avaje.http.api.Get;
10+
import io.jstach.jstache.JStache;
11+
import io.jstach.jstache.JStacheConfig;
12+
import io.jstach.jstache.JStacheLambda;
13+
import io.jstach.jstache.JStacheType;
14+
15+
@Controller("/jstache")
16+
public class JstacheController {
17+
18+
@Get("/hello")
19+
public HelloWorldZeroDependency hello() {
20+
Person rick = new Person("Rick", LocalDate.now().minusYears(70));
21+
Person morty = new Person("Morty", LocalDate.now().minusYears(14));
22+
Person beth = new Person("Beth", LocalDate.now().minusYears(35));
23+
Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
24+
return new HelloWorldZeroDependency("Hello alien", List.of(rick, morty, beth, jerry));
25+
}
26+
27+
@Get("/helloRuntime")
28+
public HelloWorld helloRuntime() {
29+
Person rick = new Person("Rick", LocalDate.now().minusYears(70));
30+
Person morty = new Person("Morty", LocalDate.now().minusYears(14));
31+
Person beth = new Person("Beth", LocalDate.now().minusYears(35));
32+
Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
33+
return new HelloWorld("Hello alien", List.of(rick, morty, beth, jerry));
34+
}
35+
36+
/*
37+
* Annotate the root model with an inline mustache template
38+
*/
39+
@JStacheConfig(type = JStacheType.STACHE)
40+
@JStache(
41+
template =
42+
"""
43+
{{#people}}
44+
{{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
45+
{{#-last}}
46+
That is all for now!
47+
{{/-last}}
48+
{{/people}}
49+
""")
50+
public record HelloWorldZeroDependency(String message, List<Person> people) implements AgeLambdaSupport {}
51+
52+
public record Person(String name, LocalDate birthday) {}
53+
54+
public record AgeInfo(long age, String date) {}
55+
56+
public interface AgeLambdaSupport {
57+
@JStacheLambda
58+
default AgeInfo ageInfo(Person person) {
59+
long age = ChronoUnit.YEARS.between(person.birthday(), LocalDate.now());
60+
String date = person.birthday().format(DateTimeFormatter.ISO_DATE);
61+
return new AgeInfo(age, date);
62+
}
63+
}
64+
65+
@JStache(
66+
template =
67+
"""
68+
{{#people}}
69+
{{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
70+
{{#-last}}
71+
That is all for now!
72+
{{/-last}}
73+
{{/people}}
74+
""")
75+
public record HelloWorld(String message, List<Person> people) implements AgeLambdaSupport {}
76+
77+
}

0 commit comments

Comments
 (0)