Skip to content

Commit

Permalink
Add Enum support in keyset
Browse files Browse the repository at this point in the history
- add expression for converting enum to it's underlying type
  • Loading branch information
vpachta committed Feb 1, 2024
1 parent c1a5490 commit 11c47bb
Show file tree
Hide file tree
Showing 4 changed files with 30 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));
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

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 @@ -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; }

Expand All @@ -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
Expand All @@ -44,3 +47,9 @@ public class NestedInner2Model

public MainModel MainModel { get; set; }
}

public enum EnumType
{
Value1,
Value2,
}

0 comments on commit 11c47bb

Please sign in to comment.