Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AsyncValidationCE binding against asyncResult #260

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 95 additions & 64 deletions src/FsToolkit.ErrorHandling/AsyncValidationCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,69 +124,100 @@ module AsyncValidationCE =
) : AsyncValidation<'left * 'right, 'error> =
AsyncValidation.zip left right

/// <summary>
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
///
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
member inline _.Source
(result: AsyncValidation<'ok, 'error>)
: AsyncValidation<'ok, 'error> =
result

let asyncValidation = AsyncValidationBuilder()

[<AutoOpen>]
module HighPriority =

// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Async<Result<'ok, 'error>>) : AsyncValidation<_, 'error> =
s
|> AsyncResult.mapError (fun e -> [ e ])

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Result<'ok, 'error>) : AsyncValidation<'ok, 'error> =
AsyncValidation.ofResult s

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
/// <returns></returns>
member inline _.Source(a: Async<'ok>) : AsyncValidation<'ok, 'error> =
async {
let! result = a
return! AsyncValidation.ok result
}

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
/// <returns></returns>
member inline _.Source(choice: Choice<'ok, 'error>) : AsyncValidation<'ok, 'error> =
AsyncValidation.ofChoice choice

/// <summary>
/// Needed to allow `for..in` and `for..do` functionality
/// </summary>
member inline _.Source(s: #seq<_>) : #seq<_> = s

[<AutoOpen>]
module LowPriority =

type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Validation<'ok, 'error>) : AsyncValidation<'ok, 'error> =
Async.retn s
[<AutoOpen>]
module LowPriority =

type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
/// <returns></returns>
member inline _.Source(a: Async<'ok>) : AsyncValidation<'ok, 'error> =
async {
let! result = a
return! AsyncValidation.ok result
}

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Result<'ok, 'error>) : AsyncValidation<'ok, 'error> =
AsyncValidation.ofResult s

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
/// <returns></returns>
member inline _.Source(choice: Choice<'ok, 'error>) : AsyncValidation<'ok, 'error> =
AsyncValidation.ofChoice choice

/// <summary>
/// Needed to allow `for..in` and `for..do` functionality
/// </summary>
member inline _.Source(s: #seq<_>) : #seq<_> = s

[<AutoOpen>]
module MediumPriority =

open System.Threading.Tasks

type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Async<Result<'ok, 'error>>) : AsyncValidation<'ok, 'error> =
AsyncResult.mapError List.singleton s

#if !FABLE_COMPILER

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Task<Result<'ok, 'error>>) : AsyncValidation<'ok, 'error> =
Async.AwaitTask s
|> AsyncResult.mapError List.singleton

#endif

[<AutoOpen>]
module HighPriority =

open System.Threading.Tasks

// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(s: Validation<'ok, 'error>) : AsyncValidation<'ok, 'error> =
Async.retn s

#if !FABLE_COMPILER

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source
(result: Task<Validation<'ok, 'error>>)
: AsyncValidation<'ok, 'error> =
Async.AwaitTask result

#endif

/// <summary>
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
///
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
member inline _.Source
(result: AsyncValidation<'ok, 'error>)
: AsyncValidation<'ok, 'error> =
result
Loading
Loading