From d5804c77183e73b745b784255f2e39a99c6c4773 Mon Sep 17 00:00:00 2001 From: jlangch Date: Tue, 17 Oct 2023 11:21:34 +0200 Subject: [PATCH] refactored chestsheet examples --- .../impl/functions/IOFunctionsSpitSlurp.java | 10 ++++---- .../impl/functions/IOFunctionsStreams.java | 24 ++++++++++++------- .../impl/functions/IOFunctionsStreamTest.java | 10 ++++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurp.java b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurp.java index 7e18ea8d3..740202a04 100644 --- a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurp.java +++ b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurp.java @@ -599,7 +599,7 @@ public VncVal apply(final VncList args) { " (let [file (io/temp-file \"test-\", \".txt\")] \n" + " (io/delete-file-on-exit file) \n" + " (io/spit file \"123456789\" :append true) \n" + - " (try-with [rd (io/buffered-reader (io/file-in-stream file) :utf-8)] \n" + + " (try-with [rd (io/buffered-reader file :encoding :utf-8)] \n" + " (io/slurp-reader rd))) \n" + ")") .seeAlso( @@ -738,10 +738,10 @@ else if (Types.isVncByteBuffer(content)) { "`io/spit-writer` for stream output available!") .examples( "(do \n" + - " (let [file (io/temp-file \"test-\", \".txt\") \n" + - " os (io/file-out-stream file)] \n" + - " (io/delete-file-on-exit file) \n" + - " (try-with [wr (io/buffered-writer os :utf-8)] \n" + + " (let [file (io/temp-file \"test-\", \".txt\") \n" + + " os (io/file-out-stream file)] \n" + + " (io/delete-file-on-exit file) \n" + + " (try-with [wr (io/buffered-writer os :encoding :utf-8)] \n" + " (io/spit-writer wr \"123456789\" :flush true)))) ") .seeAlso("io/spit-stream", "io/spit") .build() diff --git a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java index 54ad189f4..529b7e09f 100644 --- a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java +++ b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java @@ -716,8 +716,12 @@ public VncVal apply(final VncList args) { "(io/buffered-writer f & options)" ) .doc( "Creates a `java.io.Writer` for f.\n\n" + - "f may be a file or a string (file path). " + - "Options: \n\n" + + "f may be a: \n\n" + + " * `java.io.File`, e.g: `(io/file \"/temp/foo.json\")` \n" + + " * `java.nio.file.Path` \n" + + " * `java.io.OutputStream` \n" + + " * `java.io.Writer` \n" + + "Options: \n\n" + "| :append true/false | e.g.: `:append true`, defaults to false |\n" + "| :encoding enc | e.g.: `:encoding :utf-8`, defaults to :utf-8 |\n\n" + "`io/buffered-writer` supports load paths. See the `loadpath/paths` " + @@ -850,12 +854,12 @@ public VncVal apply(final VncList args) { .doc( "Create a `java.io.Reader` from f. \n\n" + "f may be a: \n\n" + - " * string file path, e.g: \"/temp/foo.json\" \n" + + " * string \n" + " * bytebuffer \n" + " * `java.io.File`, e.g: `(io/file \"/temp/foo.json\")` \n" + + " * `java.nio.file.Path` \n" + " * `java.io.InputStream` \n" + " * `java.io.Reader` \n" + - " * `java.nio.file.Path` \n" + " * `java.net.URL` \n" + " * `java.net.URI` \n\n" + "Options: \n\n" + @@ -890,6 +894,13 @@ public VncVal apply(final VncList args) { final ILoadPaths loadpaths = ThreadContext.getInterceptor().getLoadPaths(); + if (Types.isVncString(arg)) { + return new VncJavaObject( + new BufferedReader( + new StringReader( + Coerce.toVncString(arg).getValue()))); + } + final File file = convertToFile(arg); if (file != null) { try { @@ -915,11 +926,6 @@ public VncVal apply(final VncList args) { throw new VncException("Failed to create reader from the file " + file.getPath(), ex); } } - else if (Types.isVncString(arg)) { - return new VncJavaObject( - new StringReader( - Coerce.toVncString(arg).getValue())); - } else if (Types.isVncByteBuffer(arg)) { try { final VncByteBuffer buf = (VncByteBuffer)arg; diff --git a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java index cfb1414f2..5f8e5f267 100644 --- a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java +++ b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java @@ -34,7 +34,7 @@ public class IOFunctionsStreamTest { public void test_io_buffered_reader() { final Venice venice = new Venice(); - final String script = + final String script1 = "(do \n" + " (import :java.io.FileInputStream) \n" + " (let [file (io/temp-file \"test-\", \".txt\")] \n" + @@ -43,7 +43,13 @@ public void test_io_buffered_reader() { " (try-with [rd (io/buffered-reader file :encoding :utf-8)] \n" + " (pr-str [(read-line rd) (read-line rd)])))) "; - assertEquals("[\"100\" \"200\"]",venice.eval(script)); + final String script2 = + "(try-with [rd (io/buffered-reader \"100\n200\")] \n" + + " (pr-str [(read-line rd) (read-line rd)])) "; + + assertEquals("[\"100\" \"200\"]",venice.eval(script1)); + + assertEquals("[\"100\" \"200\"]",venice.eval(script2)); } @Test