Skip to content

Commit 254a26b

Browse files
committed
httpev: get request params without functor
1 parent 5e5e9a0 commit 254a26b

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

httpev.ml

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,28 @@ let () =
688688

689689
(** {2 Utilities} *)
690690

691-
module type Args = sig
692-
691+
(** get request params *)
692+
module Param = struct
693+
let get_exn req name = List.assoc name req.args
693694
exception Bad of string
695+
let get req = Exn.catch (get_exn req)
696+
let get_int req = Exn.catch (int_of_string $ get_exn req)
697+
let make f req ?default name =
698+
match get req name, default with
699+
| None, None -> raise (Bad name)
700+
| None, Some s -> s
701+
| Some s, _ -> try f s with _ -> raise (Bad name)
702+
let str = make id
703+
let int64 = make Int64.of_string
704+
let int = make int_of_string
705+
let float = make float_of_string
706+
let bool = make bool_of_string
707+
let array req name =
708+
let name = name ^ "[]" in
709+
req.args |> List.filter (fun (name',_) -> name = name') |> List.map snd
710+
end
711+
712+
module type Args = sig
694713

695714
val req : request
696715

@@ -715,26 +734,19 @@ module type Args = sig
715734
(** @param name array name without brackets e.g. [array "x"] to extract [x] from /request?x[]=1&x[]=2 *)
716735
end
717736

737+
(** functor version of {!Param} because somebody thought it is good idea *)
718738
module Args(T : sig val req : request end) : Args =
719739
struct
740+
open Param
720741
let req = T.req
721-
let arg name = List.assoc name T.req.args
722-
exception Bad of string
723-
let get = Exn.catch arg
724-
let get_int = Exn.catch (int_of_string $ arg)
725-
let make f ?default name =
726-
match get name, default with
727-
| None, None -> raise (Bad name)
728-
| None, Some s -> s
729-
| Some s, _ -> try f s with _ -> raise (Bad name)
730-
let str = make id
731-
let int64 = make Int64.of_string
732-
let int = make int_of_string
733-
let float = make float_of_string
734-
let bool = make bool_of_string
735-
let array name =
736-
let name = name ^ "[]" in
737-
T.req.args |> List.filter (fun (name',_) -> name = name') |> List.map snd
742+
let get = get req
743+
let get_int = get_int req
744+
let str = str req
745+
let int64 = int64 req
746+
let int = int req
747+
let float = float req
748+
let bool = bool req
749+
let array = array req
738750
end
739751

740752
let noclose_io io =

0 commit comments

Comments
 (0)