jehugaleahsa / FlatFiles

Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed.
The Unlicense
357 stars 64 forks source link

Thanks for a great library. Docs need examples of using FixedLengthTypeMapper please. #60

Closed nhustak closed 5 years ago

nhustak commented 5 years ago

Trying to use FixedLengthTypeMapperSelector.

What is the equivalent of SeparatedValueReader for FixedLength?

`var selector = new FixedLengthTypeMapperSelector(); selector.When(values => values.Contains("#M")).Use(CreateMasterMapper()); selector.When(values => values.Contains("#V")).Use(CreateUnitMapper()); selector.When(values => values.Contains("#S")).Use(CreateSummonsMapper());

        var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        var readerStream = new StreamReader(fileStream);

        var reader = new ???????(readerStream, selector)

`

My mappers: private IFixedLengthTypeMapper CreateMasterSchema() { var mapper = FixedLengthTypeMapper.Define();

        mapper.Property(p => p.record_type, new FlatFiles.Window(2));
        mapper.Property(p => p.unit_records_expected, new FlatFiles.Window(3));
        mapper.Property(p => p.file_created_date, new FlatFiles.Window(6));
        mapper.Property(p => p.file_created_time, new FlatFiles.Window(4));

        return mapper;
    }

    private IFixedLengthTypeMapper<tPI_NA_Unit> CreateUnitSchema()
    {
        var mapper = FixedLengthTypeMapper.Define<tPI_NA_Unit>();

        mapper.Property(p => p.record_type, new FlatFiles.Window(2));
        mapper.Property(p => p.summons_count, new FlatFiles.Window(4));
        mapper.Property(p => p.handheld_unit_id, new FlatFiles.Window(1));
        mapper.Property(p => p.precinct_number, new FlatFiles.Window(1));

        return mapper;
    }

    private IFixedLengthTypeMapper<tPI_NA_Summons> CreateSummonsSchema()
    {
        var mapper = FixedLengthTypeMapper.Define<tPI_NA_Summons>();

        mapper.Property(p => p.record_type, new FlatFiles.Window(2));
        mapper.Property(p => p.ticket_number, new FlatFiles.Window(8));
        mapper.Property(p => p.action_code, new FlatFiles.Window(1));
        mapper.Property(p => p.violation_date, new FlatFiles.Window(6));
        mapper.Property(p => p.violation_time, new FlatFiles.Window(4));
        mapper.Property(p => p.township_code, new FlatFiles.Window(2));
        mapper.Property(p => p.townzips_code, new FlatFiles.Window(3));
        mapper.Property(p => p.location_of_violation, new FlatFiles.Window(25));
        mapper.Property(p => p.officer_serial_number, new FlatFiles.Window(6));
        mapper.Property(p => p.precinct, new FlatFiles.Window(2));
        mapper.Property(p => p.court_date, new FlatFiles.Window(6));
        mapper.Property(p => p.violation_code, new FlatFiles.Window(20));
        mapper.Property(p => p.fine, new FlatFiles.Window(3));
        mapper.Property(p => p.vehicle_make, new FlatFiles.Window(5));
        mapper.Property(p => p.vehicle_color, new FlatFiles.Window(4));
        mapper.Property(p => p.vehicle_model, new FlatFiles.Window(10));
        mapper.Property(p => p.vehicle_year, new FlatFiles.Window(4));
        mapper.Property(p => p.plate_number, new FlatFiles.Window(8));
        mapper.Property(p => p.plate_expiration, new FlatFiles.Window(6));
        mapper.Property(p => p.registration_type_code, new FlatFiles.Window(2));
        mapper.Property(p => p.VIN, new FlatFiles.Window(17));

        return mapper;
    }
jehugaleahsa commented 5 years ago

There's a version of the FixedLengthReader constructor accepting the selector: https://github.com/jehugaleahsa/FlatFiles/blob/master/FlatFiles/FixedLengthReader.cs#L41 The multiple schema example in the README is using SeparatedValueReader but it's the same if you use FixedLengthReader.

nhustak commented 5 years ago

@jehugaleahsa ARGH...I'm an idiot. I see now the reader COMES from the mapper type selector class. Sorry for wasting your time.

jehugaleahsa commented 5 years ago

Ah, took me a second to see what the problem was, too. Yeah, have to call GetReader. Hmm... I feel like there's an earlier way yet.

jehugaleahsa commented 5 years ago

GetReader returns an ITypedReader, that you can directly loop over the file with. It will give back objects instead of object arrays. You can just check the current object type as you read.

nhustak commented 5 years ago

Yep, thanks after I realized I was barking up the wrong tree on the reader I figured it out. I've used the separated versions quite a bit. This library is a huge time saver. Thank you for sharing it and taking the time to reply to my issues.