CloudEventExtensionAttributes<TKey, TValue>
class CloudEventExtensionAttributes<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
using Azure.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Azure.Messaging
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal class CloudEventExtensionAttributes<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : class
{
private readonly Dictionary<TKey, TValue> _backingDictionary;
private static readonly HashSet<string> s_reservedAttributes = new HashSet<string> {
"specversion",
"id",
"source",
"type",
"datacontenttype",
"dataschema",
"subject",
"time",
"data",
"data_base64"
};
public TValue this[TKey key] {
get {
return _backingDictionary[key];
}
set {
ValidateAttribute(key as string, value);
_backingDictionary[key] = value;
}
}
public ICollection<TKey> Keys => _backingDictionary.Keys;
public ICollection<TValue> Values => _backingDictionary.Values;
public int Count => _backingDictionary.Count;
public bool IsReadOnly => ((ICollection<KeyValuePair<TKey, TValue>>)_backingDictionary).IsReadOnly;
public CloudEventExtensionAttributes()
{
_backingDictionary = new Dictionary<TKey, TValue>();
}
public void Add(TKey key, TValue value)
{
ValidateAttribute(key as string, value);
_backingDictionary.Add(key, value);
}
public void AddWithoutValidation(TKey key, TValue value)
{
_backingDictionary.Add(key, value);
}
public void Add([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
ValidateAttribute(item.Key as string, item.Value);
((ICollection<KeyValuePair<TKey, TValue>>)_backingDictionary).Add(item);
}
public void Clear()
{
_backingDictionary.Clear();
}
public bool Contains([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_backingDictionary).Contains(item);
}
public bool ContainsKey(TKey key)
{
return _backingDictionary.ContainsKey(key);
}
public void CopyTo([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1
})] KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey, TValue>>)_backingDictionary).CopyTo(array, arrayIndex);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1
})]
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _backingDictionary.GetEnumerator();
}
public bool Remove(TKey key)
{
return _backingDictionary.Remove(key);
}
public bool Remove([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_backingDictionary).Remove(item);
}
public bool TryGetValue(TKey key, out TValue value)
{
return _backingDictionary.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return _backingDictionary.GetEnumerator();
}
[System.Runtime.CompilerServices.NullableContext(2)]
private static void ValidateAttribute(string name, object value)
{
Argument.AssertNotNullOrEmpty(name, "name");
Argument.AssertNotNull<object>(value, "value");
if (s_reservedAttributes.Contains(name))
throw new ArgumentException("Attribute name cannot use the reserved attribute: '" + name + "'", "name");
foreach (char c in name) {
if ((c < '0' || c > '9') && (c < 'a' || c > 'z'))
throw new ArgumentException($"""{c}""" + "CloudEvent attribute names must consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set.", "name");
}
if (value is string || value is byte[] || value is ReadOnlyMemory<byte> || value is int || value is bool || value is Uri || value is DateTime || value is DateTimeOffset)
return;
throw new ArgumentException($"""{value.GetType()}""" + "Attribute values must be of type string, bool, byte, int, Uri, DateTime, or DateTimeOffset.");
}
}
}