| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Expressions; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Expressions.Models; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Workflows.Expressions; |
| | | 8 | | using Elsa.Workflows.Memory; |
| | | 9 | | using Elsa.Workflows.Models; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Management.Providers; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public class DefaultExpressionDescriptorProvider : IExpressionDescriptorProvider |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public IEnumerable<ExpressionDescriptor> GetDescriptors() |
| | | 19 | | { |
| | 316 | 20 | | yield return CreateLiteralDescriptor(); |
| | 316 | 21 | | yield return CreateObjectDescriptor(); |
| | 316 | 22 | | yield return CreateJsonDescriptor(); |
| | 316 | 23 | | yield return CreateDelegateDescriptor(); |
| | 316 | 24 | | yield return CreateVariableDescriptor(); |
| | 316 | 25 | | yield return CreateInputDescriptor(); |
| | 316 | 26 | | } |
| | | 27 | | |
| | | 28 | | private ExpressionDescriptor CreateLiteralDescriptor() |
| | | 29 | | { |
| | 316 | 30 | | return CreateDescriptor<LiteralExpressionHandler>( |
| | 316 | 31 | | "Literal", |
| | 316 | 32 | | "Literal", |
| | 316 | 33 | | isBrowsable: false, |
| | 1686 | 34 | | memoryBlockReferenceFactory: () => new Literal(), |
| | 316 | 35 | | deserialize: context => |
| | 316 | 36 | | { |
| | 1686 | 37 | | var elementValue = context.JsonElement.TryGetProperty("value", out var v) ? v : default; |
| | 1686 | 38 | | var value = elementValue.GetValue(); |
| | 1686 | 39 | | return new Expression("Literal", value); |
| | 316 | 40 | | }); |
| | | 41 | | } |
| | | 42 | | |
| | 316 | 43 | | private ExpressionDescriptor CreateObjectDescriptor() => CreateDescriptor<ObjectExpressionHandler>("Object", "Object |
| | | 44 | | |
| | | 45 | | [Obsolete("Use Object instead.")] |
| | 316 | 46 | | private ExpressionDescriptor CreateJsonDescriptor() => CreateDescriptor<ObjectExpressionHandler>("Json", "Json", mon |
| | | 47 | | |
| | 316 | 48 | | private ExpressionDescriptor CreateDelegateDescriptor() => CreateDescriptor<DelegateExpressionHandler>("Delegate", " |
| | | 49 | | |
| | | 50 | | private ExpressionDescriptor CreateVariableDescriptor() |
| | | 51 | | { |
| | 316 | 52 | | return CreateDescriptor<VariableExpressionHandler>( |
| | 316 | 53 | | "Variable", |
| | 316 | 54 | | "Variable", |
| | 316 | 55 | | isBrowsable: true, |
| | 0 | 56 | | memoryBlockReferenceFactory: () => new Variable(), |
| | 316 | 57 | | deserialize: context => |
| | 316 | 58 | | { |
| | 0 | 59 | | var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default; |
| | 0 | 60 | | var valueString = valueElement.GetValue()?.ToString(); |
| | 316 | 61 | | |
| | 0 | 62 | | if (string.IsNullOrWhiteSpace(valueString)) |
| | 0 | 63 | | return new Expression("Variable", null); |
| | 316 | 64 | | |
| | 316 | 65 | | try |
| | 316 | 66 | | { |
| | 0 | 67 | | var value = JsonSerializer.Deserialize(valueString, context.MemoryBlockType, context.Options); |
| | 0 | 68 | | return new Expression("Variable", value); |
| | 316 | 69 | | } |
| | 0 | 70 | | catch (Exception) |
| | 316 | 71 | | { |
| | 0 | 72 | | return new Expression("Variable", null); |
| | 316 | 73 | | } |
| | 0 | 74 | | } |
| | 316 | 75 | | ); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private ExpressionDescriptor CreateInputDescriptor() |
| | | 79 | | { |
| | 316 | 80 | | return CreateDescriptor<InputExpressionHandler>( |
| | 316 | 81 | | "Input", |
| | 316 | 82 | | "Input", // Displayed text in UI (input selection dropdown) |
| | 316 | 83 | | isBrowsable: true, |
| | 316 | 84 | | deserialize: context => |
| | 316 | 85 | | { |
| | 0 | 86 | | var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default; |
| | 0 | 87 | | var valueString = valueElement.GetValue()?.ToString(); |
| | 316 | 88 | | |
| | 0 | 89 | | if (string.IsNullOrWhiteSpace(valueString)) return new Expression("Input", null); |
| | 316 | 90 | | |
| | 316 | 91 | | try |
| | 316 | 92 | | { |
| | 0 | 93 | | var value = JsonSerializer.Deserialize(valueString, typeof(InputDefinition), context.Options); |
| | 316 | 94 | | |
| | 0 | 95 | | return new Expression("Input", value); |
| | 316 | 96 | | } |
| | 0 | 97 | | catch (Exception) |
| | 316 | 98 | | { |
| | 0 | 99 | | return new Expression("Input", null); |
| | 316 | 100 | | } |
| | 0 | 101 | | } |
| | 316 | 102 | | ); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static ExpressionDescriptor CreateDescriptor<THandler>( |
| | | 106 | | string expressionType, |
| | | 107 | | string displayName, |
| | | 108 | | bool isSerializable = true, |
| | | 109 | | bool isBrowsable = true, |
| | | 110 | | string? monacoLanguage = null, |
| | | 111 | | Func<MemoryBlockReference>? memoryBlockReferenceFactory = default, |
| | | 112 | | Func<ExpressionSerializationContext, Expression>? deserialize = default) |
| | | 113 | | where THandler : IExpressionHandler |
| | | 114 | | { |
| | 1896 | 115 | | var descriptor = new ExpressionDescriptor |
| | 1896 | 116 | | { |
| | 1896 | 117 | | Type = expressionType, |
| | 1896 | 118 | | DisplayName = displayName, |
| | 1896 | 119 | | IsSerializable = isSerializable, |
| | 1896 | 120 | | IsBrowsable = isBrowsable, |
| | 2269 | 121 | | HandlerFactory = sp => ActivatorUtilities.GetServiceOrCreateInstance<THandler>(sp), |
| | 263 | 122 | | MemoryBlockReferenceFactory = memoryBlockReferenceFactory ?? (() => new MemoryBlockReference()) |
| | 1896 | 123 | | }; |
| | | 124 | | |
| | 1896 | 125 | | if (deserialize != null) |
| | 948 | 126 | | descriptor.Deserialize = deserialize; |
| | | 127 | | |
| | 1896 | 128 | | if (monacoLanguage != null) |
| | 632 | 129 | | descriptor.Properties = new |
| | 632 | 130 | | { |
| | 632 | 131 | | MonacoLanguage = monacoLanguage |
| | 632 | 132 | | }.ToDictionary(); |
| | | 133 | | |
| | 1896 | 134 | | return descriptor; |
| | | 135 | | } |
| | | 136 | | } |