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