Skip to content

hash parsing issue #124

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 1 commit into from
Jun 30, 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
15 changes: 15 additions & 0 deletions src/Redis.OM/RedisObjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,29 @@ private static string SendToJson(IDictionary<string, string> hash, Type t)

if (type == typeof(bool) || type == typeof(bool?))
{
if (!hash.ContainsKey(propertyName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of writing this if 3 time, changing line 344 to: if (!hash.Any(x => x.Key == propertyName)) {continue;} will be more efficient.

If I understood you correctly, the line that I offered sepose to include the cases of the if's..

@slorello89 what do you think? am I missing somthing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if only it were that easy, the thing is, you only check this if the underpinning type is not an enumerable and is not another object, the reason for this, if it's an array the property name should start with propertyname[, and if it's an object with sub-fields that we want to expand, it needs to start with propertyname., so really the only valid time to check this is these three scenarios, hence the slight duplication between the three.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds logical, I thought there was a nicer way to do it, but you know the code better than I do.

{
continue;
}

ret += $"\"{propertyName}\":{hash[propertyName].ToLower()},";
}
else if (type.IsPrimitive || type == typeof(decimal))
{
if (!hash.ContainsKey(propertyName))
{
continue;
}

ret += $"\"{propertyName}\":{hash[propertyName]},";
}
else if (type == typeof(string) || type == typeof(GeoLoc) || type == typeof(DateTime) || type == typeof(DateTime?) || type == typeof(DateTimeOffset))
{
if (!hash.ContainsKey(propertyName))
{
continue;
}

ret += $"\"{propertyName}\":\"{hash[propertyName]}\",";
}
else if (type.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests
{
[Document(IndexName = "employees_idx", StorageType = StorageType.Hash)]
public class HashObjectWithTwoPropertiesWithMatchingPrefixes
{
[Searchable(Sortable = true)] public string Name { get; set; }

[Searchable(Aggregatable = true)] public string Location { get; set; }

[Searchable(Aggregatable = true)] public int? LocationNumber { get; set; }
}
}
14 changes: 14 additions & 0 deletions test/Redis.OM.Unit.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Redis.OM.Contracts;
using Redis.OM.Modeling;
using Redis.OM.Unit.Tests.RediSearchTests;
using Xunit;

namespace Redis.OM.Unit.Tests
Expand Down Expand Up @@ -99,5 +100,18 @@ public void TestNotDefaultGuid()
Assert.NotEqual(default, guid);
Assert.NotEqual(default, obj.Id);
}

[Fact]
public void TestTwoMatchingPrefixObjects()
{
var obj = new HashObjectWithTwoPropertiesWithMatchingPrefixes
{
Name = "Bob",
LocationNumber = 10
};

var id = _connection.Set(obj);
_connection.Get<HashObjectWithTwoPropertiesWithMatchingPrefixes>(id);
}
}
}