Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleans up the csv reader

  • Loading branch information...
commit 1ce1d7229008bfb1c4d00c9959c84b8aea39c139 1 parent f7b9581
@jmarnold jmarnold authored
View
46 src/FubuCore.Testing/Csv/CsvFileReaderIntegratedTester.cs
@@ -1,46 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using FubuCore.Binding;
-using FubuCore.Csv;
-using FubuTestingSupport;
-using NUnit.Framework;
-
-namespace FubuCore.Testing.Csv
-{
- [TestFixture]
- public class CsvFileReaderIntegratedTester
- {
- private CsvFileReader<TestCsvObject> theReader;
- private IObjectResolver theResolver;
- private IList<TestCsvObject> theResults;
-
- [SetUp]
- public void SetUp()
- {
- using (var writer = new StreamWriter("test.csv"))
- {
- writer.WriteLine("name,count,csv");
- writer.WriteLine("test1,true,1");
- writer.WriteLine("test2,false,2");
- }
-
- theReader = new CsvFileReader<TestCsvObject>(new TestCsvMapping());
- theResolver = ObjectResolver.Basic();
-
- theResults = theReader.Read(theResolver, "test.csv").ToList();
- }
-
- [Test]
- public void reads_the_objects()
- {
- theResults[0].Name.ShouldEqual("test1");
- theResults[0].Flag.ShouldBeTrue();
- theResults[0].Count.ShouldEqual(1);
-
- theResults[1].Name.ShouldEqual("test2");
- theResults[1].Flag.ShouldBeFalse();
- theResults[1].Count.ShouldEqual(2);
- }
- }
-}
View
46 src/FubuCore.Testing/Csv/CsvReaderIntegratedTester.cs
@@ -1,46 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using FubuCore.Binding;
-using FubuCore.Csv;
-using FubuTestingSupport;
-using NUnit.Framework;
-
-namespace FubuCore.Testing.Csv
-{
- [TestFixture]
- public class CsvReaderIntegratedTester
- {
- private CsvReader theReader;
- private IList<TestCsvObject> theObjects;
-
- [SetUp]
- public void SetUp()
- {
- using (var writer = new StreamWriter("test.csv"))
- {
- writer.WriteLine("name,count,csv");
- writer.WriteLine("test1,true,1");
- writer.WriteLine("test2,false,2");
- }
-
- theReader = new CsvReader(ObjectResolver.Basic());
-
- theObjects = new List<TestCsvObject>();
-
- var theMap = new TestCsvMapping();
- theReader.Read("test.csv", theMap, o => theObjects.Add(o));
- }
-
- [Test]
- public void reads_the_objects()
- {
- theObjects[0].Name.ShouldEqual("test1");
- theObjects[0].Flag.ShouldBeTrue();
- theObjects[0].Count.ShouldEqual(1);
-
- theObjects[1].Name.ShouldEqual("test2");
- theObjects[1].Flag.ShouldBeFalse();
- theObjects[1].Count.ShouldEqual(2);
- }
- }
-}
View
95 src/FubuCore.Testing/Csv/when_process_a_csv_file_with_headers.cs
@@ -0,0 +1,95 @@
+using System.Collections.Generic;
+using System.IO;
+using FubuCore.Binding;
+using FubuCore.Csv;
+using FubuTestingSupport;
+using NUnit.Framework;
+
+namespace FubuCore.Testing.Csv
+{
+ [TestFixture]
+ public class when_process_a_csv_file_with_headers
+ {
+ private CsvReader theReader;
+ private IList<TestCsvObject> theObjects;
+
+ [SetUp]
+ public void SetUp()
+ {
+ using (var writer = new StreamWriter("test.csv"))
+ {
+ writer.WriteLine("name,count,csv");
+ writer.WriteLine("test1,true,1");
+ writer.WriteLine("test2,false,2");
+ }
+
+ theReader = new CsvReader(ObjectResolver.Basic());
+
+ theObjects = new List<TestCsvObject>();
+
+ var theRequest = new CsvRequest<TestCsvObject>
+ {
+ FileName = "test.csv",
+ Callback = o => theObjects.Add(o),
+ Mapping = new TestCsvMapping()
+ };
+
+ theReader.Read(theRequest);
+ }
+
+ [Test]
+ public void reads_the_objects()
+ {
+ theObjects[0].Name.ShouldEqual("test1");
+ theObjects[0].Flag.ShouldBeTrue();
+ theObjects[0].Count.ShouldEqual(1);
+
+ theObjects[1].Name.ShouldEqual("test2");
+ theObjects[1].Flag.ShouldBeFalse();
+ theObjects[1].Count.ShouldEqual(2);
+ }
+ }
+
+ [TestFixture]
+ public class when_process_a_csv_file_without_headers
+ {
+ private CsvReader theReader;
+ private IList<TestCsvObject> theObjects;
+
+ [SetUp]
+ public void SetUp()
+ {
+ using (var writer = new StreamWriter("test-no-headers.csv"))
+ {
+ writer.WriteLine("test1,true,1");
+ writer.WriteLine("test2,false,2");
+ }
+
+ theReader = new CsvReader(ObjectResolver.Basic());
+
+ theObjects = new List<TestCsvObject>();
+
+ var theRequest = new CsvRequest<TestCsvObject>
+ {
+ FileName = "test-no-headers.csv",
+ IgnoreColumnHeaders = true,
+ Callback = o => theObjects.Add(o),
+ Mapping = new TestCsvMapping()
+ };
+
+ theReader.Read(theRequest);
+ }
+
+ [Test]
+ public void reads_the_objects()
+ {
+ theObjects[0].Name.ShouldEqual("test1");
+ theObjects[0].Flag.ShouldBeTrue();
+ theObjects[0].Count.ShouldEqual(1);
+
+ theObjects[1].Name.ShouldEqual("test2");
+ theObjects[1].Flag.ShouldBeFalse();
+ theObjects[1].Count.ShouldEqual(2);
+ }
+ }
+}
View
3  src/FubuCore.Testing/FubuCore.Testing.csproj
@@ -77,8 +77,7 @@
<Compile Include="Binding\ConverterFamilyTester.cs" />
<Compile Include="Binding\ConvertProblemTester.cs" />
<Compile Include="Csv\ColumnMappingTester.cs" />
- <Compile Include="Csv\CsvFileReaderIntegratedTester.cs" />
- <Compile Include="Csv\CsvReaderIntegratedTester.cs" />
+ <Compile Include="Csv\when_process_a_csv_file_with_headers.cs" />
<Compile Include="Csv\TestCsvObject.cs" />
<Compile Include="Binding\descriptors_on_all_binding_support_classes.cs" />
<Compile Include="Binding\EnumerateFlatRequestDataTester.cs" />
View
37 src/FubuCore/Csv/CsvFileReader.cs
@@ -1,37 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using FubuCore.Binding;
-
-namespace FubuCore.Csv
-{
- public class CsvFileReader<T>
- {
- private ColumnMapping<T> _mapping;
-
- public CsvFileReader(ColumnMapping<T> mapping)
- {
- _mapping = mapping;
- }
-
- public IEnumerable<T> Read(IObjectResolver resolver, string fileName)
- {
- using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
- {
- using (var reader = new StreamReader(stream))
- {
- var headers = reader.ReadLine();
- if (headers.IsEmpty()) yield break;
-
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- var source = _mapping.As<IValueSourceProvider>().Build(line);
- var result = resolver.BindModel(typeof (T), source);
-
- yield return result.Value.As<T>();
- }
- }
- }
- }
- }
-}
View
44 src/FubuCore/Csv/CsvReader.cs
@@ -0,0 +1,44 @@
+using System.IO;
+using FubuCore.Binding;
+
+namespace FubuCore.Csv
+{
+ public class CsvReader : ICsvReader
+ {
+ private readonly IObjectResolver _resolver;
+
+ public CsvReader(IObjectResolver resolver)
+ {
+ _resolver = resolver;
+ }
+
+ public void Read<T>(CsvRequest<T> request)
+ {
+ using (var stream = new FileStream(request.FileName, FileMode.Open, FileAccess.Read))
+ {
+ using (var reader = new StreamReader(stream))
+ {
+ if (!request.IgnoreColumnHeaders)
+ {
+ var headers = reader.ReadLine();
+ if (headers.IsEmpty()) return;
+ }
+
+ processData(reader, request);
+ }
+ }
+ }
+
+ private void processData<T>(StreamReader reader, CsvRequest<T> request)
+ {
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ var source = request.Mapping.As<IValueSourceProvider>().Build(line);
+ var result = _resolver.BindModel(typeof(T), source);
+
+ request.Callback(result.Value.As<T>());
+ }
+ }
+ }
+}
View
12 src/FubuCore/Csv/CsvRequest.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace FubuCore.Csv
+{
+ public class CsvRequest<T>
+ {
+ public string FileName { get; set; }
+ public bool IgnoreColumnHeaders { get; set; }
+ public ColumnMapping<T> Mapping { get; set; }
+ public Action<T> Callback { get; set; }
+ }
+}
View
24 src/FubuCore/Csv/ICsvReader.cs
@@ -1,29 +1,7 @@
-using System;
-using FubuCore.Binding;
-
namespace FubuCore.Csv
{
public interface ICsvReader
{
- void Read<T>(string fileName, ColumnMapping<T> mapping, Action<T> continuation);
- }
-
- public class CsvReader : ICsvReader
- {
- private readonly IObjectResolver _resolver;
-
- public CsvReader(IObjectResolver resolver)
- {
- _resolver = resolver;
- }
-
- public void Read<T>(string fileName, ColumnMapping<T> mapping, Action<T> continuation)
- {
- var reader = new CsvFileReader<T>(mapping);
- foreach (var item in reader.Read(_resolver, fileName))
- {
- continuation(item);
- }
- }
+ void Read<T>(CsvRequest<T> request);
}
}
View
3  src/FubuCore/FubuCore.csproj
@@ -159,7 +159,8 @@
<Compile Include="Conversion\TimeZoneConverter.cs" />
<Compile Include="Conversion\TypeDescripterConverterFamily.cs" />
<Compile Include="Csv\ColumnMapping.cs" />
- <Compile Include="Csv\CsvFileReader.cs" />
+ <Compile Include="Csv\CsvReader.cs" />
+ <Compile Include="Csv\CsvRequest.cs" />
<Compile Include="Csv\ICsvReader.cs" />
<Compile Include="Csv\IValueSourceProvider.cs" />
<Compile Include="Dates\Clock.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.