Skip to content

Commit c6da43a

Browse files
committed
nima-generator - Use UType to simplify Jsonb types and names
1 parent bd6b9c3 commit c6da43a

File tree

6 files changed

+160
-175
lines changed

6 files changed

+160
-175
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public interface UType {
1616
*/
1717
String shortType();
1818

19+
/**
20+
* Return the short name.
21+
*/
22+
String shortName();
23+
1924
/**
2025
* Return the main type (outer most type).
2126
*/
@@ -40,6 +45,10 @@ default String param1() {
4045
*/
4146
String full();
4247

48+
default boolean isGeneric() {
49+
return false;
50+
}
51+
4352
default String genericParams() {
4453
return "";
4554
}
@@ -56,6 +65,11 @@ public String shortType() {
5665
return "void";
5766
}
5867

68+
@Override
69+
public String shortName() {
70+
return "void";
71+
}
72+
5973
@Override
6074
public String mainType() {
6175
return "java.lang.Void";
@@ -92,6 +106,11 @@ public String shortType() {
92106
return Util.shortName(rawType);
93107
}
94108

109+
@Override
110+
public String shortName() {
111+
return Util.initLower(shortType());
112+
}
113+
95114
@Override
96115
public String mainType() {
97116
return rawType;
@@ -105,11 +124,13 @@ class Generic implements UType {
105124
final String rawType;
106125
final List<String> allTypes;
107126
final String shortRawType;
127+
final String shortName;
108128

109129
Generic(String rawTypeInput) {
110130
this.rawType = rawTypeInput.replace(" ",""); // trim whitespace
111131
this.allTypes = Arrays.asList(rawType.split("[<|>|,]"));
112132
this.shortRawType = shortRawType(rawType, allTypes);
133+
this.shortName = Util.name(shortRawType);
113134
}
114135

115136
private String shortRawType(String rawType, List<String> allTypes) {
@@ -140,6 +161,11 @@ public Set<String> importTypes() {
140161
return set;
141162
}
142163

164+
@Override
165+
public boolean isGeneric() {
166+
return true;
167+
}
168+
143169
@Override
144170
public String genericParams() {
145171
final StringJoiner joiner = new StringJoiner(",");
@@ -157,6 +183,11 @@ public String shortType() {
157183
return shortRawType;
158184
}
159185

186+
@Override
187+
public String shortName() {
188+
return shortName;
189+
}
190+
160191
@Override
161192
public String mainType() {
162193
return allTypes.isEmpty() ? null : allTypes.get(0);

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ public static String shortName(String fullType) {
7070
}
7171
}
7272

73-
public static String snakeCase(String name) {
73+
/**
74+
* Return a field or variable name to match the short type.
75+
*/
76+
public static String name(String name) {
77+
return initLower(name.replaceAll("([,<>\\[\\]])", ""));
78+
}
7479

80+
public static String snakeCase(String name) {
7581
StringBuilder sb = new StringBuilder(name.length() + 5);
76-
7782
int len = name.length();
7883
for (int i = 0; i < len; i++) {
7984
char ch = name.charAt(i);
@@ -89,11 +94,22 @@ public static String snakeCase(String name) {
8994
return sb.toString();
9095
}
9196

92-
public static String initcap(String input) {
97+
public static String initLower(String input) {
9398
if (input.length() < 2) {
94-
return input.toUpperCase();
99+
return input.toLowerCase();
95100
} else {
96-
return Character.toUpperCase(input.charAt(0)) + input.substring(1);
101+
StringBuilder sb = new StringBuilder(input.length());
102+
sb.append(Character.toLowerCase(input.charAt(0)));
103+
int i = 1;
104+
for (; i < input.length(); i++) {
105+
if (Character.isUpperCase(input.charAt(i))) {
106+
sb.append(Character.toLowerCase(input.charAt(i)));
107+
} else {
108+
sb.append(input.substring(i));
109+
break;
110+
}
111+
}
112+
return sb.toString();
97113
}
98114
}
99115

@@ -158,7 +174,7 @@ public static UType parseType(TypeMirror returnType) {
158174
if (returnType.getKind() == TypeKind.VOID) {
159175
return UType.VOID;
160176
}
161-
return parse(returnType.toString());//typeDef(returnType));
177+
return parse(returnType.toString());
162178
}
163179

164180
private static class RoleReader extends SimpleAnnotationValueVisitor8<List<String>, Object> {

http-generator-core/src/test/java/io/avaje/http/generator/core/UtilTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void parse_basic() {
8080

8181
assertThat(type.importTypes()).containsExactly("org.example.Repo");
8282
assertThat(type.shortType()).isEqualTo("Repo");
83+
assertThat(type.shortName()).isEqualTo("repo");
8384
}
8485

8586
@Test
@@ -88,6 +89,7 @@ void parse_generic() {
8889

8990
assertThat(type.importTypes()).containsExactly("java.util.List", "org.example.Repo");
9091
assertThat(type.shortType()).isEqualTo("List<Repo>");
92+
assertThat(type.shortName()).isEqualTo("listRepo");
9193
}
9294

9395
@Test
@@ -128,6 +130,7 @@ void parse_CompletableFutureStreamBean() {
128130

129131
assertThat(type.importTypes()).containsExactly("java.util.concurrent.CompletableFuture", "java.util.Stream", "org.example.Repo");
130132
assertThat(type.shortType()).isEqualTo("CompletableFuture<Stream<Repo>>");
133+
assertThat(type.shortName()).isEqualTo("completableFutureStreamRepo");
131134
}
132135

133136
@Test
@@ -145,6 +148,7 @@ void parse_BodyHandler_Path() {
145148

146149
assertThat(type.importTypes()).containsExactly("java.net.http.HttpResponse.BodyHandler", "java.util.Path");
147150
assertThat(type.shortType()).isEqualTo("BodyHandler<Path>");
151+
assertThat(type.shortName()).isEqualTo("bodyHandlerPath");
148152
assertThat(type.genericParams()).isEqualTo("");
149153
}
150154

@@ -154,6 +158,7 @@ void parse_BodyHandler_Multi() {
154158

155159
assertThat(type.importTypes()).containsExactly("java.net.http.HttpResponse.BodyHandler", "some.Foo");
156160
assertThat(type.shortType()).isEqualTo("BodyHandler<Foo<A,B>>");
161+
assertThat(type.shortName()).isEqualTo("bodyHandlerFooAB");
157162
assertThat(type.genericParams()).isEqualTo("<A,B> ");
158163
}
159164

@@ -163,6 +168,22 @@ void parse_BodyHandler_Multi2() {
163168

164169
assertThat(type.importTypes()).containsExactly("java.net.http.HttpResponse.BodyHandler", "some.Foo", "some.Bar");
165170
assertThat(type.shortType()).isEqualTo("BodyHandler<Foo<AB,BC,Bar<D>>>");
171+
assertThat(type.shortName()).isEqualTo("bodyHandlerFooABBCBarD");
166172
assertThat(type.genericParams()).isEqualTo("<AB,BC,D> ");
167173
}
174+
175+
@Test
176+
void utypeShortName() {
177+
UType type = Util.parse("java.util.Map<java.util.String,org.foo.Person>");
178+
assertThat(type.shortName()).isEqualTo("mapStringPerson");
179+
assertThat(type.shortType()).isEqualTo("Map<String,Person>");
180+
}
181+
182+
@Test
183+
void shortName() {
184+
assertThat(Util.name("List<Person>")).isEqualTo("listPerson");
185+
assertThat(Util.name("Set<Person>")).isEqualTo("setPerson");
186+
assertThat(Util.name("Map<String,Person>")).isEqualTo("mapStringPerson");
187+
}
188+
168189
}

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

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

3-
import java.util.AbstractMap.SimpleImmutableEntry;
3+
import io.avaje.http.api.MediaType;
4+
import io.avaje.http.generator.core.*;
5+
46
import java.util.List;
57
import java.util.Map;
68
import java.util.Optional;
79

8-
import io.avaje.http.api.MediaType;
9-
import io.avaje.http.generator.core.Append;
10-
import io.avaje.http.generator.core.MethodParam;
11-
import io.avaje.http.generator.core.MethodReader;
12-
import io.avaje.http.generator.core.ParamType;
13-
import io.avaje.http.generator.core.PathSegments;
14-
import io.avaje.http.generator.core.ProcessingContext;
15-
import io.avaje.http.generator.core.WebMethod;
16-
1710
/**
1811
* Write code to register Web route for a given controller method.
1912
*/
@@ -24,14 +17,14 @@ class ControllerMethodWriter {
2417
private final WebMethod webMethod;
2518
private final ProcessingContext ctx;
2619
private final boolean useJsonB;
27-
private final Map<String, SimpleImmutableEntry<String, String>> jsonTypes;
20+
private final Map<String, UType> jsonTypes;
2821

2922
ControllerMethodWriter(
3023
MethodReader method,
3124
Append writer,
3225
ProcessingContext ctx,
3326
boolean useJsonB,
34-
Map<String, SimpleImmutableEntry<String, String>> jsonTypes) {
27+
Map<String, UType> jsonTypes) {
3528
this.method = method;
3629
this.writer = writer;
3730
webMethod = method.getWebMethod();
@@ -51,19 +44,16 @@ void writeHandler(boolean requestScoped) {
5144
final var bodyType = method.getBodyType();
5245
if (bodyType != null) {
5346
if (useJsonB) {
54-
5547
final var fieldName =
5648
method.getParams().stream()
5749
.filter(MethodParam::isBody)
5850
.findFirst()
5951
.orElseThrow()
6052
.getUType()
61-
.full()
62-
.transform(jsonTypes::get)
63-
.getValue();
53+
.shortName();
6454
writer
6555
.append(
66-
" var %s = %sJsonType.fromJson(req.content().inputStream());",
56+
" var %s = %sJsonType.fromJson(req.content().inputStream()); // RB1",
6757
method.getBodyName(), fieldName)
6858
.eol();
6959

@@ -128,10 +118,9 @@ void writeHandler(boolean requestScoped) {
128118
if (!method.isVoid()) {
129119
writeContextReturn();
130120
if (producesJson()) {
131-
132-
final var fieldName =
133-
method.getReturnType().toString().transform(jsonTypes::get).getValue();
134-
writer.append(" %sJsonType.toJson(result, res.outputStream());", fieldName).eol();
121+
UType uType = Util.parseType(method.getReturnType());
122+
final var fieldName = uType.shortName();
123+
writer.append(" %sJsonType.toJson(result, res.outputStream()); // RB0", fieldName).eol();
135124
} else {
136125
writer.append(" res.send(result);").eol();
137126
}

0 commit comments

Comments
 (0)