| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Results; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a machine-readable reason associated with an operation result. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class OperationReason |
| | | 9 | | { |
| | 6 | 10 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 6 | 11 | | new ReadOnlyDictionary<string, string>( |
| | 6 | 12 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="OperationReason"/> class. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 18 | | /// <param name="message">The human-readable reason message.</param> |
| | | 19 | | public OperationReason(string code, string message) |
| | 245 | 20 | | : this(code, message, null) |
| | | 21 | | { |
| | 239 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="OperationReason"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 28 | | /// <param name="message">The human-readable reason message.</param> |
| | | 29 | | /// <param name="metadata">Optional metadata associated with the reason.</param> |
| | 283 | 30 | | public OperationReason( |
| | 283 | 31 | | string code, |
| | 283 | 32 | | string message, |
| | 283 | 33 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 34 | | { |
| | 283 | 35 | | ArgumentException.ThrowIfNullOrWhiteSpace(code); |
| | 280 | 36 | | ArgumentException.ThrowIfNullOrWhiteSpace(message); |
| | | 37 | | |
| | 277 | 38 | | Code = code.Trim(); |
| | 277 | 39 | | Message = message.Trim(); |
| | 277 | 40 | | Metadata = NormalizeMetadata(metadata); |
| | 277 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the machine-readable reason code. |
| | | 45 | | /// </summary> |
| | 359 | 46 | | public string Code { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the human-readable reason message. |
| | | 50 | | /// </summary> |
| | 116 | 51 | | public string Message { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets optional metadata associated with the reason. |
| | | 55 | | /// </summary> |
| | 64 | 56 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets a value indicating whether this reason has metadata. |
| | | 60 | | /// </summary> |
| | 8 | 61 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Creates an operation reason. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 67 | | /// <param name="message">The human-readable reason message.</param> |
| | | 68 | | /// <returns>An operation reason.</returns> |
| | | 69 | | public static OperationReason Create(string code, string message) |
| | | 70 | | { |
| | 245 | 71 | | return new OperationReason(code, message); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Creates an operation reason with metadata. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="code">The machine-readable reason code.</param> |
| | | 78 | | /// <param name="message">The human-readable reason message.</param> |
| | | 79 | | /// <param name="metadata">Optional metadata associated with the reason.</param> |
| | | 80 | | /// <returns>An operation reason.</returns> |
| | | 81 | | public static OperationReason Create( |
| | | 82 | | string code, |
| | | 83 | | string message, |
| | | 84 | | IReadOnlyDictionary<string, string> metadata) |
| | | 85 | | { |
| | 37 | 86 | | return new OperationReason(code, message, metadata); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 90 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 91 | | { |
| | 277 | 92 | | if (metadata is null || metadata.Count == 0) |
| | | 93 | | { |
| | 241 | 94 | | return EmptyMetadata; |
| | | 95 | | } |
| | | 96 | | |
| | 36 | 97 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 98 | | |
| | 286 | 99 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 100 | | { |
| | 107 | 101 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 102 | | { |
| | | 103 | | continue; |
| | | 104 | | } |
| | | 105 | | |
| | 104 | 106 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 107 | | } |
| | | 108 | | |
| | 36 | 109 | | return normalizedMetadata.Count == 0 |
| | 36 | 110 | | ? EmptyMetadata |
| | 36 | 111 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 112 | | } |
| | | 113 | | } |