Skip to content

VariantCodecBuilder

module VariantCodecBuilder
AssemblyRanvier.Wire
Attributes[<AutoOpen>]

Declared inside

Codec
VariantCaseThe cases declared so far by a variantCodec block
VariantCodecBuilderThe builder behind variantCodec and variantCodecWithTag
VariantEncodingHow a variantCodec writes the case a value belongs to

Functions and values

variantCodec: VariantCodecBuilder

Build 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

Build 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

Build 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
}