Skip to content

Commit db731c5

Browse files
committed
Helidon generator update
1 parent b3b9bb9 commit db731c5

File tree

9 files changed

+104
-45
lines changed

9 files changed

+104
-45
lines changed

examples/example-helidon/src/main/java/org/example/FooController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@Path("/foo")
1010
public class FooController {
1111

12-
@Produces("text/plain")
12+
//@Produces("text/plain")
1313
@Get
1414
public String hello() {
1515
return "Hello from Foo";

generator-core/src/main/java/io/dinject/webroutegen/ElementReader.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ void writeCtxGet(Append writer, PathSegments segments) {
174174
// no conversion for this parameter
175175
return;
176176
}
177-
178177
String shortType = shortType();
179178
writer.append(" %s %s = ", shortType, varName);
180179
if (setValue(writer, segments, shortType)) {
@@ -187,14 +186,12 @@ void setValue(Append writer) {
187186
}
188187

189188
private boolean setValue(Append writer, PathSegments segments, String shortType) {
190-
191189
if (formMarker && impliedParamType && typeHandler == null) {
192190
// @Form on method and this type is a "bean" so treat is as a form bean
193191
writeForm(writer, shortType, varName, ParamType.FORMPARAM);
194192
paramType = ParamType.FORM;
195193
return false;
196194
}
197-
198195
if (ParamType.FORM == paramType) {
199196
writeForm(writer, shortType, varName, ParamType.FORMPARAM);
200197
return false;
@@ -203,18 +200,16 @@ private boolean setValue(Append writer, PathSegments segments, String shortType)
203200
writeForm(writer, shortType, varName, ParamType.QUERYPARAM);
204201
return false;
205202
}
206-
207203
if (impliedParamType) {
208204
PathSegments.Segment segment = segments.segment(varName);
209205
if (segment != null) {
210206
// path or metric parameter
211207
boolean requiredParam = segment.isRequired(varName);
212208
String asMethod = (typeHandler == null) ? null : (requiredParam) ? typeHandler.asMethod() : typeHandler.toMethod();
213-
214209
if (asMethod != null) {
215210
writer.append(asMethod);
216211
}
217-
segment.writeGetVal(writer, varName);
212+
segment.writeGetVal(writer, varName, ctx.platform());
218213
if (asMethod != null) {
219214
writer.append(")");
220215
}
@@ -224,7 +219,6 @@ private boolean setValue(Append writer, PathSegments segments, String shortType)
224219
}
225220

226221
String asMethod = (typeHandler == null) ? null : typeHandler.toMethod();
227-
228222
if (asMethod != null) {
229223
writer.append(asMethod);
230224
}
@@ -236,7 +230,7 @@ private boolean setValue(Append writer, PathSegments segments, String shortType)
236230

237231
} else {
238232
if (hasParamDefault()) {
239-
writer.append("ctx.%s(\"%s\",\"%s\")", paramType, paramName, paramDefault);
233+
ctx.platform().writeReadParameter(writer, paramType, paramName, paramDefault);
240234
} else {
241235
boolean checkNull = notNullKotlin || (paramType == ParamType.FORMPARAM && typeHandler.isPrimitive());
242236
if (checkNull) {

generator-core/src/main/java/io/dinject/webroutegen/PathSegments.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ static PathSegments parse(String fullPath) {
3030
segments.add(segment);
3131
path.append(segment.path(section));
3232

33+
} else if ((section.startsWith("{") && (section.endsWith("}")))) {
34+
Segment segment = createSegment(section.substring(1, section.length() - 1));
35+
segments.add(segment);
36+
path.append(segment.path(section));
37+
3338
} else {
3439
path.append(section);
3540
}
@@ -149,11 +154,11 @@ boolean isPathParameter(String varName) {
149154
/**
150155
* Reading the value from a segment (rather than directly from pathParam).
151156
*/
152-
void writeGetVal(Append writer, String varName) {
157+
void writeGetVal(Append writer, String varName, PlatformAdapter platform) {
153158
if (!hasMetrics()) {
154-
writer.append("ctx.pathParam(\"%s\")", name);
155-
159+
platform.writeReadParameter(writer, ParamType.PATHPARAM, name);
156160
} else {
161+
// TODO: platform read segment handling ...
157162
writer.append("%s_segment.", name);
158163
if (name.equals(varName)) {
159164
writer.append("val()");
@@ -174,6 +179,7 @@ private String metricKey(String varName) {
174179
}
175180

176181
void writeCreateSegment(Append writer) {
182+
// TODO: platform read segment handling ...
177183
writer.append(" PathSegment %s_segment = PathSegment.of(ctx.pathParam(\"%s_segment\"));", name, name).eol();
178184
}
179185

@@ -185,6 +191,7 @@ String path(String section) {
185191
if (!hasMetrics()) {
186192
return section;
187193
}
194+
// TODO: platform read segment handling ...=
188195
return ":" + name + "_segment";
189196
}
190197
}

generator-core/src/main/java/io/dinject/webroutegen/PlatformAdapter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public interface PlatformAdapter {
2323
void methodRoles(List<String> roles, ControllerReader controller);
2424

2525
void writeReadParameter(Append writer, ParamType paramType, String paramName);
26+
27+
void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault);
2628
}

generator-core/src/test/java/io/dinject/webroutegen/PathSegmentsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@
1212

1313
public class PathSegmentsTest {
1414

15+
@Test
16+
public void parse_standard() {
17+
18+
PathSegments segments = PathSegments.parse("/before/{one}/{two}/after");
19+
20+
assertTrue(segments.contains("one"));
21+
assertTrue(segments.contains("two"));
22+
assertFalse(segments.contains("before"));
23+
assertFalse(segments.contains("after"));
24+
25+
segments = PathSegments.parse("/{one}/{two}");
26+
27+
assertTrue(segments.contains("one"));
28+
assertTrue(segments.contains("two"));
29+
}
30+
1531
@Test
1632
public void parse_basic() {
1733

generator-helidon/src/main/java/io/dinject/webroutegen/ControllerMethodWriter.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ void writeRule() {
2828
void writeHandler() {
2929
writer.append(" private void _%s(ServerRequest req, ServerResponse res", method.simpleName());
3030
// TODO: if POST body get that here
31-
writer.append(") {").eol();
31+
writer.append(") { //5").eol();
3232

33+
if (!method.isVoid()) {
34+
writeContextReturn();
35+
}
3336
final PathSegments segments = method.getPathSegments();
3437
// writer.append(" ctx.status(%s);", method.getStatusCode()).eol();
3538

@@ -42,9 +45,9 @@ void writeHandler() {
4245
for (MethodParam param : params) {
4346
param.writeCtxGet(writer, segments);
4447
}
45-
writer.append(" ");
48+
writer.append(" ");
4649
if (!method.isVoid()) {
47-
writeContextReturn();
50+
writer.append("res.send(");
4851
}
4952

5053
if (method.includeValidate()) {
@@ -66,36 +69,37 @@ void writeHandler() {
6669
writer.append(")");
6770
}
6871
writer.append(";").eol();
69-
writer.append(" }");
70-
71-
List<String> roles = method.roles();
72-
if (!roles.isEmpty()) {
73-
writer.append(", roles(");
74-
for (int i = 0; i < roles.size(); i++) {
75-
if (i > 0) {
76-
writer.append(", ");
77-
}
78-
writer.append(Util.shortName(roles.get(i)));
79-
}
80-
writer.append(")");
81-
}
82-
writer.append(");").eol().eol();
83-
84-
writer.append(" res.send(\"Hello\");").eol();
8572
writer.append(" }").eol();
8673

74+
// List<String> roles = method.roles();
75+
// if (!roles.isEmpty()) {
76+
// writer.append(", roles(");
77+
// for (int i = 0; i < roles.size(); i++) {
78+
// if (i > 0) {
79+
// writer.append(", ");
80+
// }
81+
// writer.append(Util.shortName(roles.get(i)));
82+
// }
83+
// writer.append(")");
84+
// }
85+
// writer.append(");").eol().eol();
86+
87+
// writer.append(" res.send(\"Hello\");").eol();
88+
// writer.append(" }").eol();
8789
}
8890

8991
private void writeContextReturn() {
9092
final String produces = method.getProduces();
91-
if (produces == null || produces.equalsIgnoreCase(MediaType.APPLICATION_JSON)) {
92-
writer.append("ctx.json(");
93-
} else if (produces.equalsIgnoreCase(MediaType.TEXT_HTML)) {
94-
writer.append("ctx.html(");
95-
} else if (produces.equalsIgnoreCase(MediaType.TEXT_PLAIN)) {
96-
writer.append("ctx.contentType(\"text/plain\").result(");
93+
if (produces == null) {
94+
//writer.append("res.writerContext().contentType(MediaType.APPLICATION_JSON);").eol();
95+
} else if (MediaType.APPLICATION_JSON.equalsIgnoreCase(produces)) {
96+
writer.append("res.writerContext().contentType(io.helidon.common.http.MediaType.APPLICATION_JSON);").eol();
97+
} else if (MediaType.TEXT_HTML.equalsIgnoreCase(produces)) {
98+
writer.append("res.writerContext().contentType(io.helidon.common.http.MediaType.TEXT_HTML);").eol();
99+
} else if (MediaType.TEXT_PLAIN.equalsIgnoreCase(produces)) {
100+
writer.append("res.writerContext().contentType(io.helidon.common.http.MediaType.TEXT_PLAIN);").eol();
97101
} else {
98-
writer.append("ctx.contentType(\"%s\").result(", produces);
102+
writer.append("res.writerContext().contentType(io.helidon.common.http.MediaType.parse(\"%s\"));", produces).eol();
99103
}
100104
}
101105

generator-helidon/src/main/java/io/dinject/webroutegen/HelidonPlatformAdapter.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,45 @@ public void writeReadParameter(Append writer, ParamType paramType, String paramN
3535
case PATHPARAM:
3636
writer.append("req.path().param(\"%s\")", paramName);
3737
break;
38+
case QUERYPARAM:
39+
case FORMPARAM:
40+
writer.append("req.queryParams().first(\"%s\").orElse(null)", paramName);
41+
break;
3842
case HEADER:
39-
writer.append("req.headers().value(\"%s\")", paramName);
43+
writer.append("req.headers().value(\"%s\").orElse(null)", paramName);
4044
break;
41-
45+
case COOKIE:
46+
writer.append("req.headers().cookies().first(\"%s\").orElse(null)", paramName);
47+
break;
48+
case BODY:
49+
case BEANPARAM:
50+
case FORM:
4251
default:
43-
writer.append("req.%s().param(\"%s\")", paramType.getType(), paramName);
52+
writer.append("null // TODO req.%s().param(\"%s\")", paramType.getType(), paramName);
53+
}
54+
}
4455

56+
@Override
57+
public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) {
58+
switch (paramType) {
59+
case PATHPARAM:
60+
writer.append("req.path().param(\"%s\")", paramName);
61+
break;
62+
case QUERYPARAM:
63+
case FORMPARAM:
64+
writer.append("req.queryParams().first(\"%s\").orElse(\"%s\")", paramName, paramDefault);
65+
break;
66+
case HEADER:
67+
writer.append("req.headers().value(\"%s\").orElse(\"%s\")", paramName, paramDefault);
68+
break;
69+
case COOKIE:
70+
writer.append("req.headers().cookies().first(\"%s\").orElse(\"%s\")", paramName, paramDefault);
71+
break;
72+
case BODY:
73+
case BEANPARAM:
74+
case FORM:
75+
default:
76+
writer.append("null // TODO req.%s().param(\"%s\")", paramType.getType(), paramName);
4577
}
4678
}
4779
}

generator-javalin/src/main/java/io/dinject/webroutegen/JavalinAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ private void addRoleImports(List<String> roles, ControllerReader controller) {
3333
public void writeReadParameter(Append writer, ParamType paramType, String paramName) {
3434
writer.append("ctx.%s(\"%s\")", paramType, paramName);
3535
}
36+
37+
@Override
38+
public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) {
39+
writer.append("ctx.%s(\"%s\",\"%s\")", paramType, paramName, paramDefault);
40+
}
3641
}

pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
<packaging>pom</packaging>
1111

1212
<modules>
13-
<module>common</module>
14-
<module>javalin</module>
15-
<module>helidon</module>
16-
<module>example-helidon</module>
13+
<module>generator-core</module>
14+
<module>generator-javalin</module>
15+
<module>generator-helidon</module>
1716
</modules>
1817
</project>
1918

0 commit comments

Comments
 (0)