-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Upgrade for CsvHelper v22.x * Fixed new-line for unit test
- Loading branch information
Showing
6 changed files
with
85 additions
and
57 deletions.
There are no files selected for viewing
49 changes: 35 additions & 14 deletions
49
CsvHelper.FastDynamic.Performance/Internal/CsvReaderExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,57 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CsvHelper.FastDynamic.Performance.Internal | ||
{ | ||
internal static class CsvReaderExtension | ||
{ | ||
internal static IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords(this CsvReader csvReader) | ||
=> csvReader.EnumerateDictionaryRecords().ToArray(); | ||
|
||
internal static IEnumerable<IDictionary<string, object>> EnumerateDictionaryRecords(this CsvReader csvReader) | ||
{ | ||
// Read Header | ||
csvReader.Read(); | ||
csvReader.ReadHeader(); | ||
if (csvReader.Configuration.HasHeaderRecord && csvReader.HeaderRecord == null) | ||
{ | ||
if (!csvReader.Read()) | ||
{ | ||
yield break; | ||
} | ||
|
||
var headerRecord = csvReader.Context | ||
.HeaderRecord | ||
csvReader.ReadHeader(); | ||
} | ||
|
||
var headerRecord = csvReader.HeaderRecord | ||
.Select((x, i) => csvReader.Configuration.PrepareHeaderForMatch(x, i)) | ||
.ToArray(); | ||
|
||
var result = new List<IDictionary<string, object>>(); | ||
|
||
while (csvReader.Read()) | ||
{ | ||
var record = new Dictionary<string, object>(headerRecord.Length); | ||
Dictionary<string, object> record; | ||
|
||
for (int i = 0; i < headerRecord.Length; i++) | ||
try | ||
{ | ||
record[headerRecord[i]] = csvReader[i]; | ||
record = new Dictionary<string, object>(headerRecord.Length); | ||
|
||
for (int i = 0; i < headerRecord.Length; i++) | ||
{ | ||
record[headerRecord[i]] = csvReader.Parser[i]; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
var readerException = new ReaderException(csvReader.Context, "An unexpected error occurred.", ex); | ||
|
||
result.Add(record); | ||
} | ||
if (csvReader.Configuration.ReadingExceptionOccurred?.Invoke(readerException) ?? true) | ||
{ | ||
throw readerException; | ||
} | ||
|
||
return result; | ||
continue; | ||
} | ||
|
||
yield return record; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters