{-# LANGUAGE FlexibleInstances #-}
module Network.HTTP.Types.QueryLike (
QueryLike (..),
QueryKeyLike (..),
QueryValueLike (..),
)
where
import Control.Arrow ((***))
import Data.ByteString as B (ByteString, concat)
import Data.ByteString.Lazy as L (ByteString, toChunks)
import Data.Maybe (catMaybes)
import Data.Text as T (Text, pack)
import Data.Text.Encoding as T (encodeUtf8)
import Network.HTTP.Types.URI (Query)
class QueryLike a where
toQuery :: a -> Query
class QueryKeyLike a where
toQueryKey :: a -> B.ByteString
class QueryValueLike a where
toQueryValue :: a -> Maybe B.ByteString
instance (QueryKeyLike k, QueryValueLike v) => QueryLike [(k, v)] where
toQuery :: [(k, v)] -> Query
toQuery = forall a b. (a -> b) -> [a] -> [b]
map (forall a. QueryKeyLike a => a -> ByteString
toQueryKey forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** forall a. QueryValueLike a => a -> Maybe ByteString
toQueryValue)
instance (QueryKeyLike k, QueryValueLike v) => QueryLike [Maybe (k, v)] where
toQuery :: [Maybe (k, v)] -> Query
toQuery = forall a. QueryLike a => a -> Query
toQuery forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Maybe a] -> [a]
catMaybes
instance QueryKeyLike B.ByteString where toQueryKey :: ByteString -> ByteString
toQueryKey = forall a. a -> a
id
instance QueryKeyLike L.ByteString where toQueryKey :: ByteString -> ByteString
toQueryKey = [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks
instance QueryKeyLike T.Text where toQueryKey :: Text -> ByteString
toQueryKey = Text -> ByteString
T.encodeUtf8
instance QueryKeyLike [Char] where toQueryKey :: [Char] -> ByteString
toQueryKey = Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack
instance QueryValueLike B.ByteString where toQueryValue :: ByteString -> Maybe ByteString
toQueryValue = forall a. a -> Maybe a
Just
instance QueryValueLike L.ByteString where toQueryValue :: ByteString -> Maybe ByteString
toQueryValue = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks
instance QueryValueLike T.Text where toQueryValue :: Text -> Maybe ByteString
toQueryValue = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
instance QueryValueLike [Char] where toQueryValue :: [Char] -> Maybe ByteString
toQueryValue = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack
instance QueryValueLike a => QueryValueLike (Maybe a) where
toQueryValue :: Maybe a -> Maybe ByteString
toQueryValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Maybe a
Nothing forall a. QueryValueLike a => a -> Maybe ByteString
toQueryValue