Module Parser.Query

Parse query parameters

Type

type 'a t

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.

Elementary Query Parsers

val string : string -> string option t

string 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

"http://host?name=Alice"

Some (Some "Alice")

"http://host?name=Bob"

Some (Some "Bob")

"http://host?name=%D0%91%D0%BE%D0%B1"

Some (Some "Боб")

"http://host?name="

Some (Some "")

"http://host?name"

Some None

"http://host"

Some None

val int : string -> int option t

int 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

"http://host?year=2025"

Some (Some 2025)

"http://host?year=Y2K"

Some None

"http://host?year="

Some None

"http://host?year"

Some None

"http://host"

Some None

val enum : string -> (string * 'a) list -> 'a option t

enum 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

"http://host?lang=ml"

Some (Some `Ocaml)

"http://host?lang=py"

Some None

"http://host?lang="

Some None

"http://host?lang"

Some None

"http://host"

Some None

val custom : string -> (string list -> 'a) -> 'a t

custom 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

"http://host?post=2"

Some [2]

"http://host?post=2&post=7"

Some [2; 7]

"http://host?post=2&post=x"

Some [2]

"http://host?hats=2"

Some []

Combining Query Parsers

val return : 'a -> 'a t

Make a query parser which parses nothing an returns a result.

val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

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

"http://host?fname=Xavier&lname=Leroy"

Some ({fname = Some "Xavier"; lname = Some "Leroy"))

"http://host?fname=Xavier"

Some ({fname = Some "Xavier"; lname = None})

"http://host?"

Some ({fname = None; lname = None})

"http://host"

Some ({fname = None; lname = None})

Mapping Query Parsers

val map : ('a -> 'b) -> 'a t -> 'b t

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

"http://host?search=ocaml"

Some (`Search "ocaml")

"http://host?search="

Some (`Search "")

"http://host?search"

Some (`Search "")

"http://host"

Some (`Search "")