Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 26, 2014
1 parent a71c990 commit 788e437
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Version: 2.4.0 (xx-xxx-2014)
#32: Allow disabling of quoteChar
(suggested by Jason D)
#40: Allow (re)ordering of columns of Schema, using `CsvSchema.sortedBy(...)`
#45: Change Default Outputting Behavior to Include Final Comma Regardless of Value.
#45: Change outputting behavior to include final commas even if values are
missing; also add `CsvGenerator.OMIT_MISSING_TAIL_COLUMNS`

------------------------------------------------------------------------
=== History: ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public enum Feature {
* @since 2.4
*/
STRICT_CHECK_FOR_QUOTING(false),

/**
* Feature that determines whether columns without matching value may be omitted,
* when they are the last values of the row.
* If <code>true</code>, values and separators between values may be omitted, to slightly reduce
* length of the row; if <code>false</code>, separators need to stay in place and values
* are indicated by empty Strings.
*
* @since 2.4
*/
OMIT_MISSING_TAIL_COLUMNS(false),
;

protected final boolean _defaultState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ public class CsvWriter
/**
* Marker flag used to determine if to do optimal (aka "strict") quoting
* checks or not (looser conservative check)
*
* @since 2.4
*/
protected boolean _cfgOptimalQuoting;


/**
* @since 2.4
*/
protected boolean _cfgIncludeMissingTail;

/*
/**********************************************************
/* Output state
Expand Down Expand Up @@ -142,6 +149,7 @@ public CsvWriter(IOContext ctxt, int csvFeatures, Writer out, CsvSchema schema)
_ioContext = ctxt;
_csvFeatures = csvFeatures;
_cfgOptimalQuoting = CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING.enabledIn(csvFeatures);
_cfgIncludeMissingTail = !CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS.enabledIn(_csvFeatures);

_outputBuffer = ctxt.allocConcatBuffer();
_bufferRecyclable = true;
Expand Down Expand Up @@ -174,6 +182,7 @@ public CsvWriter(IOContext ctxt, int csvFeatures, Writer out,
_ioContext = ctxt;
_csvFeatures = csvFeatures;
_cfgOptimalQuoting = CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING.enabledIn(csvFeatures);
_cfgIncludeMissingTail = !CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS.enabledIn(_csvFeatures);

_outputBuffer = ctxt.allocConcatBuffer();
_bufferRecyclable = true;
Expand All @@ -198,6 +207,7 @@ public CsvWriter(CsvWriter base, CsvSchema newSchema)
_ioContext = base._ioContext;
_csvFeatures = base._csvFeatures;
_cfgOptimalQuoting = base._cfgOptimalQuoting;
_cfgIncludeMissingTail = base._cfgIncludeMissingTail;

_outputBuffer = base._outputBuffer;
_bufferRecyclable = base._bufferRecyclable;
Expand Down Expand Up @@ -356,9 +366,11 @@ public void endRow() throws IOException
}
// Any missing values?
if (_nextColumnToWrite < _columnCount) {
do {
appendColumnSeparator();
} while (++_nextColumnToWrite < _columnCount);
if (_cfgIncludeMissingTail) {
do {
appendColumnSeparator();
} while (++_nextColumnToWrite < _columnCount);
}
}
// write line separator
_nextColumnToWrite = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ public void testWrite_NullSecondColumn() throws JsonProcessingException {
}

@Test
public void testWrite_NullThirdColumn() throws JsonProcessingException {
final String csv = WRITER.writeValueAsString(
public void testWrite_NullThirdColumn() throws JsonProcessingException
{
CsvMapper mapper = new CsvMapper();
assertFalse(mapper.getFactory().isEnabled(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS));
String csv = mapper.writer().withSchema(SCHEMA).writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
"value", 42));

assertEquals("\"2014-03-10T23:32:47+00:00\",42,\n", csv);
mapper.getFactory().enable(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS);
csv = mapper.writer().withSchema(SCHEMA).writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
"value", 42));
assertEquals("\"2014-03-10T23:32:47+00:00\",42\n", csv);
}
}

0 comments on commit 788e437

Please sign in to comment.