I'm trying to map some variable-format tab-separated files to my class, Foo:
public class Foo
{
public string A { get; set; }
public int? B { get; set; }
public double? C { get; set; }
}
File 1:
A B C
string 1 1.0
File 2:
Bee Sea
1 1.0
When the header format is constant, this is easy to do with CsvHelper using a CsvClassMap:
public sealed class FooMap : CsvClassMap<Foo>
{
public FooMap()
{
Map(x => x.A).Name("A");
Map(x => x.B).Name("B");
Map(x => x.C).Name("C");
}
}
However, this becomes complex when the header format is variable.
- The files can vary in width and height.
- Header order is not fixed.
- A property is only defined a maximum of once per file.
- Each property could relate to a variety of header names.
- Header names are exclusive to a single property.
- Not all properties are necessarily defined in a file.
What would be the best way of mapping to this object?
I imagine I will be populating a mapping table for headers to properties with a unique key on the headers column, then looking up each header's respective property.
Current direction of investigation:
public sealed class FooMap : CsvClassMap<Foo>
{
public FooMap()
{
}
public void SetHeaders(List<string> headers)
{
var dictionary = new Dictionary<string, List<string>>();
dictionary.Add("A", new List<string>() { "A", "Aay" });
dictionary.Add("B", new List<string>() { "B", "Bee" });
dictionary.Add("C", new List<string>() { "C", "Sea" });
...
}
}