Skip to content
Snippets Groups Projects
Commit 67033aa7 authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[server] functional API & enum lookup

parent 5d8fa139
No related branches found
No related tags found
No related merge requests found
...@@ -39,8 +39,6 @@ sig ...@@ -39,8 +39,6 @@ sig
val to_json : t -> json val to_json : t -> json
end end
type 'a data = (module S with type t = 'a)
exception InputError of string exception InputError of string
let failure ?json msg = let failure ?json msg =
...@@ -228,6 +226,47 @@ struct ...@@ -228,6 +226,47 @@ struct
datatype ~package ~name:"text" ~descr jdef datatype ~package ~name:"text" ~descr jdef
end end
(* -------------------------------------------------------------------------- *)
(* --- Functional API --- *)
(* -------------------------------------------------------------------------- *)
type 'a data = (module S with type t = 'a)
let junit : unit data = (module Junit)
let jany : json data = (module Jany)
let jbool : bool data = (module Jbool)
let jint : int data = (module Jint)
let jfloat : float data = (module Jfloat)
let jstring : string data = (module Jstring)
let jkey ~kind =
let module JkeyKind =
struct
include Jstring
let jtype = Jkey kind
end in
(module JkeyKind : S with type t = string)
let jindex ~kind =
let module JindexKind =
struct
include Jint
let jtype = Jindex kind
end in
(module JindexKind : S with type t = int)
let joption (type a) (d : a data) : a option data =
let module A = Joption(val d) in
(module A : S with type t = a option)
let jlist (type a) (d : a data) : a list data =
let module A = Jlist(val d) in
(module A : S with type t = a list)
let jarray (type a) (d : a data) : a array data =
let module A = Jarray(val d) in
(module A : S with type t = a array)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* --- Records --- *) (* --- Records --- *)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
...@@ -377,6 +416,7 @@ struct ...@@ -377,6 +416,7 @@ struct
mutable syntax : Markdown.text ; mutable syntax : Markdown.text ;
mutable published : (package * string) option ; mutable published : (package * string) option ;
mutable tags : tagInfo list ; mutable tags : tagInfo list ;
mutable lookup : ('a -> string) option ;
} }
type 'a tag = string type 'a tag = string
...@@ -393,6 +433,7 @@ struct ...@@ -393,6 +433,7 @@ struct
vindex = Hashtbl.create 0 ; vindex = Hashtbl.create 0 ;
syntax = [] ; syntax = [] ;
tags = [] ; tags = [] ;
lookup = None ;
} }
let tag ~name ?label ~descr ?value (d : 'a dictionary) : 'a tag = let tag ~name ?label ~descr ?value (d : 'a dictionary) : 'a tag =
...@@ -411,8 +452,12 @@ struct ...@@ -411,8 +452,12 @@ struct
| Some v -> Hashtbl.add d.vindex v name | Some v -> Hashtbl.add d.vindex v name
end ; name end ; name
let find_tag (d : 'a dictionary) name = let find (d : 'a dictionary) name : 'a tag =
if Hashtbl.mem d.values name then name else raise Not_found if Hashtbl.mem d.values name then name else
raise Not_found
let set_lookup (d : 'a dictionary) (tag : 'a -> 'a tag) =
d.lookup <- Some tag
let instance_name = Printf.sprintf "%s:%s" let instance_name = Printf.sprintf "%s:%s"
...@@ -434,10 +479,16 @@ struct ...@@ -434,10 +479,16 @@ struct
Package.update ~package ~name (D_enum (List.rev d.tags)) Package.update ~package ~name (D_enum (List.rev d.tags))
) ; name ) ; name
let to_json name vindex v = let to_json name lookup vindex v =
try `String (Hashtbl.find vindex v) `String begin
with Not_found -> try match lookup with
failure "[%s] Value not found" name | None ->
Hashtbl.find vindex v
| Some f ->
try f v with Not_found -> Hashtbl.find vindex v
with Not_found ->
failure "[%s] Value not found" name
end
let of_json name values js = let of_json name values js =
let tag = Ju.to_string js in let tag = Ju.to_string js in
...@@ -450,7 +501,7 @@ struct ...@@ -450,7 +501,7 @@ struct
let tags d = List.rev d.tags let tags d = List.rev d.tags
let publish (type a) ~package ~name ~descr ?tag (d : a dictionary) = let publish (type a) ~package ~name ~descr (d : a dictionary) =
( match d.published with ( match d.published with
| None -> () | None -> ()
| Some _ -> | Some _ ->
...@@ -463,10 +514,7 @@ struct ...@@ -463,10 +514,7 @@ struct
let enums = D_enum (List.rev d.tags) in let enums = D_enum (List.rev d.tags) in
Jdata (Package.declare_id ~package ~name ~descr enums) Jdata (Package.declare_id ~package ~name ~descr enums)
let of_json = of_json name d.values let of_json = of_json name d.values
let to_json = let to_json = to_json name d.lookup d.vindex
match tag with
| None -> to_json name d.vindex
| Some to_tag -> fun x -> `String (to_tag x)
end in end in
begin begin
d.published <- Some (package,name) ; d.published <- Some (package,name) ;
......
...@@ -70,9 +70,6 @@ sig ...@@ -70,9 +70,6 @@ sig
val to_json : t -> json val to_json : t -> json
end end
(** Polymorphic data value. *)
type 'a data = (module S with type t = 'a)
(** Of main kernel data. *) (** Of main kernel data. *)
val package : package val package : package
...@@ -113,6 +110,25 @@ module Jtriple(A : S)(B : S)(C : S) : S with type t = A.t * B.t * C.t ...@@ -113,6 +110,25 @@ module Jtriple(A : S)(B : S)(C : S) : S with type t = A.t * B.t * C.t
module Jlist(A : S) : S with type t = A.t list module Jlist(A : S) : S with type t = A.t list
module Jarray(A : S) : S with type t = A.t array module Jarray(A : S) : S with type t = A.t array
(* -------------------------------------------------------------------------- *)
(** {2 Functional API} *)
(* -------------------------------------------------------------------------- *)
(** Polymorphic data value. *)
type 'a data = (module S with type t = 'a)
val junit : unit data
val jany : json data
val jbool : bool data
val jint : int data
val jfloat : float data
val jstring : string data
val jindex : kind:string -> int data
val jkey : kind:string -> string data
val jlist : 'a data -> 'a list data
val jarray : 'a data -> 'a array data
val joption : 'a data -> 'a option data
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(** {2 Records} *) (** {2 Records} *)
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
...@@ -229,7 +245,7 @@ sig ...@@ -229,7 +245,7 @@ sig
(** Returns the tag from its name. (** Returns the tag from its name.
@raise Not_found if no tag has been registered with this name. *) @raise Not_found if no tag has been registered with this name. *)
val find_tag: 'a dictionary -> string -> 'a tag val find: 'a dictionary -> string -> 'a tag
(** Register a new prefix tag in the dictionary. (** Register a new prefix tag in the dictionary.
The default label is the capitalized prefix. The default label is the capitalized prefix.
...@@ -258,12 +274,17 @@ sig ...@@ -258,12 +274,17 @@ sig
(** Obtain all the tags registered in the dictionary so far. *) (** Obtain all the tags registered in the dictionary so far. *)
val tags : 'a dictionary -> Tag.t list val tags : 'a dictionary -> Tag.t list
(** Set tagging function for values. If the lookup function
raises `Not_found`, the dictionary will use the tag associated
with the provided value, if any. *)
val set_lookup : 'a dictionary -> ('a -> 'a tag) -> unit
(** (**
Publish the dictionary. No more tag nor prefix can be added afterwards. Publish the dictionary. No more tag nor prefix can be added afterwards.
If no [~tag] function is provided, the values registered with tags are used. If no [~tag] function is provided, the values registered with tags are used.
*) *)
val publish : package:package -> name:string -> descr:Markdown.text -> val publish : package:package -> name:string -> descr:Markdown.text ->
?tag:('a -> 'a tag) -> 'a dictionary -> (module S with type t = 'a) 'a dictionary -> (module S with type t = 'a)
end end
......
...@@ -129,13 +129,15 @@ module LogKind = Collection ...@@ -129,13 +129,15 @@ module LogKind = Collection
let t_failure = t_kind Log.Failure "FAILURE" "Plugin Failure" let t_failure = t_kind Log.Failure "FAILURE" "Plugin Failure"
let t_debug = t_kind Log.Debug "DEBUG" "Analyser Debug" let t_debug = t_kind Log.Debug "DEBUG" "Analyser Debug"
let tag = function let () = Enum.set_lookup kinds
| Log.Error -> t_error begin function
| Log.Warning -> t_warning | Log.Error -> t_error
| Log.Feedback -> t_feedback | Log.Warning -> t_warning
| Log.Result -> t_result | Log.Feedback -> t_feedback
| Log.Failure -> t_failure | Log.Result -> t_result
| Log.Debug -> t_debug | Log.Failure -> t_failure
| Log.Debug -> t_debug
end
let data = Request.dictionary ~package let data = Request.dictionary ~package
~name:"logkind" ~name:"logkind"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment