Skip to content

Commit

Permalink
Add option --only-matching to limit output
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Nov 27, 2024
1 parent ce6759d commit 51a30ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 15 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ 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 ->
Generator.{ with_doc = not skip_doc; protect_against_duplicates = (if pad then Some (ref []) else None) }
const (fun skip_doc pad toplevel_types ->
Generator.
{
with_doc = not skip_doc;
protect_against_duplicates = (if pad then Some (ref []) else None);
toplevel_types;
}
)
$ Arg.(value (flag (info [ "skip-doc" ] ~doc:"Skip documentation annotations.")))
$ Arg.(value (flag (info [ "protect-against-duplicates" ] ~doc:"Make sure no duplicate types are generated.")))
$ (const (function
| [] -> `All
| some -> `Only some
)
$ Arg.(
value (opt_all string [] (info [ "only-matching" ] ~docv:"REGEXP" ~doc:"Only output types matching REGEXP."))
)
)
)
in
let paths = Arg.(non_empty & pos_all file [] & info [] ~docv:"FILES" ~doc) in
Expand Down
10 changes: 9 additions & 1 deletion lib/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ open Utils
type state = {
with_doc : bool;
protect_against_duplicates : string list ref option;
toplevel_types : [ `All | `Only of string list ];
}

let default_state = { with_doc = true; protect_against_duplicates = None }
let default_state = { with_doc = true; protect_against_duplicates = None; toplevel_types = `All }

let record_field_name _state str =
let cleaned_field_name = Utils.sanitize_name str in
Expand Down Expand Up @@ -288,6 +289,13 @@ type int64 = int <ocaml repr="int64">
from

let make_atd_of_schemas state schemas =
let schemas =
match state.toplevel_types with
| `All -> schemas
| `Only l ->
let res = List.map (Printf.ksprintf Str.regexp "^%s$") l in
List.filter (fun (name, _) -> List.exists (fun re -> Str.string_match re name 0) res) schemas
in
input_toplevel_schemas :=
List.filter_map
(function
Expand Down

0 comments on commit 51a30ed

Please sign in to comment.