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

Commit

Permalink
Implemented #42
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 20, 2014
1 parent 327fa1c commit 8479018
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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(...)`

------------------------------------------------------------------------
=== History: ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ protected CsvParser _createParser(byte[] data, int offset, int len, IOContext ct
_objectCodec, _createReader(data, offset, len, null, ctxt));
}

@Override
protected CsvParser _createParser(char[] data, int offset, int len, IOContext ctxt,
boolean recyclable) throws IOException {
return new CsvParser(ctxt, _getBufferRecycler(), _parserFeatures, _csvParserFeatures,
Expand Down
71 changes: 69 additions & 2 deletions src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public enum ColumnType
;
}

/**
* Representation of info for a single column
*/
public static class Column
{
private final String _name;
Expand Down Expand Up @@ -334,7 +337,7 @@ protected void _checkIndex(int index) {
}
}
}

/*
/**********************************************************************
/* Configuration, construction
Expand Down Expand Up @@ -407,7 +410,22 @@ protected CsvSchema(Column[] columns,
_lineSeparator = lineSeparator;
_columnsByName = columnsByName;
}


/**
* Copy constructor used for creating variants using
* <code>sortedBy()</code> methods.
*/
protected CsvSchema(CsvSchema base, Column[] columns) {
_columns = columns;
_useHeader = base._useHeader;
_skipFirstDataRow = base._skipFirstDataRow;
_columnSeparator = base._columnSeparator;
_quoteChar = base._quoteChar;
_escapeChar = base._escapeChar;
_lineSeparator = base._lineSeparator;
_columnsByName = base._columnsByName;
}

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -442,6 +460,12 @@ public Builder rebuild() {
return new Builder(this);
}

/*
/**********************************************************************
/* Mutant factories
/**********************************************************************
*/

public CsvSchema withUseHeader(boolean state) {
return (_useHeader == state) ? this
: new CsvSchema(_columns, state, _skipFirstDataRow,
Expand Down Expand Up @@ -512,6 +536,49 @@ public CsvSchema withoutColumns() {
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _columnsByName);
}

/**
* Mutant factory method that will construct a new instance in which columns
* are sorted based on names given as argument. Columns not listed in argument
* will be sorted after those within list, using existing ordering.
*<p>
* For example, schema that has columns:
*<pre>"a", "d", "c", "b"
*</pre>
* ordered with <code>schema.sortedBy("a", "b");</code>
* would result instance that columns in order:
*<pre>"a", "b", "d", "c"
*</pre>
*
* @since 2.4
*/
public CsvSchema sortedBy(String... columnNames) {
LinkedHashMap<String,Column> map = new LinkedHashMap<String,Column>();
for (String colName : columnNames) {
Column col = _columnsByName.get(colName);
if (col != null) {
map.put(col.getName(), col);
}
}
for (Column col : _columns) {
map.put(col.getName(), col);
}
return new CsvSchema(this, map.values().toArray(new Column[map.size()]));
}

/**
* Mutant factory method that will construct a new instance in which columns
* are sorted using given {@link Comparator} over column names.
*
* @since 2.4
*/
public CsvSchema sortedBy(Comparator<String> cmp) {
TreeMap<String,Column> map = new TreeMap<String,Column>(cmp);
for (Column col : _columns) {
map.put(col.getName(), col);
}
return new CsvSchema(this, map.values().toArray(new Column[map.size()]));
}

/*
/**********************************************************************
/* Public API, FormatSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,17 @@ public String quote(String str) {
return '"'+str+'"';
}

protected void assertToken(JsonToken expToken, JsonToken actToken)
{
protected String aposToQuotes(String json) {
return json.replace("'", "\"");
}

protected void assertToken(JsonToken expToken, JsonToken actToken) {
if (actToken != expToken) {
fail("Expected token "+expToken+", current token "+actToken);
}
}

protected void assertToken(JsonToken expToken, JsonParser jp)
{
protected void assertToken(JsonToken expToken, JsonParser jp) {
assertToken(expToken, jp.getCurrentToken());
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/fasterxml/jackson/dataformat/csv/TestSchema.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package com.fasterxml.jackson.dataformat.csv;

import java.util.Collections;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

public class TestSchema extends ModuleTestBase
{
@JsonPropertyOrder({ "a", "b", "c", "d" })
static class Mixed {
public int a, b, c, d;
}

public void testSimpleWithAutoSchema() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class);
assertEquals("[\"firstName\",\"lastName\",\"gender\",\"verified\",\"userImage\"]", schema.getColumnDesc());
}

// for [Issue#42]
public void testReorderByName() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(Mixed.class);
assertEquals(aposToQuotes("['a','b','c','d']"), schema.getColumnDesc());
schema = schema.sortedBy("b", "c");
assertEquals(aposToQuotes("['b','c','a','d']"), schema.getColumnDesc());
}

// for [Issue#42]
public void testReorderWithComparator() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(Mixed.class);
schema = schema.sortedBy(Collections.<String>reverseOrder());
assertEquals(aposToQuotes("['d','c','b','a']"), schema.getColumnDesc());
}
}

0 comments on commit 8479018

Please sign in to comment.