Created
June 20, 2020 13:41
-
-
Save Palmr/64523e4446710d2dcc61dc79231fe0c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: src/main/java/org/example/questionn/csv/CsvRenderer.java | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/main/java/org/example/questionn/csv/CsvRenderer.java (revision 13e0a8fb07a40e89c39ea8f8176f4c2e6b590774) | |
+++ src/main/java/org/example/questionn/csv/CsvRenderer.java (date 1592660439551) | |
@@ -3,11 +3,13 @@ | |
import ratpack.handling.Context; | |
import ratpack.render.RendererSupport; | |
+import static ratpack.http.ResponseChunks.stringChunks; | |
+ | |
public class CsvRenderer extends RendererSupport<Csv> | |
{ | |
@Override | |
public void render(final Context ctx, final Csv csv) | |
{ | |
- ctx.getResponse().send("text/csv", csv.toCsv()); | |
+ ctx.render(stringChunks("text/csv", csv.toStreamingCsv())); | |
} | |
} | |
Index: src/main/java/org/example/questionn/csv/Csv.java | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- src/main/java/org/example/questionn/csv/Csv.java (revision 13e0a8fb07a40e89c39ea8f8176f4c2e6b590774) | |
+++ src/main/java/org/example/questionn/csv/Csv.java (date 1592660311228) | |
@@ -1,8 +1,14 @@ | |
package org.example.questionn.csv; | |
+import org.reactivestreams.Publisher; | |
+ | |
+ | |
+import ratpack.stream.Streams; | |
+ | |
public interface Csv | |
{ | |
String[] fieldNames(); | |
+ | |
Iterable<String[]> records(); | |
default String toCsv() | |
@@ -20,4 +26,10 @@ | |
return stringBuilder.toString(); | |
} | |
+ | |
+ default Publisher<String> toStreamingCsv() | |
+ { | |
+ return Streams.publish(records()) | |
+ .map(r -> String.join(",", r).concat("\n")); | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment