< Summary

Information
Class: Elsa.Workflows.Serialization.ReferenceHandlers.CustomPreserveReferenceResolver
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/ReferenceHandlers/CustomPreserveReferenceResolver.cs
Line coverage
73%
Covered lines: 11
Uncovered lines: 4
Coverable lines: 15
Total lines: 59
Line coverage: 73.3%
Branch coverage
33%
Covered branches: 2
Total branches: 6
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
AddReference(...)50%2266.66%
TryAddReference(...)100%210%
GetReference(...)50%2285.71%
ResolveReference(...)0%620%
HasReference(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/ReferenceHandlers/CustomPreserveReferenceResolver.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Elsa.Workflows.Serialization.ReferenceHandlers;
 5
 6/// <summary>
 7/// A custom reference resolver that uses a <see cref="ReferenceEqualityComparer"/> to compare objects.
 8/// </summary>
 9/// <remarks>We only need this class because the built-in <c>PreserveReferenceResolver"</c> is internal</remarks>
 10public class CustomPreserveReferenceResolver : ReferenceResolver
 11{
 12    private uint _referenceCount;
 44413    private readonly Dictionary<string, object> _referenceIdToObjectMap = new();
 44414    private readonly Dictionary<object, string> _objectToReferenceIdMap = new(ReferenceEqualityComparer.Instance);
 15
 16    /// <inheritdoc />
 17    public override void AddReference(string referenceId, object value)
 18    {
 119319        if (!_referenceIdToObjectMap.TryAdd(referenceId, value))
 020            throw new JsonException();
 119321    }
 22
 23    /// <summary>
 24    /// Tries to add a reference.
 25    /// </summary>
 26    public bool TryAddReference(string referenceId, object value)
 27    {
 028        return _referenceIdToObjectMap.TryAdd(referenceId, value);
 29    }
 30
 31    /// <inheritdoc />
 32    public override string GetReference(object value, out bool alreadyExists)
 33    {
 322434        if (_objectToReferenceIdMap.TryGetValue(value, out var referenceId))
 35        {
 036            alreadyExists = true;
 37        }
 38        else
 39        {
 322440            _referenceCount++;
 322441            referenceId = _referenceCount.ToString();
 322442            _objectToReferenceIdMap.Add(value, referenceId);
 322443            alreadyExists = false;
 44        }
 45
 322446        return referenceId;
 47    }
 48
 49    /// <inheritdoc />
 50    public override object ResolveReference(string referenceId) =>
 051        !_referenceIdToObjectMap.TryGetValue(referenceId, out var value) ? throw new JsonException() : value;
 52
 53    /// <summary>
 54    /// Determines whether the specified value has a reference.
 55    /// </summary>
 56    /// <param name="value">The value.</param>
 57    /// <returns><c>true</c> if the specified value has a reference; otherwise, <c>false</c>.</returns>
 8558    public bool HasReference(object value) => _objectToReferenceIdMap.ContainsKey(value);
 59}