Protobuf is an Ocaml library for communicating with Google's protobuf format. It provides a method for writing parsers and builders. There is no protoc support, yet and writing it is not a top goal right now. Protobuf is meant to be fairly lightweight and straight forward to use. The only other Protobuf support for Ocaml I am aware of is through piqi, however that was too heavy for my needs.
Protobuf is meant to be very low level, mostly dealing with representation of values and not semantics. For example, the fixed32
and sfixed32
values are both parsed as Int32.t
's. Dealing with being signed or not is left up to the user.
The source code can be viewed here. Protobuf is in opam, to install it opam install protobuf
.
The hope is that parsers and builders look reasonably close to the .proto
files such that translation is straight forward, at least until protoc support is added. This is an early release and, without a doubt, has bugs in it please submit pull requests and issues.
https://github.com/orbitz/ocaml-protobuf/tree/0.0.2/
Examples
The best collection of examples right now is the tests. An example from the file:
let simple =
P.int32 1 >>= P.return
let complex =
P.int32 1 >>= fun num ->
P.string 2 >>= fun s ->
P.embd_msg 3 simple >>= fun emsg ->
P.return (num, s, emsg)
let run_complex str =
let open Result.Monad_infix in
P.State.create (Bitstring.bitstring_of_string str)
>>= fun s ->
P.run complex s
The builder for this message looks like:
let build_simple i =
let open Result.Monad_infix in
let b = B.create () in
B.int32 b 1 i >>= fun () ->
Ok (B.to_string b)
let build_complex (i1, s, i2) =
let open Result.Monad_infix in
let b = B.create () in
B.int32 b 1 i1 >>= fun () ->
B.string b 2 s >>= fun () ->
B.embd_msg b 3 i2 build_simple >>= fun () ->
Ok (B.to_string b)
We already have biniou and binprot in OCaml.
ReplyDeleteWhy use protobuf?
If you are integrating or want to integrate with non-ocaml products. Riak does not use biniou or binprot.
Delete