-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added additional checks to Result<T> and NullableResult<T> types, add…
…ed docs, v1.0.2
- Loading branch information
Showing
9 changed files
with
380 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
|
||
namespace Incendium | ||
{ | ||
/// <summary> | ||
/// Represents a nullable result type, which can contain a nullable success value or an error value | ||
/// </summary> | ||
/// <typeparam name="T">Type of value</typeparam> | ||
public readonly struct NullableResult<T> | ||
{ | ||
/// <summary> | ||
/// Gets nullable success value | ||
/// </summary> | ||
public T? Value { get; init; } | ||
/// <summary> | ||
/// Gets nullable error value | ||
/// </summary> | ||
public Error? Error { get; init; } | ||
|
||
/// <summary> | ||
/// Initialize a result instance from the success nullable value | ||
/// </summary> | ||
/// <param name="value">Nullable success value</param> | ||
public NullableResult(T? value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
/// <summary> | ||
/// Initialize a result instance from the error value | ||
/// </summary> | ||
/// <param name="error">Error</param> | ||
public NullableResult(Error error) | ||
{ | ||
Error = error; | ||
} | ||
|
||
/// <summary> | ||
/// Initialize a result instance from the non-null error value with nullable type | ||
/// </summary> | ||
/// <param name="error">Non-null error value with nullable type</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public NullableResult(Error? error) | ||
{ | ||
Error = error ?? throw new ArgumentNullException(nameof(error)); | ||
} | ||
|
||
/// <summary> | ||
/// Initialize a result instance from the success nullable value | ||
/// </summary> | ||
/// <param name="value">Nullable success value</param> | ||
public static implicit operator NullableResult<T>(T? value) => new(value); | ||
/// <summary> | ||
/// Initialize a result instance from the error value | ||
/// </summary> | ||
/// <param name="error">Error</param> | ||
public static implicit operator NullableResult<T>(Error error) => new(error); | ||
/// <summary> | ||
/// Initialize a result instance from the non-null error value with nullable type | ||
/// </summary> | ||
/// <param name="error">Non-null error value with nullable type</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public static implicit operator NullableResult<T>(Error? error) => new(error); | ||
|
||
/// <summary> | ||
/// Deconstruct struct to success value and error value | ||
/// </summary> | ||
/// <param name="value">Nullable success value</param> | ||
/// <param name="error">Nullable error value</param> | ||
public void Deconstruct(out T? value, out Error? error) | ||
{ | ||
value = Value; | ||
error = Error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Incendium.Result | ||
[![License: MIT](https://img.shields.io/github/license/matsakiv/incendium)](https://opensource.org/licenses/MIT) ![NuGet Version](https://img.shields.io/nuget/v/Incendium.Result) ![NuGet Downloads](https://img.shields.io/nuget/dt/Incendium.Result) | ||
|
||
Incendium.Result is a small .NET Standard 2.1 library, which provides `Error`, `Result<T>` and `NullableResult<T>` useful types. | ||
|
||
These types allow you to return success value or error from asynchronous and synchronous methods without explicit indication of the result type when returning and with convenient type deconstruction during processing the result. | ||
|
||
These type also can be used for less error handling through exception mechanisms where possible. | ||
|
||
## Getting started | ||
|
||
### Installation | ||
|
||
`PM> Install-Package Incendium.Result` | ||
|
||
### Result`<T>` | ||
|
||
If you need to return either a not-null value or an error from a method, you can use the `Result<T>` type: | ||
|
||
```cs | ||
public async Result<string> GetStringAsync() { | ||
// ... | ||
if (condition1) { | ||
return "Test string result"; | ||
} else { | ||
return new Error(code: 123, message: "Test error"); | ||
} | ||
|
||
try { | ||
// ... | ||
} catch (Exception e) { | ||
return new Error(code: 321, message: "Test error", exception: e); | ||
} | ||
} | ||
``` | ||
|
||
Then processing the result might look like this: | ||
|
||
```cs | ||
var (str, error) = await GetStringAsync(); | ||
|
||
if (error != null) { | ||
log.LogError( | ||
error.Exception(), | ||
"Error with code {@code} and message {@message}", | ||
error.Code(), | ||
error.Message()); | ||
} | ||
``` | ||
|
||
The `Result<T>` instance can be created only from non-null value or from non-null error: | ||
|
||
```cs | ||
public Result<Foo> GetFooAsync() { | ||
return new Foo(); // correct | ||
return new Error(); // correct | ||
return (Foo)null; // incorrect, CS8600 warning, throws ArgumentNullException | ||
return (Foo)null!; // incorrect, throws ArgumentNullException | ||
return (Foo?)null; // incorrect, CS8604 warning, throws ArgumentNullException | ||
return (Foo?)null!; // incorrect, throws ArgumentNullException | ||
return (Error?)null; // incorrect, throws ArgumentNullException | ||
} | ||
``` | ||
|
||
## NullableResult`<T>` | ||
|
||
If the successful return value can be null, you must use the `NullableResult<T>` type: | ||
|
||
```cs | ||
public async NullableResult<Foo> GetFooAsync() { | ||
return new Foo(); // correct | ||
return new Error(); // correct | ||
return (Foo?)null; // correct | ||
return (Foo)null; // correct with CS8600 warning | ||
return (Foo)null!; // correct | ||
return (Error?)null; // incorrect, throws ArgumentNullException | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.