ocplib-json-typed: import ' Variable defintion path in json_schema' from upstream

It is upstream acfb41f711cbda67680b84d0f8292e2a1849c711
This commit is contained in:
Pierre Boutillier 2019-01-15 14:27:06 +01:00
parent a10b7dc5b5
commit 242c97d1f1
No known key found for this signature in database
GPG Key ID: C2F73508B56A193C
4 changed files with 21 additions and 21 deletions

View File

@ -403,7 +403,7 @@ let patch_description ?title ?description (elt : Json_schema.element) =
| None, Some _ -> { elt with description } | None, Some _ -> { elt with description }
| Some _, Some _ -> { elt with title ; description } | Some _, Some _ -> { elt with title ; description }
let schema encoding = let schema ?definitions_path encoding =
let open Json_schema in let open Json_schema in
let sch = ref any in let sch = ref any in
let rec prod l1 l2 = match l1 with let rec prod l1 l2 = match l1 with
@ -472,7 +472,7 @@ let schema encoding =
| Describe { id = name ; title ; description ; encoding } -> | Describe { id = name ; title ; description ; encoding } ->
let open Json_schema in let open Json_schema in
let schema = patch_description ?title ?description (schema encoding) in let schema = patch_description ?title ?description (schema encoding) in
let s, def = add_definition name schema !sch in let s, def = add_definition ?definitions_path name schema !sch in
sch := fst (merge_definitions (!sch, s)) ; sch := fst (merge_definitions (!sch, s)) ;
def def
| Custom (_, s) -> | Custom (_, s) ->
@ -484,10 +484,10 @@ let schema encoding =
| Conv (_, _, t, None) -> schema t | Conv (_, _, t, None) -> schema t
| Mu { id = name ; title ; description ; self = f } -> | Mu { id = name ; title ; description ; self = f } ->
let fake_schema = let fake_schema =
if definition_exists name !sch then if definition_exists ?definitions_path name !sch then
update (definition_ref name) !sch update (definition_ref ?definitions_path name) !sch
else else
let sch, elt = add_definition name (element Dummy) !sch in let sch, elt = add_definition ?definitions_path name (element Dummy) !sch in
update elt sch in update elt sch in
let fake_self = let fake_self =
Custom ({ write = (fun _ _ -> assert false) ; Custom ({ write = (fun _ _ -> assert false) ;
@ -497,7 +497,7 @@ let schema encoding =
patch_description patch_description
?title ?description ?title ?description
(schema (f fake_self)) in (schema (f fake_self)) in
let nsch, def = add_definition name root !sch in let nsch, def = add_definition ?definitions_path name root !sch in
sch := nsch ; def sch := nsch ; def
| Array t -> | Array t ->
element (Monomorphic_array (schema t, array_specs)) element (Monomorphic_array (schema t, array_specs))

View File

@ -400,7 +400,7 @@ val any_schema : Json_schema.schema encoding
(** Describe an encoding in JSON schema format. (** Describe an encoding in JSON schema format.
May raise {!Bad_schema}. *) May raise {!Bad_schema}. *)
val schema : 't encoding -> Json_schema.schema val schema : ?definitions_path:string -> 't encoding -> Json_schema.schema
(** Name a definition so its occurences can be shared in the JSON (** Name a definition so its occurences can be shared in the JSON
schema. The first parameter is a path, that must be unique and schema. The first parameter is a path, that must be unique and

View File

@ -1113,27 +1113,27 @@ module Make (Repr : Json_repr.Repr) = struct
collect schema.root ; collect schema.root ;
{ schema with definitions = !res } { schema with definitions = !res }
let definition_path_of_name name = let definition_path_of_name ?(definitions_path="/definitions/") name =
path_of_json_pointer ~wildcards:false @@ path_of_json_pointer ~wildcards:false @@
match String.get name 0 with match String.get name 0 with
| exception _ -> raise (Bad_reference name) | exception _ -> raise (Bad_reference name)
| '/' -> name | '/' -> name
| _ -> "/definitions/" ^ name | _ -> definitions_path ^ name
let find_definition name schema = let find_definition ?definitions_path name schema =
let path = definition_path_of_name name in let path = definition_path_of_name ?definitions_path name in
find_definition path schema.definitions find_definition path schema.definitions
let definition_ref name = let definition_ref ?definitions_path name =
let path = definition_path_of_name name in let path = definition_path_of_name ?definitions_path name in
element (Def_ref path) element (Def_ref path)
let definition_exists name schema = let definition_exists ?definitions_path name schema =
let path = definition_path_of_name name in let path = definition_path_of_name ?definitions_path name in
definition_exists path schema.definitions definition_exists path schema.definitions
let add_definition name elt schema = let add_definition ?definitions_path name elt schema =
let path = definition_path_of_name name in let path = definition_path_of_name ?definitions_path name in
(* check inside def *) (* check inside def *)
let definitions = insert_definition path elt schema.definitions in let definitions = insert_definition path elt schema.definitions in
{ schema with definitions }, element (Def_ref path) { schema with definitions }, element (Def_ref path)

View File

@ -176,19 +176,19 @@ val simplify : schema -> schema
error raised by {!Json_repr.path_of_json_pointer} with error raised by {!Json_repr.path_of_json_pointer} with
[~wildcards:false]. Returns the modified schema and the [Def_ref] [~wildcards:false]. Returns the modified schema and the [Def_ref]
node that references this definition to be used in the schema. *) node that references this definition to be used in the schema. *)
val add_definition : string -> element -> schema -> schema * element val add_definition : ?definitions_path:string -> string -> element -> schema -> schema * element
(** Finds a definition by its path, may raise [Not_found]. (** Finds a definition by its path, may raise [Not_found].
See {!add_definition} for the name format.*) See {!add_definition} for the name format.*)
val find_definition : string -> schema -> element val find_definition : ?definitions_path:string -> string -> schema -> element
(** Tells if a path leads to a definition. (** Tells if a path leads to a definition.
See {!add_definition} for the name format. *) See {!add_definition} for the name format. *)
val definition_exists : string -> schema -> bool val definition_exists : ?definitions_path:string -> string -> schema -> bool
(** Build a reference to a definition. (** Build a reference to a definition.
See {!add_definition} for the name format. *) See {!add_definition} for the name format. *)
val definition_ref : string -> element val definition_ref : ?definitions_path:string -> string -> element
(** {2 Predefined values} *) (***********************************************) (** {2 Predefined values} *) (***********************************************)