Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Feb 27, 2024
1 parent 635d993 commit 00ab978
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
22 changes: 12 additions & 10 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ 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 " " paths));
List.iter
(fun path ->
let root = 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 +34,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 ./mocks/dashboard_types_gen.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand Down
42 changes: 21 additions & 21 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 ./mocks/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 ./mocks/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 ./mocks/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 ./mocks/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 ./mocks/simple_openapi.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

Expand All @@ -46,37 +46,37 @@ 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 ./mocks/jsonchema_defs.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

type name = string

type root = {
type jsonchema_defs = {
first_name : name;
last_name : name;
}

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 ./mocks/jsonchema_definitions.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

type name = string

type root = {
type jsonchema_definitions = {
first_name : name;
last_name : name;
}

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

type root = {
type jsonschema_refs = {
first_name : string;
last_name : string;
shipping_address : address;
Expand All @@ -91,24 +91,24 @@ 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 ./mocks/jsonschema_enums.json *)
type json <ocaml module="Yojson.Basic" t="t"> = abstract
type int64 = int <ocaml repr="int64">

type rootMyProperty = [
type jsonschema_enumsMyProperty = [
| Foo <json name="foo">
| Bar <json name="bar">
]

type rootMyPropertyInt = int
type jsonschema_enumsMyPropertyInt = int

type rootMyPropertyNumber = float
type jsonschema_enumsMyPropertyNumber = float

type rootMyPropertyBool = bool
type jsonschema_enumsMyPropertyBool = bool

type root = {
myProperty : rootMyProperty;
?myPropertyInt : rootMyPropertyInt option;
?myPropertyNumber : rootMyPropertyNumber option;
?myPropertyBool : rootMyPropertyBool option;
type jsonschema_enums = {
myProperty : jsonschema_enumsMyProperty;
?myPropertyInt : jsonschema_enumsMyPropertyInt option;
?myPropertyNumber : jsonschema_enumsMyPropertyNumber option;
?myPropertyBool : jsonschema_enumsMyPropertyBool option;
}

0 comments on commit 00ab978

Please sign in to comment.