Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience functions for extracting multiple arguments #86

Open
hcarty opened this issue Nov 12, 2015 · 2 comments
Open

Convenience functions for extracting multiple arguments #86

hcarty opened this issue Nov 12, 2015 · 2 comments
Milestone

Comments

@hcarty
Copy link
Contributor

hcarty commented Nov 12, 2015

Pulling from rgrinberg/opium#32 (comment) it would be nice to have a function for extracting multiple query parameters with a single call, similar to:

(* only match if all query params are present *)
val get_query_params : Uri.t -> string list -> (string list list) option
(* allow for partial matches of parameters list *)
val get_query_params' : Uri.t -> string list -> (string list option) list

usable as:

match get_query_params uri ["ua"; "xx"; "yyy"] with
| Some [ua; xx; yyy] -> ...
| _ -> ...

or, if you're expecting and want to only accept a single argument for a parameter:

match get_query_params uri ["ua"; "xx"; "yyy"] with
| Some [[ua]; [xx]; yyy] -> ... (* Only match if ua and xx are single-valued, yyy gets everything *)
| _ -> ...
@dsheets dsheets added this to the 2.0 milestone Nov 13, 2015
@dsheets
Copy link
Member

dsheets commented Nov 13, 2015

This is an API to consider for the fabled 2.0 release.

@hcarty
Copy link
Contributor Author

hcarty commented Nov 17, 2015

An mostly untested implementation if someone wants something before 2.0:

let get_query_params uri keys = 
  let values = List.map (fun k -> Uri.get_query_param' uri k) keys in
  let result = 
    List.fold_left ( 
      fun accu mayv ->
        match accu, mayv with
        | None, _ -> None
        | _, None -> None
        | Some l, Some v -> Some (v :: l)
    ) (Some []) values
  in
  match result with
  | None -> None
  | Some l -> Some (List.rev l)

let get_query_params' uri keys = 
  List.map (fun k -> Uri.get_query_param' uri k) keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants