Skip to content

Adding comma separators to flag enums #241

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

Merged
merged 3 commits into from
Oct 26, 2022
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
7 changes: 7 additions & 0 deletions src/Redis.OM/Modeling/RedisSchemaField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private static string GetSearchFieldType(Type declaredType, SearchFieldAttribute
return TypeDeterminationUtilities.IsNumeric(declaredType) ? "NUMERIC" : "TAG";
}

private static bool IsEnumTypeFlags(Type type) => type.GetCustomAttributes(typeof(FlagsAttribute), false).Any();

private static string[] CommonSerialization(SearchFieldAttribute attr, Type declaredType, PropertyInfo propertyInfo)
{
var searchFieldType = GetSearchFieldType(declaredType, attr, propertyInfo);
Expand Down Expand Up @@ -189,6 +191,11 @@ private static string[] CommonSerialization(SearchFieldAttribute attr, Type decl
ret.Add("SEPARATOR");
ret.Add(tag.Separator.ToString());
}
else if (declaredType.IsEnum && IsEnumTypeFlags(declaredType))
{
ret.Add("SEPARATOR");
ret.Add(",");
}

if (tag.CaseSensitive)
{
Expand Down
14 changes: 14 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/EnumFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Text.Json.Serialization;

namespace Redis.OM.Unit.Tests.RediSearchTests;

[Flags]
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum EnumFlags
{
None = 0,
One = 1 << 0,
Two = 1 << 1,
Three = 1 << 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class ObjectWithStringLikeValueTypes

[Indexed]
public AnEnum AnEnumAsInt { get; set; }

[Indexed]
[JsonConverter(typeof(JsonStringEnumConverter))]
public EnumFlags Flags { get; set; }
}

[Document]
Expand Down
11 changes: 11 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,17 @@ public async Task TestStatelessCollection()
Assert.Equal(0,collection.StateManager.Snapshot.Count);
}


[Fact]
public async Task TestFlagEnumQuery()
{
var collection = new RedisCollection<ObjectWithStringLikeValueTypes>(_connection, false, 10000);
var obj = new ObjectWithStringLikeValueTypes { Flags = EnumFlags.One | EnumFlags.Two };
await collection.InsertAsync(obj);
var res = await collection.FirstOrDefaultAsync(x => x.Flags == EnumFlags.One);
Assert.NotNull(res);
}

public void CompareTimestamps(DateTime ts1, DateTime ts2)
{
Assert.Equal(ts1.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fff", CultureInfo.InvariantCulture), ts2.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fff", CultureInfo.InvariantCulture));
Expand Down
19 changes: 7 additions & 12 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public async Task TestUpdateJsonName()
var steve = collection.First(x => x.Name == "Steve");
steve.Name = "Bob";
await collection.UpdateAsync(steve);
_mock.Verify(x=>x.ExecuteAsync("EVALSHA","42","1","Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET","$.Name","\"Bob\""));
_mock.Verify(x=>x.ExecuteAsync("EVALSHA",It.IsAny<string>(),"1","Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET","$.Name","\"Bob\""));
Scripts.ShaCollection.Clear();
}

Expand Down Expand Up @@ -1630,17 +1630,12 @@ public async Task TestCreateIndexWithStringlikeValueTypes()
"1",
"Redis.OM.Unit.Tests.RediSearchTests.ObjectWithStringLikeValueTypes:",
"SCHEMA",
"$.Ulid",
"AS",
"Ulid",
"TAG", "SEPARATOR", "|",
"$.Boolean",
"AS",
"Boolean",
"TAG", "SEPARATOR", "|",
"$.Guid",
"AS", "Guid", "TAG", "SEPARATOR", "|", "$.AnEnum", "AS", "AnEnum", "TAG",
"$.AnEnumAsInt", "AS", "AnEnumAsInt","NUMERIC"
"$.Ulid", "AS", "Ulid", "TAG", "SEPARATOR", "|",
"$.Boolean", "AS", "Boolean", "TAG", "SEPARATOR", "|",
"$.Guid", "AS", "Guid", "TAG", "SEPARATOR", "|",
"$.AnEnum", "AS", "AnEnum", "TAG",
"$.AnEnumAsInt", "AS", "AnEnumAsInt","NUMERIC",
"$.Flags", "AS", "Flags", "TAG", "SEPARATOR", ","
));
}

Expand Down