| | | 1 | | using AsiBackbone.Core.Results; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Metadata; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the provider-neutral result of governance metadata sanitation and budget validation. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class GovernanceMetadataSanitizationResult |
| | | 9 | | { |
| | 51 | 10 | | private GovernanceMetadataSanitizationResult( |
| | 51 | 11 | | GovernanceMetadataSanitizationAction action, |
| | 51 | 12 | | IReadOnlyDictionary<string, string> sanitizedMetadata, |
| | 51 | 13 | | IReadOnlyList<OperationReason> reasons, |
| | 51 | 14 | | GovernanceMetadataBudgetValidationResult budgetValidation) |
| | | 15 | | { |
| | 51 | 16 | | if (!Enum.IsDefined(action)) |
| | | 17 | | { |
| | 2 | 18 | | throw new ArgumentOutOfRangeException(nameof(action), action, "Metadata sanitation action must be defined.") |
| | | 19 | | } |
| | | 20 | | |
| | 49 | 21 | | ArgumentNullException.ThrowIfNull(sanitizedMetadata); |
| | 47 | 22 | | ArgumentNullException.ThrowIfNull(reasons); |
| | 45 | 23 | | ArgumentNullException.ThrowIfNull(budgetValidation); |
| | | 24 | | |
| | 43 | 25 | | Action = action; |
| | 43 | 26 | | SanitizedMetadata = sanitizedMetadata; |
| | 43 | 27 | | Reasons = reasons; |
| | 43 | 28 | | BudgetValidation = budgetValidation; |
| | 43 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the strongest action produced by classification, sanitation, and budget validation. |
| | | 33 | | /// </summary> |
| | 65 | 34 | | public GovernanceMetadataSanitizationAction Action { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets metadata that may be passed forward when <see cref="CanProceed" /> is <see langword="true" />. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <remarks> |
| | | 40 | | /// This collection is empty when sanitation is denied so callers cannot accidentally persist or emit a denied paylo |
| | | 41 | | /// </remarks> |
| | 14 | 42 | | public IReadOnlyDictionary<string, string> SanitizedMetadata { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets stable machine-readable reasons produced by classifiers or budget validation. |
| | | 46 | | /// </summary> |
| | 28 | 47 | | public IReadOnlyList<OperationReason> Reasons { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the budget validation result evaluated after classifier redaction and dropping are complete. |
| | | 51 | | /// </summary> |
| | 12 | 52 | | public GovernanceMetadataBudgetValidationResult BudgetValidation { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets a value indicating whether the sanitized metadata may continue to downstream processing. |
| | | 56 | | /// </summary> |
| | 55 | 57 | | public bool CanProceed => Action is not GovernanceMetadataSanitizationAction.Deny; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets a value indicating whether sanitation denied the metadata collection. |
| | | 61 | | /// </summary> |
| | 16 | 62 | | public bool IsDenied => !CanProceed; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Creates a governance metadata sanitation result. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="action">The strongest sanitation action.</param> |
| | | 68 | | /// <param name="sanitizedMetadata">The safe metadata available for downstream processing.</param> |
| | | 69 | | /// <param name="reasons">The classifier and budget reasons.</param> |
| | | 70 | | /// <param name="budgetValidation">The post-sanitation budget validation result.</param> |
| | | 71 | | /// <returns>A governance metadata sanitation result.</returns> |
| | | 72 | | public static GovernanceMetadataSanitizationResult Create( |
| | | 73 | | GovernanceMetadataSanitizationAction action, |
| | | 74 | | IReadOnlyDictionary<string, string> sanitizedMetadata, |
| | | 75 | | IReadOnlyList<OperationReason> reasons, |
| | | 76 | | GovernanceMetadataBudgetValidationResult budgetValidation) |
| | | 77 | | { |
| | 51 | 78 | | return new GovernanceMetadataSanitizationResult( |
| | 51 | 79 | | action, |
| | 51 | 80 | | sanitizedMetadata, |
| | 51 | 81 | | reasons, |
| | 51 | 82 | | budgetValidation); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Throws an <see cref="ArgumentException" /> when sanitation denied the metadata collection. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="parameterName">The optional caller parameter name associated with the metadata.</param> |
| | | 89 | | public void ThrowIfDenied(string? parameterName = null) |
| | | 90 | | { |
| | 18 | 91 | | if (CanProceed) |
| | | 92 | | { |
| | 8 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | 10 | 96 | | string message = Reasons.Count == 0 |
| | 10 | 97 | | ? "Governance metadata sanitation denied the metadata collection." |
| | 16 | 98 | | : $"Governance metadata sanitation denied the metadata collection: {string.Join("; ", Reasons.Select(reason |
| | | 99 | | |
| | 10 | 100 | | throw new ArgumentException( |
| | 10 | 101 | | message, |
| | 10 | 102 | | string.IsNullOrWhiteSpace(parameterName) ? "metadata" : parameterName); |
| | | 103 | | } |
| | | 104 | | } |