From f32487fccf4a17b94d98d89afb99aa95dbefc8a7 Mon Sep 17 00:00:00 2001 From: Jamie Pollock Date: Mon, 22 Jan 2024 10:42:24 -0500 Subject: [PATCH] feat (Components): adds further null checks and docs 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. --- .../MapperContextComponentExtensions.cs | 49 ++++++++++++--- .../MapperContextComponentsExtensions.cs | 49 +++++++++++---- .../UmbracoMapperComponentExtensions.cs | 49 ++++++++++++--- .../UmbracoMapperComponentsExtensions.cs | 62 ++++++++++++++----- 4 files changed, 164 insertions(+), 45 deletions(-) diff --git a/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentExtensions.cs b/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentExtensions.cs index e6ad56c..ccedcf5 100644 --- a/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentExtensions.cs +++ b/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentExtensions.cs @@ -16,7 +16,10 @@ public static class MapperContextComponentExtensions /// The type of the component. /// The current mapper context. /// The block to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this MapperContext mapperContext, IBlockReference? block) where TComponent : class, IComponent { if (block is null) @@ -32,7 +35,10 @@ public static class MapperContextComponentExtensions /// /// The current mapper context. /// The block to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static IComponent? MapComponent(this MapperContext mapperContext, IBlockReference? block) { return mapperContext.MapComponent(block); @@ -44,7 +50,10 @@ public static class MapperContextComponentExtensions /// The type of the component. /// The current mapper context. /// The element to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this MapperContext mapperContext, IPublishedElement? element) where TComponent : class, IComponent { if (element is null) @@ -60,7 +69,10 @@ public static class MapperContextComponentExtensions /// /// The current mapper context. /// The element to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static IComponent? MapComponent(this MapperContext mapperContext, IPublishedElement? element) { return mapperContext.MapComponent(element); @@ -72,9 +84,17 @@ public static class MapperContextComponentExtensions /// The type of the component. /// The current mapper context. /// The list to map. - /// A if successful. - public static TComponent? MapComponent(this MapperContext mapperContext, BlockListModel list) where TComponent : class, IComponent + /// + /// A if successful. + /// This method will return if or its first item are . + /// + public static TComponent? MapComponent(this MapperContext mapperContext, BlockListModel? list) where TComponent : class, IComponent { + if (list is null) + { + return default; + } + return mapperContext.MapComponent(list.FirstOrDefault()); } @@ -83,8 +103,11 @@ public static class MapperContextComponentExtensions /// /// The current mapper context. /// The list to map. - /// A if successful. - public static IComponent? MapComponent(this MapperContext mapperContext, BlockListModel list) + /// + /// A if successful. + /// This method will return if or its first item are . + /// + public static IComponent? MapComponent(this MapperContext mapperContext, BlockListModel? list) { return mapperContext.MapComponent(list); } @@ -94,7 +117,10 @@ public static class MapperContextComponentExtensions /// /// The current mapper context. /// The grid to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is null. + /// public static IComponent? MapComponent(this MapperContext mapperContext, BlockGridModel? grid) { return mapperContext.MapComponent(grid); @@ -105,7 +131,10 @@ public static class MapperContextComponentExtensions /// /// The current mapper context. /// The grid to map. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this MapperContext mapperContext, BlockGridModel? grid) where TComponent : class, IComponent { if (grid is null) diff --git a/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentsExtensions.cs b/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentsExtensions.cs index d89d501..625a38d 100644 --- a/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentsExtensions.cs +++ b/src/Rhythm.Drop.Umbraco.Mapping/Components/MapperContextComponentsExtensions.cs @@ -16,9 +16,17 @@ public static class MapperContextComponentsExtensions /// The type of the component. /// The current mapper context. /// The content to map. - /// An array of . - public static TComponent[] MapComponents(this MapperContext mapperContext, IPublishedContent content) where TComponent : class, IComponent + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static TComponent[] MapComponents(this MapperContext mapperContext, IPublishedContent? content) where TComponent : class, IComponent { + if (content is null) + { + return []; + } + var component = mapperContext.Map(content); return component ?? []; @@ -29,8 +37,11 @@ public static TComponent[] MapComponents(this MapperContext mapperCo /// /// The current mapper context. /// The content to map. - /// An array of . - public static IComponent[] MapComponents(this MapperContext mapperContext, IPublishedContent content) + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static IComponent[] MapComponents(this MapperContext mapperContext, IPublishedContent? content) { return mapperContext.MapComponents(content); } @@ -41,9 +52,17 @@ public static IComponent[] MapComponents(this MapperContext mapperContext, IPubl /// The type of the component. /// The current mapper context. /// The list to map. - /// An array of . - public static TComponent[] MapComponents(this MapperContext mapperContext, BlockListModel list) where TComponent : class, IComponent + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static TComponent[] MapComponents(this MapperContext mapperContext, BlockListModel? list) where TComponent : class, IComponent { + if (list is null) + { + return []; + } + var components = new List(); foreach (var block in list) { @@ -64,8 +83,11 @@ public static TComponent[] MapComponents(this MapperContext mapperCo /// /// The current mapper context. /// The list to map. - /// An array of . - public static IComponent[] MapComponents(this MapperContext mapperContext, BlockListModel list) + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static IComponent[] MapComponents(this MapperContext mapperContext, BlockListModel? list) { return mapperContext.MapComponents(list); } @@ -74,9 +96,11 @@ public static IComponent[] MapComponents(this MapperContext mapperContext, Block /// Maps a to a collection of . /// /// The current mapper context. - /// The grid to map. - /// An array of . + /// + /// An array of . + /// This method will return an empty collection if is . + /// public static TComponent[] MapComponents(this MapperContext mapperContext, BlockGridModel? grid) where TComponent : class, IComponent { var gridComponent = mapperContext.MapComponent(grid); @@ -93,7 +117,10 @@ public static TComponent[] MapComponents(this MapperContext mapperCo /// /// The current mapper context. /// The grid to map. - /// An array of . + /// + /// An array of . + /// This method will return an empty collection if is . + /// public static IComponent[] MapComponents(this MapperContext mapperContext, BlockGridModel? grid) { return mapperContext.MapComponents(grid); diff --git a/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentExtensions.cs b/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentExtensions.cs index 3f113e1..7e4fb5c 100644 --- a/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentExtensions.cs +++ b/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentExtensions.cs @@ -19,7 +19,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The block to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this IUmbracoMapper mapper, IBlockReference? block, Action? configureContext = default) where TComponent : class, IComponent { if (block is null) @@ -36,7 +39,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The block to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static IComponent? MapComponent(this IUmbracoMapper mapper, IBlockReference? block, Action? configureContext = default) { return mapper.MapComponent(block, configureContext); @@ -49,7 +55,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The element to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this IUmbracoMapper mapper, IPublishedElement? element, Action? configureContext = default) where TComponent : class, IComponent { if (element is null) @@ -66,7 +75,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The element to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static IComponent? MapComponent(this IUmbracoMapper mapper, IPublishedElement? element, Action? configureContext = default) { return mapper.MapComponent(element, configureContext); @@ -79,9 +91,17 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The list to map. /// The optional configure mapper context action. - /// A if successful. - public static TComponent? MapComponent(this IUmbracoMapper mapper, BlockListModel list, Action? configureContext = default) where TComponent : class, IComponent + /// + /// A if successful. + /// This method will return if or its first item are . + /// + public static TComponent? MapComponent(this IUmbracoMapper mapper, BlockListModel? list, Action? configureContext = default) where TComponent : class, IComponent { + if (list is null) + { + return default; + } + return mapper.MapComponent(list.FirstOrDefault(), configureContext); } @@ -91,8 +111,11 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The list to map. /// The optional configure mapper context action. - /// A if successful. - public static IComponent? MapComponent(this IUmbracoMapper mapper, BlockListModel list, Action? configureContext = default) + /// + /// A if successful. + /// This method will return if or its first item are . + /// + public static IComponent? MapComponent(this IUmbracoMapper mapper, BlockListModel? list, Action? configureContext = default) { return mapper.MapComponent(list, configureContext); } @@ -103,7 +126,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The grid to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static IComponent? MapComponent(this IUmbracoMapper mapper, BlockGridModel? grid, Action? configureContext = default) { return mapper.MapComponent(grid, configureContext); @@ -115,7 +141,10 @@ public static class UmbracoMapperComponentExtensions /// The current umbraco mapper. /// The grid to map. /// The optional configure mapper context action. - /// A if successful. + /// + /// A if successful. + /// This method will return if is . + /// public static TComponent? MapComponent(this IUmbracoMapper mapper, BlockGridModel? grid, Action? configureContext = default) where TComponent : class, IComponent { if (grid is null) diff --git a/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentsExtensions.cs b/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentsExtensions.cs index f9516a1..07b7672 100644 --- a/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentsExtensions.cs +++ b/src/Rhythm.Drop.Umbraco.Mapping/Components/UmbracoMapperComponentsExtensions.cs @@ -19,9 +19,17 @@ public static class UmbracoMapperComponentsExtensions /// The current umbraco mapper. /// The content to map. /// The optional configure mapper context action. - /// An array of . - public static TComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent content, Action? configureContext = default) where TComponent : class, IComponent + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static TComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent? content, Action? configureContext = default) where TComponent : class, IComponent { + if (content is null) + { + return []; + } + var component = mapper.Map(content, configureContext ?? _defaultContextFunc); return component ?? []; @@ -33,8 +41,11 @@ public static TComponent[] MapComponents(this IUmbracoMapper mapper, /// The current umbraco mapper. /// The content to map. /// The optional configure mapper context action. - /// An array of . - public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent content, Action? configureContext = default) + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent? content, Action? configureContext = default) { return mapper.MapComponents(content, configureContext); } @@ -44,8 +55,11 @@ public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedC /// /// The current umbraco mapper. /// The content to map. - /// An array of . - public static TComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent content) where TComponent : class, IComponent + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static TComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent? content) where TComponent : class, IComponent { void configureContext(MapperContext context) { @@ -60,8 +74,11 @@ void configureContext(MapperContext context) /// /// The current umbraco mapper. /// The content to map. - /// An array of . - public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent content) + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedContent? content) { return mapper.MapComponents(content); } @@ -73,9 +90,17 @@ public static IComponent[] MapComponents(this IUmbracoMapper mapper, IPublishedC /// The current umbraco mapper. /// The list to map. /// The optional configure mapper context action. - /// An array of . - public static TComponent[] MapComponents(this IUmbracoMapper mapper, BlockListModel list, Action? configureContext = default) where TComponent : class, IComponent + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static TComponent[] MapComponents(this IUmbracoMapper mapper, BlockListModel? list, Action? configureContext = default) where TComponent : class, IComponent { + if (list is null) + { + return []; + } + var components = new List(); foreach (var block in list) { @@ -97,8 +122,11 @@ public static TComponent[] MapComponents(this IUmbracoMapper mapper, /// The current umbraco mapper. /// The list to map. /// The optional configure mapper context action. - /// An array of . - public static IComponent[] MapComponents(this IUmbracoMapper mapper, BlockListModel list, Action? configureContext = default) + /// + /// An array of . + /// This method will return an empty collection if is . + /// + public static IComponent[] MapComponents(this IUmbracoMapper mapper, BlockListModel? list, Action? configureContext = default) { return mapper.MapComponents(list, configureContext); } @@ -109,7 +137,10 @@ public static IComponent[] MapComponents(this IUmbracoMapper mapper, BlockListMo /// The current umbraco mapper. /// The grid to map. /// The optional configure mapper context action. - /// An array of . + /// + /// An array of . + /// This method will return an empty collection if is . + /// public static TComponent[] MapComponents(this IUmbracoMapper mapper, BlockGridModel? grid, Action? configureContext = default) where TComponent : class, IComponent { var gridComponent = mapper.MapComponent(grid, configureContext); @@ -127,7 +158,10 @@ public static TComponent[] MapComponents(this IUmbracoMapper mapper, /// The current umbraco mapper. /// The grid to map. /// The optional configure mapper context action. - /// An array of . + /// + /// An array of . + /// This method will return an empty collection if is . + /// public static IComponent[] MapComponents(this IUmbracoMapper mapper, BlockGridModel? grid, Action? configureContext = default) { return mapper.MapComponents(grid, configureContext);