OCaml Toolbox [octx]

OCaml Toolbox (octx) is a CLI tool for generating OCaml code. It uses various ppxes, and built-in functionality to produce OCaml code. The output from octx is meant to be edited and modified to suit the user's needs.

As an example, imagine you have a type t:

type t = {
  name : string;
  age : int
}

And you want to make a Jsont representation of it. You could write this by hand. You could add a dependency on ppx_deriving_jsont. Or, you could use octx in your editor, and select your type and pass it to octx run -d jsont - | ocamlformat --enable-outside-detected-project --impl -.

let jsont =
  let make name age = { name; age } in
  Jsont.Object.map ~kind:"T" make
  |> Jsont.Object.mem "name" Jsont.string ~enc:(fun t -> t.name)
  |> Jsont.Object.mem "age" Jsont.int ~enc:(fun t -> t.age)
  |> Jsont.Object.finish

Of course, the ocamlformat-ing is completely optional, but tends to produce a slightly more human-readable version of the ppx-generated code.

At the time of writing, octx supports the following derivers:

"ppx_deriving_yojson"
"ppx_deriving_yaml"
"ppx_sexp_conv"
"ppx_deriving_jsont"
"ppx_deriving"
"ppx_fields_conv"
"ppx_hash"
"ppx_string_conv"