| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using ZeroC.Slice; |
| | 4 | |
|
| | 5 | | namespace IceRpc.Slice; |
| | 6 | |
|
| | 7 | | /// <summary>Provides extension methods for <see cref="SliceDecoder" /> to decode proxies.</summary> |
| | 8 | | public static class ProxySliceDecoderExtensions |
| | 9 | | { |
| | 10 | | /// <summary>Decodes a nullable proxy struct (Slice1 only).</summary> |
| | 11 | | /// <typeparam name="TProxy">The type of the proxy struct to decode.</typeparam> |
| | 12 | | /// <param name="decoder">The Slice decoder.</param> |
| | 13 | | /// <returns>The decoded proxy, or <see langword="null" />.</returns> |
| | 14 | | public static TProxy? DecodeNullableProxy<TProxy>(this ref SliceDecoder decoder) where TProxy : struct, IProxy => |
| 38 | 15 | | decoder.DecodeNullableServiceAddress() is ServiceAddress serviceAddress ? |
| 38 | 16 | | CreateProxy<TProxy>(serviceAddress, decoder.DecodingContext) : null; |
| | 17 | |
|
| | 18 | | /// <summary>Decodes a proxy struct.</summary> |
| | 19 | | /// <typeparam name="TProxy">The type of the proxy struct to decode.</typeparam> |
| | 20 | | /// <param name="decoder">The Slice decoder.</param> |
| | 21 | | /// <returns>The decoded proxy struct.</returns> |
| | 22 | | public static TProxy DecodeProxy<TProxy>(this ref SliceDecoder decoder) where TProxy : struct, IProxy => |
| 42 | 23 | | decoder.Encoding == SliceEncoding.Slice1 ? |
| 42 | 24 | | decoder.DecodeNullableProxy<TProxy>() ?? |
| 42 | 25 | | throw new InvalidDataException("Decoded null for a non-nullable proxy.") : |
| 42 | 26 | | CreateProxy<TProxy>(decoder.DecodeServiceAddress(), decoder.DecodingContext); |
| | 27 | |
|
| | 28 | | private static TProxy CreateProxy<TProxy>(ServiceAddress serviceAddress, object? decodingContext) |
| | 29 | | where TProxy : struct, IProxy |
| 52 | 30 | | { |
| 52 | 31 | | if (decodingContext is null) |
| 41 | 32 | | { |
| 41 | 33 | | return new TProxy { Invoker = InvalidInvoker.Instance, ServiceAddress = serviceAddress }; |
| | 34 | | } |
| | 35 | | else |
| 11 | 36 | | { |
| 11 | 37 | | var baseProxy = (IProxy)decodingContext; |
| 11 | 38 | | if (serviceAddress.Protocol is null && baseProxy.ServiceAddress is not null) |
| 1 | 39 | | { |
| | 40 | | // Convert the relative service address to an absolute service address: |
| 1 | 41 | | serviceAddress = baseProxy.ServiceAddress with { Path = serviceAddress.Path }; |
| 1 | 42 | | } |
| | 43 | |
|
| 11 | 44 | | return new TProxy |
| 11 | 45 | | { |
| 11 | 46 | | EncodeOptions = baseProxy.EncodeOptions, |
| 11 | 47 | | Invoker = baseProxy.Invoker, |
| 11 | 48 | | ServiceAddress = serviceAddress |
| 11 | 49 | | }; |
| | 50 | | } |
| 52 | 51 | | } |
| | 52 | | } |