Search code examples
csvhelpercsvreader

Can CsvReader use different Record Type Maps based upon column count?


I have a bunch of CSV files that I need to read that are similar except for the addition of a column. Essentially, the old CSV format has 7 columns, and the new will have 8. No headers. Once read, the new column will be defaulted/edited and then the file will have 8 columns when written out - so future reads will now have the new column.

So I'm trying to do something like this CvsHelper example: Reading Multiple Record Types

except the only difference is in the number of columns in the file - not an identifying field value.

I tried a CsvParser in its own using() with a single Read() of the first line of the file to ascertain the column count with the idea that a CsvReader in a subsequent using() could register a count specific ClassMap for the CsvReader.GetRecords. However there doesn't seem to be a property or method that answers the number of columns that the parser has encountered. Same with the CsvReader.Read() - the CsvReader.Context.ColumnCount is zero after the Read(). Any ideas / pointers appreciated.

Update

Here's my revised code (long names, I know) - thanks for the suggestion David:

namespace TopazDomain.Classes
{
    public class FootprintComponentMap : Entity<int>
    {
        private const int footprintWidth = 40;
        [StringLength(footprintWidth)]
        public string Footprint { get; set; }

        private const int valueWidth = 40;
        [StringLength(valueWidth)]
        public string Value { get; set; }

        public int Component { get; set; }

        public int Head { get; set; }

        private const int rotationWidth = 9;
        [StringLength(rotationWidth)]
        public string CompR { get; set; }

        private const int polarizedWidth = 2;
        [StringLength(polarizedWidth)]
        public string Polarized { get; set; }

        private const int commentWidth = 40;
        public string Comment { get; set; }

        [StringLength(rotationWidth)]
        public string LibR { get; set; }

        // navigation
        [Required]
        public FootprintComponentMapFile FootprintComponentMapFile { get; set; }

        public FootprintComponentMap()
        {
            Footprint = String.Empty;
            Value = String.Empty;
            Component = 0;
            Head = 0;
            CompR = "0.00";
            Polarized = "N";
            Comment = String.Empty;
            LibR = "0.00";
        }

        public FootprintComponentMap(FootprintComponentMap footprintComponentMap) : this()
        {
            Footprint = footprintComponentMap.Footprint;
            Value = footprintComponentMap.Value;
            Component = footprintComponentMap.Component;
            Head = footprintComponentMap.Head;
            CompR = footprintComponentMap.CompR;
            Polarized = footprintComponentMap.Polarized;
            Comment = footprintComponentMap.Comment;
            LibR = footprintComponentMap.LibR;
        }

        public FootprintComponentMap(string footprint, string value, int componentNumber, int headNumber, string componentR,
            string polarized, string libraryR, string comment) : this()
        {
            Footprint = footprint;
            Value = value;
            Component = componentNumber;
            Head = headNumber;
            CompR = componentR;
            Polarized = polarized;
            Comment = comment;
            LibR = libraryR;
        }
    }
}

namespace TopazDomain.Classes.Extensions
{
    public static class FootprintComponentMapFileExtensions
    {
        public sealed class FootprintComponentReadMapper : ClassMap<FootprintComponentMap>
        {
            public FootprintComponentReadMapper()
            {
                Map(m => m.Footprint).Index(0);
                Map(m => m.Value).Index(1);
                Map(m => m.Component).Index(2);
                Map(m => m.Head).Index(3);
                Map(m => m.CompR).Index(4);
                Map(m => m.Polarized).Index(5);
                Map(m => m.Comment).Index(6);
                Map(m => m.LibR).ConvertUsing(row =>
                {
                    if (row.TryGetField(7, out string field))
                    {
                        return field;
                    }
                    else
                    {
                        return String.Empty;
                    }
                });
            }
        }

        public sealed class FootprintComponentWriteMapper : ClassMap<FootprintComponentMap>
        {
            public FootprintComponentWriteMapper()
            {
                Map(m => m.Footprint).Index(0);
                Map(m => m.Value).Index(1);
                Map(m => m.Component).Index(2);
                Map(m => m.Head).Index(3);
                Map(m => m.CompR).Index(4);
                Map(m => m.Polarized).Index(5);
                Map(m => m.Comment).Index(6);
                Map(m => m.LibR).Index(7);
            }
        }

        public static bool ImportFootprintComponentMaps(this FootprintComponentMapFile footprintComponentMapFile,
            string filename)
        {
            bool done = false;

            using (var lineReader = new LineReader(filename))
            using (var csv = new CsvReader(lineReader, CultureInfo.InvariantCulture))
            {
                csv.Configuration.RegisterClassMap<FootprintComponentReadMapper>();

                footprintComponentMapFile.FootprintComponentMaps = csv.GetRecords<FootprintComponentMap>().ToList();

                done = true;
            }

            return done;
        }

        public static bool ExportFootprintComponentMaps(this FootprintComponentMapFile footprintComponentMapFile,
            string filename)
        {
            bool done = false;

            CsvConfiguration cvsConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture);
            cvsConfiguration.HasHeaderRecord = false;

            using (var lineWriter = new StreamWriter(filename))
            using (var csv = new CsvWriter(lineWriter, cvsConfiguration))
            {
                csv.Configuration.RegisterClassMap<FootprintComponentWriteMapper>();

                csv.WriteRecords(footprintComponentMapFile.FootprintComponentMaps);

                done = true;
            }

            return done;
        }
    }
}

Solution

  • You could have a new object with the 8 properties and map the 8th column conditionally with ConvertUsing.

    public class OldFoo
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
    }
    
    public class NewFoo 
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class NewFooClassMap : ClassMap<NewFoo>
    {
        public NewFooClassMap()
        {
            Map(m => m.Id).Index(0);
            Map(m => m.FirstName).Index(1);
            Map(m => m.LastName).ConvertUsing(row => {                
                if(row.TryGetField(2, out string field))
                {
                    return field;
                }
                else
                {
                    return null;
                }
            });
        }
    }
    
    public class Program
    {
        public static void Main(string[] args)
        {
            using (MemoryStream stream = new MemoryStream())
            using (StreamWriter writer = new StreamWriter(stream))
            using (StreamReader reader = new StreamReader(stream))
            using (CsvReader csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                writer.WriteLine("1,Bob");
                writer.WriteLine("2,Davey");
                writer.Flush();
                stream.Position = 0;
    
                csv.Configuration.RegisterClassMap<NewFooClassMap>();
                csv.Configuration.HasHeaderRecord = false;
    
                var records = csv.GetRecords<NewFoo>().ToList();
    
            }
    
            using (MemoryStream stream = new MemoryStream())
            using (StreamWriter writer = new StreamWriter(stream))
            using (StreamReader reader = new StreamReader(stream))
            using (CsvReader csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                writer.WriteLine("1,Bob,Barker");
                writer.WriteLine("2,Davey,Jones");
                writer.Flush();
                stream.Position = 0;
    
                csv.Configuration.RegisterClassMap<NewFooClassMap>();
                csv.Configuration.HasHeaderRecord = false;
    
                var records = csv.GetRecords<NewFoo>().ToList();
    
            }
        }
    }