Skip to content

Commit

Permalink
csv conversion to not crash on e.g. empty string #1370
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 9, 2020
1 parent afb02ca commit 1c06f00
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

Expand All @@ -58,6 +60,8 @@
*/
public class JsonUtils {

private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

private JsonUtils() {
// only static methods
}
Expand Down Expand Up @@ -147,15 +151,16 @@ public static Map<String, Object> fromYaml(String raw) {
public static List<Map> fromCsv(String raw) {
CsvReader reader = new CsvReader();
reader.setContainsHeader(true);
List<Map> rows = new ArrayList();
try {
CsvContainer csv = reader.read(new StringReader(raw));
List<Map> rows = new ArrayList(csv.getRowCount());
for (CsvRow row : csv.getRows()) {
rows.add(row.getFieldMap());
}
return rows;
} catch (Exception e) {
throw new RuntimeException(e);
logger.warn("failed to parse csv: {}", raw);
return rows;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ void testJavaInteropBase64() {
"def res = Base64.encoder.encodeToString('hello'.getBytes())"
);
matchVar("res", java.util.Base64.getEncoder().encodeToString("hello".getBytes()));
}
}

@Test
void testTypeConversionCsvEmpty() {
run(
"csv temp = ''"
);
matchVar("temp", "[]");
}

}

0 comments on commit 1c06f00

Please sign in to comment.