Skip to content

Commit

Permalink
RD-9178: CsvWriter option for Windows friendly output (#22)
Browse files Browse the repository at this point in the history
Added a test ensuring the line separator is indeed changed depending on
the config.
  • Loading branch information
bgaidioz authored Jul 6, 2023
1 parent 633128e commit e4cf98c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ class Rql2TruffleCompiler(implicit compilerContext: CompilerContext)
val frameDescriptor = emitter.dropScope()

// Wrap output node

val rootNode: RootNode = programContext.settings.getString(ProgramSettings.output_format) match {
case "csv" => tree.analyzer.tipe(me.get) match {
case "csv" =>
val lineSeparator = if (programContext.settings.getBoolean("raw.compiler.windows-line-ending")) "\r\n" else "\n"
tree.analyzer.tipe(me.get) match {
case Rql2IterableType(Rql2RecordType(atts, rProps), iProps) =>
assert(rProps.isEmpty)
assert(iProps.isEmpty)
Expand All @@ -188,7 +191,8 @@ class Rql2TruffleCompiler(implicit compilerContext: CompilerContext)
new CsvIterableWriterNode(
bodyExpNode,
CsvWriter(atts.map(_.tipe)),
atts.map(_.idn).toArray
atts.map(_.idn).toArray,
lineSeparator
)
)
case Rql2ListType(Rql2RecordType(atts, rProps), iProps) =>
Expand All @@ -200,7 +204,8 @@ class Rql2TruffleCompiler(implicit compilerContext: CompilerContext)
new CsvListWriterNode(
bodyExpNode,
CsvWriter(atts.map(_.tipe)),
atts.map(_.idn).toArray
atts.map(_.idn).toArray,
lineSeparator
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,21 @@ trait CompilerTestContext
}
}

def saveTo(path: Path, executionLogger: ExecutionLogger = DebugExecutionLogger): SaveTo =
new SaveTo(path, Map.empty, executionLogger)
def saveTo(
path: Path,
executionLogger: ExecutionLogger = DebugExecutionLogger,
options: Map[String, String] = Map.empty
): SaveTo = new SaveTo(path, options, executionLogger)

def saveToInFormat(path: Path, format: String, executionLogger: ExecutionLogger = DebugExecutionLogger): SaveTo =
new SaveTo(path, Map("output-format" -> format), executionLogger)
def saveToInFormat(
path: Path,
format: String,
executionLogger: ExecutionLogger = DebugExecutionLogger,
options: Map[String, String] = Map.empty
): SaveTo = new SaveTo(path, options + ("output-format" -> format), executionLogger)

// a Matcher[Path] that compares the content of the file at the given path to the given string.
protected def contain(content: String) = be(content) compose { p: Path =>
protected def contain(content: String): Matcher[Path] = be(content) compose { p: Path =>
val bufferedSource = Source.fromFile(p.toFile)
val fileContent = bufferedSource.mkString
bufferedSource.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ trait CsvOutputTest extends CompilerTestContext {
RawUtils.deleteTestPath(path)
}
}

test("""[{a: 1, b: 2}, {a: 3, b: 4}]""") { it =>
it should run
val path = Files.createTempFile("", "")
try {
it should saveToInFormat(path, "csv", options = Map("windows-line-ending" -> "false"))
path should contain("a,b\n1,2\n3,4\n")
it should saveToInFormat(path, "csv", options = Map("windows-line-ending" -> "true"))
path should contain("a,b\r\n1,2\r\n3,4\r\n")
} finally {
Files.deleteIfExists(path)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import raw.runtime.truffle.ExpressionNode;
import raw.runtime.truffle.RawContext;
import raw.runtime.truffle.StatementNode;
import raw.runtime.truffle.runtime.exceptions.RawTruffleRuntimeException;
import raw.runtime.truffle.runtime.exceptions.csv.CsvWriterRawTruffleException;
import raw.runtime.truffle.runtime.generator.GeneratorLibrary;
import raw.runtime.truffle.runtime.iterable.IterableLibrary;
Expand All @@ -47,11 +46,13 @@ public class CsvIterableWriterNode extends StatementNode {
@Child
private GeneratorLibrary generators = GeneratorLibrary.getFactory().createDispatched(3);

private String[] columnNames;
private final String[] columnNames;
private final String lineSeparator;

public CsvIterableWriterNode(ExpressionNode dataNode, RootNode writerNode, String[] columnNames) {
public CsvIterableWriterNode(ExpressionNode dataNode, RootNode writerNode, String[] columnNames, String lineSeparator) {
this.dataNode = dataNode;
this.columnNames = columnNames;
this.lineSeparator = lineSeparator;
itemWriter = DirectCallNode.create(writerNode.getCallTarget());
}

Expand Down Expand Up @@ -82,7 +83,7 @@ private CsvGenerator createGenerator(OutputStream os) {
}
schemaBuilder.setColumnSeparator(',');
schemaBuilder.setUseHeader(true);
schemaBuilder.setLineSeparator('\n');
schemaBuilder.setLineSeparator(lineSeparator);
schemaBuilder.setQuoteChar('"');
schemaBuilder.setNullValue("");
generator.setSchema(schemaBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import raw.runtime.truffle.ExpressionNode;
import raw.runtime.truffle.RawContext;
import raw.runtime.truffle.StatementNode;
import raw.runtime.truffle.runtime.exceptions.RawTruffleRuntimeException;
import raw.runtime.truffle.runtime.exceptions.csv.CsvWriterRawTruffleException;
import raw.runtime.truffle.runtime.list.ListLibrary;
import raw.runtime.truffle.runtime.list.ObjectList;
Expand All @@ -45,11 +44,13 @@ public class CsvListWriterNode extends StatementNode {
private ListLibrary lists = ListLibrary.getFactory().createDispatched(3);

private String[] columnNames;
private final String lineSeparator;

public CsvListWriterNode(ExpressionNode dataNode, RootNode writerNode, String[] columnNames) {
public CsvListWriterNode(ExpressionNode dataNode, RootNode writerNode, String[] columnNames, String lineSeparator) {
this.dataNode = dataNode;
itemWriter = DirectCallNode.create(writerNode.getCallTarget());
this.columnNames = columnNames;
this.lineSeparator = lineSeparator;
}

@Override
Expand Down Expand Up @@ -78,7 +79,7 @@ private CsvGenerator createGenerator(OutputStream os) {
}
schemaBuilder.setColumnSeparator(',');
schemaBuilder.setUseHeader(true);
schemaBuilder.setLineSeparator('\n');
schemaBuilder.setLineSeparator(lineSeparator);
schemaBuilder.setQuoteChar('"');
schemaBuilder.setNullValue("");
generator.setSchema(schemaBuilder.build());
Expand Down

0 comments on commit e4cf98c

Please sign in to comment.