From 58f79cef11f4d4d11f6cd190e8032be2ef15653b Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 23 Mar 2021 21:02:19 +0100 Subject: [PATCH] Add support for nullable enum properties --- .../Activities/CallRingGroup.cs | 7 ++++++ .../ActivityPropertyOptionsResolver.cs | 24 +++++++++++++++++-- .../ActivityPropertyUIHintResolver.cs | 3 ++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/activities/Elsa.Activities.Telnyx/Activities/CallRingGroup.cs b/src/activities/Elsa.Activities.Telnyx/Activities/CallRingGroup.cs index 565d1db60..a10197331 100644 --- a/src/activities/Elsa.Activities.Telnyx/Activities/CallRingGroup.cs +++ b/src/activities/Elsa.Activities.Telnyx/Activities/CallRingGroup.cs @@ -34,6 +34,13 @@ namespace Elsa.Activities.Telnyx.Activities get => GetState(); set => SetState(value); } + + [ActivityProperty] + public RingGroupStrategy? StrategyTest + { + get => GetState(); + set => SetState(value); + } [ActivityProperty(Hint = "The 'from' number to be used as the caller id presented to the destination ('To' number). The number should be in +E164 format. This attribute will default to the 'From' number of the original call if omitted.")] diff --git a/src/core/Elsa.Core/Metadata/ActivityPropertyOptionsResolver.cs b/src/core/Elsa.Core/Metadata/ActivityPropertyOptionsResolver.cs index 52d96f114..95c03887b 100644 --- a/src/core/Elsa.Core/Metadata/ActivityPropertyOptionsResolver.cs +++ b/src/core/Elsa.Core/Metadata/ActivityPropertyOptionsResolver.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; +using AutoMapper.Internal; using Elsa.Attributes; using Elsa.Design; using Humanizer; @@ -26,8 +28,8 @@ namespace Elsa.Metadata if (activityPropertyAttribute.OptionsProvider == null) { - if (activityPropertyInfo.PropertyType.IsEnum) - return activityPropertyInfo.PropertyType.GetEnumNames().Select(x => new SelectListItem(x.Humanize(LetterCasing.Title), x)); + if (TryGetEnumOptions(activityPropertyInfo, out var items)) + return items; return activityPropertyAttribute.Options; } @@ -38,5 +40,23 @@ namespace Elsa.Metadata var provider = (IActivityPropertyOptionsProvider) ActivatorUtilities.GetServiceOrCreateInstance(scope.ServiceProvider, providerType); return provider.GetOptions(activityPropertyInfo); } + + private bool TryGetEnumOptions(PropertyInfo activityPropertyInfo, out IList? items) + { + var isNullable = activityPropertyInfo.PropertyType.IsNullableType(); + var propertyType = isNullable ? activityPropertyInfo.PropertyType.GetTypeOfNullable() : activityPropertyInfo.PropertyType; + + items = null; + + if (!propertyType.IsEnum) + return false; + + items = propertyType.GetEnumNames().Select(x => new SelectListItem(x.Humanize(LetterCasing.Title), x)).ToList(); + + if (isNullable) + items.Insert(0, new SelectListItem("-", "")); + + return true; + } } } \ No newline at end of file diff --git a/src/core/Elsa.Core/Metadata/ActivityPropertyUIHintResolver.cs b/src/core/Elsa.Core/Metadata/ActivityPropertyUIHintResolver.cs index 7f407fd59..67b018bb8 100644 --- a/src/core/Elsa.Core/Metadata/ActivityPropertyUIHintResolver.cs +++ b/src/core/Elsa.Core/Metadata/ActivityPropertyUIHintResolver.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Reflection; +using AutoMapper.Internal; using Elsa.Attributes; using Elsa.Design; @@ -25,7 +26,7 @@ namespace Elsa.Metadata if (typeof(IEnumerable).IsAssignableFrom(type)) return ActivityPropertyUIHints.Dropdown; - if (type.IsEnum) + if (type.IsEnum || type.IsNullableType() && type.GetTypeOfNullable().IsEnum) return ActivityPropertyUIHints.Dropdown; return ActivityPropertyUIHints.SingleLine;