| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Internal; |
| | 4 | | using System.Buffers; |
| | 5 | | using ZeroC.Slice; |
| | 6 | |
|
| | 7 | | namespace IceRpc; |
| | 8 | |
|
| | 9 | | /// <summary>Provides extension method for field dictionaries.</summary> |
| | 10 | | public 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 => |
| 18 | 24 | | fields.TryGetValue(key, out ReadOnlySequence<byte> value) ? |
| 18 | 25 | | 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 |
| 28 | 44 | | { |
| 28 | 45 | | if (fields.IsReadOnly) |
| 24 | 46 | | { |
| 24 | 47 | | fields = new Dictionary<TKey, OutgoingFieldValue>(fields); |
| 24 | 48 | | } |
| | 49 | |
|
| 28 | 50 | | fields[key] = new OutgoingFieldValue(bufferWriter => |
| 24 | 51 | | { |
| 24 | 52 | | var encoder = new SliceEncoder(bufferWriter, encoding); |
| 24 | 53 | | encodeAction(ref encoder, value); |
| 50 | 54 | | }); |
| 28 | 55 | | return fields; |
| 28 | 56 | | } |
| | 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 |
| 10 | 69 | | { |
| 10 | 70 | | if (fields.IsReadOnly) |
| 10 | 71 | | { |
| 10 | 72 | | fields = new Dictionary<TKey, OutgoingFieldValue>(fields); |
| 10 | 73 | | } |
| 10 | 74 | | fields[key] = new OutgoingFieldValue(value); |
| 10 | 75 | | return fields; |
| 10 | 76 | | } |
| | 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 |
| 1 | 88 | | { |
| 1 | 89 | | if (fields.IsReadOnly) |
| 1 | 90 | | { |
| 1 | 91 | | if (fields.ContainsKey(key)) |
| 0 | 92 | | { |
| 0 | 93 | | fields = new Dictionary<TKey, OutgoingFieldValue>(fields); |
| 0 | 94 | | _ = fields.Remove(key); |
| 0 | 95 | | } |
| 1 | 96 | | } |
| | 97 | | else |
| 0 | 98 | | { |
| 0 | 99 | | _ = fields.Remove(key); |
| 0 | 100 | | } |
| 1 | 101 | | return fields; |
| 1 | 102 | | } |
| | 103 | | } |