From ed9925e32d5303b33cead40c703855e07b87fb78 Mon Sep 17 00:00:00 2001 From: Sebastien Mondet Date: Wed, 4 Dec 2024 14:22:02 -0500 Subject: [PATCH] Make `from` Vs `module` keyword also parametrizable --- bin/main.ml | 7 +++++-- lib/generator.ml | 15 ++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 2273e02..91773df 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -68,9 +68,12 @@ let main = ) $ Arg.(value (flag (info [ "avoid-dangling-refs" ] ~doc:"Convert dangling refs to json."))) $ Arg.( + let keyword = enum [ "module", `Module; "from", `From ] in value - (opt (pair ~sep:':' string string) Generator.default_state.json_ocaml_type - (info [ "json-ocaml-type" ] ~docv:"MODULE.PATH:TYPE-NAME" ~doc:"Use an alternate Mod.type for `json`.") + (opt (t3 ~sep:':' keyword string string) Generator.default_state.json_ocaml_type + (info [ "json-ocaml-type" ] ~docv:"KEYWORD:MODULE.PATH:TYPE-NAME" + ~doc:"Use an alternate Mod.type for `json`, e.g. from:My_mod.Submod:json_type." + ) ) ) ) diff --git a/lib/generator.ml b/lib/generator.ml index 7b596a3..7f85dcb 100644 --- a/lib/generator.ml +++ b/lib/generator.ml @@ -7,7 +7,7 @@ type state = { protect_against_duplicates : string list ref option; toplevel_types : [ `All | `Only of string list ]; avoid_dangling_refs : bool; - json_ocaml_type : string * string; + json_ocaml_type : [ `From | `Module ] * string * string; } let default_state = @@ -16,7 +16,7 @@ let default_state = protect_against_duplicates = None; toplevel_types = `All; avoid_dangling_refs = false; - json_ocaml_type = "Yojson.Basic", "t"; + json_ocaml_type = `Module, "Yojson.Basic", "t"; } let record_field_name _state str = @@ -297,13 +297,18 @@ let process_schemas state (schemas : (string * schema or_ref) list) = [] schemas let base state from = - let module_path, type_t = state.json_ocaml_type in + let keyword, module_path, type_t = state.json_ocaml_type in sprintf {|(* Generated by jsonschema2atd from %s *) -type json = abstract +type json = abstract type int64 = int |} - from module_path type_t + from + ( match keyword with + | `From -> "from" + | `Module -> "module" + ) + module_path type_t let make_atd_of_schemas state schemas = let schemas =