|
| 1 | +using System.Collections; |
| 2 | + |
| 3 | +using k8s; |
| 4 | +using k8s.Models; |
| 5 | + |
| 6 | +namespace KubeOps.Operator.Logging; |
| 7 | + |
| 8 | +#pragma warning disable CA1710 |
| 9 | +internal sealed record EntityLoggingScope : IReadOnlyCollection<KeyValuePair<string, object>> |
| 10 | +#pragma warning restore CA1710 |
| 11 | +{ |
| 12 | + private EntityLoggingScope(IReadOnlyDictionary<string, object> state) |
| 13 | + { |
| 14 | + Values = state; |
| 15 | + } |
| 16 | + |
| 17 | + public int Count => Values.Count; |
| 18 | + |
| 19 | + private string? CachedFormattedString { get; set; } |
| 20 | + |
| 21 | + private IReadOnlyDictionary<string, object> Values { get; } |
| 22 | + |
| 23 | + public static EntityLoggingScope CreateFor<TEntity>(WatchEventType eventType, TEntity entity) |
| 24 | + where TEntity : IKubernetesObject<V1ObjectMeta> |
| 25 | + => new( |
| 26 | + new Dictionary<string, object> |
| 27 | + { |
| 28 | + { "EventType", eventType }, |
| 29 | + { nameof(entity.Kind), entity.Kind }, |
| 30 | + { "Namespace", entity.Namespace() }, |
| 31 | + { "Name", entity.Name() }, |
| 32 | + { "ResourceVersion", entity.ResourceVersion() }, |
| 33 | + }); |
| 34 | + |
| 35 | + public IEnumerator<KeyValuePair<string, object>> GetEnumerator() |
| 36 | + => Values.GetEnumerator(); |
| 37 | + |
| 38 | + public override string ToString() |
| 39 | + => CachedFormattedString ??= $"{{ {string.Join(", ", Values.Select(kvp => $"{kvp.Key} = {kvp.Value}"))} }}"; |
| 40 | + |
| 41 | + IEnumerator IEnumerable.GetEnumerator() |
| 42 | + => GetEnumerator(); |
| 43 | +} |
0 commit comments