Skip to content

Commit

Permalink
Add Mapping to Immutable Array & List
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed committed Dec 3, 2020
1 parent a3dd6f0 commit 316035c
Show file tree
Hide file tree
Showing 8 changed files with 806 additions and 24 deletions.
157 changes: 157 additions & 0 deletions Source/Boxed.Mapping/AsyncImmutableMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace Boxed.Mapping
{
using System;
using System.Collections.Generic;
#if NET5_0
using System.Collections.Immutable;
#endif
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -793,6 +796,160 @@ public static async Task<HashSet<TDestination>> MapHashSetAsync<TSource, TDestin
return destination;
}

#if NET5_0
/// <summary>
/// Maps the list of <typeparamref name="TSource"/> into an immutable array of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable array of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableArray<TDestination>> MapImmutableArrayAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
List<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableArray.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the collection of <typeparamref name="TSource"/> into an immutable array of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable array of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableArray<TDestination>> MapImmutableArrayAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
Collection<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableArray.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the array of <typeparamref name="TSource"/> into an immutable array of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable array of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableArray<TDestination>> MapImmutableArrayAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
TSource[] source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableArray.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the enumerable of <typeparamref name="TSource"/> into an immutable array of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable array of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableArray<TDestination>> MapImmutableArrayAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
IEnumerable<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableArray.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the list of <typeparamref name="TSource"/> into an immutable list of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable list of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableList<TDestination>> MapImmutableListAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
List<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableList.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the collection of <typeparamref name="TSource"/> into an immutable list of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable list of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableList<TDestination>> MapImmutableListAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
Collection<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableList.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the array of <typeparamref name="TSource"/> into an immutable list of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable list of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableList<TDestination>> MapImmutableListAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
TSource[] source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableList.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));

/// <summary>
/// Maps the enumerable of <typeparamref name="TSource"/> into an immutable list of
/// <typeparamref name="TDestination"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <param name="mapper">The mapper.</param>
/// <param name="source">The source objects.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>An immutable list of <typeparamref name="TDestination"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is
/// <c>null</c>.</exception>
public static async Task<ImmutableList<TDestination>> MapImmutableListAsync<TSource, TDestination>(
this IAsyncImmutableMapper<TSource, TDestination> mapper,
IEnumerable<TSource> source,
CancellationToken cancellationToken = default)
where TDestination : new() =>
ImmutableList.Create(await mapper.MapArrayAsync(source, cancellationToken).ConfigureAwait(false));
#endif

/// <summary>
/// Maps the list of <typeparamref name="TSource"/> into a list of
/// <typeparamref name="TDestination"/>.
Expand Down
Loading

0 comments on commit 316035c

Please sign in to comment.