Skip to content

Added EnumCursorKeySerializer #8307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,21 @@ public static (Expression<Func<T, bool>> WhereExpression, int Offset) BuildWhere
{
var handledKey = handled[j];

keyExpr = Expression.Equal(
Expression.Call(ReplaceParameter(handledKey.Expression, parameter), handledKey.CompareMethod,
cursorExpr[j]), zero);
if (handledKey.Expression.ReturnType.IsEnum)
{
keyExpr = Expression.Equal(
ReplaceParameter(handledKey.Expression, parameter),
cursorExpr[j]);
}
else
{
keyExpr = Expression.Equal(
Expression.Call(
ReplaceParameter(handledKey.Expression, parameter),
handledKey.CompareMethod,
cursorExpr[j]),
zero);
}

current = current is null ? keyExpr : Expression.AndAlso(current, keyExpr);
}
Expand All @@ -88,13 +100,36 @@ public static (Expression<Func<T, bool>> WhereExpression, int Offset) BuildWhere
? key.Direction == CursorKeyDirection.Ascending
: key.Direction == CursorKeyDirection.Descending;

keyExpr = greaterThan
? Expression.GreaterThan(
Expression.Call(ReplaceParameter(key.Expression, parameter), key.CompareMethod, cursorExpr[i]),
zero)
: Expression.LessThan(
Expression.Call(ReplaceParameter(key.Expression, parameter), key.CompareMethod, cursorExpr[i]),
zero);
if (key.Expression.ReturnType.IsEnum)
{
var underlyingType = Enum.GetUnderlyingType(key.Expression.ReturnType);

keyExpr = greaterThan
? Expression.GreaterThan(
Expression.Convert(
ReplaceParameter(key.Expression, parameter), underlyingType),
Expression.Convert(cursorExpr[i], underlyingType))
: Expression.LessThan(
Expression.Convert(
ReplaceParameter(key.Expression, parameter), underlyingType),
Expression.Convert(cursorExpr[i], underlyingType));
}
else
{
keyExpr = greaterThan
? Expression.GreaterThan(
Expression.Call(
ReplaceParameter(key.Expression, parameter),
key.CompareMethod,
cursorExpr[i]),
zero)
: Expression.LessThan(
Expression.Call(
ReplaceParameter(key.Expression, parameter),
key.CompareMethod,
cursorExpr[i]),
zero);
}

current = current is null ? keyExpr : Expression.AndAlso(current, keyExpr);
expression = expression is null ? current : Expression.OrElse(expression, current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public static class CursorKeySerializerRegistration
new BoolCursorKeySerializer(),
new UShortCursorKeySerializer(),
new UIntCursorKeySerializer(),
new ULongCursorKeySerializer()
new ULongCursorKeySerializer(),
new EnumCursorKeySerializer<byte>(),
new EnumCursorKeySerializer<sbyte>(),
new EnumCursorKeySerializer<short>(),
new EnumCursorKeySerializer<ushort>(),
new EnumCursorKeySerializer<int>(),
new EnumCursorKeySerializer<uint>(),
new EnumCursorKeySerializer<long>(),
new EnumCursorKeySerializer<ulong>()
];

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Buffers.Text;
using System.Numerics;
using System.Reflection;

namespace GreenDonut.Data.Cursors.Serializers;

internal sealed class EnumCursorKeySerializer<T> : ICursorKeySerializer where T : struct, INumber<T>
{
private static readonly MethodInfo _compareTo = CompareToResolver.GetCompareToMethod<T>();

public bool IsSupported(Type type)
=> type.IsEnum && Enum.GetUnderlyingType(type) == typeof(T);

public MethodInfo GetCompareToMethod(Type type)
=> _compareTo;

public object Parse(ReadOnlySpan<byte> formattedKey)
{
var t = typeof(T);

return t switch
{
_ when t == typeof(byte) && Utf8Parser.TryParse(formattedKey, out byte b, out _)
=> b,
_ when t == typeof(sbyte) && Utf8Parser.TryParse(formattedKey, out sbyte sb, out _)
=> sb,
_ when t == typeof(short) && Utf8Parser.TryParse(formattedKey, out short s, out _)
=> s,
_ when t == typeof(ushort) && Utf8Parser.TryParse(formattedKey, out ushort us, out _)
=> us,
_ when t == typeof(int) && Utf8Parser.TryParse(formattedKey, out int i, out _)
=> i,
_ when t == typeof(uint) && Utf8Parser.TryParse(formattedKey, out uint ui, out _)
=> ui,
_ when t == typeof(long) && Utf8Parser.TryParse(formattedKey, out long l, out _)
=> l,
_ when t == typeof(ulong) && Utf8Parser.TryParse(formattedKey, out ulong ul, out _)
=> ul,
_ => throw new InvalidOperationException("Unsupported enum type.")
};
}

public bool TryFormat(object key, Span<byte> buffer, out int written)
{
var t = typeof(T);

return t switch
{
_ when t == typeof(byte) => Utf8Formatter.TryFormat((byte)key, buffer, out written),
_ when t == typeof(sbyte) => Utf8Formatter.TryFormat((sbyte)key, buffer, out written),
_ when t == typeof(short) => Utf8Formatter.TryFormat((short)key, buffer, out written),
_ when t == typeof(ushort) => Utf8Formatter.TryFormat((ushort)key, buffer, out written),
_ when t == typeof(int) => Utf8Formatter.TryFormat((int)key, buffer, out written),
_ when t == typeof(uint) => Utf8Formatter.TryFormat((uint)key, buffer, out written),
_ when t == typeof(long) => Utf8Formatter.TryFormat((long)key, buffer, out written),
_ when t == typeof(ulong) => Utf8Formatter.TryFormat((ulong)key, buffer, out written),
_ => throw new InvalidOperationException("Unsupported enum type.")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,15 @@ public async Task Fetch_First_2_Items_Second_Page_Descending_AllTypes()
{ "TimeOnly", context.Tests.OrderByDescending(t => t.TimeOnly) },
{ "UInt", context.Tests.OrderByDescending(t => t.UInt) },
{ "ULong", context.Tests.OrderByDescending(t => t.ULong) },
{ "UShort", context.Tests.OrderByDescending(t => t.UShort) }
{ "UShort", context.Tests.OrderByDescending(t => t.UShort) },
{ "ByteEnum", context.Tests.OrderByDescending(t => t.ByteEnum) },
{ "SbyteEnum", context.Tests.OrderByDescending(t => t.SbyteEnum) },
{ "ShortEnum", context.Tests.OrderByDescending(t => t.ShortEnum) },
{ "UshortEnum", context.Tests.OrderByDescending(t => t.UshortEnum) },
{ "IntEnum", context.Tests.OrderByDescending(t => t.IntEnum) },
{ "UintEnum", context.Tests.OrderByDescending(t => t.UintEnum) },
{ "LongEnum", context.Tests.OrderByDescending(t => t.LongEnum) },
{ "UlongEnum", context.Tests.OrderByDescending(t => t.UlongEnum) }
};

// Act
Expand All @@ -434,7 +442,16 @@ public async Task Fetch_First_2_Items_Second_Page_Descending_AllTypes()
}

// Assert
pages.MatchMarkdownSnapshot();
pages.ToDictionary(
p => p.Key,
p =>
p.Value.Select(
t =>
new
{
t.Id,
Value = t.GetType().GetProperty(p.Key)?.GetValue(t)
})).MatchMarkdownSnapshot();
}

private static async Task SeedAsync(string connectionString)
Expand Down Expand Up @@ -475,19 +492,19 @@ private static async Task SeedTestAsync(string connectionString)
await using var context = new CatalogContext(connectionString);
await context.Database.EnsureCreatedAsync();

for (var i = 1; i <= 10; i++)
for (var i = 1; i <= 8; i++)
{
var test = new Test
{
Id = i,
Bool = i % 2 == 0,
Bool = i > 4,
DateOnly = DateOnly.FromDateTime(DateTime.UnixEpoch.AddDays(i - 1)),
DateTime = DateTime.UnixEpoch.AddDays(i - 1),
DateTimeOffset = DateTimeOffset.UnixEpoch.AddDays(i - 1),
Decimal = i,
Double = i,
Float = i,
Guid = Guid.ParseExact($"0000000000000000000000000000000{i - 1}", "N"),
Guid = Guid.ParseExact($"0000000000000000000000000000000{i}", "N"),
Int = i,
Long = i,
Short = (short)i,
Expand All @@ -496,7 +513,15 @@ private static async Task SeedTestAsync(string connectionString)
TimeSpan = TimeSpan.FromHours(i),
UInt = (uint)i,
ULong = (ulong)i,
UShort = (ushort)i
UShort = (ushort)i,
ByteEnum = i > 4 ? TestByteEnum.Two : TestByteEnum.One,
SbyteEnum = i > 4 ? TestSbyteEnum.Two : TestSbyteEnum.One,
ShortEnum = i > 4 ? TestShortEnum.Two : TestShortEnum.One,
UshortEnum = i > 4 ? TestUshortEnum.Two : TestUshortEnum.One,
IntEnum = i > 4 ? TestIntEnum.Two : TestIntEnum.One,
UintEnum = i > 4 ? TestUintEnum.Two : TestUintEnum.One,
LongEnum = i > 4 ? TestLongEnum.Two : TestLongEnum.One,
UlongEnum = i > 4 ? TestUlongEnum.Two : TestUlongEnum.One
};

context.Tests.Add(test);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,68 @@ public class Test
public ulong ULong { get; set; }

public ushort UShort { get; set; }

public TestByteEnum ByteEnum { get; set; }

public TestSbyteEnum SbyteEnum { get; set; }

public TestShortEnum ShortEnum { get; set; }

public TestUshortEnum UshortEnum { get; set; }

public TestIntEnum IntEnum { get; set; }

public TestUintEnum UintEnum { get; set; }

public TestLongEnum LongEnum { get; set; }

public TestUlongEnum UlongEnum { get; set; }
}

public enum TestByteEnum : byte
{
One = 1,
Two = 2
}

public enum TestSbyteEnum : sbyte
{
One = 1,
Two = 2
}

public enum TestShortEnum : short
{
One = 1,
Two = 2
}

public enum TestUshortEnum : ushort
{
One = 1,
Two = 2
}

public enum TestIntEnum
{
One = 1,
Two = 2
}

public enum TestUintEnum : uint
{
One = 1,
Two = 2
}

public enum TestLongEnum : long
{
One = 1,
Two = 2
}

public enum TestUlongEnum : ulong
{
One = 1,
Two = 2
}
Loading