Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yojson gen_rft_variant extraneous variable "v" in pattern #239

Open
yxie2023 opened this issue Feb 13, 2024 · 1 comment
Open

yojson gen_rft_variant extraneous variable "v" in pattern #239

yxie2023 opened this issue Feb 13, 2024 · 1 comment
Labels

Comments

@yxie2023
Copy link

Great work and thank you!

In pb_codegen_encode_yojson.ml, gen_rft_variant adds "v" regardless of whether vc_field_type is Vct_nullary or not, which results in errors in generated code:

          F.linep sc "| %s v ->" vc_constructor;

Corrected version should be something like:

let gen_rft_variant sc rf_label { Ot.v_constructors; _ } =
  F.linep sc "let assoc = match v.%s with" rf_label;

  F.sub_scope sc (fun sc ->
      List.iter
        (fun { Ot.vc_constructor; vc_field_type; vc_payload_kind; _ } ->
          let var_name = "v" in
          let json_label =
            Pb_codegen_util.camel_case_of_constructor vc_constructor
          in
          match vc_field_type with
            | Ot.Vct_nullary ->
               F.linep sc "| %s -> (\"%s\", `Null) :: assoc" vc_constructor json_label
            | Ot.Vct_non_nullary_constructor field_type ->
               F.linep sc "| %s v -> " vc_constructor;
               (match
                   gen_field var_name json_label field_type vc_payload_kind
                with
                  | None -> F.linep sc "(\"%s\", `Null) :: assoc" json_label
                  | Some exp -> F.linep sc "%s :: assoc " exp))
        v_constructors);

  F.linep sc "in (* match v.%s *)" rf_label
@c-cube
Copy link
Collaborator

c-cube commented Feb 13, 2024

Thank you for the report! If you're willing to open a PR with the fix in it I'll be happy to merge it 🙂 . A test case would be even better!

@c-cube c-cube added the bug label Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants