From 00ab9786d8f1712a5cb2488d746baca209afe74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20Roch=C3=A9?= Date: Tue, 27 Feb 2024 18:05:42 +0100 Subject: [PATCH] tmp --- bin/main.ml | 22 ++++++++++++---------- lib/generator.ml | 13 +++++++------ tests/base.ml | 3 +-- tests/grok.t | 2 +- tests/smoke.t | 42 +++++++++++++++++++++--------------------- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index b73cf7c..387ff79 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -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 @@ -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 diff --git a/lib/generator.ml b/lib/generator.ml index 559c086..6b338e4 100644 --- a/lib/generator.ml +++ b/lib/generator.ml @@ -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 = abstract type int64 = int |} + from let make_atd_of_schemas schemas = input_toplevel_schemas := @@ -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" diff --git a/tests/base.ml b/tests/base.ml index 0e52734..539ec54 100644 --- a/tests/base.ml +++ b/tests/base.ml @@ -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)) diff --git a/tests/grok.t b/tests/grok.t index 951de8e..b1015aa 100644 --- a/tests/grok.t +++ b/tests/grok.t @@ -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 = abstract type int64 = int diff --git a/tests/smoke.t b/tests/smoke.t index 772103a..da4cb42 100644 --- a/tests/smoke.t +++ b/tests/smoke.t @@ -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 = abstract type int64 = int @@ -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 = abstract type int64 = int @@ -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 = abstract type int64 = int @@ -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 = abstract type int64 = int @@ -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 = abstract type int64 = int @@ -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 = abstract type int64 = int 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 = abstract type int64 = int 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 = abstract type int64 = int - type root = { + type jsonschema_refs = { first_name : string; last_name : string; shipping_address : address; @@ -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 = abstract type int64 = int - type rootMyProperty = [ + type jsonschema_enumsMyProperty = [ | Foo | 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; }