Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 67 additions & 29 deletions src/CsvHelper/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,50 @@ protected virtual void ValidateHeader(ClassMap map, List<InvalidHeader> invalidH
}
}

/// <summary>
/// Advances the reader to the header record. Does not skip over records and should not be used to read records.
/// You need to call <see cref="Read"/> then <see cref="ReadHeader"/>
/// for the headers to be read.
/// </summary>
/// <returns>True if there are more records, otherwise false.</returns>
public virtual bool PreHeaderRead()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code inside the methods PreHeaderRead & PreHeaderReadAsync almost same except the first line of both the methods. Can we move rest of the lines to a private sub method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I extracted the repeated behavior, which was also present in the Read and ReadAsync methods, to the UpdateColumnCount method.

{
// Don't forget about the async method below!

bool hasMoreData = parser.Read();
hasBeenRead = true;

currentIndex = -1;

if (detectColumnCountChanges && hasMoreData)
{
UpdateColumnCount();
}

return hasMoreData;
}

/// <summary>
/// Advances the reader to the header record. Does not skip over records and should not be used to read records.
/// You need to call <see cref="ReadAsync"/> then <see cref="ReadHeader"/>
/// for the headers to be read.
/// </summary>
/// <returns>True if there are more records, otherwise false.</returns>
public virtual async Task<bool> PreHeaderReadAsync()
{
bool hasMoreData = await parser.ReadAsync().ConfigureAwait(false);
hasBeenRead = true;

currentIndex = -1;

if (detectColumnCountChanges && hasMoreData)
{
UpdateColumnCount();
}

return hasMoreData;
}

/// <inheritdoc/>
public virtual bool Read()
{
Expand All @@ -254,18 +298,7 @@ public virtual bool Read()

if (detectColumnCountChanges && hasMoreRecords)
{
if (prevColumnCount > 0 && prevColumnCount != parser.Count)
{
var csvException = new BadDataException(string.Empty, parser.RawRecord, context, "An inconsistent number of columns has been detected.");

var args = new ReadingExceptionOccurredArgs(csvException);
if (readingExceptionOccurred?.Invoke(args) ?? true)
{
throw csvException;
}
}

prevColumnCount = parser.Count;
UpdateColumnCount();
}

return hasMoreRecords;
Expand All @@ -286,21 +319,26 @@ public virtual async Task<bool> ReadAsync()

if (detectColumnCountChanges && hasMoreRecords)
{
if (prevColumnCount > 0 && prevColumnCount != parser.Count)
{
var csvException = new BadDataException(string.Empty, parser.RawRecord, context, "An inconsistent number of columns has been detected.");
UpdateColumnCount();
}

var args = new ReadingExceptionOccurredArgs(csvException);
if (readingExceptionOccurred?.Invoke(args) ?? true)
{
throw csvException;
}
}
return hasMoreRecords;
}

private void UpdateColumnCount()
{
if (prevColumnCount > 0 && prevColumnCount != parser.Count)
{
var csvException = new BadDataException(string.Empty, parser.RawRecord, context, "An inconsistent number of columns has been detected.");

prevColumnCount = parser.Count;
var args = new ReadingExceptionOccurredArgs(csvException);
if (readingExceptionOccurred?.Invoke(args) ?? true)
{
throw csvException;
}
}

return hasMoreRecords;
prevColumnCount = parser.Count;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -843,7 +881,7 @@ public virtual IEnumerable<T> GetRecords<T>()

if (hasHeaderRecord && headerRecord == null)
{
if (!Read())
if (!PreHeaderRead())
{
yield break;
}
Expand Down Expand Up @@ -924,7 +962,7 @@ public virtual IEnumerable<object> GetRecords(Type type)

if (hasHeaderRecord && headerRecord == null)
{
if (!Read())
if (!PreHeaderRead())
{
yield break;
}
Expand Down Expand Up @@ -989,7 +1027,7 @@ public virtual IEnumerable<T> EnumerateRecords<T>(T record)

if (hasHeaderRecord && headerRecord == null)
{
if (!Read())
if (!PreHeaderRead())
{
yield break;
}
Expand Down Expand Up @@ -1046,7 +1084,7 @@ public virtual IEnumerable<T> EnumerateRecords<T>(T record)

if (hasHeaderRecord && headerRecord == null)
{
if (!await ReadAsync().ConfigureAwait(false))
if (!await PreHeaderReadAsync().ConfigureAwait(false))
{
yield break;
}
Expand Down Expand Up @@ -1128,7 +1166,7 @@ public virtual async IAsyncEnumerable<object> GetRecordsAsync(Type type, [Enumer

if (hasHeaderRecord && headerRecord == null)
{
if (!await ReadAsync().ConfigureAwait(false))
if (!await PreHeaderReadAsync().ConfigureAwait(false))
{
yield break;
}
Expand Down Expand Up @@ -1194,7 +1232,7 @@ public virtual async IAsyncEnumerable<T> EnumerateRecordsAsync<T>(T record, [Enu

if (hasHeaderRecord && headerRecord == null)
{
if (!await ReadAsync().ConfigureAwait(false))
if (!await PreHeaderReadAsync().ConfigureAwait(false))
{
yield break;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/CsvHelper.Tests/Reading/ShouldSkipRecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,36 @@ public void ShouldSkipWithEmptyRows()
Assert.Equal("1", csv.GetField(0));
Assert.Equal("2", csv.GetField(1));
}

[Fact]
public void ShouldSkipIgnoreHeader()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = skipArgs => !skipArgs.Row[0]!.StartsWith("A")
};

var parser = new ParserMock(config)
{
{ "Name" },
{ "Arnold" },
{ "Andrew" },
{ "Betty" },
{ "Frank" }
};

var csv = new CsvReader(parser);
var csvReader = new CsvReader(parser);
var typeDef = new
{
Name = string.Empty
};

var records = csvReader.GetRecords(typeDef).ToList();

Assert.Equal(2, records.Count);
Assert.Equal("Arnold", records[0].Name);
Assert.Equal("Andrew", records[1].Name);
}
}
}