diff --git a/bin/main.ml b/bin/main.ml index ddb6973..2273e02 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -18,7 +18,7 @@ let generate_atd state input_format paths = | Input_format.JSONSchema -> Generator.make_atd_of_jsonschema ~state | OpenAPI -> Generator.make_atd_of_openapi ~state in - print_endline (Generator.base (String.concat " " (List.map Filename.basename paths))); + print_endline (Generator.base state (String.concat " " (List.map Filename.basename paths))); let root = match paths with | [ _ ] -> `Default @@ -46,13 +46,14 @@ let main = let doc = "Generate an ATD file from a list of JSON Schema / OpenAPI document" in let state_term = Term.( - const (fun skip_doc pad toplevel_types avoid_dangling_refs -> + const (fun skip_doc pad toplevel_types avoid_dangling_refs json_ocaml_type -> Generator. { with_doc = not skip_doc; protect_against_duplicates = (if pad then Some (ref []) else None); toplevel_types; avoid_dangling_refs; + json_ocaml_type; } ) $ Arg.(value (flag (info [ "skip-doc" ] ~doc:"Skip documentation annotations."))) @@ -66,6 +67,12 @@ let main = ) ) $ Arg.(value (flag (info [ "avoid-dangling-refs" ] ~doc:"Convert dangling refs to json."))) + $ Arg.( + 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`.") + ) + ) ) in let paths = Arg.(non_empty & pos_all file [] & info [] ~docv:"FILES" ~doc) in diff --git a/lib/generator.ml b/lib/generator.ml index 03f9b4f..7b596a3 100644 --- a/lib/generator.ml +++ b/lib/generator.ml @@ -7,10 +7,17 @@ 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; } let default_state = - { with_doc = true; protect_against_duplicates = None; toplevel_types = `All; avoid_dangling_refs = false } + { + with_doc = true; + protect_against_duplicates = None; + toplevel_types = `All; + avoid_dangling_refs = false; + json_ocaml_type = "Yojson.Basic", "t"; + } let record_field_name _state str = let cleaned_field_name = Utils.sanitize_name str in @@ -289,13 +296,14 @@ let process_schemas state (schemas : (string * schema or_ref) list) = ) [] schemas -let base from = +let base state from = + let 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 + from module_path type_t let make_atd_of_schemas state schemas = let schemas =