Skip to content
This repository has been archived by the owner on Feb 7, 2022. It is now read-only.

Commit

Permalink
Method descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gmurayama committed Mar 21, 2020
1 parent 41d9585 commit 27a59b9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/MonadicResponseHandler/MonadicResponseHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MonadicResponseHandler</PackageId>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Gustavo Murayama</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/gmurayama/monadic-response-handler</RepositoryUrl>
Expand Down
132 changes: 131 additions & 1 deletion src/MonadicResponseHandler/Resolved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

namespace MonadicResponseHandler
{
/// <summary>
/// Specifies the Resolved internal state based on it's stored value
/// </summary>
public enum ResolvedType
{
Ok,
Err
}

/// <summary>
/// Set of behaviors available in Err handling
/// </summary>
public enum Behavior
{
Forward,
Expand All @@ -30,6 +36,9 @@ public BaseResolved(ErrType value)
Type = ResolvedType.Err;
}

/// <summary>
/// Resolved Value
/// </summary>
public object Value
{
get
Expand All @@ -52,8 +61,14 @@ public object Value

protected ResolvedType Type { get; }

/// <summary>
/// Evaluate if stored value is an Ok state
/// </summary>
public bool IsOk => Type == ResolvedType.Ok;

/// <summary>
/// Evaluate if stored value is an Err state
/// </summary>
public bool IsErr => Type == ResolvedType.Err;
}

Expand All @@ -63,6 +78,13 @@ public Resolved() : base(Ok()) { }

public Resolved(Err err) : base(err) { }

/// <summary>
/// Execute one of the two functions provided based on the value stored and return a value of type T
/// </summary>
/// <typeparam name="T">The return type for both functions</typeparam>
/// <param name="Ok">Function to be executed that returns a value of type T if the value stored is an Ok</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value and returns a value of type T</param>
/// <returns>A value of type T</returns>
public T Match<T>(Func<T> Ok, Func<IEnumerable<Exception>, T> Err)
{
switch (Type)
Expand All @@ -81,6 +103,12 @@ public T Match<T>(Func<T> Ok, Func<IEnumerable<Exception>, T> Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <param name="Ok">Function to be executed that returns a Resolved if the value stored is an Ok</param>
/// <param name="Err">A pre defined behaviors to occur if the stored value is an Err</param>
/// <returns>Resolved value</returns>
public Resolved Match(Func<Resolved> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -93,7 +121,7 @@ public Resolved Match(Func<Resolved> Ok, Behavior Err)
case Behavior.Forward:
return ErrResult;
case Behavior.ThrowEx:
throw new InvalidOperationException("Resolved Value was an Err and expected an Ok value. The setted behavior was to throw an Exception.");
throw new AggregateException("Resolved Value was an Err and expected an Ok value. The setted behavior was to throw an Exception.", ErrResult.Value);
default:
throw new InvalidOperationException($"Unexpected Behavior: {Err.GetType()} {Err}");
}
Expand All @@ -102,6 +130,13 @@ public Resolved Match(Func<Resolved> Ok, Behavior Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <typeparam name="T">Generic Type for OkType in Resolved</typeparam>
/// <param name="Ok">Function to be executed that returns a Resolved&lt;T&gt; if the value stored is an Ok</param>
/// <param name="Err">A pre defined behaviors to occur if the stored value is an Err</param>
/// <returns>Resolved&lt;T&gt; value</returns>
public Resolved<T> Match<T>(Func<Resolved<T>> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -123,6 +158,11 @@ public Resolved<T> Match<T>(Func<Resolved<T>> Ok, Behavior Err)
}
}

/// <summary>
/// Execute one of the two functions provided based on the value stored
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value as a parameter</param>
public void Match(Action Ok, Action<IEnumerable<Exception>> Err)
{
switch (Type)
Expand All @@ -143,14 +183,40 @@ public void Match(Action Ok, Action<IEnumerable<Exception>> Err)
}
}

/// <summary>
/// Creates an Ok
/// </summary>
/// <returns>Ok struct</returns>
public static Ok Ok() => new Ok();

/// <summary>
/// Creates an Ok<T>
/// </summary>
/// <typeparam name="T">Generic Type</typeparam>
/// <param name="value">Value of type T</param>
/// <returns>Ok<T> struct</returns>
public static Ok<T> Ok<T>(T value) => new Ok<T>(value);

/// <summary>
/// Creates an Err
/// </summary>
/// <param name="value">An IEnumerable of Exception</param>
/// <returns>Err struct</returns>
public static Err Err(IEnumerable<Exception> value) => new Err(value);

/// <summary>
/// Creates an Err receiving a single Exception instead of an IEnumerable<Exception>
/// </summary>
/// <param name="value">Exception object</param>
/// <returns>Err struct</returns>
public static Err ErrAsIEnumerable(Exception value) => new Err(new[] { value } as IEnumerable<Exception>);

/// <summary>
/// Creates an Err<T>
/// </summary>
/// <typeparam name="T">Generic Type</typeparam>
/// <param name="value">Value of type T</param>
/// <returns>Err<T> struct</returns>
public static Err<T> Err<T>(T value) => new Err<T>(value);

public static implicit operator Resolved(Ok value)
Expand All @@ -170,6 +236,13 @@ public Resolved(Ok<OkType> ok) : base(ok) { }

public Resolved(Err err) : base(err) { }

/// <summary>
/// Execute one of the two functions provided based on the value stored and return a value of type T
/// </summary>
/// <typeparam name="T">The return type for both functions</typeparam>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored vale and returns a value of type T</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value and returns a value of type T</param>
/// <returns>A value of type T</returns>
public T Match<T>(Func<OkType, T> Ok, Func<IEnumerable<Exception>, T> Err)
{
switch (Type)
Expand All @@ -193,6 +266,12 @@ public T Match<T>(Func<OkType, T> Ok, Func<IEnumerable<Exception>, T> Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value and returns a Resolved</param>
/// <param name="Err">A pre defined behaviors to occur if the stored value is an Err</param>
/// <returns>Resolved value</returns>
public Resolved Match(Func<OkType, Resolved> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -214,6 +293,12 @@ public Resolved Match(Func<OkType, Resolved> Ok, Behavior Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved&lt;OkType&gt; value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value and returns a Resolved&lt;OkType&gt;</param>
/// <param name="Err">A pre defined behaviors to occur if the stored value is an Err</param>
/// <returns>Resolved&lt;T&gt; value</returns>
public Resolved<OkType> Match(Func<OkType, Resolved<OkType>> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -235,6 +320,13 @@ public Resolved<OkType> Match(Func<OkType, Resolved<OkType>> Ok, Behavior Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved&lt;T&gt; value if the stored value is an Ok, otherwise execute a pre defined behavior.
/// </summary>
/// <typeparam name="T">Generic Type for OkType in Resolved</typeparam>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value and returns a Resolved&lt;T&gt;</param>
/// <param name="Err">A pre defined behaviors to occur if the stored value is an Err</param>
/// <returns>Resolved&lt;T&gt; value</returns>
public Resolved<T> Match<T>(Func<OkType, Resolved<T>> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -256,6 +348,11 @@ public Resolved<T> Match<T>(Func<OkType, Resolved<T>> Ok, Behavior Err)
}
}

/// <summary>
/// Execute one of the two functions provided based on the value stored
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value</param>
public void Match(Action<OkType> Ok, Action<IEnumerable<Exception>> Err)
{
switch (Type)
Expand All @@ -281,6 +378,10 @@ public void Match(Action<OkType> Ok, Action<IEnumerable<Exception>> Err)
}
}

/// <summary>
/// Try to return the stored value in an Ok struct. If it is an Err, an Exception is throwed
/// </summary>
/// <returns>Value of type OkType</returns>
public OkType Unwrap()
{
switch (Type)
Expand Down Expand Up @@ -314,6 +415,13 @@ public Resolved(Ok<OkType> ok) : base(ok) { }

public Resolved(Err<ErrType> err) : base(err) { }

/// <summary>
/// Execute one of the two functions provided based on the value stored and return a value of type T
/// </summary>
/// <typeparam name="T">The return type for both functions</typeparam>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored vale and returns a value of type T</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value and returns a value of type T</param>
/// <returns>A value of type T</returns>
public T Match<T>(Func<OkType, T> Ok, Func<ErrType, T> Err)
{
switch (Type)
Expand All @@ -337,6 +445,12 @@ public T Match<T>(Func<OkType, T> Ok, Func<ErrType, T> Err)
}
}

/// <summary>
/// Execute a function that returns a Resolved&lt;OkType, ErrType&gt; value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value and returns a Resolved&lt;OkType, ErrType&gt;</param>
/// <param name="Err">A pre defined behavior to occur if the stored value is an Err</param>
/// <returns>Resolved&lt;OkType, ErrType&gt; value</returns>
public Resolved<OkType, ErrType> Match(Func<OkType, Resolved<OkType, ErrType>> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -358,6 +472,13 @@ public Resolved<OkType, ErrType> Match(Func<OkType, Resolved<OkType, ErrType>> O
}
}

/// <summary>
/// Execute a function that returns a Resolved&lt;T, ErrType&gt; value if the stored value is an Ok, otherwise execute a pre defined behavior
/// </summary>
/// <typeparam name="T">Generic Type</typeparam>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value and returns a Resolved&lt;T, ErrType&gt;</param>
/// <param name="Err">A pre defined behavior to occur if the stored value is an Err</param>
/// <returns>Resolved&lt;T, ErrType&gt; value</returns>
public Resolved<T, ErrType> Match<T>(Func<OkType, Resolved<T, ErrType>> Ok, Behavior Err)
{
switch (Type)
Expand All @@ -379,6 +500,11 @@ public Resolved<T, ErrType> Match<T>(Func<OkType, Resolved<T, ErrType>> Ok, Beha
}
}

/// <summary>
/// Execute one of the two functions provided based on the value stored
/// </summary>
/// <param name="Ok">Function to be executed if the value stored is an Ok that receives its stored value</param>
/// <param name="Err">Function to be executed if the value stored is an Err that receives its stored value</param>
public void Match(Action<OkType> Ok, Action<ErrType> Err)
{
switch (Type)
Expand All @@ -404,6 +530,10 @@ public void Match(Action<OkType> Ok, Action<ErrType> Err)
}
}

/// <summary>
/// Try to return the stored value in an Ok struct. If it is an Err, an Exception is throwed
/// </summary>
/// <returns>Value of type OkType</returns>
public OkType Unwrap()
{
switch (Type)
Expand Down

0 comments on commit 27a59b9

Please sign in to comment.