No description
  • OCaml 99.5%
  • Makefile 0.5%
Find a file
2020-12-08 19:37:47 +01:00
expander Add errors for GADT 2020-09-09 17:28:17 +02:00
src Ocamlformat everything 2020-09-04 14:10:55 +02:00
test Ocamlformat everything 2020-09-04 14:10:55 +02:00
.gitignore Adding project structure 2020-08-27 10:21:50 +02:00
.ocamlformat Adding ocamlformat 2020-09-09 14:52:22 +02:00
dune Adding project structure 2020-08-27 10:21:50 +02:00
dune-project Adding project structure 2020-08-27 10:21:50 +02:00
LICENCE Licence 2020-08-27 11:53:36 +02:00
Makefile Adding project structure 2020-08-27 10:21:50 +02:00
ppx_encoding.opam Licence 2020-08-27 11:53:36 +02:00
README.md Apply 1 suggestion(s) to 1 file(s) 2020-09-04 08:34:28 +00:00

Ppx_encoding

Generate the encoding description of a type.

ppx_encoding is a ppx rewriter which generates an object corresponding to the encoding description using the data-encoding library.

In order to use the preprocessor, add (preprocess (pps ppx_encoding)) to your dune file.

Basic Usage

The basic usage is simply to add [@@deriving encoding] after the type definition. For example:

type t =
  | Foo
  | Bar of bool
  | Baz of (int*float) list
  [@@deriving encoding]

This will create a value named encoding since by convention, t is the main type of the module. For other names, the name will be encoding_of_your_type_name.

To look at what is produced you can generate the code at compile time by replacing [@@deriving encoding] by [@@deriving_inline encoding][@@@end] and executing :

$ dune build --auto-promote

To execute the examples in test directory run

dune build @runtest @lint --auto-promote

For instance, the aforementioned type t will generate the following code :

let encoding =
  let open! Data_encoding in
  union ~tag_size:`Uint8
    [
      case ~title:"Foo" (Tag 0)
        (obj1 (req "Foo" unit))
        (function Foo -> Some () | _ -> None)
        (fun () -> Foo);
      case ~title:"Bar" (Tag 1)
        (obj1 (req "Bar" bool))
        (function Bar bar -> Some bar | _ -> None)
        (fun bar -> Bar bar);
      case ~title:"Baz" (Tag 2)
        (obj1 (req "Baz" (list (tup2 int31 float))))
        (function Baz baz -> Some baz | _ -> None)
        (fun baz -> Baz baz);
    ]