| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Expressions.Services; |
| | | 3 | | using Elsa.Workflows.Runtime.Entities; |
| | | 4 | | using Elsa.Workflows.Serialization.Converters; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Runtime.Comparers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Compares two <see cref="StoredTrigger"/> instances for equality. |
| | | 10 | | /// </summary> |
| | | 11 | | public class WorkflowTriggerEqualityComparer : IEqualityComparer<StoredTrigger> |
| | | 12 | | { |
| | | 13 | | private readonly JsonSerializerOptions _settings; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="WorkflowTriggerEqualityComparer"/> class. |
| | | 17 | | /// </summary> |
| | 640 | 18 | | public WorkflowTriggerEqualityComparer() |
| | | 19 | | { |
| | 640 | 20 | | _settings = new JsonSerializerOptions |
| | 640 | 21 | | { |
| | 640 | 22 | | // Enables serialization of ValueTuples, which use fields instead of properties. |
| | 640 | 23 | | IncludeFields = true, |
| | 640 | 24 | | PropertyNameCaseInsensitive = true |
| | 640 | 25 | | }; |
| | | 26 | | |
| | 640 | 27 | | _settings.Converters.Add(new TypeJsonConverter(WellKnownTypeRegistry.CreateDefault())); |
| | 640 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public bool Equals(StoredTrigger? x, StoredTrigger? y) |
| | | 32 | | { |
| | 150 | 33 | | var xJson = x != null ? Serialize(x) : null; |
| | 150 | 34 | | var yJson = y != null ? Serialize(y) : null; |
| | 150 | 35 | | return xJson == yJson; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public int GetHashCode(StoredTrigger obj) |
| | | 40 | | { |
| | 375 | 41 | | var json = Serialize(obj); |
| | 375 | 42 | | return json.GetHashCode(); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private string Serialize(StoredTrigger storedTrigger) |
| | | 46 | | { |
| | 675 | 47 | | var input = new |
| | 675 | 48 | | { |
| | 675 | 49 | | storedTrigger.Payload, |
| | 675 | 50 | | storedTrigger.Name, |
| | 675 | 51 | | storedTrigger.ActivityId, |
| | 675 | 52 | | storedTrigger.WorkflowDefinitionId, |
| | 675 | 53 | | storedTrigger.WorkflowDefinitionVersionId, |
| | 675 | 54 | | storedTrigger.Hash |
| | 675 | 55 | | }; |
| | 675 | 56 | | return JsonSerializer.Serialize(input, _settings); |
| | | 57 | | } |
| | | 58 | | } |