Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #386 Option to add .ignorePropery() to Csv builder #418

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,22 @@ public String getColumnDesc()
sb.append(']');
return sb.toString();
}
/**
* Method for ignoring fields for CsvBuilder
* @param ignoreProperties Array of column names to be ignored by the csv builder
* @return A newly built {@link CsvSchema} with the ignored properties
*/
public CsvSchema ignoreProperty(String[] ignoreProperties) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From naming perspective it should probably be "ignoreProperties".

Also, would perhaps make sense to take String... instead of String[] for convenience.

Map<String, Column> map = this._columnsByName;
for(String ignoreProperty : ignoreProperties) {
if (this._columnsByName.containsKey(ignoreProperty)) {
map.remove(ignoreProperty);
}
}
CsvSchema csvSchema = new CsvSchema(this, map.values().toArray(new Column[map.size()]));
Builder builder = new Builder(csvSchema);
return builder.build();
}

/*
/**********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,40 @@ public void testReorderByName() throws Exception
_verifyLinks(schema);
}

// for [ignore Add .ignoreProperty() to csv builder #386]
public void testIgnoreProperty() throws Exception {
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(Mixed.class);
assertEquals(a2q("['a','b','c','d']"), schema.getColumnDesc());
schema = schema.ignoreProperty(new String[]{"a", "d", "z"});
assertEquals(a2q("['b','c']"), schema.getColumnDesc());

_verifyLinks(schema);
}

public void testIgnorePropertyDoesNotModifyUnderLyingSchema() throws Exception {
CsvMapper mapper = mapperForCsv();
CsvSchema originalSchema = mapper.schemaFor(Mixed.class);
assertEquals(a2q("['a','b','c','d']"), originalSchema.getColumnDesc());
CsvSchema modifiedSchema = originalSchema.ignoreProperty(new String[]{"a", "d", "z"});
assertEquals(a2q("['b','c']"), modifiedSchema.getColumnDesc());
assertEquals(a2q("['a','b','c','d']"), originalSchema.getColumnDesc());

_verifyLinks(modifiedSchema);
_verifyLinks(originalSchema);
}

// for [ignore Add .ignoreProperty() to csv builder #386]
public void testIgnorePropertyPropertyNotPresent() throws Exception {
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(Mixed.class);
assertEquals(a2q("['a','b','c','d']"), schema.getColumnDesc());
schema = schema.ignoreProperty(new String[]{"q", "w", "e"});
assertEquals(a2q("['a','b','c','d']"), schema.getColumnDesc());

_verifyLinks(schema);
}

// for [dataformat-csv#42]
public void testReorderWithComparator() throws Exception
{
Expand Down