Skip to content

Commit 1077389

Browse files
committed
feat: add ofSingleCsvRecord methods to CsvReader for convenience
Introduce new methods to parse single CSV records: - `ofSingleCsvRecord(String)` for simple usage. - `ofSingleCsvRecord(CsvCallbackHandler, String)` for custom callback handling.
1 parent 7995a84 commit 1077389

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/src/intTest/java/blackbox/reader/AbstractCsvReaderTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import de.siegmar.fastcsv.reader.CsvParseException;
2828
import de.siegmar.fastcsv.reader.CsvReader;
2929
import de.siegmar.fastcsv.reader.CsvRecord;
30+
import de.siegmar.fastcsv.reader.CsvRecordHandler;
31+
import de.siegmar.fastcsv.reader.FieldModifiers;
3032
import testutil.CsvRecordAssert;
3133

3234
@SuppressWarnings({
@@ -377,7 +379,27 @@ void unreadable() {
377379

378380
@Test
379381
void fieldCount() {
380-
assertThat(crb.ofCsvRecord("foo,bar").iterator().next().getFieldCount()).isEqualTo(2);
382+
assertThat(crb.ofSingleCsvRecord("foo,bar").getFieldCount()).isEqualTo(2);
383+
}
384+
385+
@Test
386+
void ofSingleCsvRecord() {
387+
assertThat(crb.ofSingleCsvRecord("foo,bar"))
388+
.satisfies(rec -> CsvRecordAssert.assertThat(rec).fields().containsExactly("foo", "bar"));
389+
}
390+
391+
@Test
392+
void ofNamedSingleCsvRecordDataMissing() {
393+
assertThatThrownBy(() -> crb.ofSingleCsvRecord(""))
394+
.isInstanceOf(CsvParseException.class)
395+
.hasMessage("No record found in the provided data");
396+
}
397+
398+
@Test
399+
void ofSingleCsvRecordWithCustomHandler() {
400+
final var cbh = CsvRecordHandler.of(c -> c.fieldModifier(FieldModifiers.TRIM));
401+
assertThat(crb.ofSingleCsvRecord(cbh, " foo , bar "))
402+
.satisfies(rec -> CsvRecordAssert.assertThat(rec).fields().containsExactly("foo", "bar"));
381403
}
382404

383405
// test helpers

lib/src/main/java/de/siegmar/fastcsv/reader/CsvReader.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,38 @@ public CsvReaderBuilder maxBufferSize(final int maxBufferSize) {
584584
return this;
585585
}
586586

587+
/// Convenience method to read a single CSV record from the specified string.
588+
///
589+
/// If the string contains multiple records, only the first one is returned.
590+
///
591+
/// @param data the CSV data to read; must not be `null`
592+
/// @return a single [CsvRecord] instance containing the parsed data
593+
/// @throws NullPointerException if data is `null`
594+
/// @throws CsvParseException if the data cannot be parsed
595+
/// @see #ofSingleCsvRecord(CsvCallbackHandler, String)
596+
public CsvRecord ofSingleCsvRecord(final String data) {
597+
return ofSingleCsvRecord(CsvRecordHandler.of(), data);
598+
}
599+
600+
/// Convenience method to read a single CSV record using a custom callback handler.
601+
///
602+
/// If the string contains multiple records, only the first one is returned.
603+
///
604+
/// @param <T> the type of the CSV record.
605+
/// @param callbackHandler the record handler to use. Do not reuse a handler after it has been used!
606+
/// @param data the CSV data to read; must not be `null`
607+
/// @return a single record as processed by the callback handler
608+
/// @throws NullPointerException if callbackHandler or data is `null`
609+
/// @throws CsvParseException if the data cannot be parsed
610+
/// @see #ofSingleCsvRecord(String)
611+
public <T> T ofSingleCsvRecord(final CsvCallbackHandler<T> callbackHandler, final String data) {
612+
final T fetchedRecord = build(callbackHandler, data).fetch();
613+
if (fetchedRecord == null) {
614+
throw new CsvParseException("No record found in the provided data");
615+
}
616+
return fetchedRecord;
617+
}
618+
587619
/// Constructs a new index-based [CsvReader] for the specified input stream.
588620
///
589621
/// This is a convenience method for calling [#build(CsvCallbackHandler,InputStream)] with

0 commit comments

Comments
 (0)