Skip to content

Commit

Permalink
Add method for optional field access #97
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Nov 19, 2023
1 parent c20b884 commit 5ebb7d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for different field count for NamedCsvReader
- Metadata for OSGi capability
- Support for optional BOM header when reading CSV files
- Method `NamedCsvRecord.findField` for optional field access

### Changed
- Updated from Java 8 to Java 11
Expand Down
7 changes: 7 additions & 0 deletions lib/src/common/java/testutil/NamedCsvRecordAssert.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package testutil;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactory;
import org.assertj.core.api.MapAssert;
import org.assertj.core.api.OptionalAssert;
import org.assertj.core.api.StringAssert;

import de.siegmar.fastcsv.reader.NamedCsvRecord;
Expand Down Expand Up @@ -40,4 +42,9 @@ public StringAssert field(final String name) {
return new StringAssert(actual.getField(name));
}

public OptionalAssert<String> findField(final String name) {
isNotNull();
return Assertions.assertThat(actual.findField(name));
}

}
21 changes: 20 additions & 1 deletion lib/src/intTest/java/blackbox/reader/NamedCsvReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ void getFieldByName() {
.field("foo").isEqualTo("bar");
}

@Test
void findFieldByName() {
assertThat(parse("foo\nbar").stream())
.singleElement(NAMED_CSV_RECORD)
.findField("foo").hasValue("bar");
}

@SuppressWarnings("JoinAssertThatStatements")
@Test
void getHeader() {
Expand Down Expand Up @@ -124,12 +131,24 @@ void getHeaderWithoutNextRecordCall() {
}

@Test
void findNonExistingFieldByName() {
void getNonExistingFieldByName() {
assertThatThrownBy(() -> parse("foo\nfaz").iterator().next().getField("bar"))
.isInstanceOf(NoSuchElementException.class)
.hasMessage("Header does not contain a field 'bar'. Valid names are: [foo]");
}

@Test
void findNonExistingFieldByName() {
assertThat(parse("foo\nfaz").iterator().next().findField("bar"))
.isEmpty();
}

@Test
void findNonExistingFieldByName2() {
assertThat(parse("foo,bar\nfaz").iterator().next().findField("bar"))
.isEmpty();
}

@Test
void toStringWithHeader() {
assertThat(parse("headerA,headerB,headerC\nfieldA,fieldB,fieldC\n").stream())
Expand Down
17 changes: 17 additions & 0 deletions lib/src/main/java/de/siegmar/fastcsv/reader/NamedCsvRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.StringJoiner;

/**
Expand Down Expand Up @@ -40,6 +41,7 @@ public long getOriginalLineNumber() {
* @param name field name
* @return field value, never {@code null}
* @throws NoSuchElementException if this record has no such field
* @see #findField(String)
*/
public String getField(final String name) {
final int fieldPos = header.indexOf(name);
Expand All @@ -54,6 +56,21 @@ public String getField(final String name) {
return csvRecord.getField(fieldPos);
}

/**
* Finds a field value by its name.
*
* @param name field name
* @return the field value ({@link Optional#empty()} if record doesn't contain that field),
* never {@code null}
*/
public Optional<String> findField(final String name) {
final int fieldPos = header.indexOf(name);
if (fieldPos == -1 || fieldPos >= csvRecord.getFieldCount()) {
return Optional.empty();
}
return Optional.of(csvRecord.getField(fieldPos));
}

/**
* Builds an unmodifiable and ordered map of header names and field values of this record.
* <p>
Expand Down

0 comments on commit 5ebb7d5

Please sign in to comment.