| | | 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="AsiBackboneConstraintEvaluationContext"/> 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> |
| | 25 | 15 | | public sealed class AsiBackboneConstraintEvaluationContext( |
| | 25 | 16 | | string? correlationId = null, |
| | 25 | 17 | | string? policyVersion = null, |
| | 25 | 18 | | string? policyHash = null, |
| | 25 | 19 | | IReadOnlyDictionary<string, string>? metadata = null) : IAsiBackboneConstraintEvaluationContext |
| | | 20 | | { |
| | 1 | 21 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 1 | 22 | | new ReadOnlyDictionary<string, string>( |
| | 1 | 23 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 46 | 26 | | public string? CorrelationId { get; } = NormalizeOptional(correlationId); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 46 | 29 | | public string? PolicyVersion { get; } = NormalizeOptional(policyVersion); |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | 46 | 32 | | public string? PolicyHash { get; } = NormalizeOptional(policyHash); |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 60 | 35 | | public IReadOnlyDictionary<string, string> Metadata { get; } = NormalizeMetadata(metadata); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets a value indicating whether this context contains metadata. |
| | | 39 | | /// </summary> |
| | 8 | 40 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 41 | | |
| | | 42 | | private static string? NormalizeOptional(string? value) |
| | | 43 | | { |
| | 75 | 44 | | return string.IsNullOrWhiteSpace(value) |
| | 75 | 45 | | ? null |
| | 75 | 46 | | : value.Trim(); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 50 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 51 | | { |
| | 25 | 52 | | if (metadata is null || metadata.Count == 0) |
| | | 53 | | { |
| | 6 | 54 | | return EmptyMetadata; |
| | | 55 | | } |
| | | 56 | | |
| | 19 | 57 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 58 | | |
| | 146 | 59 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 60 | | { |
| | 54 | 61 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 62 | | { |
| | | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 51 | 66 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 67 | | } |
| | | 68 | | |
| | 19 | 69 | | return normalizedMetadata.Count == 0 |
| | 19 | 70 | | ? EmptyMetadata |
| | 19 | 71 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 72 | | } |
| | | 73 | | } |