mransan / ocaml-protoc

A Protobuf Compiler for OCaml
https://mransan.github.io/ocaml-protoc/
MIT License
179 stars 33 forks source link

Wrong number of constructor arguments for empty message inside oneof #228

Open Lupus opened 10 months ago

Lupus commented 10 months ago

For the below protobuf snippet:

syntax = "proto2";

message Null {}

message A {
    optional bool ignored = 1072;
    oneof foo {
        Null foo_not_set = 99999;
        bool bar = 1;
    }
}

The generated json encoder function contains an error:

let rec encode_json_a_foo (v:a_foo) = 
  begin match v with
  | Foo_not_set -> `Assoc [("fooNotSet", `Null)]
  | Bar v -> `Assoc [("bar", Pbrt_yojson.make_bool v)]
  end

and encode_json_a (v:a) = 
  let assoc = [] in 
  let assoc = match v.ignored with
    | None -> assoc
    | Some v -> ("ignored", Pbrt_yojson.make_bool v) :: assoc
  in
  let assoc = match v.foo with
    | Foo_not_set v ->               (* Error: The constructor Foo_not_set expects 0 argument(s), but is applied here to 1 argument(s) *)
      ("fooNotSet", `Null) :: assoc
    | Bar v ->
      ("bar", Pbrt_yojson.make_bool v) :: assoc 
  in (* match v.foo *)
  `Assoc assoc

If I remove optional bool ignored from the message A, the code is generated differently and actually compiles:

let rec encode_json_a (v:a) = 
  begin match v with
  | Foo_not_set -> `Assoc [("fooNotSet", `Null)]
  | Bar v -> `Assoc [("bar", Pbrt_yojson.make_bool v)]
  end