Skip to content

Commit

Permalink
Merge pull request #54 from vpachta/main
Browse files Browse the repository at this point in the history
Add Enum support in keyset
  • Loading branch information
mrahhal authored Feb 2, 2024
2 parents c1a5490 + d896998 commit 4e8c782
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
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,
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,
}

0 comments on commit 4e8c782

Please sign in to comment.