| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Constraints; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Default framework-neutral context value used during constraint evaluation. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Initializes a new instance of the <see cref="GovernanceEvaluationContext"/> class. |
| | | 10 | | /// </remarks> |
| | | 11 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 12 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 13 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 14 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | 124 | 15 | | public sealed class GovernanceEvaluationContext( |
| | 124 | 16 | | string? correlationId = null, |
| | 124 | 17 | | string? policyVersion = null, |
| | 124 | 18 | | string? policyHash = null, |
| | 124 | 19 | | IReadOnlyDictionary<string, string>? metadata = null) : IGovernanceEvaluationContext |
| | | 20 | | { |
| | 3 | 21 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 22 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 23 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public string? CorrelationId { get; } = NormalizeOptional(correlationId); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public string? TraceId { get; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public string? PolicyVersion { get; } = NormalizeOptional(policyVersion); |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public string? PolicyHash { get; } = NormalizeOptional(policyHash); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public IReadOnlyDictionary<string, string> Metadata { get; } = NormalizeMetadata(metadata); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets a value indicating whether this context contains metadata. |
| | | 42 | | /// </summary> |
| | 9 | 43 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of the <see cref="GovernanceEvaluationContext"/> class with a trace identifier. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 49 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 50 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 51 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 52 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 53 | | public GovernanceEvaluationContext( |
| | | 54 | | string? correlationId, |
| | | 55 | | string? policyVersion, |
| | | 56 | | string? policyHash, |
| | | 57 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 58 | | string? traceId) |
| | 55 | 59 | | : this(correlationId, policyVersion, policyHash, metadata) |
| | | 60 | | { |
| | 55 | 61 | | TraceId = NormalizeOptional(traceId); |
| | 55 | 62 | | } |
| | | 63 | | |
| | | 64 | | private static string? NormalizeOptional(string? value) |
| | | 65 | | { |
| | 427 | 66 | | return string.IsNullOrWhiteSpace(value) |
| | 427 | 67 | | ? null |
| | 427 | 68 | | : value.Trim(); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 72 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 73 | | { |
| | 124 | 74 | | if (metadata is null || metadata.Count == 0) |
| | | 75 | | { |
| | 44 | 76 | | return EmptyMetadata; |
| | | 77 | | } |
| | | 78 | | |
| | 80 | 79 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 80 | | |
| | 968 | 81 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 82 | | { |
| | 404 | 83 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 84 | | { |
| | | 85 | | continue; |
| | | 86 | | } |
| | | 87 | | |
| | 400 | 88 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 89 | | } |
| | | 90 | | |
| | 80 | 91 | | return normalizedMetadata.Count == 0 |
| | 80 | 92 | | ? EmptyMetadata |
| | 80 | 93 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 94 | | } |
| | | 95 | | } |