Module

URI.Extra.QueryPairs

#QueryPairs

newtype QueryPairs k v

A query string split into an array of key/value pairs. There is no precise spec for this, but the format is commonly used, so this attempts to handle these strings in a sensible way.

  • The representation uses an array rather than a map, so duplicate keys are supported.
  • Keys are not required to have a value associated.
  • & and ; are both treated as pair delimiters.

Constructors

Instances

#parse

parse :: forall k v. (Key -> Either URIPartParseError k) -> (Value -> Either URIPartParseError v) -> Query -> Either URIPartParseError (QueryPairs k v)

Parses a query into key/value pairs.

This function allows for the Key and Value components to be parsed into custom representations. If this is not necessary, use pure for both these arguments.

#print

print :: forall k v. (k -> Key) -> (v -> Value) -> QueryPairs k v -> Query

A printer for key/value pairs style query string.

As a counterpart to the parser this function also requires the Key and Value components to be printed back from their custom representations. If no custom types are being used, pass identity for both of these arguments.

#keyPartChar

keyPartChar :: Parser String Char

The supported key characters, excluding percent-encodings.

#valuePartChar

valuePartChar :: Parser String Char

The supported value characters, excluding percent-encodings.

#Key

newtype Key

The default Key type used for QueryPairs.

Instances

#keyFromString

keyFromString :: String -> Key

Constructs a key value from a string, percent-encoding any characters that require it. Note that running this on a string that has already had percent-encoding applied will double-encode it, for those situations use unsafeKeyFromString instead.

keyFromString "foo" = unsafeKeyFromString "foo"
keyFromString "foo#bar" = unsafeKeyFromString "foo%23bar"
keyFromString "foo%23bar" = unsafeKeyFromString "foo%2523bar"

#keyToString

keyToString :: Key -> String

Returns the string value for a key, percent-decoding any characters that require it.

keyToString (unsafeKeyFromString "foo") = "foo"
keyToString (unsafeKeyFromString "foo%23bar") = "foo#bar"

#unsafeKeyFromString

unsafeKeyFromString :: String -> Key

Constructs a key value from a string directly - no percent-encoding will be applied. This is useful when using a custom encoding scheme for the key, to prevent double-encoding.

#unsafeKeyToString

unsafeKeyToString :: Key -> String

Returns the string value for a key without percent-decoding. Only "unsafe" in the sense that values this produces may need further decoding, the name is more for symmetry with the fromString/unsafeFromString pairing.

#Value

newtype Value

The default Value type used for QueryPairs.

Instances

#valueFromString

valueFromString :: String -> Value

Constructs a value from a string, percent-encoding any characters that require it. Note that running this on a string that has already had percent-encoding applied will double-encode it, for those situations use unsafeValueFromString instead.

valueFromString "foo" = unsafeValueFromString "foo"
valueFromString "foo#bar" = unsafeValueFromString "foo%23bar"
valueFromString "foo%23bar" = unsafeValueFromString "foo%2523bar"

#valueToString

valueToString :: Value -> String

Returns the string value for a value, percent-decoding any characters that require it.

valueToString (unsafeValueFromString "foo") = "foo"
valueToString (unsafeValueFromString "foo%23bar") = "foo#bar"

#unsafeValueFromString

unsafeValueFromString :: String -> Value

Constructs a value from a string directly - no percent-encoding will be applied. This is useful when using a custom encoding scheme for the value, to prevent double-encoding.

#unsafeValueToString

unsafeValueToString :: Value -> String

Returns the string value for a value without percent-decoding. Only "unsafe" in the sense that values this produces may need further decoding, the name is more for symmetry with the fromString/unsafeFromString pairing.

Modules