< Summary

Information
Class: TeleFlow.Framework.States.UpdateStateData
Assembly: TeleFlow.Framework.Core
File(s): /_/src/TeleFlow.Framework.Core/States/UpdateStateData.cs
Line coverage
89%
Covered lines: 26
Uncovered lines: 3
Coverable lines: 29
Total lines: 85
Line coverage: 89.6%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.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%
GetAsync()100%22100%
GetRequiredAsync()75%4488.88%
SetAsync(...)100%11100%
RemoveAsync(...)100%210%
ClearAsync(...)100%11100%
ValidateDataKey(...)100%11100%

File(s)

/_/src/TeleFlow.Framework.Core/States/UpdateStateData.cs

#LineLine coverage
 1namespace TeleFlow.Framework.States;
 2
 3public sealed class UpdateStateData
 4{
 5    private readonly IStateDataStore _store;
 6    private readonly IStateDataSerializer _serializer;
 7
 8    public UpdateStateData(
 9        IStateDataStore store,
 10        IStateDataSerializer serializer,
 11        StateKey key)
 12    {
 1613        ArgumentNullException.ThrowIfNull(store);
 1614        ArgumentNullException.ThrowIfNull(serializer);
 15
 1616        _store = store;
 1617        _serializer = serializer;
 18        Key = key;
 1619    }
 20
 21    public StateKey Key { get; }
 22
 23    public async ValueTask<TValue?> GetAsync<TValue>(
 24        string key,
 25        CancellationToken cancellationToken = default)
 26    {
 727        ValidateDataKey(key);
 28
 629        var value = await _store.GetDataAsync(Key, key, cancellationToken).ConfigureAwait(false);
 630        return value is null
 631            ? default
 632            : _serializer.Deserialize<TValue>(value);
 633    }
 34
 35    public async ValueTask<TValue> GetRequiredAsync<TValue>(
 36        string key,
 37        CancellationToken cancellationToken = default)
 38    {
 839        ValidateDataKey(key);
 40
 841        var value = await _store.GetDataAsync(Key, key, cancellationToken).ConfigureAwait(false);
 842        if (value is null)
 43        {
 144            throw new KeyNotFoundException($"State data key '{key}' was not found.");
 45        }
 46
 747        var result = _serializer.Deserialize<TValue>(value);
 748        if (result is null)
 49        {
 050            throw new InvalidOperationException($"State data key '{key}' deserialized to null.");
 51        }
 52
 753        return result;
 754    }
 55
 56    public ValueTask SetAsync<TValue>(
 57        string key,
 58        TValue value,
 59        CancellationToken cancellationToken = default)
 60    {
 1261        ValidateDataKey(key);
 1262        ArgumentNullException.ThrowIfNull(value);
 63
 1164        var serialized = _serializer.Serialize(value);
 1165        return _store.SetDataAsync(Key, key, serialized, cancellationToken);
 66    }
 67
 68    public ValueTask RemoveAsync(
 69        string key,
 70        CancellationToken cancellationToken = default)
 71    {
 072        ValidateDataKey(key);
 073        return _store.RemoveDataAsync(Key, key, cancellationToken);
 74    }
 75
 76    public ValueTask ClearAsync(CancellationToken cancellationToken = default)
 77    {
 978        return _store.ClearDataAsync(Key, cancellationToken);
 79    }
 80
 81    private static void ValidateDataKey(string key)
 82    {
 2783        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 2684    }
 85}