From 11c47bb7f1ac22a6bc5029f02179e4d47fb0aba6 Mon Sep 17 00:00:00 2001 From: "Pachta, Vaclav" Date: Thu, 1 Feb 2024 08:08:42 +0100 Subject: [PATCH 1/3] Add Enum support in keyset - add expression for converting enum to it's underlying type --- .../KeysetFilterPredicateStrategy.cs | 16 ++++++++++++++-- .../DatabaseFixture.cs | 1 + .../KeysetPaginationTest.cs | 6 ++++++ .../TestModels/MainModel.cs | 9 +++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs index c5dfcd9..4431cbd 100644 --- a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs +++ b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs @@ -92,11 +92,23 @@ protected static BinaryExpression MakeComparisonExpression( else { return compare( - memberAccess, - EnsureMatchingType(memberAccess, referenceValue)); + EnsureEnumIsConvertedToUnderlyingType(memberAccess), + EnsureEnumIsConvertedToUnderlyingType(EnsureMatchingType(memberAccess, referenceValue))); } } + private static Expression EnsureEnumIsConvertedToUnderlyingType(Expression memberAccess) + { + if (memberAccess.Type.IsEnum) + { + var enumUnderlyingType = Enum.GetUnderlyingType(memberAccess.Type); + + return Expression.Convert(memberAccess, enumUnderlyingType); + } + + return memberAccess; + } + protected static Expression EnsureMatchingType( Expression memberExpression, Expression targetExpression) diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs index 27b614c..1e12893 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs @@ -79,6 +79,7 @@ private void Seed(TestDbContext context) Created = created, }, Inners2 = Enumerable.Range(0, rand.Next(10)).Select(i => new NestedInner2Model()).ToList(), + EnumValue = i % 2 == 0 ? EnumType.Value1 : EnumType.Value2, }); } diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs index 68154b1..98a892b 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs @@ -25,6 +25,8 @@ public enum QueryType NullCoalescing, NullCoalescing2, Count, + Enum, + NestedEnum } private const int Size = 10; @@ -58,6 +60,8 @@ public KeysetPaginationTest(DatabaseFixture fixture) QueryType.NullCoalescing => q => q.OrderBy(x => x.CreatedNullable ?? DateTime.MinValue).ThenBy(x => x.Id), QueryType.NullCoalescing2 => q => q.OrderBy(x => x.CreatedNullable ?? x.Created).ThenBy(x => x.Id), QueryType.Count => q => q.OrderBy(x => x.Inners2.Count).ThenBy(x => x.Id), + QueryType.Enum => q => q.OrderBy(x => x.EnumValue).ThenBy(x => x.Id), + QueryType.NestedEnum => q => q.OrderBy(x => x.Inner.NestedEnumValue).ThenBy(x => x.Id), _ => throw new NotImplementedException(), }; Action> keysetPaginationBuilder = queryType switch @@ -75,6 +79,8 @@ public KeysetPaginationTest(DatabaseFixture fixture) QueryType.NullCoalescing => b => b.Ascending(x => x.CreatedNullable ?? DateTime.MinValue).Ascending(x => x.Id), QueryType.NullCoalescing2 => b => b.Ascending(x => x.CreatedNullable ?? x.Created).Ascending(x => x.Id), QueryType.Count => b => b.Ascending(x => x.Inners2.Count).Ascending(x => x.Id), + QueryType.Enum => q => q.Ascending(x => x.EnumValue).Ascending(x => x.Id), + QueryType.NestedEnum => q => q.Ascending(x => x.Inner.NestedEnumValue).Ascending(x => x.Id), _ => throw new NotImplementedException(), }; diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs index 558d97c..b056f36 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs @@ -22,6 +22,7 @@ public class MainModel public DateTime? CreatedNullable { get; set; } public DateTime CreatedComputed { get; } + public EnumType EnumValue { get; set; } public NestedInnerModel Inner { get; set; } @@ -34,6 +35,8 @@ public class NestedInnerModel public int Id { get; set; } public DateTime Created { get; set; } + + public EnumType NestedEnumValue { get; set; } } public class NestedInner2Model @@ -44,3 +47,9 @@ public class NestedInner2Model public MainModel MainModel { get; set; } } + +public enum EnumType +{ + Value1, + Value2, +} From dbb8d9bcb48480908c00898e9bf4f0803db39887 Mon Sep 17 00:00:00 2001 From: "Pachta, Vaclav" Date: Fri, 2 Feb 2024 10:25:22 +0100 Subject: [PATCH 2/3] Renaming + fill some test data for NestedEnumValue. --- .../KeysetFilterPredicateStrategy.cs | 14 +++++++------- .../DatabaseFixture.cs | 3 ++- .../TestModels/MainModel.cs | 7 ++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs index 4431cbd..a5cbeb1 100644 --- a/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs +++ b/src/MR.EntityFrameworkCore.KeysetPagination/KeysetFilterPredicateStrategy.cs @@ -92,21 +92,21 @@ protected static BinaryExpression MakeComparisonExpression( else { return compare( - EnsureEnumIsConvertedToUnderlyingType(memberAccess), - EnsureEnumIsConvertedToUnderlyingType(EnsureMatchingType(memberAccess, referenceValue))); + EnsureAdditionalConversions(memberAccess), + EnsureAdditionalConversions(EnsureMatchingType(memberAccess, referenceValue))); } } - private static Expression EnsureEnumIsConvertedToUnderlyingType(Expression memberAccess) + private static Expression EnsureAdditionalConversions(Expression expression) { - if (memberAccess.Type.IsEnum) + if (expression.Type.IsEnum) { - var enumUnderlyingType = Enum.GetUnderlyingType(memberAccess.Type); + var enumUnderlyingType = Enum.GetUnderlyingType(expression.Type); - return Expression.Convert(memberAccess, enumUnderlyingType); + return Expression.Convert(expression, enumUnderlyingType); } - return memberAccess; + return expression; } protected static Expression EnsureMatchingType( diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs index 1e12893..252e355 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/DatabaseFixture.cs @@ -77,9 +77,10 @@ private void Seed(TestDbContext context) Inner = new NestedInnerModel { Created = created, + NestedEnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2, }, Inners2 = Enumerable.Range(0, rand.Next(10)).Select(i => new NestedInner2Model()).ToList(), - EnumValue = i % 2 == 0 ? EnumType.Value1 : EnumType.Value2, + EnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2, }); } diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs index b056f36..4c12677 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/TestModels/MainModel.cs @@ -22,7 +22,8 @@ public class MainModel public DateTime? CreatedNullable { get; set; } public DateTime CreatedComputed { get; } - public EnumType EnumValue { get; set; } + + public TestEnum EnumValue { get; set; } public NestedInnerModel Inner { get; set; } @@ -36,7 +37,7 @@ public class NestedInnerModel public DateTime Created { get; set; } - public EnumType NestedEnumValue { get; set; } + public TestEnum NestedEnumValue { get; set; } } public class NestedInner2Model @@ -48,7 +49,7 @@ public class NestedInner2Model public MainModel MainModel { get; set; } } -public enum EnumType +public enum TestEnum { Value1, Value2, From d896998f1f705fd4e6f0881874fb2b135c9843c8 Mon Sep 17 00:00:00 2001 From: vpachta <154536297+vpachta@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:27:00 +0100 Subject: [PATCH 3/3] Add comma Co-authored-by: Mohammad Rahhal --- .../KeysetPaginationTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs index 98a892b..1b8913c 100644 --- a/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs +++ b/test/MR.EntityFrameworkCore.KeysetPagination.Tests/KeysetPaginationTest.cs @@ -26,7 +26,7 @@ public enum QueryType NullCoalescing2, Count, Enum, - NestedEnum + NestedEnum, } private const int Size = 10;