Parsing Enums

Sometimes it is neccessary to parse the CSV data into an enum, which can be done with an EnumConverter.

Example

Imagine we have a CSV file containing a vehicle, with a Name and a VehicleType. The VehicleType can only be a Car or a Bike.

VehicleType;Name
Car;Suzuki Swift
Bike;A Bike

It useful to represent the VehicleType as an enumeration in our C# code. So first define the enum in code:

private enum VehicleTypeEnum
{
    Car,
    Bike
}

Then define the class the results should be mapped to:

private class Vehicle
{
    public VehicleTypeEnum VehicleType { get; set; }

    public string Name { get; set; }
}

And now the mapping between the CSV File and the domain model has to be defined. For parsing the VehicleType a custom converter has to be used, which simply is a EnumConverter<VehicleTypeEnum, the constructor argument signals to ignore the case (upper-case/lower-case) for parsing the enum.

private class CsvVehicleMapping : CsvMapping<Vehicle>
{
    public CsvVehicleMapping()
    {
        MapProperty(0, x => x.VehicleType, new EnumConverter<VehicleTypeEnum>(true));
        MapProperty(1, x => x.Name);
    }
}

And then the CSV data can be parsed as usual:

[Test]
public void CustomEnumConverterTest()
{
    CsvParserOptions csvParserOptions = new CsvParserOptions(true, ';');
    CsvReaderOptions csvReaderOptions = new CsvReaderOptions(new[] { Environment.NewLine });
    CsvVehicleMapping csvMapper = new CsvVehicleMapping();
    CsvParser<Vehicle> csvParser = new CsvParser<Vehicle>(csvParserOptions, csvMapper);

    var stringBuilder = new StringBuilder()
        .AppendLine("VehicleType;Name")
        .AppendLine("Car;Suzuki Swift")
        .AppendLine("Bike;A Bike");

    var result = csvParser
        .ReadFromString(csvReaderOptions, stringBuilder.ToString())
        .ToList();

    Assert.AreEqual(VehicleTypeEnum.Car, result[0].Result.VehicleType);
    Assert.AreEqual("Suzuki Swift", result[0].Result.Name);

    Assert.AreEqual(VehicleTypeEnum.Bike, result[1].Result.VehicleType);
    Assert.AreEqual("A Bike", result[1].Result.Name);
}

How easy was that?