From 09892074bb6b4fa067c4a78aec566418366e9799 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 3 Apr 2021 20:54:46 +0200 Subject: [PATCH] Add Default Syntax and Supported Syntax activity property attributes --- .../Attributes/ActivityPropertyAttribute.cs | 11 +++++++++ .../Metadata/ActivityPropertyDescriptor.cs | 24 +++++++++++++++++-- .../Metadata/TypedActivityTypeDescriber.cs | 5 +++- .../Services/WorkflowBlueprintMaterializer.cs | 16 +++++++------ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/core/Elsa.Abstractions/Attributes/ActivityPropertyAttribute.cs b/src/core/Elsa.Abstractions/Attributes/ActivityPropertyAttribute.cs index c2c707837..200bce212 100644 --- a/src/core/Elsa.Abstractions/Attributes/ActivityPropertyAttribute.cs +++ b/src/core/Elsa.Abstractions/Attributes/ActivityPropertyAttribute.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Elsa.Attributes { @@ -49,5 +50,15 @@ namespace Elsa.Attributes /// The type that provides a default value. /// public Type? DefaultValueProvider { get; set; } + + /// + /// The syntax to use by default when evaluating the value. Only used when the property definition doesn't have a syntax specified. + /// + public string? DefaultSyntax { get; set; } + + /// + /// The syntax to use by default when evaluating the value. Only used when the property definition doesn't have a syntax specified. + /// + public string[]? SupportedSyntaxes { get; set; } } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Metadata/ActivityPropertyDescriptor.cs b/src/core/Elsa.Abstractions/Metadata/ActivityPropertyDescriptor.cs index 02942283a..f21809b2f 100644 --- a/src/core/Elsa.Abstractions/Metadata/ActivityPropertyDescriptor.cs +++ b/src/core/Elsa.Abstractions/Metadata/ActivityPropertyDescriptor.cs @@ -1,3 +1,7 @@ +using System; +using System.Collections.Generic; +using System.Linq; + namespace Elsa.Metadata { public class ActivityPropertyDescriptor @@ -6,23 +10,39 @@ namespace Elsa.Metadata { } - public ActivityPropertyDescriptor(string name, string uiHint, string label, string? hint = default, object? options = default, string? category = default, object? defaultValue = default) + public ActivityPropertyDescriptor( + string name, + Type type, + string uiHint, + string label, + string? hint = default, + object? options = default, + string? category = default, + object? defaultValue = default, + string? defaultSyntax = "Literal", + IEnumerable? supportedSyntaxes = default) { Name = name; + Type = type; UIHint = uiHint; Label = label; Hint = hint; Options = options; Category = category; DefaultValue = defaultValue; + DefaultSyntax = defaultSyntax; + SupportedSyntaxes = supportedSyntaxes?.ToList() ?? new List(); } - + public string Name { get; set; } = default!; + public Type Type { get; set; } = default!; public string UIHint { get; set; } = default!; public string Label { get; set; } = default!; public string? Hint { get; set; } public object? Options { get; set; } public string? Category { get; set; } public object? DefaultValue { get; set; } + public string? DefaultSyntax { get; set; } + public IList SupportedSyntaxes { get; set; } = new List(); } } \ No newline at end of file diff --git a/src/core/Elsa.Core/Metadata/TypedActivityTypeDescriber.cs b/src/core/Elsa.Core/Metadata/TypedActivityTypeDescriber.cs index 63fb33555..ea450157f 100644 --- a/src/core/Elsa.Core/Metadata/TypedActivityTypeDescriber.cs +++ b/src/core/Elsa.Core/Metadata/TypedActivityTypeDescriber.cs @@ -57,12 +57,15 @@ namespace Elsa.Metadata yield return new ActivityPropertyDescriptor ( (activityPropertyAttribute.Name ?? propertyInfo.Name).Pascalize(), + propertyInfo.PropertyType, _uiHintResolver.GetUIHint(propertyInfo), activityPropertyAttribute.Label ?? propertyInfo.Name.Humanize(LetterCasing.Title), activityPropertyAttribute.Hint, _optionsResolver.GetOptions(propertyInfo), activityPropertyAttribute.Category, - _defaultValueResolver.GetDefaultValue(propertyInfo) + _defaultValueResolver.GetDefaultValue(propertyInfo), + activityPropertyAttribute.DefaultSyntax, + activityPropertyAttribute.SupportedSyntaxes ); } } diff --git a/src/core/Elsa.Core/Services/WorkflowBlueprintMaterializer.cs b/src/core/Elsa.Core/Services/WorkflowBlueprintMaterializer.cs index 3b3a1ad97..cc3a49f09 100644 --- a/src/core/Elsa.Core/Services/WorkflowBlueprintMaterializer.cs +++ b/src/core/Elsa.Core/Services/WorkflowBlueprintMaterializer.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using Elsa.ActivityProviders; using Elsa.Builders; +using Elsa.Expressions; using Elsa.Models; using Elsa.Services.Models; using Microsoft.Extensions.DependencyInjection; @@ -73,20 +74,21 @@ namespace Elsa.Services foreach (var activityDefinition in activityDefinitions) { var activityType = await _activityTypeService.GetActivityTypeAsync(activityDefinition.Type, cancellationToken); - var type = activityType.Type; - var props = type.GetProperties(); + var activityDescriptor = activityType.Describe(); + var propertyDescriptors = activityDescriptor.Properties; foreach (var property in activityDefinition.Properties) { - var prop = props.FirstOrDefault(x => x.Name == property.Name); - - if (prop == null) + var propertyDescriptor = propertyDescriptors.FirstOrDefault(x => x.Name == property.Name); + + if (propertyDescriptor == null) { - _logger.LogWarning("Could not find the specified property '{PropertyName}' for activity type {ActivityTypeName}. Was the activity property renamed/removed/refactored after the workflow definition was created?", property.Name, activityType.Type.Name); + _logger.LogWarning("Could not find the specified property '{PropertyName}' for activity type {ActivityTypeName}", property.Name, activityType.TypeName); continue; } - var provider = new ExpressionActivityPropertyValueProvider(property.Expression, property.Syntax ?? "Literal", prop.PropertyType); + var syntax = property.Syntax ?? propertyDescriptor.DefaultSyntax ?? SyntaxNames.Literal; + var provider = new ExpressionActivityPropertyValueProvider(property.Expression, syntax, propertyDescriptor.Type); propertyProviders.AddProvider(activityDefinition.ActivityId, property.Name, provider); } }