Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/FastIDs.TypeId/TypeId.Core/TypeIdDecoded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ namespace FastIDs.TypeId;
public readonly struct TypeIdDecoded : IEquatable<TypeIdDecoded>, ISpanFormattable, IUtf8SpanFormattable
{
private static readonly UuidGenerator UuidGenerator = new();

/// <summary>
/// The type part of the TypeId.
/// </summary>
public string Type { get; }

/// <summary>
/// The ID part of the TypeId.
/// </summary>
public Guid Id { get; }

internal TypeIdDecoded(string type, Guid id)
{
Type = type;
Id = id;
}

/// <summary>
/// Returns the ID part of the TypeId as an encoded string.
/// </summary>
Expand All @@ -50,7 +50,7 @@ public int GetSuffix(Span<char> output)

return Base32.Encode(idBytes, output);
}

public int GetSuffix(Span<byte> utf8Output)
{
Span<byte> idBytes = stackalloc byte[Base32Constants.DecodedLength];
Expand Down Expand Up @@ -115,7 +115,7 @@ public override string ToString()
/// This method ignores <paramref name="format"/> and <paramref name="formatProvider"/> parameters and outputs the same result as <see cref="ToString()"/>.
/// </remarks>
public string ToString(string? format, IFormatProvider? formatProvider) => ToString();

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
Expand All @@ -139,7 +139,7 @@ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan
var suffixSpan = destination[charsWritten..];
if (suffixSpan.Length < Base32Constants.EncodedLength)
return false;

var suffixCharsWritten = GetSuffix(suffixSpan);
charsWritten += suffixCharsWritten;

Expand Down Expand Up @@ -169,13 +169,22 @@ public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnly
var suffixSpan = utf8Destination[bytesWritten..];
if (suffixSpan.Length < Base32Constants.EncodedLength)
return false;

var suffixBytesWritten = GetSuffix(suffixSpan);
bytesWritten += suffixBytesWritten;

return true;
}

/// <summary>
/// Implicitly converts TypeIdDecoded to Guid by extracting the ID part.
/// </summary>
/// <param name="typeIdDecoded">The TypeIdDecoded instance.</param>
public static implicit operator Guid(TypeIdDecoded typeIdDecoded)
{
return typeIdDecoded.Id;
}

public bool Equals(TypeIdDecoded other) => Type == other.Type && Id.Equals(other.Id);

public override bool Equals(object? obj) => obj is TypeIdDecoded other && Equals(other);
Expand All @@ -196,7 +205,7 @@ public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnly
/// This method validates the type. If you are sure that type is valid use <see cref="New(string, bool)"/> to skip type validation.
/// </remarks>
public static TypeIdDecoded New(string type) => FromUuidV7(type, UuidGenerator.New());

/// <summary>
/// Generates new TypeId with the specified type and random UUIDv7. If <paramref name="validateType"/> is false, type is not validated.
/// </summary>
Expand Down