Type.Coded
Coded types.
Coded types store arbitrary OCaml values by encoding them to a base type. To let other, non OCaml-based, systems access the database, be conservative in your encodings.
See an example.
type 'a repr := 'a t
val make :
?doc:string ->
?pp:'a fmt ->
?equal:'a eq ->
name:string ->
'b repr ->
enc:('a -> 'b) ->
dec:('b -> 'a) ->
('a, 'b) t
make ~name repr ~enc ~dec
is a coded type with:
name
, the name of the coded type.repr
, the base type used to encode values.enc
, the function for encoding values. The function must raise Failure
for partial encodings.dec
, the function for decoding values. The function must raise Failure
for partial decodings.equal
, an equality function on the values of the type. Defaults to Stdlib.(=)
pp
, a formatter for the type values. Defaults uses enc
and repr
's formatterdoc
, a doc string for the coded type.val name : ('a, 'b) t -> string
name c
is the name of c
.
val doc : ('a, 'b) t -> string
doc c
is the documentation string of c
.
val enc : ('a, 'b) t -> 'a -> 'b
enc c
is the encoder of c
.
val dec : ('a, 'b) t -> 'b -> 'a
dec c
is the decoder of c
.
This encodes a binary enum to a boolean:
open Rel
type status = [ `Ok | `Failed ]
let pp_status ppf v =
Format.pp_print_string ppf (match v with `Ok -> "ok" | `Failed -> "failed")
let status : status Type.t =
let enc = function `Ok -> true | `Failed -> false in
let dec = function true -> `Ok | false -> `Failed in
Type.coded (Type.Coded.make ~name:"status" Type.bool ~enc ~dec ~pp:pp_status)