|
| 1 | +using System.Buffers; |
| 2 | +using System.Buffers.Text; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | + |
| 9 | +namespace JsonApiDotNetCoreTests.IntegrationTests.IdCompaction; |
| 10 | + |
| 11 | +[JsonConverter(typeof(IdJsonConverter))] |
| 12 | +public readonly struct CompactGuid(Guid value) : |
| 13 | + IComparable, |
| 14 | + IComparable<CompactGuid>, |
| 15 | + IEquatable<CompactGuid>, |
| 16 | + ISpanParsable<CompactGuid>, |
| 17 | + IUtf8SpanParsable<CompactGuid>, |
| 18 | + ISpanFormattable, |
| 19 | + IUtf8SpanFormattable |
| 20 | +{ |
| 21 | + private const int GuidByteSize = 16; |
| 22 | + private const int IdCharMaxSize = 24; |
| 23 | + |
| 24 | + public static readonly CompactGuid Empty = new(Guid.Empty); |
| 25 | + |
| 26 | + public static CompactGuid Create() |
| 27 | + { |
| 28 | + return new CompactGuid(Guid.NewGuid()); |
| 29 | + } |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + public static CompactGuid Parse(string s, IFormatProvider? provider = null) |
| 33 | + { |
| 34 | + ArgumentNullException.ThrowIfNull(s); |
| 35 | + return Parse(s.AsSpan(), provider); |
| 36 | + } |
| 37 | + |
| 38 | + /// <inheritdoc /> |
| 39 | + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out CompactGuid result) |
| 40 | + { |
| 41 | + return TryParse(s.AsSpan(), provider, out result); |
| 42 | + } |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + public static CompactGuid Parse(ReadOnlySpan<char> s, IFormatProvider? provider = null) |
| 46 | + { |
| 47 | + if (!TryParse(s, provider, out var result)) |
| 48 | + { |
| 49 | + throw new ArgumentException(null, nameof(s)); |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + /// <inheritdoc /> |
| 56 | + public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out CompactGuid result) |
| 57 | + { |
| 58 | + Span<byte> charBytes = stackalloc byte[IdCharMaxSize]; |
| 59 | + if (!Encoding.ASCII.TryGetBytes(s, charBytes, out var charBytesLength)) |
| 60 | + { |
| 61 | + result = default; |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + return TryParse(charBytes[..charBytesLength], provider, out result); |
| 66 | + } |
| 67 | + |
| 68 | + /// <inheritdoc /> |
| 69 | + public static CompactGuid Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider = null) |
| 70 | + { |
| 71 | + if (!TryParse(utf8Text, provider, out var result)) |
| 72 | + { |
| 73 | + throw new ArgumentException(null, nameof(utf8Text)); |
| 74 | + } |
| 75 | + |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + /// <inheritdoc /> |
| 80 | + public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out CompactGuid result) |
| 81 | + { |
| 82 | + Span<byte> valueBytes = stackalloc byte[GuidByteSize]; |
| 83 | + OperationStatus status = Base64.DecodeFromUtf8(utf8Text, valueBytes, out _, out int written); |
| 84 | + |
| 85 | + if (status != OperationStatus.Done || written < GuidByteSize) |
| 86 | + { |
| 87 | + result = default; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + Guid value = new(valueBytes); |
| 92 | + result = new CompactGuid(value); |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + public static explicit operator Guid(CompactGuid id) |
| 97 | + { |
| 98 | + return id._value; |
| 99 | + } |
| 100 | + |
| 101 | + public static bool operator ==(CompactGuid id1, CompactGuid id2) |
| 102 | + { |
| 103 | + return id1.Equals(id2); |
| 104 | + } |
| 105 | + |
| 106 | + public static bool operator !=(CompactGuid id1, CompactGuid id2) |
| 107 | + { |
| 108 | + return !id1.Equals(id2); |
| 109 | + } |
| 110 | + |
| 111 | + public static bool operator <(CompactGuid id1, CompactGuid id2) |
| 112 | + { |
| 113 | + return id1._value < id2._value; |
| 114 | + } |
| 115 | + |
| 116 | + public static bool operator >(CompactGuid id1, CompactGuid id2) |
| 117 | + { |
| 118 | + return id1._value > id2._value; |
| 119 | + } |
| 120 | + |
| 121 | + public static bool operator <=(CompactGuid id1, CompactGuid id2) |
| 122 | + { |
| 123 | + return id1.CompareTo(id2) <= 0; |
| 124 | + } |
| 125 | + |
| 126 | + public static bool operator >=(CompactGuid id1, CompactGuid id2) |
| 127 | + { |
| 128 | + return id1.CompareTo(id2) >= 0; |
| 129 | + } |
| 130 | + |
| 131 | + private readonly Guid _value = value; |
| 132 | + |
| 133 | + /// <inheritdoc /> |
| 134 | + public override bool Equals([NotNullWhen(true)] object? obj) |
| 135 | + { |
| 136 | + return base.Equals(obj); |
| 137 | + } |
| 138 | + |
| 139 | + /// <inheritdoc /> |
| 140 | + public bool Equals(CompactGuid other) |
| 141 | + { |
| 142 | + return _value.Equals(other._value); |
| 143 | + } |
| 144 | + |
| 145 | + /// <inheritdoc /> |
| 146 | + public int CompareTo(object? obj) |
| 147 | + { |
| 148 | + if (obj == null) |
| 149 | + { |
| 150 | + return 1; |
| 151 | + } |
| 152 | + |
| 153 | + if (obj is CompactGuid other) |
| 154 | + { |
| 155 | + return CompareTo(other); |
| 156 | + } |
| 157 | + |
| 158 | + throw new ArgumentException(null, nameof(obj)); |
| 159 | + } |
| 160 | + |
| 161 | + /// <inheritdoc /> |
| 162 | + public int CompareTo(CompactGuid other) |
| 163 | + { |
| 164 | + return _value.CompareTo(other._value); |
| 165 | + } |
| 166 | + |
| 167 | + /// <inheritdoc /> |
| 168 | + public override int GetHashCode() |
| 169 | + { |
| 170 | + return _value.GetHashCode(); |
| 171 | + } |
| 172 | + |
| 173 | + /// <inheritdoc /> |
| 174 | + public override string ToString() |
| 175 | + { |
| 176 | + Span<byte> valueBytes = stackalloc byte[GuidByteSize]; |
| 177 | + bool ok = _value.TryWriteBytes(valueBytes); |
| 178 | + Debug.Assert(ok); |
| 179 | + |
| 180 | + Span<byte> charBytes = stackalloc byte[IdCharMaxSize]; |
| 181 | + OperationStatus status = Base64.EncodeToUtf8(valueBytes, charBytes, out int consumed, out int written); |
| 182 | + Debug.Assert(status == OperationStatus.Done && consumed == GuidByteSize && written <= IdCharMaxSize); |
| 183 | + |
| 184 | + return Encoding.ASCII.GetString(charBytes[..written]); |
| 185 | + } |
| 186 | + |
| 187 | + /// <inheritdoc /> |
| 188 | + public string ToString(string? format, IFormatProvider? formatProvider = null) |
| 189 | + { |
| 190 | + return ToString(); |
| 191 | + } |
| 192 | + |
| 193 | + /// <inheritdoc /> |
| 194 | + public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider = null) |
| 195 | + { |
| 196 | + Span<byte> charBytes = stackalloc byte[IdCharMaxSize]; |
| 197 | + if (!TryFormat(charBytes, out int bytesWritten, format, provider)) |
| 198 | + { |
| 199 | + charsWritten = 0; |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + Debug.Assert(bytesWritten <= IdCharMaxSize); |
| 204 | + |
| 205 | + charsWritten = Encoding.ASCII.GetChars(charBytes[..bytesWritten], destination); |
| 206 | + Debug.Assert(charsWritten == bytesWritten); |
| 207 | + return true; |
| 208 | + } |
| 209 | + |
| 210 | + /// <inheritdoc /> |
| 211 | + public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider = null) |
| 212 | + { |
| 213 | + Span<byte> valueBytes = stackalloc byte[GuidByteSize]; |
| 214 | + if (!_value.TryWriteBytes(valueBytes)) |
| 215 | + { |
| 216 | + bytesWritten = 0; |
| 217 | + return false; |
| 218 | + } |
| 219 | + |
| 220 | + OperationStatus status = Base64.EncodeToUtf8(valueBytes, utf8Destination, out int consumed, out bytesWritten); |
| 221 | + Debug.Assert(status == OperationStatus.Done && consumed == GuidByteSize && bytesWritten <= IdCharMaxSize); |
| 222 | + |
| 223 | + return true; |
| 224 | + } |
| 225 | + |
| 226 | + private sealed class IdJsonConverter : JsonConverter<CompactGuid> |
| 227 | + { |
| 228 | + public override CompactGuid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 229 | + { |
| 230 | + if (reader.TokenType != JsonTokenType.String) |
| 231 | + { |
| 232 | + throw new ArgumentException("String expected"); |
| 233 | + } |
| 234 | + |
| 235 | + if (reader.HasValueSequence) |
| 236 | + { |
| 237 | + var seq = reader.ValueSequence; |
| 238 | + return Parse(seq.IsSingleSegment ? seq.FirstSpan : seq.ToArray()); |
| 239 | + } |
| 240 | + |
| 241 | + return Parse(reader.ValueSpan); |
| 242 | + } |
| 243 | + |
| 244 | + public override void Write(Utf8JsonWriter writer, CompactGuid value, JsonSerializerOptions options) |
| 245 | + { |
| 246 | + Span<byte> idBytes = stackalloc byte[IdCharMaxSize]; |
| 247 | + _ = value.TryFormat(idBytes, out _, ReadOnlySpan<char>.Empty); |
| 248 | + writer.WriteStringValue(idBytes); |
| 249 | + } |
| 250 | + } |
| 251 | +} |
0 commit comments