Skip to content

Commit 6c90974

Browse files
committed
nima-generator - tidy up only
1 parent c6da43a commit 6c90974

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package io.avaje.http.generator.core;
22

3+
import javax.lang.model.type.TypeMirror;
34
import java.util.*;
45

56
public interface UType {
67

8+
/**
9+
* Create the UType from the given TypeMirror.
10+
*/
11+
static UType parse(TypeMirror type) {
12+
return Util.parseType(type);
13+
}
14+
715
UType VOID = new VoidType();
816

917
/**

http-generator-nima/src/main/java/io/avaje/http/generator/helidon/nima/ControllerMethodWriter.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@ class ControllerMethodWriter {
1717
private final WebMethod webMethod;
1818
private final ProcessingContext ctx;
1919
private final boolean useJsonB;
20-
private final Map<String, UType> jsonTypes;
21-
22-
ControllerMethodWriter(
23-
MethodReader method,
24-
Append writer,
25-
ProcessingContext ctx,
26-
boolean useJsonB,
27-
Map<String, UType> jsonTypes) {
20+
21+
ControllerMethodWriter(MethodReader method, Append writer, ProcessingContext ctx, boolean useJsonB) {
2822
this.method = method;
2923
this.writer = writer;
30-
webMethod = method.getWebMethod();
24+
this.webMethod = method.getWebMethod();
3125
this.ctx = ctx;
3226
this.useJsonB = useJsonB;
33-
this.jsonTypes=jsonTypes;
3427
}
3528

3629
void writeRule() {
@@ -52,9 +45,7 @@ void writeHandler(boolean requestScoped) {
5245
.getUType()
5346
.shortName();
5447
writer
55-
.append(
56-
" var %s = %sJsonType.fromJson(req.content().inputStream()); // RB1",
57-
method.getBodyName(), fieldName)
48+
.append(" var %s = %sJsonType.fromJson(req.content().inputStream());", method.getBodyName(), fieldName)
5849
.eol();
5950

6051
} else {
@@ -118,9 +109,8 @@ void writeHandler(boolean requestScoped) {
118109
if (!method.isVoid()) {
119110
writeContextReturn();
120111
if (producesJson()) {
121-
UType uType = Util.parseType(method.getReturnType());
122-
final var fieldName = uType.shortName();
123-
writer.append(" %sJsonType.toJson(result, res.outputStream()); // RB0", fieldName).eol();
112+
UType uType = UType.parse(method.getReturnType());
113+
writer.append(" %sJsonType.toJson(result, res.outputStream());", uType.shortName()).eol();
124114
} else {
125115
writer.append(" res.send(result);").eol();
126116
}

http-generator-nima/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.avaje.http.generator.helidon.nima;
22

3-
import java.io.IOException;
4-
import java.util.*;
5-
6-
import javax.lang.model.type.TypeMirror;
7-
83
import io.avaje.http.generator.core.*;
94

5+
import java.io.IOException;
6+
import java.util.LinkedHashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
1010
/** Write Helidon specific web route adapter (a Helidon Service). */
1111
class ControllerWriter extends BaseControllerWriter {
1212

@@ -39,21 +39,20 @@ private void initJsonTypes(ControllerReader reader) {
3939
.forEach(methodReader -> {
4040
addJsonBodyType(methodReader);
4141
if (!methodReader.isVoid()) {
42-
TypeMirror returnType = methodReader.getReturnType();
43-
addJsonBodyType(Util.parseType(returnType));
42+
addJsonType(UType.parse(methodReader.getReturnType()));
4443
}
4544
});
4645
}
4746

48-
private void addJsonBodyType(UType type) {
47+
private void addJsonType(UType type) {
4948
jsonTypes.put(type.full(), type);
5049
}
5150

5251
private void addJsonBodyType(MethodReader methodReader) {
5352
if (methodReader.getBodyType() != null) {
5453
methodReader.getParams().stream()
5554
.filter(MethodParam::isBody)
56-
.forEach(param -> addJsonBodyType(param.getUType()));
55+
.forEach(param -> addJsonType(param.getUType()));
5756
}
5857
}
5958

@@ -65,15 +64,15 @@ void write() {
6564
writeClassEnd();
6665
}
6766

68-
private List<ControllerMethodWriter> getWriterMethods() {
67+
private List<ControllerMethodWriter> writerMethods() {
6968
return reader.getMethods().stream()
7069
.filter(MethodReader::isWebMethod)
71-
.map(it -> new ControllerMethodWriter(it, writer, ctx, useJsonB, jsonTypes))
70+
.map(it -> new ControllerMethodWriter(it, writer, ctx, useJsonB))
7271
.toList();
7372
}
7473

7574
private void writeAddRoutes() {
76-
final var methods = getWriterMethods();
75+
final var methods = writerMethods();
7776
writeRoutes(methods);
7877
for (final ControllerMethodWriter methodWriter : methods) {
7978
methodWriter.writeHandler(isRequestScoped());
@@ -109,8 +108,8 @@ private void writeClassStart() {
109108
if (reader.isIncludeValidator()) {
110109
writer.append(" private final Validator validator;").eol();
111110
}
112-
for (UType value : jsonTypes.values()) {
113-
writer.append("private final JsonType<%s> %sJsonType; //RBx1", primitiveWrap(value.full()), value.shortName()).eol();
111+
for (UType type : jsonTypes.values()) {
112+
writer.append(" private final JsonType<%s> %sJsonType;", primitiveWrap(type.full()), type.shortName()).eol();
114113
}
115114
writer.eol();
116115

@@ -127,9 +126,9 @@ private void writeClassStart() {
127126
writer.append(" this.validator = validator;").eol();
128127
}
129128
if (useJsonB) {
130-
for (UType value : jsonTypes.values()) {
131-
writer.append(" this.%sJsonType = jsonB.type(", value.shortName());
132-
writeJsonbType(value);
129+
for (UType type : jsonTypes.values()) {
130+
writer.append(" this.%sJsonType = jsonB.type(", type.shortName());
131+
writeJsonbType(type);
133132
}
134133
}
135134
writer.append(" }").eol().eol();
@@ -146,7 +145,7 @@ private void writeJsonbType(UType type) {
146145
default -> throw new UnsupportedOperationException("Only java.util Map, Set and List are supported JsonB Controller Collection Types");
147146
}
148147
}
149-
writer.append("; // Rbx3").eol();
148+
writer.append(";").eol();
150149
}
151150

152151
private String primitiveWrap(String full) {

tests/test-nima-jsonb/src/main/java/org/example/HelloController.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,31 @@ String addMultiple(List<Person> newGuys) {
9292
return "New Guys Added";
9393
}
9494

95-
@Put("person/int")
95+
@Put("test/int")
9696
int testIntReturn() {
9797
return 422;
9898
}
9999

100-
@Put("person/long")
100+
@Put("test/long")
101101
long testLongReturn() {
102102
return 69;
103103
}
104104

105-
// curl -X POST http://localhost:8081/form -H "Content-Type: application/x-www-form-urlencoded" -d
106-
// "name=Jimmy&email=jim@foo&url=notaurl"
105+
// curl -X POST http://localhost:8081/form
106+
// -H "Content-Type: application/x-www-form-urlencoded"
107+
// -d "name=Jimmy&email=jim@foo&url=notaurl"
107108
@Form
108109
@Post("form")
109110
String form(String name, String email, String url) {
110111
return name + "-" + email + "-" + url;
111112
}
112113

113-
// curl -X POST http://localhost:8081/formBean -H "Content-Type:
114-
// application/x-www-form-urlencoded" -d "name=FormBeanJimmy&email=jim@foo&url=notaurl"
114+
// curl -X POST http://localhost:8081/formBean
115+
// -H "Content-Type:application/x-www-form-urlencoded"
116+
// -d "name=FormBeanJimmy&email=jim@foo&url=notaurl"
115117
@Form
116118
@Post("formBean")
117119
String formBean(MyForm form) {
118-
return form.name + "-" + form.email + "-" + form.url;
120+
return form.name + "|" + form.email + "|" + form.url;
119121
}
120122
}

tests/test-nima/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
<dependency>
3636
<groupId>io.helidon.nima.webserver</groupId>
3737
<artifactId>helidon-nima-webserver</artifactId>
38-
<version>4.0.0-ALPHA1</version>
38+
<version>4.0.0-ALPHA2</version>
3939
</dependency>
4040
<dependency>
4141
<groupId>io.helidon.nima.http.media</groupId>
4242
<artifactId>helidon-nima-http-media-jsonb</artifactId>
43-
<version>4.0.0-ALPHA1</version>
43+
<version>4.0.0-ALPHA2</version>
4444
</dependency>
4545

4646
<!-- <dependency>-->

0 commit comments

Comments
 (0)