Skip to content
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

ISerializer,IRedisDatabase Non-generic Enhance #330

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public partial interface IRedisDatabase
/// </returns>
Task<T> GetAsync<T>(string key, TimeSpan expiresIn, CommandFlags flag = CommandFlags.None);

/// <summary>
/// Get the object with the specified key from Redis database
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="returnType">The type of the object to convert to and return.</param>
/// <param name="flag">Behaviour markers associated with a given command</param>
/// <returns>Null if not present, otherwise the instance of T.</returns>
Task<object> GetAsync(string key, Type returnType, CommandFlags flag = CommandFlags.None);

/// <summary>
/// Adds the specified instance to the Redis database.
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion src/core/StackExchange.Redis.Extensions.Core/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System;

namespace StackExchange.Redis.Extensions.Core
{
Expand All @@ -23,5 +23,15 @@ public interface ISerializer
/// The instance of the specified Item
/// </returns>
T Deserialize<T>(byte[] serializedObject);

/// <summary>
/// Deserializes the specified bytes.
/// </summary>
/// <param name="serializedObject">The serialized object.</param>
/// <param name="returnType">The type of the object to convert to and return.</param>
/// <returns>
/// The instance of the specified Item
/// </returns>
object Deserialize(byte[] serializedObject, Type returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public async Task<T> GetAsync<T>(string key, TimeSpan expiresIn, CommandFlags fl
return result;
}

/// <inheritdoc/>
public async Task<object> GetAsync(string key, Type returnType, CommandFlags flag = CommandFlags.None)
{
var valueBytes = await Database.StringGetAsync(key, flag).ConfigureAwait(false);

if (!valueBytes.HasValue)
return default;

return Serializer.Deserialize(valueBytes, returnType);
}

/// <inheritdoc/>
public Task<bool> AddAsync<T>(string key, T value, When when = When.Always, CommandFlags flag = CommandFlags.None)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using StackExchange.Redis.Extensions.Core;
Expand All @@ -20,6 +21,14 @@ public T Deserialize<T>(byte[] serializedObject)
return (T)binaryFormatter.Deserialize(ms);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
using var ms = new MemoryStream(serializedObject);

return binaryFormatter.Deserialize(ms);
}

/// <inheritdoc/>
public byte[] Serialize(object item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ public T Deserialize<T>(byte[] serializedObject)
var jsonString = encoding.GetString(serializedObject);
return JSON.Deserialize<T>(jsonString);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
var jsonString = encoding.GetString(serializedObject);
return JSON.Deserialize(jsonString, returnType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public T Deserialize<T>(byte[] serializedObject)
return serializer.Unpack(byteStream);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
if (returnType == typeof(string))
{
return Convert.ChangeType(encoding.GetString(serializedObject), returnType);
}

var serializer = MessagePackSerializer.Get(returnType);

using var byteStream = new MemoryStream(serializedObject);

return serializer.Unpack(byteStream);
}

/// <inheritdoc/>
public byte[] Serialize(object item)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;

using Newtonsoft.Json;

Expand Down Expand Up @@ -53,5 +54,12 @@ public T Deserialize<T>(byte[] serializedObject)
var jsonString = encoding.GetString(serializedObject);
return JsonConvert.DeserializeObject<T>(jsonString, settings);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
var jsonString = encoding.GetString(serializedObject);
return JsonConvert.DeserializeObject(jsonString, returnType, settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;

using ProtoBuf;

Expand Down Expand Up @@ -28,5 +29,13 @@ public T Deserialize<T>(byte[] serializedObject)

return Serializer.Deserialize<T>(ms);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
using var ms = new MemoryStream(serializedObject);

return Serializer.Deserialize(returnType, ms);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System;
using System.Text.Json;

using StackExchange.Redis.Extensions.Core;

Expand All @@ -20,5 +21,11 @@ public byte[] Serialize(object item)
{
return JsonSerializer.SerializeToUtf8Bytes(item, SerializationOptions.Flexible);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
return JsonSerializer.Deserialize(serializedObject, returnType, SerializationOptions.Flexible);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;
using System.Threading.Tasks;

using StackExchange.Redis.Extensions.Core;
Expand Down Expand Up @@ -28,5 +29,11 @@ public T Deserialize<T>(byte[] serializedObject)
{
return global::Utf8Json.JsonSerializer.Deserialize<T>(serializedObject);
}

/// <inheritdoc/>
public object Deserialize(byte[] serializedObject, Type returnType)
{
return global::Utf8Json.JsonSerializer.NonGeneric.Deserialize(returnType, serializedObject);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

using Moq;

using StackExchange.Redis.Extensions.Binary;
using StackExchange.Redis.Extensions.Core.Abstractions;
using StackExchange.Redis.Extensions.Core.Configuration;
using StackExchange.Redis.Extensions.Core.Implementations;
using StackExchange.Redis.Extensions.Protobuf;
using StackExchange.Redis.Extensions.Tests.Extensions;
using StackExchange.Redis.Extensions.Tests.Helpers;

Expand Down Expand Up @@ -536,6 +538,53 @@ public async Task Adding_Value_Type_Should_Return_Correct_Value()
Assert.Equal(dbValue, d);
}

[Fact]
public async Task Adding_Value_Type_Should_Return_Non_Generic_Correct_Value()
{
var d = new TestClass<string> { Key = $"key21", Value = "value21" };
var added = await Sut.GetDbFromConfiguration().AddAsync("my Key", d);

// TODO: Assuming that only the type of TestClass<string> can be obtained here.
var returnType = typeof(TestClass<string>);

// TODO: Use non-generic methods to get results.
var dbValue = await Sut.GetDbFromConfiguration().GetAsync("my Key", returnType);
Assert.True(added);
Assert.True(db.KeyExists("my Key"));

// TODO: dbValue equal.
Assert.Equal(dbValue, d);
}

[Fact]
public async Task Adding_Value_Type_Validate_Generic_Return_Object()
{
var d = new TestClass<string> { Key = $"key21", Value = "value21" };
var added = await Sut.GetDbFromConfiguration().AddAsync("my Key", d);
Assert.True(added);
Assert.True(db.KeyExists("my Key"));
if (Sut.GetDbFromConfiguration().Serializer is ProtobufSerializer)
{
// TODO: ProtobufSerializer generic get object throws an ArgumentNullException.
await Assert.ThrowsAsync<ArgumentNullException>(() => Sut.GetDbFromConfiguration().GetAsync<object>("my Key"));
}
else
{
// TODO: Use generic methods to get results.
var dbValue = await Sut.GetDbFromConfiguration().GetAsync<object>("my Key");
if (Sut.GetDbFromConfiguration().Serializer is BinarySerializer)
{
// TODO: BinarySerializer dbValue equal.
Assert.Equal(dbValue, d);
}
else
{
// TODO: dbValue not equal.
Assert.NotEqual(dbValue, d);
}
}
}

[Fact]
public async Task Adding_Collection_To_Redis_Should_Work_Correctly()
{
Expand Down