-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for .NET 6's DateOnly and TimeOnly in *TypeMapper #80
Comments
The alternative I described above of converting to |
The only working solution I've found thus far is to use a |
Using Normally when I want to add support for a new type, I create sub-classes of 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 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. |
Is there any traction here? Would love to use |
I'd also like to see first-class support for When consumed, it looks indistinguishable from the rest of the interface: mapper.Property(x => x.SomeDateOnly, 8); // uses default yyyyMMdd format
mapper.Property(x => x.SomeDateOnly, 8, "yyyy-MM-dd"); // accepts custom format The extension method: /// <summary>
/// Associates the property with the type mapper and returns an object for configuration.
/// Provides a workaround to the lack of native <see cref="DateOnly"/> support.
/// </summary>
/// <typeparam name="TEntity">The type of the entity whose properties will be mapped.</typeparam>
/// <param name="mapper">The type mapper being configured.</param>
/// <param name="accessor">An expression representing the DateOnly property being mapped.</param>
/// <param name="window">Specifies how the fixed-width column appears in a flat file.</param>
/// <returns>The configured type mapper.</returns>
/// <exception cref="ArgumentException">Thrown if the property being mapped is not valid.</exception>
public static IFixedLengthTypeMapper<TEntity> Property<TEntity>(
this IFixedLengthTypeMapper<TEntity> mapper,
Expression<Func<TEntity, DateOnly>> accessor,
Window window,
string dateFormat = "yyyyMMdd"
)
{
var memberExpression = (MemberExpression)accessor.Body;
var columnName = memberExpression.Member.Name;
if (string.IsNullOrWhiteSpace(columnName))
{
throw new ArgumentException("Invalid property accessor", nameof(accessor));
}
var column = new DateTimeColumn(columnName)
{
InputFormat = dateFormat,
OutputFormat = dateFormat,
};
mapper.CustomMapping(column, window)
.WithReader((entity, raw) =>
{
if (raw is not DateTime dateTimeValue)
{
throw new TypeConversionException(typeof(DateTime), typeof(DateOnly));
}
var value = DateOnly.FromDateTime(dateTimeValue);
var property = memberExpression.Member as PropertyInfo;
property?.SetValue(entity, value, null);
})
.WithWriter((_, entity) =>
{
var compiled = accessor.Compile();
var dateOnlyValue = compiled.Invoke(entity);
return dateOnlyValue.ToString(dateFormat, CultureInfo.CurrentCulture);
});
return mapper;
} |
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
andTimeOnly
, but FlatFiles does not appear to have a way to handle those yet on the *TypeMapper classes (SeparatedValueTypeMapper
andFixedLengthTypeMapper
).Describe the solution you'd like
I'd like to see FlatFiles have support for the
DateOnly
andTimeOnly
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 nullableDateOnly
orTimeOnly
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.
The text was updated successfully, but these errors were encountered: