Unverified Commit eef7b8d6 authored by Marc Gravell's avatar Marc Gravell Committed by GitHub

fix conversions from null RedisResult (#1392)

parent 503a279c
...@@ -96,12 +96,12 @@ internal static RedisResult TryCreate(PhysicalConnection connection, in RawResul ...@@ -96,12 +96,12 @@ internal static RedisResult TryCreate(PhysicalConnection connection, in RawResul
/// Interprets the result as a <see cref="string"/>. /// Interprets the result as a <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="string"/>.</param> /// <param name="result">The result to convert to a <see cref="string"/>.</param>
public static explicit operator string(RedisResult result) => result.AsString(); public static explicit operator string(RedisResult result) => result?.AsString();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:byte[]"/>. /// Interprets the result as a <see cref="T:byte[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:byte[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:byte[]"/>.</param>
public static explicit operator byte[](RedisResult result) => result.AsByteArray(); public static explicit operator byte[](RedisResult result) => result?.AsByteArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="double"/>. /// Interprets the result as a <see cref="double"/>.
/// </summary> /// </summary>
...@@ -132,89 +132,89 @@ internal static RedisResult TryCreate(PhysicalConnection connection, in RawResul ...@@ -132,89 +132,89 @@ internal static RedisResult TryCreate(PhysicalConnection connection, in RawResul
/// Interprets the result as a <see cref="RedisValue"/>. /// Interprets the result as a <see cref="RedisValue"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="RedisValue"/>.</param> /// <param name="result">The result to convert to a <see cref="RedisValue"/>.</param>
public static explicit operator RedisValue(RedisResult result) => result.AsRedisValue(); public static explicit operator RedisValue(RedisResult result) => result?.AsRedisValue() ?? RedisValue.Null;
/// <summary> /// <summary>
/// Interprets the result as a <see cref="RedisKey"/>. /// Interprets the result as a <see cref="RedisKey"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="RedisKey"/>.</param> /// <param name="result">The result to convert to a <see cref="RedisKey"/>.</param>
public static explicit operator RedisKey(RedisResult result) => result.AsRedisKey(); public static explicit operator RedisKey(RedisResult result) => result?.AsRedisKey() ?? default;
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:Nullable{double}"/>. /// Interprets the result as a <see cref="T:Nullable{double}"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:Nullable{double}"/>.</param> /// <param name="result">The result to convert to a <see cref="T:Nullable{double}"/>.</param>
public static explicit operator double?(RedisResult result) => result.AsNullableDouble(); public static explicit operator double?(RedisResult result) => result?.AsNullableDouble();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:Nullable{long}"/>. /// Interprets the result as a <see cref="T:Nullable{long}"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:Nullable{long}"/>.</param> /// <param name="result">The result to convert to a <see cref="T:Nullable{long}"/>.</param>
public static explicit operator long?(RedisResult result) => result.AsNullableInt64(); public static explicit operator long?(RedisResult result) => result?.AsNullableInt64();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:Nullable{ulong}"/>. /// Interprets the result as a <see cref="T:Nullable{ulong}"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:Nullable{ulong}"/>.</param> /// <param name="result">The result to convert to a <see cref="T:Nullable{ulong}"/>.</param>
[CLSCompliant(false)] [CLSCompliant(false)]
public static explicit operator ulong?(RedisResult result) => result.AsNullableUInt64(); public static explicit operator ulong?(RedisResult result) => result?.AsNullableUInt64();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:Nullable{int}"/>. /// Interprets the result as a <see cref="T:Nullable{int}"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:Nullable{int}"/>.</param> /// <param name="result">The result to convert to a <see cref="T:Nullable{int}"/>.</param>
public static explicit operator int?(RedisResult result) => result.AsNullableInt32(); public static explicit operator int?(RedisResult result) => result?.AsNullableInt32();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:Nullable{bool}"/>. /// Interprets the result as a <see cref="T:Nullable{bool}"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:Nullable{bool}"/>.</param> /// <param name="result">The result to convert to a <see cref="T:Nullable{bool}"/>.</param>
public static explicit operator bool?(RedisResult result) => result.AsNullableBoolean(); public static explicit operator bool?(RedisResult result) => result?.AsNullableBoolean();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:string[]"/>. /// Interprets the result as a <see cref="T:string[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:string[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:string[]"/>.</param>
public static explicit operator string[](RedisResult result) => result.AsStringArray(); public static explicit operator string[](RedisResult result) => result?.AsStringArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:byte[][]"/>. /// Interprets the result as a <see cref="T:byte[][]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:byte[][]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:byte[][]"/>.</param>
public static explicit operator byte[][](RedisResult result) => result.AsByteArrayArray(); public static explicit operator byte[][](RedisResult result) => result?.AsByteArrayArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:double[]"/>. /// Interprets the result as a <see cref="T:double[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:double[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:double[]"/>.</param>
public static explicit operator double[](RedisResult result) => result.AsDoubleArray(); public static explicit operator double[](RedisResult result) => result?.AsDoubleArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:long[]"/>. /// Interprets the result as a <see cref="T:long[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:long[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:long[]"/>.</param>
public static explicit operator long[](RedisResult result) => result.AsInt64Array(); public static explicit operator long[](RedisResult result) => result?.AsInt64Array();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:ulong[]"/>. /// Interprets the result as a <see cref="T:ulong[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:ulong[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:ulong[]"/>.</param>
[CLSCompliant(false)] [CLSCompliant(false)]
public static explicit operator ulong[](RedisResult result) => result.AsUInt64Array(); public static explicit operator ulong[](RedisResult result) => result?.AsUInt64Array();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:int[]"/>. /// Interprets the result as a <see cref="T:int[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:int[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:int[]"/>.</param>
public static explicit operator int[](RedisResult result) => result.AsInt32Array(); public static explicit operator int[](RedisResult result) => result?.AsInt32Array();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:bool[]"/>. /// Interprets the result as a <see cref="T:bool[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:bool[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:bool[]"/>.</param>
public static explicit operator bool[](RedisResult result) => result.AsBooleanArray(); public static explicit operator bool[](RedisResult result) => result?.AsBooleanArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:RedisValue[]"/>. /// Interprets the result as a <see cref="T:RedisValue[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:RedisValue[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:RedisValue[]"/>.</param>
public static explicit operator RedisValue[](RedisResult result) => result.AsRedisValueArray(); public static explicit operator RedisValue[](RedisResult result) => result?.AsRedisValueArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:RedisKey[]"/>. /// Interprets the result as a <see cref="T:RedisKey[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:RedisKey[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:RedisKey[]"/>.</param>
public static explicit operator RedisKey[](RedisResult result) => result.AsRedisKeyArray(); public static explicit operator RedisKey[](RedisResult result) => result?.AsRedisKeyArray();
/// <summary> /// <summary>
/// Interprets the result as a <see cref="T:RedisResult[]"/>. /// Interprets the result as a <see cref="T:RedisResult[]"/>.
/// </summary> /// </summary>
/// <param name="result">The result to convert to a <see cref="T:RedisResult[]"/>.</param> /// <param name="result">The result to convert to a <see cref="T:RedisResult[]"/>.</param>
public static explicit operator RedisResult[](RedisResult result) => result.AsRedisResultArray(); public static explicit operator RedisResult[](RedisResult result) => result?.AsRedisResultArray();
/// <summary> /// <summary>
/// Interprets a multi-bulk result with successive key/name values as a dictionary keyed by name /// Interprets a multi-bulk result with successive key/name values as a dictionary keyed by name
......
...@@ -941,5 +941,45 @@ public void ScriptWithKeyPrefixCompare() ...@@ -941,5 +941,45 @@ public void ScriptWithKeyPrefixCompare()
Assert.Equal(string.Join(",", viaArr), string.Join(",", viaArgs)); Assert.Equal(string.Join(",", viaArr), string.Join(",", viaArgs));
} }
} }
[Fact]
public void RedisResultUnderstandsNullArrayArray() => TestNullArray(RedisResult.NullArray);
[Fact]
public void RedisResultUnderstandsNullArrayNull() => TestNullArray(null);
static void TestNullArray(RedisResult value)
{
Assert.True(value == null || value.IsNull);
Assert.Null((RedisValue[])value);
Assert.Null((RedisKey[])value);
Assert.Null((bool[])value);
Assert.Null((long[])value);
Assert.Null((ulong[])value);
Assert.Null((string[])value);
Assert.Null((int[])value);
Assert.Null((double[])value);
Assert.Null((byte[][])value);
Assert.Null((RedisResult[])value);
}
[Fact]
public void RedisResultUnderstandsNullNull() => TestNullValue(null);
[Fact]
public void RedisResultUnderstandsNullValue() => TestNullValue(RedisResult.Create(RedisValue.Null, ResultType.None));
static void TestNullValue(RedisResult value)
{
Assert.True(value == null || value.IsNull);
Assert.True(((RedisValue)value).IsNull);
Assert.True(((RedisKey)value).IsNull);
Assert.Null((bool?)value);
Assert.Null((long?)value);
Assert.Null((ulong?)value);
Assert.Null((string)value);
Assert.Null((double?)value);
Assert.Null((byte[])value);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment