2

I have a set of protobuf types, and I want to generate reasonML code for serialization. What I've found so far is ocaml-protoc which fails to install on my system. Using docker,

FROM ocaml/opam:alpine
RUN opam remote add dev git://github.com/mirage/mirage-dev
RUN opam depext -i mirage

RUN opam depext conf-m4.1
RUN yes | opam install ocaml-protoc

then running the command:

docker run \
        --mount type=bind,source=$(PWD)/../src/proto,target=/home/opam/ocaml \
        --mount type=bind,source="$(PWD)"/../../proto,target=/home/opam/proto \
        -ti eb1b1ed36a35 \
        ocaml-protoc -ml_out=/home/opam/ocaml /home/opam/proto/chat.proto > /dev/null"

I've been able to compile a set of ml/mli files. However these files depend on some uninstalled libs such as Pbrt. Excerpt:

let rec decode_friend_event_etype d = 
  match Pbrt.Decoder.int_as_varint d with
  | 0 -> (Chat_types.Arrival:Chat_types.friend_event_etype)
  | 1 -> (Chat_types.Departure:Chat_types.friend_event_etype)
  | _ -> Pbrt.Decoder.malformed_variant "friend_event_etype"

--------------^

Is there a clean way to solve this problem without installing any native libs? Whatever is included has to be provided source in order for bucklescript to be able to compile it into JS.

The solution that comes to mind were it available would be to generate the common dependencies (such as Pbrt) along with the proto implementations. Has anyone found a way to solve this problem?

Keynan
  • 1,338
  • 1
  • 10
  • 18

1 Answers1

1

There's a bucklescript runtime and a demo project showing how to use it.

I haven't tried this myself, but from what I understand you'll have to add the bucklescript protobuf runtime as an ordinary bucklescript dependency

npm install bs-ocaml-protoc-json

add it to bs-dependencies in bsconfig.json

{
  ...
  "bs-dependencies": [ "bs-ocaml-protoc-json"]
}

and then generate bucklescript-specific code with ocaml-protoc using the -bs flag:

ocaml-protoc -bs -ml_out=/home/opam/ocaml /home/opam/proto/chat.proto

You should now have the generated source code in /home/opam/ocaml and, assuming you have a bucklescript project properly set up with this as a source folder, should be able to compile it with bsb -make-world

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • I am using `bs-ocaml-protoc-json` in the interm, however I'm asking for protobuf encoding, not generating json encoders from proto files which, afaik is all that library supplies. – Keynan Apr 03 '18 at 03:58
  • 1
    You mean binary encoding? That isn't at all made clear form your question. Looks like `ocaml-protoc` includes the binary encoding runtime itself. You might be able to extract and package it for bucklescript. Another option is to compile it with `js_of_ocaml`, which interacts better with the opam ecosystem, but will require writing bucklescript bindings for interop and might become really bloated. – glennsl Apr 03 '18 at 05:46