Skip to content

Commit

Permalink
Managing possible null on redisvalue implicit operator
Browse files Browse the repository at this point in the history
  • Loading branch information
imperugo committed Jan 20, 2025
1 parent 79ca503 commit 0f9b260
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public interface ISerializer
/// <returns>
/// The instance of the specified Item
/// </returns>
T? Deserialize<T>(byte[] serializedObject);
T? Deserialize<T>(byte[]? serializedObject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public RedisClientFactory(IEnumerable<RedisConfiguration> configurations, ILogge
defaultConnectionName = configuration.Name;
}

if (hashSet.Contains(configuration.Name!))
if (!hashSet.Add(configuration.Name))
throw new ArgumentException($"{nameof(RedisConfiguration.Name)} must be unique");

hashSet.Add(configuration.Name!);
}

if (!hasDefaultConfigured)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Task<bool> HashExistsAsync(string hashKey, string key, CommandFlags flag
{
var redisValue = await Database.HashGetAsync(hashKey, key, flag).ConfigureAwait(false);

return redisValue.HasValue ? Serializer.Deserialize<T>(redisValue!) : default;
return redisValue.HasValue ? Serializer.Deserialize<T>(redisValue) : default;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -126,7 +126,7 @@ await Parallel.ForEachAsync(keys, async (key, _) =>
return (await Database.HashGetAllAsync(hashKey, flag).ConfigureAwait(false))
.ToDictionary(
x => x.Name.ToString(),
x => Serializer.Deserialize<T>(x.Value!),
x => Serializer.Deserialize<T>(x.Value),
StringComparer.Ordinal);
}

Expand Down Expand Up @@ -177,12 +177,12 @@ public Task HashSetAsync<T>(string hashKey, IDictionary<string, T> values, Comma
{
return (await Database.HashValuesAsync(hashKey, flag)
.ConfigureAwait(false))
.ToFastArray(x => Serializer.Deserialize<T>(x!));
.ToFastArray(x => Serializer.Deserialize<T>(x));
}

/// <inheritdoc/>
public Dictionary<string, T?> HashScan<T>(string hashKey, string pattern, int pageSize = 10, CommandFlags flag = CommandFlags.None)
{
return Database.HashScan(hashKey, pattern, pageSize, flag).ToDictionary(x => x.Name.ToString(), x => Serializer.Deserialize<T>(x.Value!), StringComparer.Ordinal);
return Database.HashScan(hashKey, pattern, pageSize, flag).ToDictionary(x => x.Name.ToString(), x => Serializer.Deserialize<T>(x.Value), StringComparer.Ordinal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public Task<long> ListAddToLeftAsync<T>(string key, T[] items, CommandFlags flag

return item == RedisValue.Null
? default
: Serializer.Deserialize<T>(item!);
: Serializer.Deserialize<T>(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Task SubscribeAsync<T>(RedisChannel channel, Func<T?, Task> handler, Comm
return sub.SubscribeAsync(channel, Handler, flag);

void Handler(RedisChannel redisChannel, RedisValue value) =>
_ = handler(Serializer.Deserialize<T>(value!)).ConfigureAwait(false);
_ = handler(Serializer.Deserialize<T>(value)).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -38,7 +38,7 @@ public Task UnsubscribeAsync<T>(RedisChannel channel, Func<T?, Task> handler, Co
throw new ArgumentNullException(nameof(handler));

var sub = connectionPoolManager.GetConnection().GetSubscriber();
return sub.UnsubscribeAsync(channel, (_, value) => handler(Serializer.Deserialize<T>(value!)), flag);
return sub.UnsubscribeAsync(channel, (_, value) => handler(Serializer.Deserialize<T>(value)), flag);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Task<bool> SortedSetRemoveAsync<T>(
{
var result = await Database.SortedSetRangeByScoreAsync(key, start, stop, exclude, order, skip, take, flag).ConfigureAwait(false);

return result.Select(m => m == RedisValue.Null ? default : Serializer.Deserialize<T>(m!));
return result.Select(m => m == RedisValue.Null ? default : Serializer.Deserialize<T>(m));
}

/// <inheritdoc/>
Expand All @@ -60,6 +60,6 @@ public async Task<IEnumerable<ScoreRankResult<T>>> SortedSetRangeByRankWithScore
{
var result = await Database.SortedSetRangeByRankWithScoresAsync(key, start, stop, order, commandFlags).ConfigureAwait(false);

return result.ToFastArray(x => new ScoreRankResult<T>(Serializer.Deserialize<T>(x.Element!), x.Score));
return result.ToFastArray(x => new ScoreRankResult<T>(Serializer.Deserialize<T>(x.Element), x.Score));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Task<long> RemoveAllAsync(string[] keys, CommandFlags flag = CommandFlags

return !valueBytes.HasValue
? default
: Serializer.Deserialize<T>(valueBytes!);
: Serializer.Deserialize<T>(valueBytes);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -189,7 +189,7 @@ public Task<bool> ReplaceAsync<T>(string key, T value, TimeSpan expiresIn, When
var value = result[index];
dict.Add(redisKeys[index]!, value == RedisValue.Null
? default
: Serializer.Deserialize<T>(value!));
: Serializer.Deserialize<T>(value));
}

return dict;
Expand Down Expand Up @@ -286,7 +286,7 @@ public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = Command

return item == RedisValue.Null
? default
: Serializer.Deserialize<T>(item!);
: Serializer.Deserialize<T>(item);
}

/// <inheritdoc/>
Expand All @@ -297,7 +297,7 @@ public Task<bool> SetAddAsync<T>(string key, T item, CommandFlags flag = Command

var items = await Database.SetPopAsync(key, count, flag).ConfigureAwait(false);

return items.Select(item => item == RedisValue.Null ? default : Serializer.Deserialize<T>(item!));
return items.Select(item => item == RedisValue.Null ? default : Serializer.Deserialize<T>(item));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -382,7 +382,7 @@ public async Task<T[]> SetMembersAsync<T>(string key, CommandFlags flag = Comman
if (members.Length == 0)
return [];

return members.ToFastArray(m => Serializer.Deserialize<T>(m!)!);
return members.ToFastArray(m => Serializer.Deserialize<T>(m)!);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public byte[] Serialize<T>(T? item)
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

return global::MemoryPack.MemoryPackSerializer.Deserialize<T>(serializedObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public MsgPackObjectSerializer(Action<SerializerRepository>? customSerializerReg
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

if (typeof(T) == typeof(string))
return (T)Convert.ChangeType(encoding.GetString(serializedObject), typeof(T), CultureInfo.InvariantCulture);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class NewtonsoftSerializer(JsonSerializerSettings? settings) : ISerialize
/// Initializes a new instance of the <see cref="NewtonsoftSerializer"/> class.
/// </summary>
public NewtonsoftSerializer()
: this(default)
: this(null)
{
}

Expand All @@ -48,8 +48,11 @@ public byte[] Serialize<T>(T? item)
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

var jsonString = encoding.GetString(serializedObject);
return JsonConvert.DeserializeObject<T>(jsonString, settings)!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public byte[] Serialize<T>(T? item)
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

using var ms = new MemoryStream(serializedObject);

return Serializer.Deserialize<T>(ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public ServiceStackJsonSerializer()
}

/// <inheritdoc/>
public T Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

var json = JsConfig.UTF8Encoding.GetString(serializedObject);
return JsonSerializer.DeserializeFromString<T>(json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public SystemTextJsonSerializer()
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
if (serializedObject == null)
return default;

return JsonSerializer.Deserialize<T>(serializedObject, defaultSerializer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public byte[] Serialize<T>(T? item)
}

/// <inheritdoc/>
public T? Deserialize<T>(byte[] serializedObject)
public T? Deserialize<T>(byte[]? serializedObject)
{
return JsonSerializer.Deserialize<T>(serializedObject);
return JsonSerializer.Deserialize<T?>(serializedObject);
}
}
Loading

0 comments on commit 0f9b260

Please sign in to comment.