| | | 1 | | namespace AsiBackbone.Core.Metadata; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the result of optional governance metadata budget validation. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class GovernanceMetadataBudgetValidationResult |
| | | 7 | | { |
| | 63 | 8 | | private GovernanceMetadataBudgetValidationResult( |
| | 63 | 9 | | IReadOnlyDictionary<string, string> normalizedMetadata, |
| | 63 | 10 | | IReadOnlyList<string> violations, |
| | 63 | 11 | | int estimatedSerializedBytes) |
| | | 12 | | { |
| | 63 | 13 | | ArgumentNullException.ThrowIfNull(normalizedMetadata); |
| | 63 | 14 | | ArgumentNullException.ThrowIfNull(violations); |
| | | 15 | | |
| | 63 | 16 | | NormalizedMetadata = normalizedMetadata; |
| | 63 | 17 | | Violations = violations; |
| | 63 | 18 | | EstimatedSerializedBytes = estimatedSerializedBytes; |
| | 63 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets a value indicating whether the metadata satisfied the supplied budget. |
| | | 23 | | /// </summary> |
| | 39 | 24 | | public bool IsValid => Violations.Count == 0; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets normalized metadata after trimming keys and values and dropping blank keys. |
| | | 28 | | /// </summary> |
| | 16 | 29 | | public IReadOnlyDictionary<string, string> NormalizedMetadata { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets budget validation violations. Empty when <see cref="IsValid" /> is <see langword="true" />. |
| | | 33 | | /// </summary> |
| | 55 | 34 | | public IReadOnlyList<string> Violations { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the estimated UTF-8 serialized metadata size used for budget comparison. |
| | | 38 | | /// </summary> |
| | 4 | 39 | | public int EstimatedSerializedBytes { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Creates a validation result. |
| | | 43 | | /// </summary> |
| | | 44 | | public static GovernanceMetadataBudgetValidationResult Create( |
| | | 45 | | IReadOnlyDictionary<string, string> normalizedMetadata, |
| | | 46 | | IReadOnlyList<string> violations, |
| | | 47 | | int estimatedSerializedBytes) |
| | | 48 | | { |
| | 63 | 49 | | return new GovernanceMetadataBudgetValidationResult( |
| | 63 | 50 | | normalizedMetadata, |
| | 63 | 51 | | violations, |
| | 63 | 52 | | estimatedSerializedBytes); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Throws an <see cref="ArgumentException" /> when validation failed. |
| | | 57 | | /// </summary> |
| | | 58 | | public void ThrowIfInvalid(string? parameterName = null) |
| | | 59 | | { |
| | 4 | 60 | | if (IsValid) |
| | | 61 | | { |
| | 2 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | 2 | 65 | | throw new ArgumentException( |
| | 2 | 66 | | $"Metadata budget validation failed: {string.Join("; ", Violations)}", |
| | 2 | 67 | | string.IsNullOrWhiteSpace(parameterName) ? "metadata" : parameterName); |
| | | 68 | | } |
| | | 69 | | } |