Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Oct 17, 2023
1 parent 33a39dc commit d059af3
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ public DocSection section() {
all.addSection(rd_wr);
rd_wr.addItem(diBuilder.getDocItem("io/buffered-reader"));
rd_wr.addItem(diBuilder.getDocItem("io/buffered-writer"));
rd_wr.addItem(diBuilder.getDocItem("io/reader"));
rd_wr.addItem(diBuilder.getDocItem("io/writer"));
rd_wr.addItem(diBuilder.getDocItem("io/string-reader"));
rd_wr.addItem(diBuilder.getDocItem("io/string-writer"));
rd_wr.addItem(diBuilder.getDocItem("io/flush"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,148 +706,29 @@ public VncVal apply(final VncList args) {
private static final long serialVersionUID = -1848883965231344442L;
};

public static VncFunction io_buffered_reader =
new VncFunction(
"io/buffered-reader",
VncFunction
.meta()
.arglists(
"(io/buffered-reader is encoding?)",
"(io/buffered-reader rdr)")
.doc(
"Creates a `java.io.BufferedReader` from a `java.io.InputStream` is with optional " +
"encoding (defaults to :utf-8), from a `java.io.Reader` or from a string.\n\n" +
"Note: The caller is responsible for closing the reader!")
.examples(
"(let [data (bytebuf [108 105 110 101 32 49 10 108 105 110 101 32 50]) \n" +
" is (io/bytebuf-in-stream data)] \n" +
" (try-with [rd (io/buffered-reader is :utf-8)] \n" +
" (println (read-line rd)) \n" +
" (println (read-line rd)))) ",
"(try-with [rd (io/buffered-reader \"1\\n2\\n3\\n4\")] \n" +
" (println (read-line rd)) \n" +
" (println (read-line rd))) ")
.seeAlso(
"io/reader", "io/string-reader", "io/buffered-writer")
.build()
) {
@Override
public VncVal apply(final VncList args) {
ArityExceptions.assertArity(this, args, 1, 2);

if (Types.isVncString(args.first())) {
return new VncJavaObject(
new BufferedReader(
new StringReader(((VncString)args.first()).getValue())));
}
else if (Types.isVncJavaObject(args.first())) {
final Object delegate = ((VncJavaObject)args.first()).getDelegate();
if (delegate instanceof InputStream) {
try {
final InputStream is = (InputStream)delegate;
final Charset charset = CharsetUtil.charset(args.second());

return new VncJavaObject(
new BufferedReader(
new InputStreamReader(is, charset)));
}
catch (Exception ex) {
throw new VncException(ex.getMessage(), ex);
}
}
else if (delegate instanceof BufferedReader) {
return args.first();
}
else if (delegate instanceof Reader) {
return new VncJavaObject(new BufferedReader((Reader)delegate));
}
}

throw new VncException(String.format(
"Function 'io/buffered-reader' requires a :java.io.InputStream, " +
"a :java.io.Reader, or a string. %s as is not allowed!",
Types.getType(args.first())));
}

private static final long serialVersionUID = -1848883965231344442L;
};

public static VncFunction io_buffered_writer =
new VncFunction(
"io/buffered-writer",
VncFunction
.meta()
.arglists(
"(io/buffered-writer os encoding?)",
"(io/buffered-writer wr)")
.doc(
"Creates a `java.io.BufferedWriter` from a `java.io.OutputStream` os with optional " +
"encoding (defaults to :utf-8) or from a `java.io.Writer`.\n\n" +
"Note: The caller is responsible for closing the writer!")
.examples()
.seeAlso(
"io/writer", "io/string-writer", "io/buffered-reader")
.build()
) {
@Override
public VncVal apply(final VncList args) {
ArityExceptions.assertArity(this, args, 1, 2);

if (Types.isVncJavaObject(args.first())) {
final Object delegate = ((VncJavaObject)args.first()).getDelegate();
if (delegate instanceof OutputStream) {
try {
final OutputStream os = (OutputStream)delegate;
final Charset charset = CharsetUtil.charset(args.second());

return new VncJavaObject(
new BufferedWriter(
new OutputStreamWriter(os, charset)));
}
catch (Exception ex) {
throw new VncException(ex.getMessage(), ex);
}
}
else if (delegate instanceof BufferedWriter) {
return args.first();
}
else if (delegate instanceof Writer) {
return new VncJavaObject(new BufferedWriter((Writer)delegate));
}
}

throw new VncException(String.format(
"Function 'io/buffered-writer' requires a :java.io.OutputStream " +
"or a :java.io.Writer. %s as is not allowed!",
Types.getType(args.first())));
}

private static final long serialVersionUID = -1848883965231344442L;
};


public static VncFunction io_writer =
new VncFunction(
"io/writer",
VncFunction
.meta()
.arglists(
"(io/writer f & options)" )
"(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" +
"| :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/writer` supports load paths. See the `loadpath/paths` " +
"`io/buffered-writer` supports load paths. See the `loadpath/paths` " +
"doc for a description of the *load path* feature.")
.seeAlso(
"str", "io/string-writer", "io/buffered-writer", "io/reader")
"println", "io/string-writer", "io/buffered-reader")
.build()
) {
@Override
public VncVal apply(final VncList args) {
ArityExceptions.assertMinArity(this, args, 2);
ArityExceptions.assertMinArity(this, args, 1);

sandboxFunctionCallValidation();

Expand Down Expand Up @@ -909,12 +790,15 @@ else if (Types.isVncJavaObject(args.first(), OutputStream.class)) {
ex);
}
}
else if (Types.isVncJavaObject(args.first(), BufferedWriter.class)) {
return args.first();
}
else if (Types.isVncJavaObject(args.first(), Writer.class)) {
return new VncJavaObject(args.first());
}
else {
throw new VncException(String.format(
"Function 'io/writer' does not allow %s as f",
"Function 'io/buffered-writer' does not allow %s as f",
Types.getType(args.first())));
}
}
Expand Down Expand Up @@ -942,7 +826,7 @@ else if (Types.isVncJavaObject(args.first(), Writer.class)) {
" (flush sw) \n" +
" (println @sw)) ")
.seeAlso(
"str", "io/writer", "io/buffered-writer", "io/string-reader")
"println", "io/buffered-writer", "io/buffered-reader")
.build()
) {
@Override
Expand All @@ -955,13 +839,13 @@ public VncVal apply(final VncList args) {
private static final long serialVersionUID = -1848883965231344442L;
};

public static VncFunction io_reader =
public static VncFunction io_buffered_reader =
new VncFunction(
"io/reader",
"io/buffered-reader",
VncFunction
.meta()
.arglists(
"(io/reader f & options)" )
"(io/buffered-reader f & options)" )
.doc(
"Create a `java.io.Reader` from f. \n\n" +
"f may be a: \n\n" +
Expand All @@ -975,11 +859,19 @@ public VncVal apply(final VncList args) {
" * `java.net.URI` \n\n" +
"Options: \n\n" +
"| :encoding enc | e.g.: `:encoding :utf-8`, defaults to :utf-8 | \n\n" +
"`io/reader` supports load paths. See the `loadpath/paths` " +
"`io/buffered-reader` supports load paths. See the `loadpath/paths`" +
"doc for a description of the *load path* feature. \n\n" +
"Note: The caller is responsible for closing the reader!")
.seeAlso(
"io/string-reader", "io/buffered-reader", "io/writer")
.examples(
"(let [data (bytebuf [108 105 110 101 32 49 10 108 105 110 101 32 50])] \n" +
" (try-with [rd (io/buffered-reader data :encoding :utf-8)] \n" +
" (println (read-line rd)) \n" +
" (println (read-line rd)))) ",
"(try-with [rd (io/buffered-reader \"1\\n2\\n3\\n4\")] \n" +
" (println (read-line rd)) \n" +
" (println (read-line rd))) ")
.seeAlso(
"read-line", "io/string-reader", "io/buffered-writer")
.build()
) {
@Override
Expand Down Expand Up @@ -1070,7 +962,7 @@ else if (Types.isVncJavaObject(arg, Reader.class)) {
}
else {
throw new VncException(String.format(
"Function 'io/reader' does not allow %s as f",
"Function 'io/buffered-reader' does not allow %s as f",
Types.getType(args.first())));
}
}
Expand Down Expand Up @@ -1099,7 +991,7 @@ else if (Types.isVncJavaObject(arg, Reader.class)) {
" (println (read-line br)) \n" +
" (println (read-line br)))) ")
.seeAlso(
"io/reader", "io/buffered-reader", "io/string-writer")
"read-line", "io/buffered-reader", "io/string-writer")
.build()
) {
@Override
Expand Down Expand Up @@ -1333,10 +1225,8 @@ public static void validateReadableFile(final File file) {
.add(io_wrap_os_with_print_writer)
.add(io_wrap_is_with_buffered_reader)
.add(io_buffered_writer)
.add(io_writer)
.add(io_string_writer)
.add(io_buffered_reader)
.add(io_reader)
.add(io_string_reader)
.add(io_capturing_print_stream)
.add(io_print)
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/com/github/jlangch/venice/jsonl.venice
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@

slurp [in & options]

(try-with [rd (apply io/buffered-reader in options))]
(loop [line (. rd :readLine) data '()]
(try-with [rd (apply io/buffered-reader in options)]
(loop [line (read-line rd) data '()]
(if (some? line)
(let [l (str/trim-to-nil line)
v (if (nil? l) nil (apply json/read-str l options))]
(recur (. rd :readLine) (if (nil? v) data (conj data v))))
data)))
(recur (read-line rd) (if (nil? v) data (conj data v))))
data))))
Loading

0 comments on commit d059af3

Please sign in to comment.