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

Runtime csv field mappings #52

Open
wants to merge 2 commits into
base: master
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
14 changes: 12 additions & 2 deletions LINQtoCSV/CsvContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ public class CsvContext
/// <param name="fileDescription">
/// Additional information how the input file is to be interpreted, such as the culture of the input dates.
/// </param>
/// <param name="fieldNameOverrides">Runtime overrides for field names in the CSV file</param>
/// <returns>
/// Values read from the stream or file.
/// </returns>
public IEnumerable<T> Read<T>(string fileName, CsvFileDescription fileDescription, Dictionary<string, string> fieldNameOverrides) where T : class, new()
{
// Note that ReadData will not be called right away, but when the returned
// IEnumerable<T> actually gets accessed.

IEnumerable<T> ie = ReadData<T>(fileName, null, fileDescription, fieldNameOverrides);
return ie;
}
public IEnumerable<T> Read<T>(string fileName, CsvFileDescription fileDescription) where T : class, new()
{
// Note that ReadData will not be called right away, but when the returned
Expand Down Expand Up @@ -81,7 +90,8 @@ public class CsvContext
private IEnumerable<T> ReadData<T>(
string fileName,
StreamReader stream,
CsvFileDescription fileDescription) where T : class, new()
CsvFileDescription fileDescription,
Dictionary<string, string> fieldNameOverrides = null) where T : class, new()
{
// If T implements IDataRow, then we're reading raw data rows
bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T));
Expand Down Expand Up @@ -169,7 +179,7 @@ private IEnumerable<T> ReadData<T>(

if (firstRow && fileDescription.FirstLineHasColumnNames)
{
if (!readingRawDataRows) { fm.ReadNames(row); }
if (!readingRawDataRows) { fm.ReadNames(row, fieldNameOverrides); }
}
else
{
Expand Down
12 changes: 9 additions & 3 deletions LINQtoCSV/FieldMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public FieldMapper_Reading(
/// <param name="firstRow"></param>
/// <returns></returns>
///
public void ReadNames(IDataRow row)
public void ReadNames(IDataRow row, Dictionary<string, string> fieldNameOverrides = null)
{
// It is now the order of the field names that determines
// the order of the elements in m_IndexToInfo, instead of
Expand All @@ -410,7 +410,7 @@ public void ReadNames(IDataRow row)

int currentNameIndex = 0;
for (int i = 0; i < row.Count; i++) {
if (!m_NameToInfo.ContainsKey(row[i].Value)) {
if (string.IsNullOrEmpty(row[i].Value) || !(m_NameToInfo.ContainsKey(row[i].Value) || (fieldNameOverrides != null && fieldNameOverrides.ContainsKey(row[i].Value) && m_NameToInfo.ContainsKey(fieldNameOverrides[row[i].Value])))) {
//If we have to ignore this column
if (m_fileDescription.IgnoreUnknownColumns) {
continue;
Expand All @@ -433,7 +433,13 @@ public void ReadNames(IDataRow row)
continue;
}

m_IndexToInfo[_mappingIndexes[i]] = m_NameToInfo[row[i].Value];
if (fieldNameOverrides == null || !fieldNameOverrides.ContainsKey(row[i].Value))
m_IndexToInfo[_mappingIndexes[i]] = m_NameToInfo[row[i].Value];
else
{
m_IndexToInfo[_mappingIndexes[i]] = m_NameToInfo[fieldNameOverrides[row[i].Value]];
}


if (m_fileDescription.EnforceCsvColumnAttribute && (!m_IndexToInfo[i].hasColumnAttribute)) {
// enforcing column attr, but this field/prop has no column attr.
Expand Down
13 changes: 13 additions & 0 deletions TestConsoleApplication/ProductDataDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TestConsoleApplication
{
public class ProductDataDto
{
public string Name { get; set; }

public string ShopsAvailable { get; set; }

public string HexProductCode { get; set; }

public string Description { get; set; }
}
}
20 changes: 20 additions & 0 deletions TestConsoleApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ static void Main(string[] args)

try
{
var productFieldOverrides = new Dictionary<string, string>()
{
{ "name", "Name" },
{ "shopsAvailable", "ShopsAvailable" },
{ "code", "HexProductCode" },
{ "description", "Description" },
};

fileDescription_namesUs.IgnoreUnknownColumns = true;

var productDtos = cc.Read<ProductDataDto>("../../TestFiles/goodfile_us.csv", fileDescription_namesUs,
productFieldOverrides);

foreach (var productDataDto in productDtos)
{
Console.WriteLine($"{productDataDto.Name}, {productDataDto.Description}, {productDataDto.HexProductCode}, {productDataDto.ShopsAvailable}");
}

fileDescription_namesUs.IgnoreUnknownColumns = false;

dataRows_namesUs =
cc.Read<ProductData>("../../TestFiles/goodfile_us.csv", fileDescription_namesUs);

Expand Down
1 change: 1 addition & 0 deletions TestConsoleApplication/TestConsoleApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ProductDataDto.cs" />
<Compile Include="ProductData_MissingFieldIndex.cs" />
<Compile Include="ProductData_DuplicateIndices.cs" />
<Compile Include="ProductData.cs" />
Expand Down