Skip to content

Commit

Permalink
Add option --json-ocaml-type Module:type
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Dec 4, 2024
1 parent abd931e commit dcf1757
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 9 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")))
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions lib/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <ocaml module="Yojson.Basic" t="t"> = abstract
type json <ocaml module="%s" t="%s"> = abstract
type int64 = int <ocaml repr="int64">
|}
from
from module_path type_t

let make_atd_of_schemas state schemas =
let schemas =
Expand Down

0 comments on commit dcf1757

Please sign in to comment.