Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICsvReader returns IEnumerable instead of List #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
11 changes: 7 additions & 4 deletions NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;

namespace Knapcode.NCsvPerf.CsvReadable.TestCases
Expand Down Expand Up @@ -43,14 +44,16 @@ private void Execute(ICsvReader reader)
{
var result = reader.GetRecords<PackageAsset>(memoryStream);

if (result.Count != LineCount)
if (_saveResult)
{
throw new InvalidDataException($"ICsvReader '{reader.GetType().FullName}' failed to produce correct number of rows. Expected: {LineCount}, actual: {result.Count}.");
result = LatestResult = result.ToList();
}

if (_saveResult)
var count = result.Count();

if (count != LineCount)
{
LatestResult = result;
throw new InvalidDataException($"ICsvReader '{reader.GetType().FullName}' failed to produce correct number of rows. Expected: {LineCount}, actual: {count}.");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion NCsvPerf/CsvReadable/ICsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Knapcode.NCsvPerf.CsvReadable
{
public interface ICsvReader
{
List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new();
IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new();
}
}
6 changes: 2 additions & 4 deletions NCsvPerf/CsvReadable/Implementations/Angara_Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ public override FSharpOption<Type> Invoke(Tuple<int, string> func)
}
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);

using (var reader = new StreamReader(stream))
{
var cols = new FSharpOption<FSharpFunc<Tuple<int, string>, FSharpOption<Type>>>(new Types());
var table = Table.Load(reader, new ReadSettings(Delimiter.Comma, false, false, FSharpOption<int>.None, cols));
var allRecords = new List<T>(table.RowsCount);
for (int r = 0; r < table.RowsCount; r++)
{
var item = activate();
item.Read(i => table[i].Rows.Item(r).AsString);
allRecords.Add(item);
yield return item;
}
return allRecords;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/CSVFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public CSVFile(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

var config = new global::CSVFile.CSVSettings
{
Expand All @@ -32,11 +31,9 @@ public CSVFile(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
12 changes: 5 additions & 7 deletions NCsvPerf/CsvReadable/Implementations/Cesil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@ public Cesil(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
// specialize for T == PackageAsset
return GetAssets(stream) as List<T>;
return GetAssets(stream) as IEnumerable<T>;
}

public List<PackageAsset> GetAssets(MemoryStream stream) {
var allRecords = new List<PackageAsset>();
public IEnumerable<PackageAsset> GetAssets(MemoryStream stream) {
using (var reader = new StreamReader(stream))
{
var config = Configuration.For<PackageAsset>();
var csv = config.CreateReader(reader);
foreach (var row in csv.EnumerateAll())
{
allRecords.Add(row);
// for some reason this final field is null for certain records
// which was causing tests to fail
if (row.PlatformVersion == null)
row.PlatformVersion = "";

yield return row;
}
}

return allRecords;
}
}
}
12 changes: 4 additions & 8 deletions NCsvPerf/CsvReadable/Implementations/ChoEtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ public ChoETL(ActivationMethod _)
{
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
return GetAssets(stream) as List<T>;
return GetAssets(stream) as IEnumerable<T>;
}

List<PackageAsset> GetAssets(MemoryStream stream)
IEnumerable<PackageAsset> GetAssets(MemoryStream stream)
{
var allRecords = new List<PackageAsset>();

var config = new global::ChoETL.ChoCSVRecordConfiguration
{
FileHeaderConfiguration = new global::ChoETL.ChoCSVFileHeaderConfiguration
Expand All @@ -37,10 +35,8 @@ List<PackageAsset> GetAssets(MemoryStream stream)
{
var asset = new PackageAsset();
asset.Read(i => record.GetString(i));
allRecords.Add(asset);
yield return asset;
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/CommonLibrary_Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public CommonLibrary_NET(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

string text;
using (var reader = new StreamReader(stream))
Expand All @@ -37,11 +36,9 @@ public CommonLibrary_NET(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/Csv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public Csv(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -33,11 +32,9 @@ public Csv(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/CsvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public CsvHelper(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -33,11 +32,9 @@ public CsvHelper(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => csvParser[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/CsvTextFieldParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public CsvTextFieldParser(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -29,11 +28,9 @@ public CsvTextFieldParser(ActivationMethod activationMethod)
var fields = parser.ReadFields();
var record = activate();
record.Read(i => fields[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
9 changes: 3 additions & 6 deletions NCsvPerf/CsvReadable/Implementations/CsvTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public CsvTools(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

if (stream.Length > 0)
{
Expand All @@ -30,17 +29,15 @@ public CsvTools(ActivationMethod activationMethod)
// Read header as a row.
var r = activate();
r.Read(i => table.ColumnNames.ElementAt(i));
allRecords.Add(r);
yield return r;

foreach (var row in table.Rows)
{
var record = activate();
record.Read(i => row.Values[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/Ctl_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public Ctl_Data(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var streamReader = new StreamReader(stream))
{
Expand All @@ -32,11 +31,9 @@ public Ctl_Data(ActivationMethod activationMethod)
// Empty fields are returned as null by this library. Convert that to empty string to be more
// consistent with other libraries.
record.Read(i => csvReader.CurrentRow[i].Value ?? string.Empty);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
2 changes: 1 addition & 1 deletion NCsvPerf/CsvReadable/Implementations/Cursively.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Cursively(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
using var vis = new Vis<T>(_activationMethod);
if (stream.TryGetBuffer(out var buf))
Expand Down
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/DSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public Dsv(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -30,11 +29,9 @@ public Dsv(ActivationMethod activationMethod)
{
var record = activate();
record.Read(i => row[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}

static IEnumerable<string> EnumerateLines(TextReader r)
Expand Down
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/FastCsvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ public FastCsvParser(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var parser = new global::FastCsvParser.CsvReader(stream, Encoding.UTF8))
{
while (parser.MoveNext())
{
var record = activate();
record.Read(i => parser.Current[i]);
allRecords.Add(record);
yield return record;
}
}

return allRecords;
}
}
}
7 changes: 2 additions & 5 deletions NCsvPerf/CsvReadable/Implementations/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public FileHelpers(ActivationMethod activationMethod)
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
public IEnumerable<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using (var reader = new StreamReader(stream))
{
Expand All @@ -36,12 +35,10 @@ public FileHelpers(ActivationMethod activationMethod)
// bind directly to the PackageAsset.
var record = activate();
record.Read(i => item.GetString(i));
allRecords.Add(record);
yield return record;
}
}
}

return allRecords;
}
[global::FileHelpers.DelimitedRecord(",")]
public class PackageAssetData
Expand Down
Loading