< Summary

Information
Class: IceRpc.FieldsExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/FieldsExtensions.cs
Tag: 1321_24790053727
Line coverage
75%
Covered lines: 22
Uncovered lines: 7
Coverable lines: 29
Total lines: 103
Line coverage: 75.8%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage
100%
Covered methods: 5
Fully covered methods: 4
Total methods: 5
Method coverage: 100%
Full method coverage: 80%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DecodeValue(...)100%22100%
With(...)100%22100%
With(...)100%11100%
With(...)100%11100%
Without(...)50%6450%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/FieldsExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Buffers;
 4using ZeroC.Slice.Codec;
 5
 6namespace IceRpc;
 7
 8/// <summary>Provides extension method for field dictionaries.</summary>
 9public static class FieldsExtensions
 10{
 11    /// <summary>Retrieves the decoded field value associated with a field key.</summary>
 12    /// <typeparam name="TKey">The type of the field keys.</typeparam>
 13    /// <typeparam name="TValue">The type of the decoded field value.</typeparam>
 14    /// <param name="fields">The field dictionary.</param>
 15    /// <param name="key">The key to lookup in the field dictionary.</param>
 16    /// <param name="decodeFunc">The function used to decode the field value.</param>
 17    /// <returns>The decoded field value, or default if the key was not found in <paramref name="fields" />.
 18    /// </returns>
 19    public static TValue? DecodeValue<TKey, TValue>(
 20        this IDictionary<TKey, ReadOnlySequence<byte>> fields,
 21        TKey key,
 22        DecodeFunc<TValue> decodeFunc) where TKey : struct =>
 1723        fields.TryGetValue(key, out ReadOnlySequence<byte> value) ? value.DecodeSliceBuffer(decodeFunc) : default;
 24
 25    /// <summary>Sets an entry in the outgoing fields dictionary and returns the fields dictionary. If
 26    /// <paramref name="fields" /> is read-only, a copy is created, modified then returned.</summary>
 27    /// <typeparam name="TKey">The type of the field key.</typeparam>
 28    /// <param name="fields">A fields dictionary.</param>
 29    /// <param name="key">The key of the entry to set.</param>
 30    /// <param name="value">The value of the entry to set.</param>
 31    /// <returns>The fields dictionary.</returns>
 32    public static IDictionary<TKey, OutgoingFieldValue> With<TKey>(
 33        this IDictionary<TKey, OutgoingFieldValue> fields,
 34        TKey key,
 35        OutgoingFieldValue value) where TKey : struct
 3836    {
 3837        if (fields.IsReadOnly)
 3638        {
 3639            fields = new Dictionary<TKey, OutgoingFieldValue>(fields);
 3640        }
 3841        fields[key] = value;
 3842        return fields;
 3843    }
 44
 45    /// <summary>Sets an entry in the outgoing fields dictionary and returns the fields dictionary. If
 46    /// <paramref name="fields" /> is read-only, a copy is created, modified and then returned.</summary>
 47    /// <typeparam name="TKey">The type of the field key.</typeparam>
 48    /// <typeparam name="TValue">The type of the value to encode.</typeparam>
 49    /// <param name="fields">A fields dictionary.</param>
 50    /// <param name="key">The key of the entry to set.</param>
 51    /// <param name="value">The value of the entry to set.</param>
 52    /// <param name="encodeAction">The encode action.</param>
 53    /// <returns>The fields dictionary.</returns>
 54    public static IDictionary<TKey, OutgoingFieldValue> With<TKey, TValue>(
 55        this IDictionary<TKey, OutgoingFieldValue> fields,
 56        TKey key,
 57        TValue value,
 58        EncodeAction<TValue> encodeAction)
 59        where TKey : struct =>
 1360        fields.With(key, new OutgoingFieldValue(bufferWriter =>
 961        {
 962            var encoder = new SliceEncoder(bufferWriter);
 963            encodeAction(ref encoder, value);
 2164        }));
 65
 66    /// <summary>Sets an entry in the outgoing fields dictionary and returns the fields dictionary. If
 67    /// <paramref name="fields" /> is read-only, a copy is created, modified then returned.</summary>
 68    /// <typeparam name="TKey">The type of the field key.</typeparam>
 69    /// <param name="fields">A fields dictionary.</param>
 70    /// <param name="key">The key of the entry to set.</param>
 71    /// <param name="value">The value of the entry to set.</param>
 72    /// <returns>The fields dictionary.</returns>
 73    public static IDictionary<TKey, OutgoingFieldValue> With<TKey>(
 74        this IDictionary<TKey, OutgoingFieldValue> fields,
 75        TKey key,
 1076        ReadOnlySequence<byte> value) where TKey : struct => fields.With(key, new OutgoingFieldValue(value));
 77
 78    /// <summary>Removes an entry in the fields dictionary and returns the fields dictionary. If
 79    /// <paramref name="fields" /> is read-only and contains the value, a copy is created, modified then returned.
 80    /// </summary>
 81    /// <typeparam name="TKey">The type of the field key.</typeparam>
 82    /// <param name="fields">A fields dictionary.</param>
 83    /// <param name="key">The key of the entry to check.</param>
 84    /// <returns>The fields dictionary.</returns>
 85    public static IDictionary<TKey, OutgoingFieldValue> Without<TKey>(
 86        this IDictionary<TKey, OutgoingFieldValue> fields,
 87        TKey key) where TKey : struct
 188    {
 189        if (fields.IsReadOnly)
 190        {
 191            if (fields.ContainsKey(key))
 092            {
 093                fields = new Dictionary<TKey, OutgoingFieldValue>(fields);
 094                _ = fields.Remove(key);
 095            }
 196        }
 97        else
 098        {
 099            _ = fields.Remove(key);
 0100        }
 1101        return fields;
 1102    }
 103}