Skip to content

Commit

Permalink
Fix oj_emit for inline records (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Jan 3, 2025
1 parent 8e06c07 commit 720638c
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 256 deletions.
64 changes: 46 additions & 18 deletions atdgen/src/oj_emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,23 @@ and make_variant_writer p ~tick ~open_enum x : Indent.t list =
Line ") ob x;";
];
]
| Some (Record (_, a, Record o, Record _)) ->
(* We need a special case because inline-records cannot escape their scope. *)
let op, sep, cl =
if p.std then "[", ",", ']'
else "<", ":", '>'
in
[
Line (sprintf "| %s%s x ->" tick ocaml_cons);
Block [
Line (sprintf "Buffer.add_string ob %S;"
(op ^ make_json_string json_cons ^ sep));
Line "begin";
Block (make_record_writer p a o);
Line "end (* ob x *);";
Line (sprintf "Buffer.add_char ob %C" cl);
]
]
| Some v ->
let op, sep, cl =
if p.std then "[", ",", ']'
Expand Down Expand Up @@ -833,36 +850,47 @@ and make_case_reader
] in
true, expr
| Some v ->
let read_x, put_x =
match v with
| Record (loc, a, Record o, Record j) ->
(* We need a special case for inline-records: *)
let create_record_prefix = sprintf "%s%s" tick ocaml_cons in
Inline [
Line "let x = (";
Block (make_record_reader ~create_record_prefix p type_annot loc a j);
Line ") in";
],
Line "x"
| other ->
Inline [
Line "let x = (";
Block [
Block (make_reader p None other);
Line ") p lb";
];
Line "in";
],
Line (Ox_emit.opt_annot
type_annot (sprintf "%s%s x" tick ocaml_cons));
in
let expr =
if std then
[
Line "Yojson.Safe.read_space p lb;";
Line "Yojson.Safe.read_comma p lb;";
Line "Yojson.Safe.read_space p lb;";
Line "let x = (";
Block [
Block (make_reader p None v);
Line ") p lb";
];
Line "in";
read_x;
Line "Yojson.Safe.read_space p lb;";
Line "Yojson.Safe.read_rbr p lb;";
Line (Ox_emit.opt_annot
type_annot (sprintf "%s%s x" tick ocaml_cons));
put_x;
]
else
[
Line "Atdgen_runtime.Oj_run.read_until_field_value p lb;";
Line "let x = (";
Block [
Block (make_reader p None v);
Line ") p lb";
];
Line "in";
read_x;
Line "Yojson.Safe.read_space p lb;";
Line "Yojson.Safe.read_gt p lb;";
Line (Ox_emit.opt_annot
type_annot (sprintf "%s%s x" tick ocaml_cons));
put_x;
]
in
false, expr
Expand Down Expand Up @@ -909,7 +937,7 @@ and make_cases_reader p type_annot ~tick ~open_enum ~std ~fallback_expr l =
in
all_cases @ catch_all
and make_record_reader p type_annot loc a json_options =
and make_record_reader ?(create_record_prefix = "") p type_annot loc a json_options =
let keep_nulls = json_options.json_keep_nulls in
let fields = Ox_emit.get_fields p.deref a in
let init_fields, create_record =
Expand Down Expand Up @@ -1014,7 +1042,7 @@ and make_record_reader p type_annot loc a json_options =
Line "with Yojson.End_of_object -> (";
Block [
Block [
Line "(";
Line (sprintf "(%s" create_record_prefix);
Block create_record;
Line (sprintf "%s)" (Ox_emit.insert_annot type_annot));
];
Expand Down
Loading

0 comments on commit 720638c

Please sign in to comment.