Skip to content

Remove -j-std #425

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ Unreleased
----------

* atdgen: Add option `-j-gen-modules` to generate JSON generic submodules (#420)

* atdgen: Remove option `-j-std`, now it's the default, one cannot generate extended-JSON (#425).
Options `-j-std` and `-std-json` are still available as backward-compatibility no-ops unless
environment variable `ATDGEN_FAIL_DEPRECATED_OPTIONS` is set to `true` in
which case their use results in an exception.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.


2.16.0 (2025-01-22)
-------------------
Expand Down
37 changes: 21 additions & 16 deletions atdgen/bin/ag_main.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
open Atd.Import
open Atdgen_emit

let fail_deprecated_variable = "ATDGEN_FAIL_DEPRECATED_OPTIONS"
let maybe_fail_deprecated where =
match Sys.getenv fail_deprecated_variable with
| "true" -> Printf.ksprintf failwith "Error: option %S is forbidden." where
| _ | exception _ -> ()

let append l1 l2 =
List.concat_map (fun s1 -> List.map (fun s2 -> s1 ^ s2) l2) l1

Expand Down Expand Up @@ -61,7 +67,6 @@ let main () =
let all_rec = ref false in
let out_prefix = ref None in
let mode = ref (None : mode option) in
let std_json = ref false in
let add_generic_modules = ref false in
let j_preprocess_input = ref None in
let j_defaults = ref false in
Expand Down Expand Up @@ -163,6 +168,7 @@ let main () =

"-biniou",
Arg.Unit (fun () ->
maybe_fail_deprecated "-biniou";
set_once "output type" mode Biniou),
"
[deprecated in favor of -t and -b]
Expand All @@ -171,19 +177,17 @@ let main () =

"-json",
Arg.Unit (fun () ->
maybe_fail_deprecated "-json";
set_once "output type" mode Json),
"
[deprecated in favor of -t and -j]
Produce serializers and deserializers for JSON
including OCaml type definitions.";

"-j-std",
Arg.Unit (fun () ->
std_json := true),
Arg.Unit (fun () -> maybe_fail_deprecated "-j-std"),
"
Convert tuples and variants into standard JSON and
refuse to print NaN and infinities (implying -json mode
unless another mode is specified).";
[deprecated] This option does nothing; kept for backwards compatibility.";

"-j-gen-modules",
Arg.Unit (fun () ->
Expand All @@ -194,11 +198,9 @@ let main () =
module Typename: sig type t ... val read: ... val to_string: ... end";

"-std-json",
Arg.Unit (fun () ->
std_json := true),
Arg.Unit (fun () -> maybe_fail_deprecated "-std-json"),
"
[deprecated in favor of -j-std]
Same as -j-std.";
[deprecated] No-op: same as -j-std.";

"-j-pp",
Arg.String (fun s -> set_once "-j-pp" j_preprocess_input s),
Expand Down Expand Up @@ -238,6 +240,7 @@ let main () =

"-validate",
Arg.Unit (fun () ->
maybe_fail_deprecated "-validate";
set_once "output type" mode Validate),
"
[deprecated in favor of -t and -v]
Expand Down Expand Up @@ -288,19 +291,22 @@ let main () =
Print the version identifier of atdgen and exit.";
]
in
let msg = sprintf "\
Generate OCaml code offering:
let msg = sprintf
{|Generate OCaml code offering:
* OCaml type definitions translated from ATD file (-t)
* serializers and deserializers for Biniou (-b)
* serializers and deserializers for JSON (-j)
* record-creating functions supporting default fields (-v)
* user-specified data validators (-v)

Recommended usage: %s (-t|-b|-j|-v|-dep|-list|-mel) example.atd" Sys.argv.(0) in
Recommended usage: %s (-t|-b|-j|-v|-dep|-list|-mel) example.atd

Some options are deprecated. Use the environment variable `%s=true`
to make their use fail and hence help clean-up your build scripts.
|} Sys.argv.(0) fail_deprecated_variable in
Arg.parse options (fun file -> files := file :: !files) msg;

if (!std_json
|| !unknown_field_handler <> None) && !mode = None then
if (!unknown_field_handler <> None) && !mode = None then
set_once "output mode" mode Json;

let mode =
Expand Down Expand Up @@ -408,7 +414,6 @@ Recommended usage: %s (-t|-b|-j|-v|-dep|-list|-mel) example.atd" Sys.argv.(0) in
Ob_emit.make_ocaml_files
| J | Json ->
Oj_emit.make_ocaml_files
~std: !std_json
~unknown_field_handler: !unknown_field_handler
~preprocess_input: !j_preprocess_input
~add_generic_modules: !add_generic_modules
Expand Down
44 changes: 11 additions & 33 deletions atdgen/src/oj_emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ let annot_schema = Ocaml.annot_schema_of_target target
type param = {
deref : (Ocaml.Repr.t, Json.json_repr) Mapping.mapping ->
(Ocaml.Repr.t, Json.json_repr) Mapping.mapping;
std : bool;
unknown_field_handler : string option;
(* Optional handler that takes a field name as argument
and does something with it such as displaying a warning message. *)
Expand Down Expand Up @@ -157,14 +156,9 @@ let rec get_writer_name

| Float (_, Float, Float j) ->
(match j with
Float None ->
if p.std then "Yojson.Safe.write_std_float"
else "Yojson.Safe.write_float"
Float None -> "Yojson.Safe.write_std_float"
| Float (Some precision) ->
if p.std then
sprintf "Yojson.Safe.write_std_float_prec %i" precision
else
sprintf "Yojson.Safe.write_float_prec %i" precision
sprintf "Yojson.Safe.write_std_float_prec %i" precision
| Int ->
"Atdgen_runtime.Oj_run.write_float_as_int"
)
Expand Down Expand Up @@ -348,10 +342,7 @@ let rec make_writer ?type_constraint p (x : Oj_mapping.t) : Indent.t list =
let l =
insert (Line "Buffer.add_char ob ',';") (Array.to_list a)
in
let op, cl =
if p.std then '[', ']'
else '(', ')'
in
let op, cl = '[', ']' in
[
Annot ("fun", Line "fun ob x ->");
Block [
Expand Down Expand Up @@ -393,8 +384,7 @@ let rec make_writer ?type_constraint p (x : Oj_mapping.t) : Indent.t list =

| Option (_, x, Option, Option) ->
[
Line (sprintf "Atdgen_runtime.Oj_run.write_%soption ("
(if p.std then "std_" else ""));
Line "Atdgen_runtime.Oj_run.write_std_option (";
Block (make_writer p x);
Line ")";
]
Expand Down Expand Up @@ -434,14 +424,10 @@ and make_variant_writer p ~tick ~open_enum x : Indent.t list =
let json_cons = j.Json.json_cons in
match x.var_arg with
| None ->
let enclose s =
if p.std then s
else "<" ^ s ^ ">"
in
[
Line (sprintf "| %s%s -> Buffer.add_string ob %S"
tick ocaml_cons
(enclose (make_json_string json_cons)))
(make_json_string json_cons))
]
| Some v when open_enum ->
(* v should resolve to type string. *)
Expand All @@ -454,10 +440,7 @@ and make_variant_writer p ~tick ~open_enum x : Indent.t list =
]
| 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
let op, sep, cl = "[", ",", ']' in
[
Line (sprintf "| %s%s x ->" tick ocaml_cons);
Block [
Expand All @@ -470,10 +453,7 @@ and make_variant_writer p ~tick ~open_enum x : Indent.t list =
]
]
| Some v ->
let op, sep, cl =
if p.std then "[", ",", ']'
else "<", ":", '>'
in
let op, sep, cl = "[", ",", ']' in
[
Line (sprintf "| %s%s x ->" tick ocaml_cons);
Block [
Expand Down Expand Up @@ -1295,13 +1275,12 @@ let make_ocaml_json_reader p ~original_types is_rec let1 let2 def =


let make_ocaml_json_impl
~std ~unknown_field_handler
~unknown_field_handler
~with_create ~force_defaults ~preprocess_input ~original_types
~ocaml_version
buf deref defs =
let p =
{ deref
; std
; unknown_field_handler
; force_defaults
; preprocess_input
Expand Down Expand Up @@ -1346,7 +1325,7 @@ let make_mli

let make_ml
~header ~opens ~with_typedefs ~with_create ~with_fundefs
~std ~unknown_field_handler
~unknown_field_handler
~force_defaults ~preprocess_input ~original_types
~ocaml_version
ocaml_typedefs deref defs =
Expand All @@ -1359,7 +1338,7 @@ let make_ml
bprintf buf "\n";
if with_fundefs then
make_ocaml_json_impl
~std ~unknown_field_handler
~unknown_field_handler
~with_create ~force_defaults ~preprocess_input ~original_types
~ocaml_version
buf deref defs;
Expand Down Expand Up @@ -1415,7 +1394,6 @@ let make_ocaml_files
~with_create
~with_fundefs
~all_rec
~std
~unknown_field_handler
~pos_fname
~pos_lnum
Expand Down Expand Up @@ -1476,7 +1454,7 @@ let make_ocaml_files
in
let ml =
make_ml ~header ~opens ~with_typedefs ~with_create ~with_fundefs
~std ~unknown_field_handler
~unknown_field_handler
~force_defaults ~preprocess_input ~original_types
~ocaml_version
ocaml_typedefs (Mapping.make_deref defs) defs
Expand Down
1 change: 0 additions & 1 deletion atdgen/src/oj_emit.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ val make_ocaml_files
-> with_create:bool
-> with_fundefs:bool
-> all_rec:bool
-> std:bool
-> unknown_field_handler:string option
-> pos_fname:string option
-> pos_lnum:int option
Expand Down
24 changes: 12 additions & 12 deletions atdgen/test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
(targets test2j.ml test2j.mli)
(deps test2.atd)
(action
(run %{bin:atdgen} -json -std-json -o test2j -open Test,Test2,Testj -ntd %{deps})))
(run %{bin:atdgen} -json -o test2j -open Test,Test2,Testj -ntd %{deps})))

(rule
(targets test_ambiguous_record_t.ml test_ambiguous_record_t.mli)
Expand All @@ -94,7 +94,7 @@
(targets test_ambiguous_record_j.ml test_ambiguous_record_j.mli)
(deps test_ambiguous_record.atd)
(action
(run %{bin:atdgen} -json -std-json -j-gen-modules -o test_ambiguous_record_j -open Test_ambiguous_record_t -ntd %{deps})))
(run %{bin:atdgen} -json -j-gen-modules -o test_ambiguous_record_j -open Test_ambiguous_record_t -ntd %{deps})))

(rule
(targets test_ambiguous_variant_t.ml test_ambiguous_variant_t.mli)
Expand All @@ -106,7 +106,7 @@
(targets test_ambiguous_variant_j.ml test_ambiguous_variant_j.mli)
(deps test_ambiguous_variant.atd)
(action
(run %{bin:atdgen} -j -j-std -j-gen-modules %{deps})))
(run %{bin:atdgen} -j -j-gen-modules %{deps})))

(rule
(targets test_polymorphic_wrap_t.ml test_polymorphic_wrap_t.mli)
Expand All @@ -118,7 +118,7 @@
(targets test_polymorphic_wrap_j.ml test_polymorphic_wrap_j.mli)
(deps test_polymorphic_wrap.atd)
(action
(run %{bin:atdgen} -json -std-json -j-gen-modules -o test_polymorphic_wrap_j %{deps})))
(run %{bin:atdgen} -json -j-gen-modules -o test_polymorphic_wrap_j %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -148,7 +148,7 @@
(rule
(targets test3j_j.ml test3j_j.mli)
(deps test3j.atd)
(action (run %{bin:atdgen} -j -j-std %{deps})))
(action (run %{bin:atdgen} -j %{deps})))

(rule
(alias runtest)
Expand All @@ -161,19 +161,19 @@
(action (diff test3j_j.expected.mli test3j_j.mli)))

(rule
(targets testjstd.ml testjstd.mli)
(targets testjstd_j.ml testjstd_j.mli)
(deps test.atd)
(action (run %{bin:atdgen} -std-json -extend Test test.atd -o testjstd)))
(action (run %{bin:atdgen} -extend Test test.atd -json -o testjstd_j)))

(rule
(alias runtest)
(package atdgen)
(action (diff testjstd.expected.ml testjstd.ml)))
(action (diff testjstd_j.expected.ml testjstd_j.ml)))

(rule
(alias runtest)
(package atdgen)
(action (diff testjstd.expected.mli testjstd.mli)))
(action (diff testjstd_j.expected.mli testjstd_j.mli)))

(rule
(targets testv.ml testv.mli)
Expand Down Expand Up @@ -228,7 +228,7 @@
(rule
(targets test_annot_j.ml test_annot_j.mli)
(deps test_annot.atd)
(action (run %{bin:atdgen} -j -j-std %{deps})))
(action (run %{bin:atdgen} -j %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -286,7 +286,7 @@
(rule
(targets test_classic_inline_record_j.ml test_classic_inline_record_j.mli)
(deps test_classic_inline_record.atd)
(action (run %{bin:atdgen} -j-std -j %{deps})))
(action (run %{bin:atdgen} -j %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -402,7 +402,7 @@
test_ambiguous_variant_j
test_polymorphic_wrap_t
test_polymorphic_wrap_j
testjstd
testjstd_j
testj
testv
test_unit_biniou_t
Expand Down
2 changes: 1 addition & 1 deletion atdgen/test/melange/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(rule
(targets melangespec_j.ml melangespec_j.mli)
(deps melangespec.atd)
(action (run atdgen %{deps} -j -j-std)))
(action (run atdgen %{deps} -j)))

(library
(name melangespec_types)
Expand Down
2 changes: 1 addition & 1 deletion atdgen/test/spec_js/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(rule
(targets spec_j.ml spec_j.mli)
(deps spec.atd)
(action (run atdgen %{deps} -j -j-std)))
(action (run atdgen %{deps} -j)))

(rule
(targets spec_mel.ml spec_mel.mli)
Expand Down
6 changes: 3 additions & 3 deletions atdgen/test/test_atdgen_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ let test_json_correctness () =
let s' = Testj.string_of_test x' in
let x'' = Testj.test_of_string s' in
save "test-2.json" s';
let std_x' = Testjstd.test_of_string s in
let std_s' = Testjstd.string_of_test std_x' in
let std_x'' = Testjstd.test_of_string std_s' in
let std_x' = Testjstd_j.test_of_string s in
let std_s' = Testjstd_j.string_of_test std_x' in
let std_x'' = Testjstd_j.test_of_string std_s' in
save "test-std.json" std_s';
if x <> x' then (
eprintf "%s\n" (Yojson.Safe.prettify s);
Expand Down
Loading