Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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