VariantCodecBuilder
module VariantCodecBuilder| Assembly | Ranvier.Wire |
| Attributes | [<AutoOpen>] |
Declared inside
Codec | |
VariantCase | The cases declared so far by a variantCodec block |
VariantCodecBuilder | The builder behind variantCodec and variantCodecWithTag |
VariantEncoding | How a variantCodec writes the case a value belongs to |
Functions and values
variantCodec: VariantCodecBuilder
variantCodec: VariantCodecBuilderBuild a codec for a union, writing an object with a single property named after the case.
// { "square": 4 }
let codec : Codec<Shape> =
variantCodec {
let! square = Codec.case "square" Square Codec.int
and! circle = Codec.case "circle" Circle Codec.int
return function
| Square width -> square width
| Circle radius -> circle radius
}
variantCodecTuple: VariantCodecBuilder
variantCodecTuple: VariantCodecBuilderBuild a codec for a union, writing the tag and the value into a 2 element array.
// [ "square", 4 ]
let codec : Codec<Shape> =
variantCodecTuple {
let! square = Codec.case "square" Square Codec.int
and! circle = Codec.case "circle" Circle Codec.int
return function
| Square width -> square width
| Circle radius -> circle radius
}
variantCodecWithTag (tagName: string) (valueName: string) : VariantCodecBuilder
variantCodecWithTag (tagName: string) (valueName: string) : VariantCodecBuilderBuild a codec for a union, writing the tag and the value under the given property names.
// { "type": "square", "value": 4 }
let codec : Codec<Shape> =
variantCodecWithTag "type" "value" {
let! square = Codec.case "square" Square Codec.int
and! circle = Codec.case "circle" Circle Codec.int
return function
| Square width -> square width
| Circle radius -> circle radius
}