Using with FsToolkit.ErrorHandling
JDeck.MessagePack returns Result values, so the FsToolkit computation
expressions work as drop-in replacements for the built-in decode {} builder.
Use result {} for first-error decoding and validation {} when decoding
must collect every error.
let builtInDecoder =
fun value -> decode {
let! number = Required.int value
return {| value = number |}
}
let resultDecoder =
fun value -> result {
let! number = Required.int value
return {| value = number |}
}
Validations
Collect-errors decoders return Result<'T, DecodeError list>.
The functions with the Col suffix produce those decoders, and they pair
with validation {}.
let values = Encode.sequence([ 1; 2 ], Encode.int)
let decoded =
Decode.sequenceCol
(fun i value ->
Required.int value
|> Result.mapError(fun err -> [ DecodeError.withIndex i err ]))
values
match decoded with
| Ok numbers -> printfn $"Values: %A{numbers}"
| Error errors -> printfn $"Errors: %A{(errors |> List.map _.message)}"
sequenceCol, mapCol, and dictCol pair with validation {}.
Each collect decoder returns one DecodeError list per value.
namespace FsToolkit
namespace FsToolkit.ErrorHandling
namespace JDeck
namespace JDeck.MessagePack
val builtInDecoder: value: Nerdbank.MessagePack.MessagePackValue -> Result<{| value: int |},DecodeError>
val value: Nerdbank.MessagePack.MessagePackValue
val decode: DecodeBuilder
val number: int
module Required
from JDeck.MessagePack.Decode
<summary> Required decoders fail when the value is missing or has the wrong kind. </summary>
<summary> Required decoders fail when the value is missing or has the wrong kind. </summary>
val int: Decoder<int>
val resultDecoder: value: Nerdbank.MessagePack.MessagePackValue -> Result<{| value: int |},DecodeError>
val result: ResultBuilder
val values: Nerdbank.MessagePack.MessagePackValue
module Encode
from JDeck.MessagePack.Encoding
val sequence: values: 'T seq * encoder: Encoder<'T> -> Nerdbank.MessagePack.MessagePackValue
val int: value: int -> Nerdbank.MessagePack.MessagePackValue
val decoded: Result<int seq,DecodeError list>
Multiple items
module Decode from JDeck.MessagePack.Decode
--------------------
module Decode from JDeck.MessagePack
module Decode from JDeck.MessagePack.Decode
--------------------
module Decode from JDeck.MessagePack
val sequenceCol: decoder: IndexedCollectErrorsDecoder<'TResult> -> value: Nerdbank.MessagePack.MessagePackValue -> Result<'TResult seq,DecodeError list>
val i: int
Multiple items
module Result from FsToolkit.ErrorHandling
--------------------
module Result from Microsoft.FSharp.Core
--------------------
type Result<'T,'TError> = | Ok of ResultValue: 'T | Error of ErrorValue: 'TError
module Result from FsToolkit.ErrorHandling
--------------------
module Result from Microsoft.FSharp.Core
--------------------
type Result<'T,'TError> = | Ok of ResultValue: 'T | Error of ErrorValue: 'TError
Multiple items
val mapError: errorMapper: ('errorInput -> 'errorOutput) -> input: Result<'ok,'errorInput> -> Result<'ok,'errorOutput>
--------------------
val mapError: mapping: ('TError -> 'U) -> result: Result<'T,'TError> -> Result<'T,'U>
val mapError: errorMapper: ('errorInput -> 'errorOutput) -> input: Result<'ok,'errorInput> -> Result<'ok,'errorOutput>
--------------------
val mapError: mapping: ('TError -> 'U) -> result: Result<'T,'TError> -> Result<'T,'U>
val err: DecodeError
Multiple items
module DecodeError from JDeck.MessagePack
--------------------
type DecodeError = { value: MessagePackValue kind: MessagePackValueKind targetType: Type message: string exn: exn option index: int option property: string option }
<summary> Error details for a failed MessagePackValue decode. </summary>
module DecodeError from JDeck.MessagePack
--------------------
type DecodeError = { value: MessagePackValue kind: MessagePackValueKind targetType: Type message: string exn: exn option index: int option property: string option }
<summary> Error details for a failed MessagePackValue decode. </summary>
val withIndex: i: int -> error: DecodeError -> DecodeError
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val numbers: int seq
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val errors: DecodeError list
Multiple items
module List from FsToolkit.ErrorHandling
--------------------
module List from Microsoft.FSharp.Collections
--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
module List from FsToolkit.ErrorHandling
--------------------
module List from Microsoft.FSharp.Collections
--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
val map: mapping: ('T -> 'U) -> list: 'T list -> 'U list
JDeck