mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Support generic `IQueryable<T>` mapping

Open latonz opened this issue 2 years ago • 6 comments

Support for generic IQueryable<T> mapping methods:

public static partial System.Linq.IQueryable<TTarget> Map<TSource, TTarget>(System.Linq.IQueryable<TSource> source);

private B Map(A source);
private D Map(C source);

// generates
public static partial System.Linq.IQueryable<TTarget> Map<TSource, TTarget>(System.Linq.IQueryable<TSource> source)
{
    return source switch
    {
        IQueryable<global::A> x when typeof(TTarget).IsAssignableFrom(typeof(global::B)) => source.Select(...),
        IQueryable<global::C> x when typeof(TTarget).IsAssignableFrom(typeof(global::D)) => source.Select(...),
        _ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {typeof(IQueryable<TTarget>)} as there is no known type mapping", nameof(source)),
    };
}

Similar to the existing generic mapping methods (docs), type constraints should also be supported.

Brought up by @19bartek92 in https://github.com/riok/mapperly/discussions/960#discussion-5910036

latonz avatar Dec 02 '23 14:12 latonz

Thanks for this, i briefly look into it. Are switch expressions supported for IQueryable? ### Also should the source parameter be IQueryable<object>, can't look at the tests or source code rn

TimothyMakkison avatar Dec 02 '23 15:12 TimothyMakkison

@TimothyMakkison The switch expression doesn't happen inside the Expression/Select, therefore I think it should work. We should support the same feature set as we are supporting for regular generic mappings (where possible), therefore we should support IQueryable<T> as well as IQueryable<object> as source.

latonz avatar Dec 02 '23 15:12 latonz

@TimothyMakkison The switch expression doesn't happen inside the Expression/Select, therefore I think it should work.

Thanks, I was a little confused because the docs only mentioned the object source for generic mapping.

We should support the same feature set as we are supporting for regular generic mappings (where possible), therefore we should support IQueryable<T> as well as IQueryable<object> as source.

Might be worth creating an issue to track all the variants for this and existing target mapping. I have an idea for IQueryable<TSource> but haven't looked into the others

TimothyMakkison avatar Dec 02 '23 15:12 TimothyMakkison

@TimothyMakkison I think existing target mapping isn't relevant for IQueryable, is it?

latonz avatar Dec 02 '23 15:12 latonz

Sorry, I was suggesting that an issue needs to track each variant of generic mapping for IQueryable and for generic existing target mapping

TimothyMakkison avatar Dec 02 '23 15:12 TimothyMakkison

Ah got it, I'll probably create one if needed after the initial implementation. Probably it's easy enough to implement all here in the first version of the implementation.

latonz avatar Dec 02 '23 15:12 latonz

:tada: This issue has been resolved in version 3.5.0-next.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Mar 27 '24 12:03 github-actions[bot]