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 1 commit
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
33 changes: 16 additions & 17 deletions src/FsToolkit.ErrorHandling/AsyncValidationCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module AsyncValidationCE =
let asyncValidation = AsyncValidationBuilder()

[<AutoOpen>]
module HighPriority =
module LowPriority =

// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
Expand All @@ -148,15 +148,19 @@ module HighPriority =
/// <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 ])
member inline _.Source(s: Async<Result<'ok, 'error>>) : AsyncValidation<'ok, 'error> =
AsyncResult.mapError List.singleton s

/// <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
member inline _.Source(s: Result<'ok, 'error list>) : AsyncValidation<'ok, 'error> =
Async.retn s

[<AutoOpen>]
module HighPriority =

type AsyncValidationBuilder with

/// <summary>
/// Method lets us transform data types into our internal representation.
Expand All @@ -168,6 +172,12 @@ module HighPriority =
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>
Expand All @@ -179,14 +189,3 @@ module HighPriority =
/// 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
31 changes: 28 additions & 3 deletions tests/FsToolkit.ErrorHandling.Tests/AsyncValidationCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ let ``AsyncValidationCE bind Tests`` =
testCaseAsync "let! Error Validation"
<| async {
let innerData = "Foo"
let expected = Error [ innerData ]
let error = Error innerData
let expected = Error [ [ innerData ] ]
1eyewonder marked this conversation as resolved.
Show resolved Hide resolved

let! actual =
asyncValidation {
let! f = validation { return! expected }
let! f = validation { return! error }
return f
}

Expand Down Expand Up @@ -448,7 +449,7 @@ let ``AsyncValidationCE applicative tests`` =
Expect.equal actual (Ok 4) "Should be ok"
}

testCaseAsync "Happy Path Result/Valiation"
testCaseAsync "Happy Path Result/Validation"
<| async {
let! actual =
asyncValidation {
Expand Down Expand Up @@ -487,6 +488,30 @@ let ``AsyncValidationCE applicative tests`` =
Expect.equal actual (Ok 4) "Should be ok"
}

testCaseAsync "Happy Path Async Result/Async Result"
1eyewonder marked this conversation as resolved.
Show resolved Hide resolved
<| async {
let! actual =
asyncValidation {
let! _ =
async {
do! Async.Sleep(1000)
printfn "Hello"
1eyewonder marked this conversation as resolved.
Show resolved Hide resolved
return Error "Hello"
}

and! _ =
async {
do! Async.Sleep(1000)
printfn "World"
1eyewonder marked this conversation as resolved.
Show resolved Hide resolved
return Error "World"
}

return ()
}

Expect.equal actual (Ok()) "Should be ok"
}

testCaseAsync "Fail Path Result"
<| async {
let expected =
Expand Down
Loading