Parser.QueryParse query parameters
The query parser type.
An query parser of type 'a t parses the query part of a url and returns an object of type 'a.
val string : string -> string option tstring key will parse the query parameter named key as string.
Both the key and value of the query parameter will be percent-decoded automatically.
For example. the parser top </> Query.string "name" will behave like this:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
val int : string -> int option tint key will parse the query parameter named key as integer.
The key of the query parameter will be percent-decoded automatically.
For example. the parser top </> Query.int "year" will behave like this:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|
|
|
val enum : string -> (string * 'a) list -> 'a option tenum key table will parse the query parameter named key as string and will try to transform this string into a value by looking it up in the given table.
Example:
let lang_parser:
([`Haskell | `OCaml | `Rust] option -> 'a, 'a) Parser.t
=
let open Parser in
top <?>
Query.enum
"lang"
[("hs", `Haskell); ("ml", `OCaml); ("rs", `Rust)]The example parser produces the following results:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|
|
|
val custom : string -> (string list -> 'a) -> 'a tcustom key f will parse the query parameter named key by applying the function f to the list of raw (undecoded) string values referenced by key in the query string.
While the other query parsers, string, int and enum, only allow at most one occurrence of a key in the query string, custom allows handling multiple occurrences.
Example:
let posts: int list option Parser.t =
let open Parser in
top <?> Query.custom "post" (List.filter_map int_of_string)The example parser produces the following results:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|
val return : 'a -> 'a tMake a query parser which parses nothing an returns a result.
This function can be used to transform more than one query parser into a compound query parser.
Example:
type user = {fname: string option; lname: string option}
let user_parser: (user -> 'a, 'a) Parser.t =
let open Parser in
top <?>
Query.(
return (fun fname lname -> {fname; lname})
<*> string "fname"
<*> string "lname"
)The example parser produces the following results:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|
map f query_parser transforms the query_parser which parses one query parameter via f.
Example:
let search_term_parser: ([`Search of string] -> 'a, 'a) Parser.t =
let open Parser in
top <?>
Query.map
(fun s -> `Search (Option.value ~default:"" s))
(Query.string "search")The example parser produces the following results:
input | parse result |
|---|---|
|
|
|
|
|
|
|
|