Skip to content

Commit

Permalink
CSVParser.cs: Continue the implementation of issue #5. Pending more t…
Browse files Browse the repository at this point in the history
…ests.
  • Loading branch information
arkaragian committed Jan 30, 2022
1 parent b131fed commit 5577ad1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
31 changes: 24 additions & 7 deletions AlphaCSV/CSVParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ public DataTable ParseDefinedCSV(DataTable schema, string path, CSVParseOptions
return table;
}

/// <summary>
/// Parses a CSV file containing values for the public set properties of the specified type
/// </summary>
/// <typeparam name="T">The type that we will receive</typeparam>
/// <param name="path">The path of the CSV File</param>
/// <param name="options">CSV Parsing options</param>
/// <param name="validationPatterns">Validation Patterns</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public List<T> ParseType<T>(string path, CSVParseOptions options = null, List<string> validationPatterns = null) {
Type genType = typeof(T);
ConstructorInfo constructor = genType.GetConstructor(new Type[] { });
Expand All @@ -185,18 +194,26 @@ public List<T> ParseType<T>(string path, CSVParseOptions options = null, List<st
}
}

DataTable schema = new DataTable();
for (int i = 0; i < propertNames.Count; i++) {
schema.Columns.Add(new DataColumn(propertNames[i], propertyTypes[i]));
}
DataTable table = ParseSimpleCSV(path, options, validationPatterns);

DataTable table = ParseDefinedCSV(schema, path, options, validationPatterns);
if (table.Columns.Count != propertyTypes.Count) {
throw new InvalidOperationException($"The number of parsed columns ({table.Columns.Count}) do not match the number of Type properties {propertyTypes.Count}");
}

List<T> result = new List<T>(table.Rows.Count);
foreach (DataRow row in table.Rows) {
object GenericInstance = constructor.Invoke(null);
for (int i = 0; i < propertySetMethods.Count; i++) {
propertySetMethods[i].Invoke(GenericInstance, new object[] { row[i] });
for (int i = 0; i < table.Columns.Count; i++) {
string name = table.Columns[i].ColumnName;
//We cannot be sure that the CSV field order is correct especially when dealing with derived classes.
//Thus we need to read the file first and then correlate the column name to the field of the class that we
//need to instantiate.
int indexToUse = propertNames.IndexOf(name);
if (indexToUse == -1) {
throw new InvalidOperationException($"There is no property with name {name}");
}
object covertedValue = Convert.ChangeType(row[i], propertyTypes[indexToUse]);
propertySetMethods[indexToUse].Invoke(GenericInstance, new object[] { covertedValue });
}
result.Add((T)GenericInstance);
}
Expand Down
33 changes: 33 additions & 0 deletions TestAlphaCSV/CSVReaderTests.ClassParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public record TestRecord {
public string Surname { get; set; }
}

public record DerivedRecord : TestRecord {
public string FatherName { get; set; }
}



[TestMethod]
public void TestSimpleClassParsing() {
Expand All @@ -39,6 +44,34 @@ public void TestSimpleClassParsing() {
List<TestRecord> actual = parser.ParseType<TestRecord>(path);


//Assert
CollectionAssert.AreEqual(expected, actual);
}

[TestMethod]
public void TestDerivedClass() {
string input = "Name,Surname,FatherName\nJohn,Doe,Donald\n";

//Arrange
MockFileSystem fs = new MockFileSystem();
MockFileData mockInputFile = new MockFileData(input);
string path = @"C:\test.csv";
fs.AddFile(path, mockInputFile);

DerivedRecord rc = new DerivedRecord {
Name = "John",
Surname = "Doe",
FatherName = "Donald"
};
List<DerivedRecord> expected = new List<DerivedRecord> { rc };


//Act
CSVParser parser = new CSVParser(fs); //Inject dependency here
//Since we have the expected result we just clone the schema instead of building it by hand.
List<DerivedRecord> actual = parser.ParseType<DerivedRecord>(path);


//Assert
CollectionAssert.AreEqual(expected, actual);
}
Expand Down

0 comments on commit 5577ad1

Please sign in to comment.