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

Add TimeSpanColumn #38

Closed pbolduc closed 6 years ago

pbolduc commented 6 years ago

TimeSpan is a supported data type in a DataTable. It should be included.

I have started on this and will be submitting a pull request.

jehugaleahsa commented 6 years ago

Just a heads up. I am migrating most of the column classes to inherit from ColumnDefinition<T>, rather than the non-generic ColumnDefinition. It will deal with all of the preprocessing, null checks and value trimming consistently.

Primarily, I am adding a new property to the IColumnDefinition interface labeled IsNullable. By default, all columns will be nullable. However, the type mapper layer will discern between int and int?, for example, and set the IsNullable property automatically.

Doing the null checks at the column level, rather than in the type mappers will make null checking more universal for folks not using type mappers but also improves performance slightly while simplifying the code. Currently, I am simply throwing an InvalidCastException which will be caught by the new column error handling.

Going full circle, moving null checking into the column will make it easier for people trying to provide a default value for the Substitution property of the new ColumnErrorEventArgs class. They can write this code:

e.IsHandled = true;
var column = e.ColumnContext.ColumnDefinition;
e.Substitution = column.IsNullable ? null : Activator.CreateInstance(column.ColumnType);
pbolduc commented 6 years ago

Sounds good. I saw ColumnDefinition, but I just followed the other column types to maintain consistency. Thanks for the heads up.

jehugaleahsa commented 6 years ago

I went ahead and implemented this today. I added some helper functions/classes for automatically converting double columns to TimeSpan.

pbolduc commented 6 years ago

This is great! I did not have time to start on this yet at all. I noticed all the extra work you added for the DateTimeOffset column. I think FlatFiles now implements all the same column/data types that ADO.NET does (ie for IDataReader).

jehugaleahsa commented 6 years ago

For that reason, I just pushed out a 4.2.0 that provides GetDateTimeOffset and GetTimeSpan accessors on the FlatFileDataReader, along with the extensions methods.