on master help sign in
Use Functor class for functor business.
commitd106ed9c9f36d6c815d492a5ddad5b3c4fe41c65 authorMatthias Fischmann <mf@zerobuzz.net> dateMon 12 Oct 2015, 18:51 (11 years ago) parent96f10ad
1 file changed+12−8
servant-server/src/Servant/Server/Internal/Router.hs+12−8
servant-server/src/Servant/Server/Internal/Router.hs+12−8
@@ -1,3 +1,5 @@
1+{-# LANGUAGE DeriveFunctor #-}
2+
13module Servant.Server.Internal.Router where
24
35import Data.Map (Map)
@@ -8,25 +10,27 @@ import Network.Wai (Request, Response,
810import Servant.Server.Internal.PathInfo
911import Servant.Server.Internal.RoutingApplication
1012
13+type Router = Router' RoutingApplication
14+
1115-- | Internal representation of a router.
12−data Router =
16+data Router' a =
1317 WithRequest (Request -> Router)
1418 -- ^ current request is passed to the router
1519 | StaticRouter (Map Text Router)
1620 -- ^ first path component used for lookup and removed afterwards
1721 | DynamicRouter (Text -> Router)
1822 -- ^ first path component used for lookup and removed afterwards
19− | LeafRouter RoutingApplication
23+ | LeafRouter a
2024 -- ^ to be used for routes that match an empty path
2125 | Choice Router Router
2226 -- ^ left-biased choice between two routers
27+ deriving Functor
2328
24−fmapRouter :: (RouteResult Response -> RouteResult Response) -> Router -> Router
25−fmapRouter f (LeafRouter a) = LeafRouter $ \req cont -> a req (cont . f)
26−fmapRouter f (StaticRouter m) = StaticRouter (fmapRouter f <$> m)
27−fmapRouter f (DynamicRouter d) = DynamicRouter (fmapRouter f <$> d)
28−fmapRouter f (Choice r1 r2) = Choice (fmapRouter f r1) (fmapRouter f r2)
29−fmapRouter f (WithRequest g) = WithRequest (fmapRouter f . g)
29+-- | Apply a function to the result of a router in functor style. The result contains the failure
30+-- cases so one use case is to turn failures into middleware response values with appropriate status
31+-- codes, message bodies, etc.
32+tweakResponse :: (RouteResult Response -> RouteResult Response) -> Router -> Router
33+tweakResponse f = fmap (\a -> \req cont -> a req (cont . f))
3034
3135-- | Smart constructor for the choice between routers.
3236-- We currently optimize the following cases: