< Summary

Information
Class: IceRpc.FieldsExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/FieldsExtensions.cs
Tag: 275_13775359185
Line coverage
80%
Covered lines: 29
Uncovered lines: 7
Coverable lines: 36
Total lines: 103
Line coverage: 80.5%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage
100%
Covered methods: 4
Total methods: 4
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DecodeValue(...)100%22100%
With(...)100%22100%
With(...)100%22100%
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 IceRpc.Internal;
 4using System.Buffers;
 5using ZeroC.Slice;
 6
 7namespace IceRpc;
 8
 9/// <summary>Provides extension method for field dictionaries.</summary>
 10public static class FieldsExtensions
 11{
 12    /// <summary>Retrieves the decoded field value associated with a field key.</summary>
 13    /// <typeparam name="TKey">The type of the field keys.</typeparam>
 14    /// <typeparam name="TValue">The type of the decoded field value.</typeparam>
 15    /// <param name="fields">The field dictionary.</param>
 16    /// <param name="key">The key to lookup in the field dictionary.</param>
 17    /// <param name="decodeFunc">The function used to decode the field value.</param>
 18    /// <returns>The decoded field value, or default if the key was not found in <paramref name="fields" />.
 19    /// </returns>
 20    public static TValue? DecodeValue<TKey, TValue>(
 21        this IDictionary<TKey, ReadOnlySequence<byte>> fields,
 22        TKey key,
 23        DecodeFunc<TValue> decodeFunc) where TKey : struct =>
 1824        fields.TryGetValue(key, out ReadOnlySequence<byte> value) ?
 1825            SliceEncoding.Slice2.DecodeBuffer(value, decodeFunc) : default;
 26
 27    /// <summary>Sets an entry in the outgoing fields dictionary and returns the fields dictionary. If
 28    /// <paramref name="fields" /> is read-only, a copy is created, modified and then returned.</summary>
 29    /// <typeparam name="TKey">The type of the field key.</typeparam>
 30    /// <typeparam name="TValue">The type of the value to encode.</typeparam>
 31    /// <param name="fields">A fields dictionary.</param>
 32    /// <param name="key">The key of the entry to set.</param>
 33    /// <param name="value">The value of the entry to set.</param>
 34    /// <param name="encodeAction">The encode action.</param>
 35    /// <param name="encoding">The encoding.</param>
 36    /// <returns>The fields dictionary.</returns>
 37    public static IDictionary<TKey, OutgoingFieldValue> With<TKey, TValue>(
 38        this IDictionary<TKey, OutgoingFieldValue> fields,
 39        TKey key,
 40        TValue value,
 41        EncodeAction<TValue> encodeAction,
 42        SliceEncoding encoding = SliceEncoding.Slice2)
 43        where TKey : struct
 2844    {
 2845        if (fields.IsReadOnly)
 2446        {
 2447            fields = new Dictionary<TKey, OutgoingFieldValue>(fields);
 2448        }
 49
 2850        fields[key] = new OutgoingFieldValue(bufferWriter =>
 2451        {
 2452            var encoder = new SliceEncoder(bufferWriter, encoding);
 2453            encodeAction(ref encoder, value);
 5054        });
 2855        return fields;
 2856    }
 57
 58    /// <summary>Sets an entry in the outgoing fields dictionary and returns the fields dictionary. If
 59    /// <paramref name="fields" /> is read-only, a copy is created, modified then returned.</summary>
 60    /// <typeparam name="TKey">The type of the field key.</typeparam>
 61    /// <param name="fields">A fields dictionary.</param>
 62    /// <param name="key">The key of the entry to set.</param>
 63    /// <param name="value">The value of the entry to set.</param>
 64    /// <returns>The fields dictionary.</returns>
 65    public static IDictionary<TKey, OutgoingFieldValue> With<TKey>(
 66        this IDictionary<TKey, OutgoingFieldValue> fields,
 67        TKey key,
 68        ReadOnlySequence<byte> value) where TKey : struct
 1069    {
 1070        if (fields.IsReadOnly)
 1071        {
 1072            fields = new Dictionary<TKey, OutgoingFieldValue>(fields);
 1073        }
 1074        fields[key] = new OutgoingFieldValue(value);
 1075        return fields;
 1076    }
 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}