| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Emissions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Describes a minimized provider-neutral payload associated with a governance emission envelope. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// This type intentionally describes payload identity, shape, and safe diagnostics. It does not require raw protected c |
| | | 10 | | /// </remarks> |
| | | 11 | | public sealed class GovernanceEmissionPayload |
| | | 12 | | { |
| | 3 | 13 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 14 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 15 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 16 | | |
| | 54 | 17 | | private GovernanceEmissionPayload( |
| | 54 | 18 | | string payloadType, |
| | 54 | 19 | | string? schemaVersion, |
| | 54 | 20 | | string? contentType, |
| | 54 | 21 | | string? contentHash, |
| | 54 | 22 | | long? sizeBytes, |
| | 54 | 23 | | IReadOnlyDictionary<string, string> metadata) |
| | | 24 | | { |
| | 54 | 25 | | ArgumentException.ThrowIfNullOrWhiteSpace(payloadType); |
| | | 26 | | |
| | 53 | 27 | | PayloadType = payloadType.Trim(); |
| | 53 | 28 | | SchemaVersion = NormalizeOptional(schemaVersion); |
| | 53 | 29 | | ContentType = NormalizeOptional(contentType); |
| | 53 | 30 | | ContentHash = NormalizeOptional(contentHash); |
| | 53 | 31 | | SizeBytes = NormalizeSizeBytes(sizeBytes); |
| | 52 | 32 | | Metadata = metadata; |
| | 52 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the provider-neutral payload type. |
| | | 37 | | /// </summary> |
| | 39 | 38 | | public string PayloadType { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the payload schema version, when available. |
| | | 42 | | /// </summary> |
| | 37 | 43 | | public string? SchemaVersion { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the payload content type, when available. |
| | | 47 | | /// </summary> |
| | 37 | 48 | | public string? ContentType { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets a hash of the payload content, when available. |
| | | 52 | | /// </summary> |
| | 39 | 53 | | public string? ContentHash { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the payload size in bytes, when available. |
| | | 57 | | /// </summary> |
| | 37 | 58 | | public long? SizeBytes { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets minimized provider-neutral payload metadata. |
| | | 62 | | /// </summary> |
| | 35 | 63 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a value indicating whether payload metadata is present. |
| | | 67 | | /// </summary> |
| | 1 | 68 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Creates a minimized provider-neutral payload descriptor. |
| | | 72 | | /// </summary> |
| | | 73 | | public static GovernanceEmissionPayload Create( |
| | | 74 | | string payloadType, |
| | | 75 | | string? schemaVersion = null, |
| | | 76 | | string? contentType = null, |
| | | 77 | | string? contentHash = null, |
| | | 78 | | long? sizeBytes = null, |
| | | 79 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 80 | | { |
| | 54 | 81 | | return new GovernanceEmissionPayload( |
| | 54 | 82 | | payloadType, |
| | 54 | 83 | | schemaVersion, |
| | 54 | 84 | | contentType, |
| | 54 | 85 | | contentHash, |
| | 54 | 86 | | sizeBytes, |
| | 54 | 87 | | NormalizeMetadata(metadata)); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private static string? NormalizeOptional(string? value) |
| | | 91 | | { |
| | 159 | 92 | | return string.IsNullOrWhiteSpace(value) |
| | 159 | 93 | | ? null |
| | 159 | 94 | | : value.Trim(); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private static long? NormalizeSizeBytes(long? sizeBytes) |
| | | 98 | | { |
| | 53 | 99 | | return sizeBytes < 0 |
| | 53 | 100 | | ? throw new ArgumentOutOfRangeException(nameof(sizeBytes), sizeBytes, "Payload size must be greater than or |
| | 53 | 101 | | : sizeBytes; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 105 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 106 | | { |
| | 54 | 107 | | if (metadata is null || metadata.Count == 0) |
| | | 108 | | { |
| | 12 | 109 | | return EmptyMetadata; |
| | | 110 | | } |
| | | 111 | | |
| | 42 | 112 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 113 | | |
| | 180 | 114 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 115 | | { |
| | 48 | 116 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 117 | | { |
| | | 118 | | continue; |
| | | 119 | | } |
| | | 120 | | |
| | 47 | 121 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 122 | | } |
| | | 123 | | |
| | 42 | 124 | | return normalizedMetadata.Count == 0 |
| | 42 | 125 | | ? EmptyMetadata |
| | 42 | 126 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 127 | | } |
| | | 128 | | } |