FlatFiles icon indicating copy to clipboard operation
FlatFiles copied to clipboard

Add support for .NET 6's DateOnly and TimeOnly in *TypeMapper

Open CyberBotX opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe. I don't know if I'd consider it a problem or not, but with the release of .NET 6, they introduced the types DateOnly and TimeOnly, but FlatFiles does not appear to have a way to handle those yet on the *TypeMapper classes (SeparatedValueTypeMapper and FixedLengthTypeMapper).

Describe the solution you'd like I'd like to see FlatFiles have support for the DateOnly and TimeOnly types of .NET 6, perhaps not just for the *TypeMapper classes but all throughout the library.

Describe alternatives you've considered The only alternative I've considered is to convert those types to DateTime to use the existing support in FlatFiles for that type. This is a bit painful, though, especially in the case of a nullable DateOnly or TimeOnly since the null-coalesce operator cannot be used. I also considered adding an extension method for the *TypeMapper classes, but that seems to be hard to accomplish with how many methods are private to those classes and not wanting to repeat too much code that is already in the library itself.

Additional context Since these types are .NET 6+ only, I do not know how easy it would be to support those alongside everything else.

CyberBotX avatar Jan 20 '22 20:01 CyberBotX

The alternative I described above of converting to DateTime doesn't actually work either because the *TypeMapper Property method expect an expression with just a flat property, so I can't use a full expression to convert the DateOnly or TimeOnly into a DateTime.

CyberBotX avatar Jan 20 '22 20:01 CyberBotX

The only working solution I've found thus far is to use a CustomMapping instead of Property, with the WithWriter handling the conversion from DateOnly or TimeOnly to DateTime. (In my particular case I didn't need a WithReader but the same would apply there.)

CyberBotX avatar Jan 20 '22 20:01 CyberBotX

Using CustomMapping was going to be my suggestion to you. I did some serious maintenance of this project late last year for a few months and ended up deciding not to release any of my changes (currently what's in master). I am at a cross-roads right now; I need to decide whether to invest a huge amount of effort making a 5.0 or just let this project slide into abyss of non-maintenance. Just trying to set your expectations...

Normally when I want to add support for a new type, I create sub-classes of ColumnDefinition<T>. For these classes, in particular, mimicking DateTimeColumn would be a good way to start. Then you need to add Property methods to the type mapper classes (delimited vs fixed-width).

For older versions of .NET, first I look to see if there's a NuGet package that makes it available. It's nice if a NuGet package will allow older environments to utilize newer functionality. In this case, I am not seeing anything published by MS, which is a bit surprising.

Since .NET 6 isn't something I am currently targeting, a new entry would need to be added in the FlatFiles.csproj. Then in the actual C# code, to conditionally include code depending on the .NET version, you need to wrap the code in a #if pragma (click here to see an example). For .NET 6, it looks like the compile-time constant is NET6_0, according to these docs.

I have been debating for a while if I should pursue separating the mapping exercise (a.k.a., "type mapper" stuff) from the actual act of parsing files. I feel like I could define a generic interface to grab values from a data source and provide the same service. That would allow the convenience of defining things via type mappers to work over non-FlatFiles classes. Take one of the more popular CSV libraries for .NET and just create an adapter that allowed type mappers to work. ¯_(ツ)_/¯ There's only about a zillion other improvements I'd like to make also, but I have zero time for anything.

jehugaleahsa avatar Jan 23 '22 01:01 jehugaleahsa

Is there any traction here? Would love to use DateOnly and TimeOnly

benjaminsampica avatar Aug 02 '23 18:08 benjaminsampica