Skip to content

Commit c9a56f6

Browse files
committed
改行や空白だけの行の時にエラーになる問題を修正
1 parent fb0b812 commit c9a56f6

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

src/main/java/org/embulk/parser/jsonl/JsonlParserPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutpu
175175
}
176176
lineNumber++;
177177

178+
// Skip empty lines
179+
if (line.trim().isEmpty()) {
180+
continue;
181+
}
182+
178183
try {
179184
Value value = jsonParser.parse(line);
180185

src/test/java/org/embulk/parser/jsonl/TestJsonlParserPlugin.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,116 @@ record = records.get(1);
241241
}
242242
}
243243

244+
@Test
245+
public void testTrailingEmptyLine() throws Exception {
246+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
247+
ConfigSource config = config().set("columns", schema);
248+
249+
// Simulates a file with a trailing newline: the last element "" represents the
250+
// empty line
251+
List<Object[]> records = runParser(
252+
config,
253+
Arrays.asList(
254+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
255+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}",
256+
"")); // Empty line at the end
257+
258+
assertEquals(2, records.size());
259+
assertEquals(true, records.get(0)[0]);
260+
assertEquals(10L, records.get(0)[1]);
261+
assertEquals("first", records.get(0)[2]);
262+
}
263+
264+
@Test
265+
public void testLeadingEmptyLine() throws Exception {
266+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
267+
ConfigSource config = config().set("columns", schema);
268+
269+
List<Object[]> records = runParser(
270+
config,
271+
Arrays.asList(
272+
"", // Empty line at the beginning
273+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
274+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
275+
276+
assertEquals(2, records.size());
277+
}
278+
279+
@Test
280+
public void testMiddleEmptyLine() throws Exception {
281+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
282+
ConfigSource config = config().set("columns", schema);
283+
284+
List<Object[]> records = runParser(
285+
config,
286+
Arrays.asList(
287+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
288+
"", // Empty line in the middle
289+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
290+
291+
assertEquals(2, records.size());
292+
}
293+
294+
@Test
295+
public void testMultipleConsecutiveEmptyLines() throws Exception {
296+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
297+
ConfigSource config = config().set("columns", schema);
298+
299+
List<Object[]> records = runParser(
300+
config,
301+
Arrays.asList(
302+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
303+
"", // Empty line
304+
"", // Empty line
305+
"", // Empty line
306+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
307+
308+
assertEquals(2, records.size());
309+
}
310+
311+
@Test
312+
public void testWhitespaceOnlyLines() throws Exception {
313+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
314+
ConfigSource config = config().set("columns", schema);
315+
316+
List<Object[]> records = runParser(
317+
config,
318+
Arrays.asList(
319+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
320+
" ", // Spaces only
321+
"\t", // Tab only
322+
" \t ", // Mixed whitespace
323+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
324+
325+
assertEquals(2, records.size());
326+
}
327+
328+
@Test
329+
public void testOnlyEmptyLines() throws Exception {
330+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
331+
ConfigSource config = config().set("columns", schema);
332+
333+
List<Object[]> records = runParser(config, Arrays.asList("", " ", "\t", ""));
334+
335+
assertEquals(0, records.size());
336+
}
337+
338+
@Test
339+
public void testEmptyLinesWithStopOnInvalidRecord() throws Exception {
340+
SchemaConfig schema = schema(column("_c0", BOOLEAN), column("_c1", LONG), column("_c2", STRING));
341+
ConfigSource config = config().set("columns", schema).set("stop_on_invalid_record", true);
342+
343+
// Empty lines should be skipped even when stop_on_invalid_record is true
344+
List<Object[]> records = runParser(
345+
config,
346+
Arrays.asList(
347+
"{\"_c0\":true,\"_c1\":10,\"_c2\":\"first\"}",
348+
"",
349+
"{\"_c0\":false,\"_c1\":20,\"_c2\":\"second\"}"));
350+
351+
assertEquals(2, records.size());
352+
}
353+
244354
private ConfigSource config() {
245355
return CONFIG_MAPPER_FACTORY.newConfigSource();
246356
}

0 commit comments

Comments
 (0)