module Nero.Param
(
Param(..)
, MultiMap
, fromList
, null
) where
import Prelude hiding (null)
import Data.Text.Lazy (Text, intercalate)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text.Lazy.Lens (utf8)
import Nero.Prelude
import Nero.Binary
class Param a where
param :: Text -> Traversal' a Text
newtype MultiMap = MultiMap { unMultiMap :: Map Text [Text] }
deriving (Eq, Show)
instance Monoid MultiMap where
mappend (MultiMap m1) (MultiMap m2) =
MultiMap $ Map.unionWith mappend m1 m2
mempty = MultiMap mempty
instance Wrapped MultiMap where
type Unwrapped MultiMap = Map Text [Text]
_Wrapped' = iso unMultiMap MultiMap
type instance Index MultiMap = Text
type instance IxValue MultiMap = [Text]
instance Ixed MultiMap where
ix k = _Wrapped' . ix k
instance At MultiMap where
at k = _Wrapped' . at k
instance Param MultiMap where
param k = ix k . traverse
instance Renderable MultiMap where
render = review utf8
. intercalate "&"
. fold . Map.mapWithKey (map . mappend . flip mappend "=")
. unMultiMap
fromList :: [(Text, [Text])] -> MultiMap
fromList = MultiMap . Map.fromListWith (++)
null :: MultiMap -> Bool
null = Map.null . unMultiMap