Skip to content

Commit

Permalink
Merge pull request #114 from J-Tech-Japan:113-guid-can-not-use-checknull
Browse files Browse the repository at this point in the history
Add CheckNull method overloads for nullable value types
  • Loading branch information
tomohisa authored Jul 30, 2024
2 parents 8c16bf1 + c01534b commit e0b674d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/CheckNullSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
76 changes: 76 additions & 0 deletions src/ResultBoxes/ResultBoxes/ResultBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,59 @@ public static ResultBox<TValue> CheckNull<TValue>(TValue? value, Exception? exce
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};
public static ResultBox<TValue> CheckNull<TValue>(TValue? value, Exception? exception = null)
where TValue : struct => value switch
{
{ } v => ResultBox.FromValue(v),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};

public static async Task<ResultBox<TValue>> CheckNull<TValue>(Task<TValue?> value, Exception? exception = null)
where TValue : notnull => await value switch
{
{ } v => ResultBox.FromValue(v),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};
public static async Task<ResultBox<TValue>> CheckNull<TValue>(Task<TValue?> value, Exception? exception = null)
where TValue : struct => await value switch
{
{ } v => ResultBox.FromValue(v),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};

public static ResultBox<TValue> CheckNull<TValue>(Func<TValue?> valueFunc, Exception? exception = null)
where TValue : notnull => valueFunc() switch
{
{ } value => ResultBox.FromValue(value),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};
public static ResultBox<TValue> CheckNull<TValue>(Func<TValue?> valueFunc, Exception? exception = null)
where TValue : struct => valueFunc() switch
{
{ } value => ResultBox.FromValue(value),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};

public static async Task<ResultBox<TValue>> CheckNull<TValue>(Func<Task<TValue?>> valueFunc, Exception? exception = null)
where TValue : notnull => await valueFunc() switch
{
{ } value => ResultBox.FromValue(value),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};
public static async Task<ResultBox<TValue>> CheckNull<TValue>(Func<Task<TValue?>> valueFunc, Exception? exception = null)
where TValue : struct => await valueFunc() switch
{
{ } value => ResultBox.FromValue(value),
_ => ResultBox<TValue>.FromException(
exception ?? new ResultValueNullException(typeof(TValue).Name))
};

public static ResultBox<TValue> CheckNullWrapTry<TValue>(Func<TValue?> func, Func<Exception, Exception>? exceptionMapper = null)
where TValue : notnull
{
Expand All @@ -183,6 +222,25 @@ public static ResultBox<TValue> CheckNullWrapTry<TValue>(Func<TValue?> func, Fun
return exceptionMapper?.Invoke(e) ?? e;
}
}
public static ResultBox<TValue> CheckNullWrapTry<TValue>(Func<TValue?> func, Func<Exception, Exception>? exceptionMapper = null)
where TValue : struct
{
try
{
var result = func();
return result.HasValue switch
{
true => ResultBox<TValue>.FromValue(result.Value),
_ => ResultBox<TValue>.FromException(
new ResultValueNullException(typeof(TValue).Name)
)
};
}
catch (Exception e)
{
return exceptionMapper?.Invoke(e) ?? e;
}
}
public static async Task<ResultBox<TValue>> CheckNullWrapTry<TValue>(Func<Task<TValue?>> funcAsync, Func<Exception, Exception>? exceptionMapper = null)
where TValue : notnull
{
Expand All @@ -201,4 +259,22 @@ public static async Task<ResultBox<TValue>> CheckNullWrapTry<TValue>(Func<Task<T
return exceptionMapper?.Invoke(e) ?? e;
}
}
public static async Task<ResultBox<TValue>> CheckNullWrapTry<TValue>(Func<Task<TValue?>> funcAsync, Func<Exception, Exception>? exceptionMapper = null)
where TValue : struct
{
try
{
return await funcAsync() switch
{
{ } value => ResultBox.FromValue(value),
_ => ResultBox<TValue>.FromException(
new ResultValueNullException(typeof(TValue).Name)
)
};
}
catch (Exception e)
{
return exceptionMapper?.Invoke(e) ?? e;
}
}
}
6 changes: 3 additions & 3 deletions src/ResultBoxes/ResultBoxes/ResultBoxes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<RootNamespace>ResultBoxes</RootNamespace>
<LangVersion>preview</LangVersion>
<PackageId>ResultBoxes</PackageId>
<Version>0.3.19</Version>
<Version>0.3.20</Version>
<Authors>J-Tech Group</Authors>
<Company>J-Tech-Japan</Company>
<PackageDescription>ResultBoxes - C# Results Library that focus on Railway Programming.</PackageDescription>
<RepositoryUrl>https://github.com/J-Tech-Japan/ResultBoxes</RepositoryUrl>
<PackageVersion>0.3.19</PackageVersion>
<Description>Conveyer, Verify can take no param. Verify can take Task and Result</Description>
<PackageVersion>0.3.20</PackageVersion>
<Description>Guid? int? can use CheckNull, CheckNullWrapTry as well</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
Expand Down

0 comments on commit e0b674d

Please sign in to comment.