Skip to content

Filter by enum variable #534

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ private static string GetOperandStringForMember(MemberExpression member, bool tr
return string.Join("|", ulids);
}

if (resolvedType.IsEnum && member.Expression is ConstantExpression enumValue)
{
return Convert.ToInt32(resolved).ToString();
}

if (resolvedType.IsArray || resolvedType.GetInterfaces().Contains(typeof(IEnumerable)))
{
var asEnumerable = (IEnumerable)resolved;
Expand Down
37 changes: 37 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/FilterPredicateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,42 @@ public void TestFilterDatetimeFunction()

Assert.Equal("Blah", res[0]["FakeResult"]);
}

[Fact]
public void TestFilterEnum()
{
var expectedPredicate = "@Feel == 2";
_substitute.Execute(
"FT.AGGREGATE",
"person-idx",
"*",
"FILTER",
expectedPredicate)
.Returns(_mockReply);
var collection = new RedisAggregationSet<Person>(_substitute);

var res = collection.Filter(x => x.RecordShell.Feel == PersonFeel.Happy).ToArray();

Assert.Equal("Blah", res[0]["FakeResult"]);
}

[Fact]
public void TestFilterEnumFromVariable()
{
var expectedPredicate = "@Feel == 2";
_substitute.Execute(
"FT.AGGREGATE",
"person-idx",
"*",
"FILTER",
expectedPredicate)
.Returns(_mockReply);
var collection = new RedisAggregationSet<Person>(_substitute);

PersonFeel feel = PersonFeel.Happy;
var res = collection.Filter(x => x.RecordShell.Feel == feel).ToArray();

Assert.Equal("Blah", res[0]["FakeResult"]);
}
}
}
11 changes: 10 additions & 1 deletion test/Redis.OM.Unit.Tests/RediSearchTests/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

namespace Redis.OM.Unit.Tests.RediSearchTests
{
public enum PersonFeel : byte
{
Sad = 0,
Upset,
Happy,
Angry,
}

[Document(StorageType = StorageType.Json, IndexName = "person-idx")]
public partial class Person
{
Expand Down Expand Up @@ -72,6 +80,7 @@ public partial class Person
[Indexed(Aggregatable = true)] public string FirstName { get; set; }
[Indexed(Aggregatable = true)] public string LastName { get; set; }


[Indexed]
public PersonFeel Feel { get; set; }
}
}