Skip to content

Commit

Permalink
Merge branch 'master' into add_support_voption_to_require_function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd authored Dec 10, 2023
2 parents c32eda0 + a7edd80 commit c3ec5d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ module TaskResult =
Result.requireNone error
>> Task.singleton
)

/// Bind the TaskResult and requireValueSome on the inner voption value.
let inline bindRequireValueSome error x =
x
Expand All @@ -276,3 +276,11 @@ module TaskResult =
Result.requireValueNone error
>> Task.singleton
)

let inline foldResult
([<InlineIfLambda>] onSuccess: 'input -> 'output)
([<InlineIfLambda>] onError: 'inputError -> 'output)
(input: Task<Result<'input, 'inputError>>)
: Task<'output> =
Task.map (Result.either onSuccess onError) input

23 changes: 23 additions & 0 deletions tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -869,5 +869,28 @@ let TaskResultBindRequireValueOptionTests =
|> TaskResult.ok
|> TaskResult.bindRequireValueSome "User doesn't exist"
|> Expect.hasTaskOkValue "john_doe"

[<Tests>]
let foldResultTests =
testList "TaskResult.foldResult tests" [
testCaseTask "foldResult with Task(Ok x)"
<| fun _ ->
task {
let! actual =
createPostSuccess validCreatePostRequest
|> TaskResult.foldResult (fun (PostId id) -> id.ToString()) string

Expect.same (newPostId.ToString()) actual
}

testCaseTask "foldResult with Task(Error x)"
<| fun _ ->
task {
let! actual =
createPostFailure validCreatePostRequest
|> TaskResult.foldResult string (fun ex -> ex.Message)

Expect.same (commonEx.Message) actual

}
]

0 comments on commit c3ec5d4

Please sign in to comment.