| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Api.Client.Resources.WorkflowDefinitions.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Api.Client.Resources.WorkflowDefinitions.Extensions; |
| | | 5 | | |
| | | 6 | | public static class WorkflowDefinitionExtensions |
| | | 7 | | { |
| | | 8 | | private const string VariableTestValuesKey = "VariableTestValues"; |
| | | 9 | | |
| | | 10 | | public static IDictionary<string, object?> GetVariableTestValues(this WorkflowDefinition workflowDefinition) |
| | | 11 | | { |
| | 0 | 12 | | if (!workflowDefinition.CustomProperties.TryGetValue(VariableTestValuesKey, out var dictionary)) |
| | | 13 | | { |
| | 0 | 14 | | var testValues = new Dictionary<string, object?>(); |
| | 0 | 15 | | workflowDefinition.CustomProperties[VariableTestValuesKey] = testValues; |
| | 0 | 16 | | return testValues; |
| | | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | if (dictionary is JsonElement jsonElement) |
| | 0 | 20 | | dictionary = JsonSerializer.Deserialize<Dictionary<string, object?>>(jsonElement.GetRawText()); |
| | | 21 | | |
| | 0 | 22 | | return dictionary as IDictionary<string, object?> ?? throw new InvalidOperationException("Invalid variable test |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public static object? GetVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId) |
| | | 26 | | { |
| | 0 | 27 | | var testValues = workflowDefinition.GetVariableTestValues(); |
| | 0 | 28 | | return testValues.TryGetValue(variableId, out var value) ? value : null; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public static void SetVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId, object? value |
| | | 32 | | { |
| | 0 | 33 | | var testValues = workflowDefinition.GetVariableTestValues(); |
| | 0 | 34 | | testValues[variableId] = value; |
| | 0 | 35 | | workflowDefinition.CustomProperties[VariableTestValuesKey] = testValues; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static void ClearVariableTestValue(this WorkflowDefinition workflowDefinition, string variableId) |
| | | 39 | | { |
| | 0 | 40 | | var testValues = workflowDefinition.GetVariableTestValues(); |
| | 0 | 41 | | testValues.Remove(variableId); |
| | 0 | 42 | | workflowDefinition.CustomProperties[VariableTestValuesKey] = testValues; |
| | 0 | 43 | | } |
| | | 44 | | } |