Skip to content

Commit

Permalink
Start working on a DataTableParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Aug 22, 2024
1 parent b44b0b4 commit 8147628
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CSharp.Nixill/src/Grid/CSVParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Nixill.Collections.Grid.CSV
/// </description></item>
/// </list>
/// </summary>
public class CSVParser
public static class CSVParser
{
/// <summary>
/// Reads a CSV file into a Grid of strings.
Expand Down
60 changes: 60 additions & 0 deletions CSharp.Nixill/src/Grid/DataTableParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Nixill.Utils;

namespace Nixill.Collections.Grid.CSV;

/// <summary>
/// This class contains static methods to convert between DataTables and
/// comma separated value format files and text.
/// </summary>
public static class DataTableCSVParser
{
public static DataTable FileToDataTable(string path, IDictionary<string, DataColumn> columnDefs = null,
IDictionary<Type, Func<string, object>> deserializers = null, IEnumerable<string> primaryKey = null)
=> EnumerableToDataTable(FileUtils.FileCharEnumerator(path), columnDefs, deserializers, primaryKey);

public static DataTable StreamToDataTable(StreamReader reader, IDictionary<string, DataColumn> columnDefs = null,
IDictionary<Type, Func<string, object>> deserializers = null, IEnumerable<string> primaryKey = null)
=> EnumerableToDataTable(FileUtils.StreamCharEnumerator(reader), columnDefs, deserializers, primaryKey);

public static DataTable EnumerableToDataTable(IEnumerable<char> input,

Check failure on line 23 in CSharp.Nixill/src/Grid/DataTableParser.cs

View workflow job for this annotation

GitHub Actions / test

'DataTableCSVParser.EnumerableToDataTable(IEnumerable<char>, IDictionary<string, DataColumn>, IDictionary<Type, Func<string, object>>, IEnumerable<string>)': not all code paths return a value
IDictionary<string, DataColumn> columnDefs = null, IDictionary<Type, Func<string, object>> deserializers = null,
IEnumerable<string> primaryKey = null)
{
DataTable table = new();

columnDefs ??= new Dictionary<string, DataColumn>();
deserializers ??= new Dictionary<Type, Func<string, object>>();

var types = new List<Type>();

IEnumerable<IList<string>> rows = CSVParser.EnumerableToRows(input);
bool isHeaderRow = true;

foreach (var row in rows)
{
if (isHeaderRow)
{
foreach (string item in row)
{
if (columnDefs.TryGetValue(item, out DataColumn col))
{
table.Columns.Add(col);
types.Add(col.DataType);
}
else
{
table.Columns.Add(new DataColumn
{
ColumnName = item
});
types.Add(typeof(string));
}
}
}
}
}
}

0 comments on commit 8147628

Please sign in to comment.