Skip to content

Commit 9c45dc7

Browse files
authored
Select fixes for JSON and value types. (#298)
1 parent 3a8a5b0 commit 9c45dc7

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/Redis.OM/RedisObjectHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal static T FromHashSet<T>(IDictionary<string, string> hash)
4747

4848
var attr = Attribute.GetCustomAttribute(typeof(T), typeof(DocumentAttribute)) as DocumentAttribute;
4949
string asJson;
50-
if (attr != null && attr.StorageType == StorageType.Json)
50+
if (attr != null && attr.StorageType == StorageType.Json && hash.ContainsKey("$"))
5151
{
5252
asJson = hash["$"];
5353
}
@@ -398,6 +398,11 @@ internal static IDictionary<string, string> BuildHashSet(this object obj)
398398
private static string SendToJson(IDictionary<string, string> hash, Type t)
399399
{
400400
var properties = t.GetProperties();
401+
if ((!properties.Any() || t == typeof(Ulid) || t == typeof(Ulid?)) && hash.Count == 1)
402+
{
403+
return $"\"{hash.First().Value}\"";
404+
}
405+
401406
var ret = "{";
402407
foreach (var property in properties)
403408
{
@@ -427,7 +432,7 @@ private static string SendToJson(IDictionary<string, string> hash, Type t)
427432

428433
ret += $"\"{propertyName}\":{hash[propertyName]},";
429434
}
430-
else if (type == typeof(string) || type == typeof(GeoLoc) || type == typeof(DateTime) || type == typeof(DateTime?) || type == typeof(DateTimeOffset))
435+
else if (type == typeof(string) || type == typeof(GeoLoc) || type == typeof(DateTime) || type == typeof(DateTime?) || type == typeof(DateTimeOffset) || type == typeof(Guid) || type == typeof(Guid?) || type == typeof(Ulid) || type == typeof(Ulid?))
431436
{
432437
if (!hash.ContainsKey(propertyName))
433438
{

test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.Linq;
5+
using System.Text.Json;
56
using System.Threading.Tasks;
67
using Microsoft.VisualBasic;
78
using Newtonsoft.Json.Linq;
@@ -969,5 +970,38 @@ public async Task TestListMultipleContains()
969970
Assert.Contains(people, x => x.Id == person1.Id);
970971
Assert.Contains(people, x => x.Id == person2.Id);
971972
}
973+
974+
[Fact]
975+
public void TestGuidSelects()
976+
{
977+
var obj = new ObjectWithStringLikeValueTypes { Guid = Guid.NewGuid() };
978+
var collection = new RedisCollection<ObjectWithStringLikeValueTypes>(_connection);
979+
var key = collection.Insert(obj);
980+
var res = collection.Where(x => x.Guid == obj.Guid).Select(x => x.Guid).ToList();
981+
Assert.NotEmpty(res);
982+
_connection.Unlink(key);
983+
}
984+
985+
[Fact]
986+
public void TestUlidSelects()
987+
{
988+
var obj = new ObjectWithStringLikeValueTypes { Ulid = Ulid.NewUlid() };
989+
var collection = new RedisCollection<ObjectWithStringLikeValueTypes>(_connection);
990+
var key = collection.Insert(obj);
991+
var res = collection.Where(x => x.Ulid == obj.Ulid).Select(x => x.Ulid).ToList();
992+
Assert.NotEmpty(res);
993+
_connection.Unlink(key);
994+
}
995+
996+
[Fact]
997+
public void TestIntSelects()
998+
{
999+
var obj = new Person { Name = "steve", Age = 33};
1000+
var collection = new RedisCollection<Person>(_connection);
1001+
collection.Insert(obj);
1002+
var res = collection.Where(x => x.Age == obj.Age).Select(x => x.Age).ToList();
1003+
Assert.NotEmpty(res);
1004+
collection.Delete(obj);
1005+
}
9721006
}
9731007
}

test/Redis.OM.Unit.Tests/Serialization/SerializationTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ public void TestExplicitlySetUlit()
9191
Assert.Equal(ulid,obj.Id);
9292
}
9393

94+
[Fact]
95+
public void TestUlidMaterialization()
96+
{
97+
var ulid = Ulid.NewUlid();
98+
var obj = new ObjectWithUlidId() {Id = ulid};
99+
var key = _connection.Set(obj);
100+
var reconstitutedObject = _connection.Get<ObjectWithUlidId>(key);
101+
Assert.Equal(ulid, reconstitutedObject.Id);
102+
}
103+
94104
[Fact]
95105
public void TestNotDefaultGuid()
96106
{

0 commit comments

Comments
 (0)