Skip to content

Commit 603418c

Browse files
committed
make renderer a function (yay!)
1 parent f01c0a5 commit 603418c

File tree

7 files changed

+25
-77
lines changed

7 files changed

+25
-77
lines changed

jooby/src/main/java/io/jooby/Context.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ default boolean accept(MediaType contentType) {
337337

338338
default @Nonnull Context render(@Nonnull Object result) {
339339
try {
340-
route().renderer().render(this, result);
340+
byte[] encode = route().renderer().encode(this, result);
341+
sendBytes(encode);
341342
return this;
342343
} catch (Exception x) {
343344
throw Throwing.sneakyThrow(x);

jooby/src/main/java/io/jooby/Renderer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616
package io.jooby;
1717

1818
import javax.annotation.Nonnull;
19+
import java.nio.charset.StandardCharsets;
1920

2021
public interface Renderer {
2122

22-
Renderer TO_STRING = (ctx, value) -> {
23-
ctx.sendString(value.toString());
24-
return true;
25-
};
23+
Renderer TO_STRING = (ctx, value) -> value.toString().getBytes(StandardCharsets.UTF_8);
2624

27-
boolean render(@Nonnull Context ctx, @Nonnull Object result) throws Exception;
25+
byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception;
2826

2927
@Nonnull default Renderer accept(@Nonnull MediaType contentType) {
30-
return (ctx, result) -> {
28+
return (ctx, value) -> {
3129
if (ctx.accept(contentType)) {
32-
return render(ctx, result);
30+
return encode(ctx, value);
3331
}
34-
return false;
32+
return null;
3533
};
3634
}
3735

jooby/src/main/java/io/jooby/TemplateEngine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package io.jooby;
1717

1818
import javax.annotation.Nonnull;
19+
import java.nio.charset.StandardCharsets;
1920

2021
public interface TemplateEngine extends Renderer {
2122

2223
String apply(Context ctx, ModelAndView modelAndView) throws Exception;
2324

24-
@Override default boolean render(@Nonnull Context ctx, @Nonnull Object result) throws Exception {
25-
String output = apply(ctx, (ModelAndView) result);
26-
ctx.sendString(output);
27-
return true;
25+
@Override default byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
26+
String output = apply(ctx, (ModelAndView) value);
27+
return output.getBytes(StandardCharsets.UTF_8);
2828
}
2929
}

jooby/src/main/java/io/jooby/internal/CompositeRenderer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ private Renderer computeRenderer(Renderer it, Renderer next) {
5757
}
5858
}
5959

60-
@Override public boolean render(@Nonnull Context ctx, @Nonnull Object result) throws Exception {
61-
if (result instanceof ModelAndView) {
62-
return templateEngine.render(ctx, result);
60+
@Override public byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
61+
if (value instanceof ModelAndView) {
62+
return templateEngine.encode(ctx, value);
6363
} else {
6464
Iterator<Renderer> iterator = list.iterator();
65-
boolean done = false;
6665
/** NOTE: looks like an infinite loop but there is a default renderer at the end of iterator. */
67-
while (!done) {
66+
byte[] result = null;
67+
while (result == null) {
6868
Renderer next = iterator.next();
69-
done = next.render(ctx, result);
69+
result = next.encode(ctx, value);
7070
}
71-
return done;
71+
return result;
7272
}
7373
}
7474
}

jooby/src/test/java/io/jooby/RendererTest.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

modules/jooby-jackson/src/main/java/io/jooby/json/Jackson.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,15 @@ public static final ObjectMapper defaultObjectMapper() {
6262
application.renderer(MediaType.json, this);
6363
}
6464

65-
@Override public boolean render(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
65+
@Override public byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
66+
ctx.type(MediaType.json);
6667
if (value instanceof CharSequence) {
6768
// Ignore string/charsequence responses, those are going to be processed by the default
6869
// renderer and let route to return raw JSON
69-
ctx.type(MediaType.json)
70-
.sendBytes(value.toString().getBytes(StandardCharsets.UTF_8));
70+
return value.toString().getBytes(StandardCharsets.UTF_8);
7171
} else {
72-
ctx.type(MediaType.json)
73-
.sendBytes(mapper.writeValueAsBytes(value));
72+
return mapper.writeValueAsBytes(value);
7473
}
75-
return true;
7674
}
7775

7876
@Override public <T> T parse(Context ctx, Type type) throws Exception {

modules/jooby-jackson/src/test/java/io/jooby/json/JacksonTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ public class JacksonTest {
1515
public void render() throws Exception {
1616
Jackson jackson = new Jackson();
1717
MockContext ctx = new MockContext();
18-
boolean renderered = jackson.render(ctx, Map.of("k", "v"));
19-
assertTrue(renderered);
20-
21-
/** Body: */
22-
byte[] bytes = (byte[]) ctx.getResult();
18+
byte [] bytes = jackson.encode(ctx, Map.of("k", "v"));
2319
assertEquals("{\"k\":\"v\"}", new String(bytes, StandardCharsets.UTF_8));
2420
/** Type: */
2521
assertEquals("application/json", ctx.getResponseContentType().value());
@@ -30,11 +26,7 @@ public void render() throws Exception {
3026
public void renderRawString() throws Exception {
3127
Jackson jackson = new Jackson();
3228
MockContext ctx = new MockContext();
33-
boolean renderered = jackson.render(ctx, "{\"k\":\"v\"}");
34-
assertTrue(renderered);
35-
36-
/** Body: */
37-
byte[] bytes = (byte[]) ctx.getResult();
29+
byte [] bytes = jackson.encode(ctx, "{\"k\":\"v\"}");
3830
assertEquals("{\"k\":\"v\"}", new String(bytes, StandardCharsets.UTF_8));
3931
/** Type: */
4032
assertEquals("application/json", ctx.getResponseContentType().value());

0 commit comments

Comments
 (0)