Module Type.Coded

Coded types.

Types coded by other types. This allows columns value to be represented by arbitrary OCaml values. To let other, non OCaml-based, systems access the database, be conservative in your encoding.

Here is a simple example encoding a binary enum to a boolean:

type status = [ `Ok | `Failed ]
let (status : status Type.t) =
  let enc = function `Ok -> Ok true | `Failed -> Ok false in
  let dec = function true -> Ok `Ok | false -> Ok `Failed in
  Type.Coded (Type.Coded.v ~name:"status" enc dec Type.Bool)
type 'a repr = 'a t
type ('a, 'b) map = 'a -> ('b, string) Stdlib.result

The type for partially mapping values of type 'a to values of type 'b.

type ('a, 'b) t = ('a, 'b) coded
val v : ?pp:(Stdlib.Format.formatter -> 'a -> unit) -> name:string -> ('a, 'b) map -> ('b, 'a) map -> 'b repr -> ('a, 'b) coded

v ~pp ~name enc dec repr is a coding using enc and dec to codec values to representation repr. name is a name for the coded type. pp is an optional formatter for the values of the type.

val name : ('a, 'b) coded -> string

name c is the name of c.

val enc : ('a, 'b) coded -> ('a, 'b) map

enc c is the encoder of c.

val dec : ('a, 'b) coded -> ('b, 'a) map

dec c is the decoder of c.

val repr : ('a, 'b) coded -> 'b repr

repr c is the type of the encoding of c

val pp : ('a, 'b) coded -> (Stdlib.Format.formatter -> 'a -> unit) option

pp c is c's pretty printer (if any).