Esempio n. 1
0
 public static Task <IActionResult> ToActionResult <T>(this EitherAsync <RfcErrorInfo, T> result)
 {
     return(result.Match(
                r => (IActionResult) new ObjectResult(r),
                l => new ObjectResult(l.Message)
     {
         StatusCode = (int)HttpStatusCode.InternalServerError
     }));
 }
 public static EitherAsync <TLeft, TRight> LogWarningLeft <TLeft, TRight>(this EitherAsync <TLeft, TRight> @this, ILogger logger, Func <TLeft, string> messageFunc)
 {
     @this.Match(
         _ => unit,
         leftCase =>
     {
         logger.LogWarning(messageFunc(leftCase));
         return(unit);
     });
     return(@this);
 }
 public static async Task ShouldBeRight <TLeft, TRight>(this EitherAsync <TLeft, TRight> @this, Action <TRight> rightValidation)
 => await @this.Match(rightValidation, Common.ThrowIfLeft);
 public static async Task ShouldBeLeft <TLeft, TRight>(this EitherAsync <TLeft, TRight> @this, Action <TLeft> leftValidation)
 => await @this.Match(Common.ThrowIfRight, leftValidation);
Esempio n. 5
0
 public MB Bind <MONADB, MB, B>(EitherAsync <L, R> ma, Func <R, MB> f) where MONADB : struct, MonadAsync <Unit, Unit, MB, B> =>
 default(MONADB).RunAsync(_ =>
                          ma.Match(
                              Left : l => default(MONADB).Fail(l),
                              Right : r => f(r),
                              Bottom : () => default(MONADB).Fail(BottomException.Default)));
Esempio n. 6
0
 public Task <Unit> Match(EitherAsync <L, R> choice, Action <L> Left, Action <R> Right, Action Bottom = null) =>
 choice.Match(Right, Left, Bottom);
Esempio n. 7
0
 public Task <C> Match <C>(EitherAsync <L, R> choice, Func <L, C> Left, Func <R, C> Right, Func <C> Bottom = null) =>
 choice.Match(Right, Left, Bottom);
Esempio n. 8
0
 public Task <Unit> Match(EitherAsync <L, R> ma, Action <R> Some, Action None) =>
 ma.Match(Some, l => None());
Esempio n. 9
0
 public Task <B> Match <B>(EitherAsync <L, R> ma, Func <R, B> Some, Func <B> None) =>
 ma.Match(Some, l => None());
Esempio n. 10
0
 public Func <Unit, Task <int> > Count(EitherAsync <L, R> fa) => _ =>
 fa.Match(r => 1, l => 0, () => 0);
 /// <summary>
 /// Converts a <see cref="EitherAsync{L, R}"/> to an <see cref="IActionResult"/>.
 /// Default right case conversion will convert units to NoContent and all other values to Ok.
 /// Default left case conversion will convert values to an InternalServerError.
 /// </summary>
 /// <typeparam name="L">The left <see cref="EitherAsync{L, R}"/> parameter type.</typeparam>
 /// <typeparam name="R">The right <see cref="EitherAsync{L, R}"/> parameter type.</typeparam>
 /// <param name="this">The <see cref="EitherAsync{L, R}"/> to convert to an <see cref="IActionResult"/>.</param>
 /// <param name="Right">Optionally, a custom conversion for the right case.</param>
 /// <param name="Left">Optionally, a custom conversion for the left case.</param>
 /// <returns>An <see cref="IActionResult"/> which can be returned from a controller.</returns>
 public static Task <IActionResult> ToActionResult <L, R>(this EitherAsync <L, R> @this, Func <R, IActionResult> Right = null, Func <L, IActionResult> Left = null) =>
 @this.Match(Right: Right ?? GetGenericSuccessResult, Left: Left ?? GetGenericErrorResult);
Esempio n. 12
0
 public static Task <IActionResult> ToActionResult <TL, TR>(this EitherAsync <TL, TR> self, Func <TR, IActionResult> right = null, Func <TL, IActionResult> left = null) =>
 self.Match(Right: right ?? GetGenericSuccessResult, Left: left ?? GetGenericErrorResult);
 /// <summary>
 /// By default, Right case is converted to 200 OK.
 /// By default, Left case is returned as 500 server error.
 /// Optionally provide functions to handle the Left and Right cases.
 /// </summary>
 /// <typeparam name="L"></typeparam>
 /// <typeparam name="R"></typeparam>
 /// <param name="either"></param>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 public static Task <IActionResult> ToActionResult <L, R>(this EitherAsync <L, R> either, Func <L, IActionResult> left = null, Func <R, IActionResult> right = null) =>
 either.Match(right ?? Ok, left ?? ServerError);
 /// <summary>
 /// Right case is converted to 200 OK.
 /// Left case is serialized as a 500 server error.
 /// </summary>
 /// <typeparam name="L"></typeparam>
 /// <typeparam name="R"></typeparam>
 /// <param name="either"></param>
 /// <returns></returns>
 public static Task <IActionResult> ToActionResult <L, R>(this EitherAsync <L, R> either) =>
 either.Match(Ok, ServerError);