Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/StructId.FunctionalTests/Functional.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -140,6 +141,26 @@ public void StringIdDoesNotImplementISpanFormattable()
Assert.IsNotAssignableFrom<ISpanFormattable>(WalletId.New("foo"));
}

[Fact]
public void GuidImplementUtf8SpanFormattable()
{
var id = ProductId.New();

Assert.IsAssignableFrom<IUtf8SpanFormattable>(id);

Span<byte> utf8Destination = new byte[36]; // Typical GUID length in string form

if (id.TryFormat(utf8Destination, out int bytesWritten, default, null))
{
var guid = new Guid(Encoding.UTF8.GetString(utf8Destination.Slice(0, bytesWritten)));
Assert.Equal(id.Value, guid);
}
else
{
Assert.Fail("TryFormat failed");
}
}

public class Context : DbContext
{
public Context(DbContextOptions<Context> options) : base(options) { }
Expand Down
12 changes: 12 additions & 0 deletions src/StructId/Templates/Utf8SpanFormattable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// <auto-generated />
#nullable enable

using System;
using StructId;

[TStructId]
file partial record struct Utf8SpanFormattable(IUtf8SpanFormattable Value) : IUtf8SpanFormattable
{
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
=> Value.TryFormat(utf8Destination, out bytesWritten, format, provider);
}
Loading