Protocol Buffers codec for hand-written schemas
README.md

protobuf #

Protocol Buffers codec for OCaml, for hand-written message schemas.

If you want to talk gRPC or plain protobuf from OCaml, the usual answer is a .proto compiler and a pile of generated code in your tree. That is a lot to carry when the protocol has half a dozen messages and they hardly ever change. With nox-protobuf you write those codecs by hand instead, a few lines each. You build an 'a codec with combinators and use the same value to encode and to decode.

All 15 protobuf scalar types are there. Fields can be required, optional, repeated, packed, maps or oneofs, and messages can be recursive. Got bytes and no codec? Decode them to a Protobuf.Value.t and look inside. There is no .proto code generator yet.

Installation #

Install with opam:

$ opam install nox-protobuf

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-protobuf

Usage #

type person = { name : string; age : int32; hobbies : string list }

let person_codec : person Protobuf.codec =
  let open Protobuf.Message in
  v (fun name age hobbies -> { name; age; hobbies })
  |> required 1 Protobuf.string ~enc:(fun p -> p.name)
  |> required 2 Protobuf.int32 ~enc:(fun p -> p.age)
  |> repeated 3 Protobuf.string ~enc:(fun p -> p.hobbies)
  |> seal

let wire =
  Protobuf.to_string person_codec
    { name = "Ada"; age = 36l; hobbies = [ "math"; "sewing" ] }

let () =
  match Protobuf.of_string person_codec wire with
  | Ok p -> Printf.printf "%s, %ld, %d hobbies\n" p.name p.age (List.length p.hobbies)
  | Error e -> prerr_endline (Protobuf.Error.to_string e)

Scalar codecs #

Varints go seven bits per byte, low group first, with the top bit set on every byte but the last. So 150 comes out as 0x96 0x01. Be careful with negative numbers in int32: they get sign-extended to 64 bits and always cost you 10 bytes. If your values are often negative, use sint32 or sint64. Those zig-zag the value first (0, -1, 1, -2 become 0, 1, 2, 3), so -1 fits in one byte.

Codec OCaml type Wire
int32 int32 varint, signed
int64 int64 varint, signed
uint32 int32 varint, unsigned bit pattern
uint64 int64 varint, unsigned bit pattern
sint32 int32 varint, zig-zag
sint64 int64 varint, zig-zag
fixed32 int32 4 bytes little-endian
fixed64 int64 8 bytes little-endian
sfixed32 int32 4 bytes little-endian, signed
sfixed64 int64 8 bytes little-endian, signed
float float 4 bytes IEEE 754
double float 8 bytes IEEE 754
bool bool varint
string string length-delimited UTF-8
bytes string length-delimited bytes

Field combinators #

You start a message with Message.v ctor, add one field per step and finish with Message.seal. In every step n is the tag, c the codec and ~enc pulls the field out of your OCaml value.

  • required n c ~enc is a plain field. When tag n is missing you get the proto3 default (0, "", false).
  • optional n c ~enc gives you an 'a option, None iff tag n is absent.
  • repeated n c ~enc is a non-packed repeated field. Every tag n on the wire adds one element to the list.
  • packed n c ~enc is a packed repeated field: all the values in one length-delimited body. The decoder takes both packed and non-packed input, as the specification asks.
  • map n kc vc ~enc is a map<K, V> field.
  • oneof ~default ~enc cases groups alternative fields, each built with case. At most one of them is set.

Each step takes a builder ('o, 'a -> 'dec) t_ and returns ('o, 'dec) t_. seal turns ('o, 'o) t_ into an 'o Protobuf.codec. For a recursive message you need Protobuf.fix.

Limitations #

You write every codec by hand. There is no .proto code generation.

Unknown fields are dropped by the plain decoders. If you need to pass them through, of_string_with_unknowns and to_string_with_unknowns keep those of the top-level message across a round trip. Unknown fields inside a nested message are still lost.

Nothing stops you from giving map a key codec protobuf does not allow. Protobuf only accepts integer, bool and string scalars as keys, and map does not check.

Licence #

ISC. See LICENSE.md.