| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Encodings.Web; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using System.Text.Unicode; |
| | | 6 | | using Elsa.Expressions.Helpers; |
| | | 7 | | using Elsa.Expressions.Models; |
| | | 8 | | using Elsa.Workflows; |
| | | 9 | | using Elsa.Workflows.Memory; |
| | | 10 | | using Elsa.Workflows.Serialization.Converters; |
| | | 11 | | |
| | | 12 | | // ReSharper disable once CheckNamespace |
| | | 13 | | namespace Elsa.Extensions; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Adds extension methods to <see cref="Variable"/> for configuring storage. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class VariableExtensions |
| | | 19 | | { |
| | | 20 | | private static JsonSerializerOptions? _serializerOptions; |
| | | 21 | | |
| | | 22 | | private static JsonSerializerOptions SerializerOptions => |
| | 172 | 23 | | _serializerOptions ??= new JsonSerializerOptions |
| | 172 | 24 | | { |
| | 172 | 25 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 172 | 26 | | ReferenceHandler = ReferenceHandler.Preserve, |
| | 172 | 27 | | PropertyNameCaseInsensitive = true, |
| | 172 | 28 | | Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) |
| | 172 | 29 | | }.WithConverters( |
| | 172 | 30 | | new JsonStringEnumConverter(), |
| | 172 | 31 | | new ExpandoObjectConverterFactory()); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Configures the variable to use the <see cref="WorkflowInstanceStorageDriver"/>. |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public static Variable WithWorkflowStorage(this Variable variable) => variable.WithStorage<WorkflowInstanceStorageDr |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Configures the variable to use the <see cref="WorkflowInstanceStorageDriver"/>. |
| | | 40 | | /// </summary> |
| | 193 | 41 | | public static Variable<T> WithWorkflowStorage<T>(this Variable<T> variable) => (Variable<T>)variable.WithStorage<Wor |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Configures the variable to use the <see cref="MemoryStorageDriver"/>. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public static Variable WithMemoryStorage(this Variable variable) => variable.WithStorage<MemoryStorageDriver>(); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Configures the variable to use the <see cref="MemoryStorageDriver"/>. |
| | | 50 | | /// </summary> |
| | 0 | 51 | | public static Variable<T> WithMemoryStorage<T>(this Variable<T> variable) => (Variable<T>)variable.WithStorage<Memor |
| | | 52 | | |
| | | 53 | | extension(Variable variable) |
| | | 54 | | { |
| | | 55 | | /// <summary> |
| | | 56 | | /// Configures the variable to use the specified <see cref="IStorageDriver"/> type. |
| | | 57 | | /// </summary> |
| | 193 | 58 | | public Variable WithStorage<T>() => variable.WithStorage(typeof(T)); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Configures the variable to use the specified <see cref="IStorageDriver"/> type. |
| | | 62 | | /// </summary> |
| | | 63 | | public Variable WithStorage(Type storageDriverType) |
| | | 64 | | { |
| | 193 | 65 | | variable.StorageDriverType = storageDriverType; |
| | 193 | 66 | | return variable; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public void Set(ActivityExecutionContext context, object? value) |
| | | 70 | | { |
| | 23 | 71 | | var parsedValue = variable.ParseValue(value); |
| | | 72 | | // Set the value. |
| | 22 | 73 | | ((MemoryBlockReference)variable).Set(context, parsedValue); |
| | 22 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Converts the specified value into a type that is compatible with the variable. |
| | | 78 | | /// </summary> |
| | | 79 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions |
| | | 80 | | public object? ParseValue(object? value) |
| | | 81 | | { |
| | 142 | 82 | | var genericType = variable.GetType(); |
| | 141 | 83 | | return ParseValue(genericType, value); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public static object? ParseValue(Type type, object? value) |
| | | 88 | | { |
| | 172 | 89 | | var genericType = type.GenericTypeArguments.FirstOrDefault(); |
| | 172 | 90 | | var converterOptions = new ObjectConverterOptions(SerializerOptions); |
| | 172 | 91 | | return genericType == null ? value : value?.ConvertTo(genericType, converterOptions); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | extension(Variable variable) |
| | | 95 | | { |
| | | 96 | | /// <summary> |
| | | 97 | | /// Converts the specified value into a type that is compatible with the variable. |
| | | 98 | | /// </summary> |
| | | 99 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions |
| | | 100 | | public bool TryParseValue(object? value, out object? parsedValue) |
| | | 101 | | { |
| | | 102 | | try |
| | | 103 | | { |
| | 15 | 104 | | parsedValue = variable.ParseValue(value); |
| | 15 | 105 | | return true; |
| | | 106 | | } |
| | 0 | 107 | | catch |
| | | 108 | | { |
| | 0 | 109 | | parsedValue = null; |
| | 0 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Return the type of the variable. |
| | | 116 | | /// </summary> |
| | | 117 | | public Type GetVariableType() |
| | | 118 | | { |
| | 15 | 119 | | var variableType = variable.GetType(); |
| | 15 | 120 | | return variableType.GenericTypeArguments.Any() ? variableType.GetGenericArguments().First() : typeof(object) |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | } |