Skip to content

Commit

Permalink
feat (Links): adds further null checks and docs
Browse files Browse the repository at this point in the history
Updates all methods to accept a nullable source. This is handled within the methods now.

Updates all the docs to note this will be an empty collection or default if the source parameter is null.
  • Loading branch information
jamiepollock committed Jan 22, 2024
1 parent f32487f commit 6000efa
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static class MapperContextLinkExtensions
/// <typeparam name="TLink">The type of the link.</typeparam>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="block">The block to map.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="block"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this MapperContext mapperContext, IBlockReference<IPublishedElement, IPublishedElement>? block) where TLink : class, ILink
{
if (block is null)
Expand All @@ -32,7 +35,10 @@ public static class MapperContextLinkExtensions
/// </summary>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="block">The block to map.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="block"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this MapperContext mapperContext, IBlockReference<IPublishedElement, IPublishedElement>? block)
{
return mapperContext.MapLink<ILink>(block);
Expand All @@ -44,7 +50,10 @@ public static class MapperContextLinkExtensions
/// <typeparam name="TLink">The type of the link.</typeparam>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="element">The element to map.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="element"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this MapperContext mapperContext, IPublishedElement? element) where TLink : class, ILink
{
if (element is null)
Expand All @@ -60,7 +69,10 @@ public static class MapperContextLinkExtensions
/// </summary>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="element">The element to map.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="element"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this MapperContext mapperContext, IPublishedElement? element)
{
return mapperContext.MapLink<ILink>(element);
Expand All @@ -72,7 +84,10 @@ public static class MapperContextLinkExtensions
/// <typeparam name="TLink">The type of the link.</typeparam>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="list">The list to map.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="list"/> or its first item are <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this MapperContext mapperContext, BlockListModel? list) where TLink : class, ILink
{
if (list is null)
Expand All @@ -88,7 +103,10 @@ public static class MapperContextLinkExtensions
/// </summary>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="list">The list to map.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="list"/> or its first item are <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this MapperContext mapperContext, BlockListModel? list)
{
return mapperContext.MapLink<ILink>(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ public static class MapperContextLinksExtensions
/// <typeparam name="TLink">The type of the link.</typeparam>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="list">The list to map.</param>
/// <returns>An array of <typeparamref name="TLink"/>.</returns>
public static TLink[] MapLinks<TLink>(this MapperContext mapperContext, BlockListModel list) where TLink : class, ILink
/// <returns>
/// <para>An array of <typeparamref name="TLink"/>.</para>
/// <para>This method will return an empty collection if <paramref name="list"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink[] MapLinks<TLink>(this MapperContext mapperContext, BlockListModel? list) where TLink : class, ILink
{
if (list is null)
{
return [];
}

var links = new List<TLink>();
foreach (var block in list)
{
Expand All @@ -38,8 +46,11 @@ public static TLink[] MapLinks<TLink>(this MapperContext mapperContext, BlockLis
/// </summary>
/// <param name="mapperContext">The current mapper context.</param>
/// <param name="list">The list to map.</param>
/// <returns>An array of <see cref="ILink"/>.</returns>
public static ILink[] MapLinks(this MapperContext mapperContext, BlockListModel list)
/// <returns>
/// <para>An array of <see cref="ILink"/>.</para>
/// <para>This method will return an empty collection if <paramref name="list"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink[] MapLinks(this MapperContext mapperContext, BlockListModel? list)
{
return mapperContext.MapLinks<ILink>(list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="block">The block to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="block"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this IUmbracoMapper mapper, IBlockReference<IPublishedElement, IPublishedElement>? block, Action<MapperContext>? configureContext = default) where TLink : class, ILink
{
if (block is null)
Expand All @@ -36,7 +39,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="block">The block to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="block"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this IUmbracoMapper mapper, IBlockReference<IPublishedElement, IPublishedElement>? block, Action<MapperContext>? configureContext = default)
{
return mapper.MapLink<ILink>(block, configureContext);
Expand All @@ -49,7 +55,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="element">The element to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="element"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this IUmbracoMapper mapper, IPublishedElement? element, Action<MapperContext>? configureContext = default) where TLink : class, ILink
{
if (element is null)
Expand All @@ -66,7 +75,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="element">The element to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="element"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this IUmbracoMapper mapper, IPublishedElement? element, Action<MapperContext>? configureContext = default)
{
return mapper.MapLink<ILink>(element, configureContext);
Expand All @@ -79,7 +91,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="list">The list to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <typeparamref name="TLink"/> if successful.</returns>
/// <returns>
/// <para>A <typeparamref name="TLink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="list"/> or its first item are <see langword="null"/>.</para>
/// </returns>
public static TLink? MapLink<TLink>(this IUmbracoMapper mapper, BlockListModel? list, Action<MapperContext>? configureContext = default) where TLink : class, ILink
{
if (list is null)
Expand All @@ -96,7 +111,10 @@ public static class UmbracoMapperLinkExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="list">The list to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>A <see cref="ILink"/> if successful.</returns>
/// <returns>
/// <para>A <see cref="ILink"/> if successful.</para>
/// <para>This method will return <see langword="default" /> if <paramref name="list"/> or its first item are <see langword="null"/>.</para>
/// </returns>
public static ILink? MapLink(this IUmbracoMapper mapper, BlockListModel? list, Action<MapperContext>? configureContext = default)
{
return mapper.MapLink<ILink>(list, configureContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ public static class UmbracoMapperLinksExtensions
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="list">The list to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>An array of <typeparamref name="TLink"/>.</returns>
public static TLink[] MapLinks<TLink>(this IUmbracoMapper mapper, BlockListModel list, Action<MapperContext>? configureContext = default) where TLink : class, ILink
/// <returns>
/// <para>An array of <typeparamref name="TLink"/>.</para>
/// <para>This method will return an empty collection if <paramref name="list"/> is <see langword="null"/>.</para>
/// </returns>
public static TLink[] MapLinks<TLink>(this IUmbracoMapper mapper, BlockListModel? list, Action<MapperContext>? configureContext = default) where TLink : class, ILink
{
if (list is null)
{
return [];
}

var links = new List<TLink>();
foreach (var block in list)
{
Expand All @@ -42,8 +50,11 @@ public static TLink[] MapLinks<TLink>(this IUmbracoMapper mapper, BlockListModel
/// <param name="mapper">The current umbraco mapper.</param>
/// <param name="list">The list to map.</param>
/// <param name="configureContext">The optional configure mapper context action.</param>
/// <returns>An array of <see cref="ILink"/>.</returns>
public static ILink[] MapLinks(this IUmbracoMapper mapper, BlockListModel list, Action<MapperContext>? configureContext = default)
/// <returns>
/// <para>An array of <see cref="ILink"/>.</para>
/// <para>This method will return an empty collection if <paramref name="list"/> is <see langword="null"/>.</para>
/// </returns>
public static ILink[] MapLinks(this IUmbracoMapper mapper, BlockListModel? list, Action<MapperContext>? configureContext = default)
{
return mapper.MapLinks<ILink>(list, configureContext);
}
Expand Down

0 comments on commit 6000efa

Please sign in to comment.