Skip to content
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

Add Enum support in keyset #54

Merged
merged 3 commits into from
Feb 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,23 @@ protected static BinaryExpression MakeComparisonExpression<T>(
else
{
return compare(
memberAccess,
EnsureMatchingType(memberAccess, referenceValue));
EnsureAdditionalConversions(memberAccess),
EnsureAdditionalConversions(EnsureMatchingType(memberAccess, referenceValue)));
}
}

private static Expression EnsureAdditionalConversions(Expression expression)
{
if (expression.Type.IsEnum)
{
var enumUnderlyingType = Enum.GetUnderlyingType(expression.Type);

return Expression.Convert(expression, enumUnderlyingType);
}

return expression;
}

protected static Expression EnsureMatchingType(
Expression memberExpression,
Expression targetExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ private void Seed(TestDbContext context)
Inner = new NestedInnerModel
{
Created = created,
mrahhal marked this conversation as resolved.
Show resolved Hide resolved
NestedEnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2,
},
Inners2 = Enumerable.Range(0, rand.Next(10)).Select(i => new NestedInner2Model()).ToList(),
EnumValue = i % 2 == 0 ? TestEnum.Value1 : TestEnum.Value2,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public enum QueryType
NullCoalescing,
NullCoalescing2,
Count,
Enum,
NestedEnum,
}

private const int Size = 10;
Expand Down Expand Up @@ -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<MainModel>> keysetPaginationBuilder = queryType switch
Expand All @@ -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(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class MainModel

public DateTime CreatedComputed { get; }

public TestEnum EnumValue { get; set; }

public NestedInnerModel Inner { get; set; }

public List<NestedInner2Model> Inners2 { get; set; }
Expand All @@ -34,6 +36,8 @@ public class NestedInnerModel
public int Id { get; set; }

public DateTime Created { get; set; }

public TestEnum NestedEnumValue { get; set; }
}

public class NestedInner2Model
Expand All @@ -44,3 +48,9 @@ public class NestedInner2Model

public MainModel MainModel { get; set; }
}

public enum TestEnum
{
Value1,
Value2,
}
Loading