Skip to content

Commit

Permalink
Remove quotes enclosing fields when using TAB separator (#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Nov 23, 2023
1 parent 1450055 commit e31c8e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/transformation/CsvExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public String of(final String fields, final String separator) {
try {
Object value = JsonPath.read(Configuration.defaultConfiguration()
.jsonProvider().parse(org.toString()), "$." + field);
return String.format("\"%s\"",
value.toString().replaceAll("\"", "\"\""));
return separator==DEFAULT_SEPARATOR ? String.format("\"%s\"",
value.toString().replaceAll("\"", "\"\"")) : value.toString();
}
catch (PathNotFoundException x) {
Logger.trace(x.getMessage());
Expand Down
26 changes: 20 additions & 6 deletions test/transformation/CsvExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public void testFlatFieldsTabulatorSeparator() {
testFlatFields(CsvExport.TAB_SEPARATOR);
}

@Test
public void testNestedFieldsDefaultSeparator() {
testNestedFields(CsvExport.DEFAULT_SEPARATOR);
}

@Test
public void testNestedFieldsTabulatorSeparator() {
testNestedFields(CsvExport.TAB_SEPARATOR);
}

private void testFlatFields(final String sep) {
ObjectNode node1 = Json.newObject();
node1.put("field1", "org1-value1");
Expand All @@ -40,11 +50,13 @@ private void testFlatFields(final String sep) {
"field1", "field3", //
"\"org1-value1\"", "\"org1-value3\"", //
"\"org2-value1\"", "\"org2-value3\"");
if (sep.equals(CsvExport.TAB_SEPARATOR)) {
expected=expected.replaceAll("\"","");
}
assertThat(export.of("field1" + sep + "field3", sep)).isEqualTo(expected);
}

@Test
public void testNestedFields() {
private void testNestedFields(final String sep) {
ObjectNode org1 = Json.newObject();
ObjectNode sub1 = Json.newObject();
org1.put("field1", "org1-value1");
Expand All @@ -63,15 +75,17 @@ public void testNestedFields() {
sub2.put("field3", "org2-sub3");
List<ObjectNode> orgs = Arrays.asList(org1, org2);
CsvExport export = new CsvExport(Json.stringify(Json.toJson(orgs)));
String expected = String.format("%s,%s\n%s,%s\n%s,%s\n", //
String expected = String.format("%s" + sep + "%s\n%s" + sep + "%s\n%s" + sep + "%s\n", //
"field1", "field3.field2", //
"\"org1-value1\"", "\"org1-sub2\"", //
"\"org2-value1\"", "\"org2-sub2\"");
assertThat(export.of("field1,field3.field2")).isEqualTo(expected);
if (sep.equals(CsvExport.TAB_SEPARATOR)) {
expected=expected.replaceAll("\"","");
}
assertThat(export.of("field1" + sep + "field3.field2", sep)).isEqualTo(expected);
}

@Test
public void testMissingField() {
private void testMissingField() {
ObjectNode org = Json.newObject();
org.put("field1", "org1-value1");
org.put("field2", "org1-value2");
Expand Down

0 comments on commit e31c8e5

Please sign in to comment.