Skip to content

Commit

Permalink
Fix: CSV and Raw output, escape quotes
Browse files Browse the repository at this point in the history
Fixes opensearch-project#3050

Signed-off-by: Mike Swierczek <[email protected]>
  • Loading branch information
Michael-S committed Oct 9, 2024
1 parent ac8678c commit 5d20234
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected List<List<String>> formatData(List<List<String>> lines) {

protected String quoteIfRequired(String separator, String cell) {
final String quote = "\"";
return cell.contains(separator) ? quote + cell.replaceAll("\"", "\"\"") + quote : cell;
if (cell != null && (cell.contains(separator) || cell.contains(quote))) {
return quote + cell.replaceAll(quote, quote + quote) + quote;
} else {
return cell;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ void quoteIfRequired() {
QueryResult response =
new QueryResult(
schema,
Arrays.asList(tupleValue(ImmutableMap.of("na,me", "John,Smith", ",,age", "30,,,"))));
String expected = "\"na,me\",\",,age\"%n\"John,Smith\",\"30,,,\"";
Arrays.asList(
tupleValue(ImmutableMap.of("na,me", "John,Smith", ",,age", "30,,,")),
tupleValue(ImmutableMap.of("na,me", "\"Janice Jones", ",,age", "26\""))));
String expected =
"\"na,me\",\",,age\"%n\"John,Smith\",\"30,,,\"%n\"\"\"Janice Jones\",\"26\"\"\"";
assertEquals(format(expected), formatter.format(response));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ void quoteIfRequired() {
QueryResult response =
new QueryResult(
schema,
Arrays.asList(tupleValue(ImmutableMap.of("na|me", "John|Smith", "||age", "30|||"))));
String expected = "\"na|me\"|\"||age\"%n" + "\"John|Smith\"|\"30|||\"";
Arrays.asList(
tupleValue(ImmutableMap.of("na|me", "John|Smith", "||age", "30|||")),
tupleValue(ImmutableMap.of("na|me", "Ja\"ne J\"ones", "||age", "\"40\""))));
String expected =
"\"na|me\"|\"||age\"%n"
+ "\"John|Smith\"|\"30|||\"%n"
+ "\"Ja\"\"ne J\"\"ones\"|\"\"\"40\"\"\"";
assertEquals(format(expected), getRawFormatter().format(response));
String expectedPretty = "\"na|me\" |\"||age\"%n" + "\"John|Smith\"|\"30|||\"";
String expectedPretty =
"\"na|me\" |\"||age\" %n"
+ "\"John|Smith\" |\"30|||\" %n"
+ "\"Ja\"\"ne J\"\"ones\"|\"\"\"40\"\"\"";
assertEquals(format(expectedPretty), getRawFormatterPretty().format(response));
}

Expand Down

0 comments on commit 5d20234

Please sign in to comment.