Using with FsToolkit.ErrorHandling
JDeck.Xml 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 el -> decode {
let! value = Required.int el
return {| value = value |}
}
let resultDecoder =
fun el -> result {
let! value = Required.int el
return {| value = value |}
}
Validations
Collect-errors decoders return Result<'T, DecodeError list>.
The functions with the Col suffix produce those decoders, and they pair
with validation {}.
let collectResult =
Decoding.fromStringCol(
"<items><i>1</i><i>x</i><i>y</i></items>",
Decode.sequenceCol(fun _ el ->
Required.int el |> Result.mapError List.singleton
)
)
match collectResult with
| Ok values -> printfn $"Values: %A{values}"
| Error errors -> printfn $"Errors: %A{(errors |> List.map _.message)}"
The XML example collects two errors, one per bad child element. Single-error decoding would stop at the first bad child.
namespace CloudNimble
namespace CloudNimble.Text
namespace CloudNimble.Text.Xml
namespace FsToolkit
namespace FsToolkit.ErrorHandling
namespace JDeck
namespace JDeck.Xml
val builtInDecoder: el: XmlElement -> Result<{| value: int |},DecodeError>
val el: XmlElement
val decode: DecodeBuilder
val value: int
module Required
from JDeck.Xml.Decode
<summary> Decoders that fail when the value is missing, nil, or has the wrong shape. </summary>
<summary> Decoders that fail when the value is missing, nil, or has the wrong shape. </summary>
val int: Decoder<int>
val resultDecoder: el: XmlElement -> Result<{| value: int |},DecodeError>
val result: ResultBuilder
val collectResult: Result<int seq,DecodeError list>
type Decoding =
static member auto: value: string -> Result<'TResult,DecodeError> + 2 overloads
static member autoWithOptions: value: string * options: XmlSerializerOptions -> Result<'TResult,DecodeError> + 2 overloads
static member fromBytes: value: byte array * decoder: Decoder<'TResult> -> Result<'TResult,DecodeError>
static member fromBytesCol: value: byte array * decoder: CollectErrorsDecoder<'TResult> -> Result<'TResult,DecodeError list>
static member fromElement: element: XmlElement * decoder: Decoder<'TResult> -> Result<'TResult,DecodeError>
static member fromStream: value: Stream * decoder: Decoder<'TResult> -> Task<Result<'TResult,DecodeError>>
static member fromStreamCol: value: Stream * decoder: CollectErrorsDecoder<'TResult> -> Task<Result<'TResult,DecodeError list>>
static member fromString: value: string * decoder: Decoder<'TResult> -> Result<'TResult,DecodeError>
static member fromStringCol: value: string * decoder: CollectErrorsDecoder<'TResult> -> Result<'TResult,DecodeError list>
<summary> Entry points that parse XML text and run a decoder on the document element. </summary>
<summary> Entry points that parse XML text and run a decoder on the document element. </summary>
static member Decoding.fromStringCol: value: string * decoder: CollectErrorsDecoder<'TResult> -> Result<'TResult,DecodeError list>
Multiple items
module Decode from JDeck.Xml.Decode
--------------------
module Decode from JDeck.Xml
module Decode from JDeck.Xml.Decode
--------------------
module Decode from JDeck.Xml
val sequenceCol: decoder: IndexedCollectErrorsDecoder<'TResult> -> el: XmlElement -> Result<'TResult seq,DecodeError list>
<summary> Decodes each child element with the given indexed decoder, collecting every failure. </summary>
<summary> Decodes each child element with the given indexed decoder, collecting every failure. </summary>
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>
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 singleton: value: 'T -> 'T list
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val values: int seq
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val errors: DecodeError list
val map: mapping: ('T -> 'U) -> list: 'T list -> 'U list
JDeck