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

[@defaut <value>] seems broken #14

Open
toots opened this issue Aug 2, 2022 · 1 comment
Open

[@defaut <value>] seems broken #14

toots opened this issue Aug 2, 2022 · 1 comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@toots
Copy link

toots commented Aug 2, 2022

Test:

type foo = {
  bar : string;
  foo : (int [@default 123]);
} [@@deriving yojson]

let () =
  try
    ignore(foo_of_yojson (Yojson.Safe.from_string {|{"bar":"baz"}|}))
  with
    | Ppx_yojson_conv_lib.Yojson_conv.Of_yojson_error (exn, _) ->
        Printf.printf "Error: %s\n%!" (Printexc.to_string exn);
        exit 1

Result:

Error: Failure("test.ml.foo_of_yojson: the following record elements were undefined: foo")
@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Aug 2, 2022
@ScottAlexanderCameron
Copy link

Answering in case anyone ends up searching for this, but the parenthesis around (int [@default 123]); are the cause of this error. The proper syntax should be foo : int; [@default 123]

eg:

open Ppx_yojson_conv_lib.Yojson_conv.Primitives

type foo = {
  bar : string;
  foo : int; [@default 123]    (* <-- this line *)
} [@@deriving yojson]

let () =
  try
    let foo = foo_of_yojson (Yojson.Safe.from_string {|{"bar":"baz"}|}) in
    Stdio.printf "{bar = \"%s\"; foo = %d}\n" foo.bar foo.foo
  with Ppx_yojson_conv_lib.Yojson_conv.Of_yojson_error (exn, _) ->
    Printf.printf "Error: %s\n%!" (Printexc.to_string exn);
    exit 1

output:

{bar = "baz"; foo = 123}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

3 participants