mransan/ocaml-protoc

encode bug in repeated composite type?

Closed this issue · 3 comments

syntax = "proto3";

message Id {
    string id = 1;
}
  
// repeated composite type is not handled by protoc?
message IdList { repeated Id ids = 1; }

run ocaml-protoc -bs -ml_out . test.proto

part of test_bs.ml

let rec encode_id (v:Test_types.id) = 
  let json = Js.Dict.empty () in
  Js.Dict.set json "id" (Js.Json.string v.Test_types.id);
  json

let rec encode_id_list (v:Test_types.id_list) = 
  let json = Js.Dict.empty () in
  let ids' = List.map (fun v ->
    let json' = Js.Dict.empty () in
    encode_id v json';
    Js.Dict.set json "ids" (Js.Json.object_ json');
    json' |> Array.of_list
  ) v.Test_types.ids in
  Js.Dict.set json "ids" (Js.Json.array ids');
  json
We've found a bug for you!
  
  65 ┆ let ids' = List.map (fun v ->
  66 ┆   let json' = Js.Dict.empty () in
  67 ┆   encode_id v json';  <-- error here
  68 ┆   Js.Dict.set json "ids" (Js.Json.object_ json');
  69 ┆   json' |> Array.of_list
  
  This function has type Test_types.id -> Js.Json.t Js.Dict.t
  It only accepts 1 argument; here, it's called with more.

possible correct version?

val encode_id_list : Test_types.id_list -> Js.Json.t Js.Dict.t array 


let rec encode_id_list (v:Test_types.id_list) = 
  let json = Js.Dict.empty () in
  let ids' = List.map (fun v ->
    encode_id v
  ) v.Test_types.ids in
  Js.Dict.set json "ids" (Array.of_list ids');
  json

Good catch, will fix shortly