@@ -688,9 +688,28 @@ let () =
688
688
689
689
(* * {2 Utilities} *)
690
690
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
693
694
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
694
713
695
714
val req : request
696
715
@@ -715,26 +734,19 @@ module type Args = sig
715
734
(* * @param name array name without brackets e.g. [array "x"] to extract [x] from /request?x[]=1&x[]=2 *)
716
735
end
717
736
737
+ (* * functor version of {!Param} because somebody thought it is good idea *)
718
738
module Args (T : sig val req : request end ) : Args =
719
739
struct
740
+ open Param
720
741
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
738
750
end
739
751
740
752
let noclose_io io =
0 commit comments