diff --git a/src/ResultBoxes/ResultBoxes.Test/CheckNullSpec.cs b/src/ResultBoxes/ResultBoxes.Test/CheckNullSpec.cs index f2fdc9c..7660f74 100644 --- a/src/ResultBoxes/ResultBoxes.Test/CheckNullSpec.cs +++ b/src/ResultBoxes/ResultBoxes.Test/CheckNullSpec.cs @@ -9,6 +9,37 @@ public void CheckNullTest1() Assert.True(result.IsSuccess); } + + private Guid? TestGuid(int value) => value == 0 ? null : Guid.NewGuid(); + private int? TestInt(int value) => value == 0 ? null : value; + + + [Fact] + public void CheckNullTestWithPrimitive() + { + var result = ResultBox.CheckNull(TestGuid(0)); + Assert.False(result.IsSuccess); + } + [Fact] + public void CheckNullTestWithPrimitive2() + { + var result = ResultBox.CheckNull(TestGuid(1)); + Assert.True(result.IsSuccess); + } + [Fact] + public void CheckNullTestWithPrimitiveInt() + { + var result = ResultBox.CheckNull(TestInt(0)); + Assert.False(result.IsSuccess); + } + [Fact] + public void CheckNullTestWithPrimitiveInt2() + { + var result = ResultBox.CheckNull(TestInt(1)); + Assert.True(result.IsSuccess); + Assert.Equal(1, result.GetValue()); + } + [Fact] public void CheckNullTest2() { diff --git a/src/ResultBoxes/ResultBoxes/ResultBox.cs b/src/ResultBoxes/ResultBoxes/ResultBox.cs index 9c29880..67479d9 100644 --- a/src/ResultBoxes/ResultBoxes/ResultBox.cs +++ b/src/ResultBoxes/ResultBoxes/ResultBox.cs @@ -151,6 +151,29 @@ public static ResultBox CheckNull(TValue? value, Exception? exce _ => ResultBox.FromException( exception ?? new ResultValueNullException(typeof(TValue).Name)) }; + public static ResultBox CheckNull(TValue? value, Exception? exception = null) + where TValue : struct => value switch + { + { } v => ResultBox.FromValue(v), + _ => ResultBox.FromException( + exception ?? new ResultValueNullException(typeof(TValue).Name)) + }; + + public static async Task> CheckNull(Task value, Exception? exception = null) + where TValue : notnull => await value switch + { + { } v => ResultBox.FromValue(v), + _ => ResultBox.FromException( + exception ?? new ResultValueNullException(typeof(TValue).Name)) + }; + public static async Task> CheckNull(Task value, Exception? exception = null) + where TValue : struct => await value switch + { + { } v => ResultBox.FromValue(v), + _ => ResultBox.FromException( + exception ?? new ResultValueNullException(typeof(TValue).Name)) + }; + public static ResultBox CheckNull(Func valueFunc, Exception? exception = null) where TValue : notnull => valueFunc() switch { @@ -158,6 +181,14 @@ public static ResultBox CheckNull(Func valueFunc, Excep _ => ResultBox.FromException( exception ?? new ResultValueNullException(typeof(TValue).Name)) }; + public static ResultBox CheckNull(Func valueFunc, Exception? exception = null) + where TValue : struct => valueFunc() switch + { + { } value => ResultBox.FromValue(value), + _ => ResultBox.FromException( + exception ?? new ResultValueNullException(typeof(TValue).Name)) + }; + public static async Task> CheckNull(Func> valueFunc, Exception? exception = null) where TValue : notnull => await valueFunc() switch { @@ -165,6 +196,14 @@ public static async Task> CheckNull(Func _ => ResultBox.FromException( exception ?? new ResultValueNullException(typeof(TValue).Name)) }; + public static async Task> CheckNull(Func> valueFunc, Exception? exception = null) + where TValue : struct => await valueFunc() switch + { + { } value => ResultBox.FromValue(value), + _ => ResultBox.FromException( + exception ?? new ResultValueNullException(typeof(TValue).Name)) + }; + public static ResultBox CheckNullWrapTry(Func func, Func? exceptionMapper = null) where TValue : notnull { @@ -183,6 +222,25 @@ public static ResultBox CheckNullWrapTry(Func func, Fun return exceptionMapper?.Invoke(e) ?? e; } } + public static ResultBox CheckNullWrapTry(Func func, Func? exceptionMapper = null) + where TValue : struct + { + try + { + var result = func(); + return result.HasValue switch + { + true => ResultBox.FromValue(result.Value), + _ => ResultBox.FromException( + new ResultValueNullException(typeof(TValue).Name) + ) + }; + } + catch (Exception e) + { + return exceptionMapper?.Invoke(e) ?? e; + } + } public static async Task> CheckNullWrapTry(Func> funcAsync, Func? exceptionMapper = null) where TValue : notnull { @@ -201,4 +259,22 @@ public static async Task> CheckNullWrapTry(Func> CheckNullWrapTry(Func> funcAsync, Func? exceptionMapper = null) + where TValue : struct + { + try + { + return await funcAsync() switch + { + { } value => ResultBox.FromValue(value), + _ => ResultBox.FromException( + new ResultValueNullException(typeof(TValue).Name) + ) + }; + } + catch (Exception e) + { + return exceptionMapper?.Invoke(e) ?? e; + } + } } \ No newline at end of file diff --git a/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj b/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj index 46f7734..cfeff72 100644 --- a/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj +++ b/src/ResultBoxes/ResultBoxes/ResultBoxes.csproj @@ -7,13 +7,13 @@ ResultBoxes preview ResultBoxes - 0.3.19 + 0.3.20 J-Tech Group J-Tech-Japan ResultBoxes - C# Results Library that focus on Railway Programming. https://github.com/J-Tech-Japan/ResultBoxes - 0.3.19 - Conveyer, Verify can take no param. Verify can take Task and Result + 0.3.20 + Guid? int? can use CheckNull, CheckNullWrapTry as well true README.md true