Skip to content

Commit

Permalink
cli: convert multiple schemas at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Feb 28, 2024
1 parent 635d993 commit e3a6824
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
31 changes: 21 additions & 10 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@ module Input_format = struct
let all = [ JSONSchema; OpenAPI ]
end

let generate_atd input_format path_in =
let ic = open_in path_in in
let input_content = really_input_string ic (in_channel_length ic) in
close_in ic;

let generate_atd input_format paths =
let generate =
match input_format with
| Input_format.JSONSchema -> Generator.make_atd_of_jsonschema
| OpenAPI -> Generator.make_atd_of_openapi
in
input_content |> generate |> print_string;
()
print_endline (Generator.base (String.concat " " (List.map Filename.basename paths)));
let root =
match paths with
| [ _ ] -> `Default
| _ -> `Per_file
in
List.iter
(fun path ->
let root =
match root with
| `Default -> None
| `Per_file -> Some (path |> Filename.basename |> Filename.remove_extension |> Utils.sanitize_name)
in
let input_content = In_channel.with_open_bin path In_channel.input_all in
input_content |> generate ?root |> print_string
)
paths

let input_format_term =
let formats = List.map (fun fmt -> Input_format.stringify fmt, fmt) Input_format.all in
Expand All @@ -32,9 +43,9 @@ let input_format_term =
Arg.(value & opt format JSONSchema & info [ "format"; "f" ] ~docv:"FORMAT" ~doc)

let main =
let doc = "Generate an ATD file from a JSON Schema / OpenAPI document" in
let path_in = Arg.(required & pos 0 (some file) None & info [] ~docv:"input file" ~doc) in
let term = Term.(const generate_atd $ input_format_term $ path_in) in
let doc = "Generate an ATD file from a list of JSON Schema / OpenAPI document" in
let paths = Arg.(non_empty & pos_all file [] & info [] ~docv:"FILES" ~doc) in
let term = Term.(const generate_atd $ input_format_term $ paths) in
let info = Cmd.info "jsonschema2atd" ~doc ~version:(Version.get ()) in
Cmd.v info term

Expand Down
13 changes: 7 additions & 6 deletions lib/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@ let process_schemas (schemas : (string * schema or_ref) list) =
)
[] schemas

let base =
{|(* Generated by jsonschema2atd *)
let base from =
sprintf
{|(* Generated by jsonschema2atd from %s *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">
|}
from

let make_atd_of_schemas schemas =
input_toplevel_schemas :=
Expand All @@ -270,20 +272,19 @@ let make_atd_of_schemas schemas =
)
schemas;
Buffer.clear output;
Buffer.add_string output (base ^ "\n");
Buffer.add_string output (String.concat "\n" (process_schemas schemas));
Buffer.contents output

let make_atd_of_jsonschema input =
let make_atd_of_jsonschema ?(root = "root") input =
let schema = Json_schema_j.schema_of_string input in
let root_type_name = Option.value ~default:"root" schema.title in
let root_type_name = Option.value ~default:root schema.title in
let defs =
let defs = List.concat_map Utils.list_of_nonempty [ schema.defs; schema.definitions ] in
List.map (fun (name, schema) -> name, Obj schema) defs
in
make_atd_of_schemas ([ root_type_name, Obj schema ] @ defs)

let make_atd_of_openapi input =
let make_atd_of_openapi ?root:_ input =
let root = Openapi_j.root_of_string input in
match root.components with
| None -> failwith "components are empty"
Expand Down
3 changes: 1 addition & 2 deletions tests/base.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ let openapi_json_template schemas =
schemas

let replace_whitespace str = Str.global_replace (Str.regexp "[ \t\n\r]+") "" str
let remove_prelude str = Str.global_replace (Str.regexp (Str.quote Generator.base)) "" str
let test_strings_cmp a b = String.equal (replace_whitespace a) (replace_whitespace b)

let assert_schema input output =
assert_equal ~cmp:test_strings_cmp
~printer:(fun str -> str)
output
(remove_prelude (Generator.make_atd_of_openapi (openapi_json_template input)))
(Generator.make_atd_of_openapi (openapi_json_template input))
2 changes: 1 addition & 1 deletion tests/grok.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Generate ATD types from grok (Grafana Object Development Kit) dashboard types
$ jsonschema2atd --format openapi ./mocks/dashboard_types_gen.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from dashboard_types_gen.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand Down
18 changes: 9 additions & 9 deletions tests/smoke.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Generate ATD out of JSON Scheme
$ jsonschema2atd ./mocks/simple_jsonschema.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from simple_jsonschema.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -9,7 +9,7 @@ Generate ATD out of JSON Scheme
}
Generate ATD out of JSON Scheme with --format attribute
$ jsonschema2atd --format jsonschema ./mocks/simple_jsonschema.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from simple_jsonschema.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -18,7 +18,7 @@ Generate ATD out of JSON Scheme with --format attribute
}
Generate ATD out of JSON Scheme with -f attribute
$ jsonschema2atd -f jsonschema ./mocks/simple_jsonschema.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from simple_jsonschema.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -27,7 +27,7 @@ Generate ATD out of JSON Scheme with -f attribute
}
Generate ATD out of OpenAPI doc with --format attribute
$ jsonschema2atd --format openapi ./mocks/simple_openapi.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from simple_openapi.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -36,7 +36,7 @@ Generate ATD out of OpenAPI doc with --format attribute
}
Generate ATD out of OpenAPI doc with -f attribute
$ jsonschema2atd -f openapi ./mocks/simple_openapi.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from simple_openapi.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -46,7 +46,7 @@ Generate ATD out of OpenAPI doc with -f attribute

Generate ATD out of JSON Schema that contains defs
$ jsonschema2atd --format=jsonschema ./mocks/jsonchema_defs.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from jsonchema_defs.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -59,7 +59,7 @@ Generate ATD out of JSON Schema that contains defs

Generate ATD out of JSON Schema that contains definitions (legacy support)
$ jsonschema2atd --format=jsonschema ./mocks/jsonchema_definitions.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from jsonchema_definitions.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -72,7 +72,7 @@ Generate ATD out of JSON Schema that contains definitions (legacy support)

Generate ATD out of JSON Schema that uses references
$ jsonschema2atd --format=jsonschema ./mocks/jsonschema_refs.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from jsonschema_refs.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -91,7 +91,7 @@ Generate ATD out of JSON Schema that uses references

Generate ATD out of JSON Schema that uses enums
$ jsonschema2atd --format=jsonschema ./mocks/jsonschema_enums.json
(* Generated by jsonschema2atd *)
(* Generated by jsonschema2atd from jsonschema_enums.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand Down

0 comments on commit e3a6824

Please sign in to comment.