diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso index 111e71c9e333..2eed0f9a9e2f 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso @@ -8,6 +8,7 @@ import Standard.Table.Io.Spreadsheet_Write_Mode import Standard.Table.Io.File_Format import Standard.Base.System.File import Standard.Base.System.File.Existing_File_Behavior +import Standard.Base.Error.Common as Errors import Standard.Table.Internal.Table_Helpers import Standard.Table.Internal.Aggregate_Column_Helper import Standard.Table.Internal.Parse_Values_Helper @@ -34,8 +35,6 @@ polyglot java import org.enso.table.data.table.Table as Java_Table polyglot java import org.enso.table.data.table.Column as Java_Column polyglot java import org.enso.table.format.xlsx.Writer as Spreadsheet_Writer polyglot java import org.enso.table.operations.OrderBuilder -polyglot java import java.io.StringReader -polyglot java import java.io.StringWriter ## Creates a new table from a vector of `[name, items]` pairs. @@ -1284,10 +1283,9 @@ print_table header rows indices_count format_term = ([" " + header_line, divider] + row_lines).join '\n' Table.from (that : Text) (format:File_Format.Delimited|File_Format.Fixed_Width = File_Format.Delimited '\t') (on_problems:Problem_Behavior=Report_Warning) = - java_reader = StringReader.new that - Delimited_Reader.read_from_reader format java_reader on_problems + if format.is_a File_Format.Delimited then Delimited_Reader.read_text that format on_problems else + Errors.unimplemented "Table.from for fixed-width files is not yet implemented." Text.from (that : Table) (format:File_Format.Delimited|File_Format.Fixed_Width = File_Format.Delimited '\t') = - java_writer = StringWriter.new - Delimited_Writer.write_to_writer that format java_writer - java_writer.toString + if format.is_a File_Format.Delimited then Delimited_Writer.write_text that format else + Errors.unimplemented "Text.from for fixed-width files is not yet implemented." diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Reader.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Reader.enso index bc76443258b5..c0fc1ba0197c 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Reader.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Reader.enso @@ -22,6 +22,7 @@ polyglot java import com.univocity.parsers.common.TextParsingException polyglot java import org.enso.base.Encoding_Utils polyglot java import java.io.InputStream polyglot java import java.io.Reader +polyglot java import java.io.StringReader polyglot java import org.enso.table.parsing.IdentityParser polyglot java import org.enso.table.parsing.TypeInferringParser @@ -46,6 +47,11 @@ read_file format file on_problems = stream.with_java_stream java_stream-> here.read_stream format java_stream on_problems related_file=file +read_text : Text -> Delimited -> Problem_Behavior -> Table +read_text text format on_problems = + java_reader = StringReader.new text + Delimited_Reader.read_from_reader format java_reader on_problems + ## PRIVATE Reads an input stream according to the provided format. diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Writer.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Writer.enso index fa4106b6303a..244df4a1bb46 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Writer.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Internal/Delimited_Writer.enso @@ -16,6 +16,7 @@ polyglot java import org.enso.table.write.WriteQuoteBehavior polyglot java import java.io.PrintWriter polyglot java import java.io.IOException polyglot java import org.enso.table.formatting.TextFormatter +polyglot java import java.io.StringWriter ## Writes a delimited file according to the provided format. @@ -28,16 +29,24 @@ polyglot java import org.enso.table.formatting.TextFormatter operation. By default, a warning is issued, but the operation proceeds. If set to `Report_Error`, the operation fails with a dataflow error. If set to `Ignore`, the operation proceeds without errors or warnings. -write_file : Table -> Delimited -> File -> Existing_File_Behavior -> Problem_Behavior -> Any +write_file : Table -> File_Format.Delimited -> File -> Existing_File_Behavior -> Problem_Behavior -> Any write_file table format file on_existing_file on_problems = case on_existing_file of Existing_File_Behavior.Append -> - Errors.unimplemented "Appending to an existing Delimited file is not implemented yet." + Errors.unimplemented "Appending to an existing File_Format.Delimited file is not implemented yet." _ -> on_existing_file.write file stream-> stream.with_java_stream java_stream-> here.write_to_stream table format java_stream on_problems related_file=file +## PRIVATE + Returns a Text value representing the table in the delimited format. +write_text : Table -> File_Format.Delimited -> Text +write_text table format = + java_writer = StringWriter.new + here.write_to_writer table format java_writer + java_writer.toString + ## PRIVATE Writes to an output stream according to the provided format. @@ -51,7 +60,7 @@ write_file table format file on_existing_file on_problems = If set to `Ignore`, the operation proceeds without errors or warnings. - related_file: The file related to the provided `java_stream`, if available, or `Nothing`. It is used for more detailed error reporting. -write_to_stream : Table -> Delimited -> OutputStream -> Problem_Behavior -> File | Nothing -> Any +write_to_stream : Table -> File_Format.Delimited -> OutputStream -> Problem_Behavior -> File | Nothing -> Any write_to_stream table format java_stream on_problems related_file=Nothing = handle_io_exception ~action = Panic.catch IOException action caught_panic-> Error.throw (File.wrap_io_exception related_file caught_panic.payload.cause) @@ -74,7 +83,7 @@ write_to_stream table format java_stream on_problems related_file=Nothing = - table: The table to serialize. - format: The specification of the delimited file format. - java_writer: A Java `Writer` to which characters will be written. -write_to_writer : Table -> Delimited -> Writer -> Any +write_to_writer : Table -> File_Format.Delimited -> Writer -> Any write_to_writer table format java_writer = column_formatters = Panic.recover Illegal_Argument_Error <| case format.value_formatter of Nothing -> table.columns.map column-> case column.storage_type of diff --git a/std-bits/table/src/main/java/org/enso/table/formatting/DecimalFormatter.java b/std-bits/table/src/main/java/org/enso/table/formatting/DecimalFormatter.java index 9b1f3296c125..c5d0fde2dbd4 100644 --- a/std-bits/table/src/main/java/org/enso/table/formatting/DecimalFormatter.java +++ b/std-bits/table/src/main/java/org/enso/table/formatting/DecimalFormatter.java @@ -51,15 +51,11 @@ public String format(Object value) { return format(decimal.doubleValue()); } - if (value instanceof Long integer) { - return format(integer.doubleValue()); - } - throw new IllegalArgumentException("Unsupported type for DecimalFormatter."); } @Override public boolean canFormat(Object value) { - return value instanceof Double || value instanceof Long; + return value instanceof Double; } } diff --git a/std-bits/table/src/main/java/org/enso/table/formatting/IntegerFormatter.java b/std-bits/table/src/main/java/org/enso/table/formatting/IntegerFormatter.java index 116239560153..d895a510f347 100644 --- a/std-bits/table/src/main/java/org/enso/table/formatting/IntegerFormatter.java +++ b/std-bits/table/src/main/java/org/enso/table/formatting/IntegerFormatter.java @@ -1,6 +1,5 @@ package org.enso.table.formatting; -import java.math.BigInteger; import java.text.DecimalFormat; public class IntegerFormatter implements DataFormatter { @@ -42,7 +41,7 @@ public String format(Object value) { return format(integer.longValue()); } - throw new IllegalArgumentException("Unsupported type for DecimalFormatter."); + throw new IllegalArgumentException("Unsupported type for IntegerFormatter."); } @Override