Protocol Buffers codec for hand-written schemas
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-protobuf: enable MDX on lib/protobuf.mli

Run mdx on lib/protobuf.mli so the {[ ... ]} odoc block now type-checks
and the encode-decode round-trip is verified.

Renamed the local `person` value to `person_codec` so it doesn't
shadow the `person` type, bound `ada` once and reused it for both
the encode and the round-trip assertion. Asserted that the wire
output is non-empty and that decoding it via `Protobuf.of_string`
returns the original record; the error path reports via
`Fmt.failwith "%a" Protobuf.Error.pp`.

+13 -4
+4
lib/dune
··· 2 2 (name protobuf) 3 3 (public_name nox-protobuf) 4 4 (libraries bytesrw fmt leb128 nox-loc)) 5 + 6 + (mdx 7 + (files protobuf.mli) 8 + (libraries nox-protobuf fmt))
+9 -4
lib/protobuf.mli
··· 11 11 {[ 12 12 type person = { name : string; age : int32; hobbies : string list } 13 13 14 - let person : person Protobuf.t = 14 + let person_codec : person Protobuf.t = 15 15 let open Protobuf.Message in 16 16 v (fun name age hobbies -> { name; age; hobbies }) 17 17 |> required 1 Protobuf.string ~enc:(fun p -> p.name) ··· 19 19 |> repeated 3 Protobuf.string ~enc:(fun p -> p.hobbies) 20 20 |> seal 21 21 22 - let wire = 23 - Protobuf.to_string person 24 - { name = "Ada"; age = 36l; hobbies = [ "math" ] } 22 + let ada = { name = "Ada"; age = 36l; hobbies = [ "math" ] } 23 + let wire = Protobuf.to_string person_codec ada 24 + let () = assert (String.length wire > 0) 25 + 26 + let () = 27 + match Protobuf.of_string person_codec wire with 28 + | Ok decoded -> assert (decoded = ada) 29 + | Error e -> Fmt.failwith "decode: %a" Protobuf.Error.pp e 25 30 ]} *) 26 31 27 32 module Wire = Wire