{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ViewPatterns #-}
module Yesod.Routes.TH.Dispatch
( MkDispatchSettings (..)
, mkDispatchClause
, defaultGetHandler
, SDC(..)
, mkDispatchInstance
, mkNestedDispatchInstance
, mkNestedDispatchInstanceWith
, NestedTarget (..)
, SameSpliceNestedInstances (..)
, mkMDS
, mkYesodSubDispatch
, mkYesodSubDispatchWith
, mkYesodSubDispatchWithDelegate
, subTopDispatch
, parseYesodName
) where
import Text.Parsec (parse, many1, many, eof, try, option, sepBy1)
import Text.ParserCombinators.Parsec.Char (alphaNum, spaces, string, char)
import Data.Maybe
import Data.Proxy (Proxy(..))
import Yesod.Routes.TH.RenderRoute
import qualified Network.Wai as W
import Yesod.Core.Content (ToTypedContent (..))
import Language.Haskell.TH hiding (cxt, instanceD)
import Yesod.Core.Types hiding (Body)
import Yesod.Core.Class.Dispatch
import Prelude hiding (exp)
import Yesod.Routes.TH.Internal
import Control.Monad
import Control.Monad.Reader (ReaderT, runReaderT, asks, local, lift)
import Data.List (foldl')
import Yesod.Routes.TH.Types
import Data.Char (toLower)
import Yesod.Core.Internal.Run
import Yesod.Core.Handler
import Yesod.Core.Class.Dispatch.ToParentRoute (ToParentRoute(..))
import Yesod.Core.Class.Yesod (Yesod)
data MkDispatchSettings b site c = MkDispatchSettings
{ forall b site c. MkDispatchSettings b site c -> Q Exp
mdsRunHandler :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mdsSubDispatcher :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mdsGetPathInfo :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mdsSetPathInfo :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mdsMethod :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mds404 :: Q Exp
, forall b site c. MkDispatchSettings b site c -> Q Exp
mds405 :: Q Exp
, forall b site c.
MkDispatchSettings b site c -> Maybe String -> String -> Q Exp
mdsGetHandler :: Maybe String -> String -> Q Exp
, forall b site c. MkDispatchSettings b site c -> Exp -> Q Exp
mdsUnwrapper :: Exp -> Q Exp
, forall b site c. MkDispatchSettings b site c -> Bool
mdsNestedRouteFallthrough :: !Bool
, forall b site c. MkDispatchSettings b site c -> NestedTarget
mdsNestedTarget :: NestedTarget
, forall b site c.
MkDispatchSettings b site c -> SameSpliceNestedInstances
mdsSameSpliceNestedInstances :: !SameSpliceNestedInstances
}
data SDC = SDC
{ :: [Exp]
, :: [Exp]
, SDC -> Exp
envExp :: Exp
, SDC -> Exp
reqExp :: Exp
}
data DispatchPhase = TopLevelPhase | NestedPhase
wrapForPhase :: DispatchPhase -> Exp -> Exp
wrapForPhase :: DispatchPhase -> Exp -> Exp
wrapForPhase DispatchPhase
TopLevelPhase = Exp -> Exp
forall a. a -> a
id
wrapForPhase DispatchPhase
NestedPhase = \Exp
e -> Name -> Exp
ConE 'Just Exp -> Exp -> Exp
`AppE` Exp
e
data Env = Env
{ Env -> SDC
envSdc :: SDC
, Env -> DispatchPhase
envPhase :: DispatchPhase
}
type DispatchM = ReaderT Env Q
data NestedTarget
= TopLevelNested
| SubsiteNested
deriving (NestedTarget -> NestedTarget -> Bool
(NestedTarget -> NestedTarget -> Bool)
-> (NestedTarget -> NestedTarget -> Bool) -> Eq NestedTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NestedTarget -> NestedTarget -> Bool
== :: NestedTarget -> NestedTarget -> Bool
$c/= :: NestedTarget -> NestedTarget -> Bool
/= :: NestedTarget -> NestedTarget -> Bool
Eq, Int -> NestedTarget -> ShowS
[NestedTarget] -> ShowS
NestedTarget -> String
(Int -> NestedTarget -> ShowS)
-> (NestedTarget -> String)
-> ([NestedTarget] -> ShowS)
-> Show NestedTarget
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NestedTarget -> ShowS
showsPrec :: Int -> NestedTarget -> ShowS
$cshow :: NestedTarget -> String
show :: NestedTarget -> String
$cshowList :: [NestedTarget] -> ShowS
showList :: [NestedTarget] -> ShowS
Show)
data SameSpliceNestedInstances
= GeneratesNestedInstances
| NoSameSpliceNestedInstances
deriving (SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
(SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool)
-> (SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool)
-> Eq SameSpliceNestedInstances
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
== :: SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
$c/= :: SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
/= :: SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
Eq, Int -> SameSpliceNestedInstances -> ShowS
[SameSpliceNestedInstances] -> ShowS
SameSpliceNestedInstances -> String
(Int -> SameSpliceNestedInstances -> ShowS)
-> (SameSpliceNestedInstances -> String)
-> ([SameSpliceNestedInstances] -> ShowS)
-> Show SameSpliceNestedInstances
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SameSpliceNestedInstances -> ShowS
showsPrec :: Int -> SameSpliceNestedInstances -> ShowS
$cshow :: SameSpliceNestedInstances -> String
show :: SameSpliceNestedInstances -> String
$cshowList :: [SameSpliceNestedInstances] -> ShowS
showList :: [SameSpliceNestedInstances] -> ShowS
Show)
nestedTargetClass :: NestedTarget -> Name
nestedTargetClass :: NestedTarget -> Name
nestedTargetClass NestedTarget
TopLevelNested = ''YesodDispatchNested
nestedTargetClass NestedTarget
SubsiteNested = ''YesodSubDispatchNested
nestedTargetFn :: NestedTarget -> Name
nestedTargetFn :: NestedTarget -> Name
nestedTargetFn NestedTarget
TopLevelNested = 'yesodDispatchNested
nestedTargetFn NestedTarget
SubsiteNested = 'yesodSubDispatchNested
nestedTargetRunner :: NestedTarget -> Name
nestedTargetRunner :: NestedTarget -> Name
nestedTargetRunner NestedTarget
TopLevelNested = 'yesodRunner
nestedTargetRunner NestedTarget
SubsiteNested = 'subHelper
nestedTargetCallSite :: NestedTarget -> ArityCallSite
nestedTargetCallSite :: NestedTarget -> ArityCallSite
nestedTargetCallSite NestedTarget
TopLevelNested = ArityCallSite
TopLevelCall
nestedTargetCallSite NestedTarget
SubsiteNested = ArityCallSite
SubsiteCall
mkDispatchClause :: forall a b site c. TyArgs -> MkDispatchSettings b site c -> [ResourceTree a] -> Q ([String], Clause)
mkDispatchClause :: forall a b site c.
TyArgs
-> MkDispatchSettings b site c
-> [ResourceTree a]
-> Q ([String], Clause)
mkDispatchClause TyArgs
tyargs MkDispatchSettings {Bool
Q Exp
SameSpliceNestedInstances
NestedTarget
Maybe String -> String -> Q Exp
Exp -> Q Exp
mdsRunHandler :: forall b site c. MkDispatchSettings b site c -> Q Exp
mdsSubDispatcher :: forall b site c. MkDispatchSettings b site c -> Q Exp
mdsGetPathInfo :: forall b site c. MkDispatchSettings b site c -> Q Exp
mdsSetPathInfo :: forall b site c. MkDispatchSettings b site c -> Q Exp
mdsMethod :: forall b site c. MkDispatchSettings b site c -> Q Exp
mds404 :: forall b site c. MkDispatchSettings b site c -> Q Exp
mds405 :: forall b site c. MkDispatchSettings b site c -> Q Exp
mdsGetHandler :: forall b site c.
MkDispatchSettings b site c -> Maybe String -> String -> Q Exp
mdsUnwrapper :: forall b site c. MkDispatchSettings b site c -> Exp -> Q Exp
mdsNestedRouteFallthrough :: forall b site c. MkDispatchSettings b site c -> Bool
mdsNestedTarget :: forall b site c. MkDispatchSettings b site c -> NestedTarget
mdsSameSpliceNestedInstances :: forall b site c.
MkDispatchSettings b site c -> SameSpliceNestedInstances
mdsRunHandler :: Q Exp
mdsSubDispatcher :: Q Exp
mdsGetPathInfo :: Q Exp
mdsSetPathInfo :: Q Exp
mdsMethod :: Q Exp
mds404 :: Q Exp
mds405 :: Q Exp
mdsGetHandler :: Maybe String -> String -> Q Exp
mdsUnwrapper :: Exp -> Q Exp
mdsNestedRouteFallthrough :: Bool
mdsNestedTarget :: NestedTarget
mdsSameSpliceNestedInstances :: SameSpliceNestedInstances
..} [ResourceTree a]
resources = do
envName <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"env"
reqName <- newName "req"
helperName <- newName "dispatchHelper"
let envE = Name -> Exp
VarE Name
envName
reqE = Name -> Exp
VarE Name
reqName
helperE = Name -> Exp
VarE Name
helperName
clause404' <- mkClause404 envE reqE
getPathInfo <- mdsGetPathInfo
let pathInfo = Exp
getPathInfo Exp -> Exp -> Exp
`AppE` Exp
reqE
let sdc = SDC
{ extraParams :: [Exp]
extraParams = []
, extraCons :: [Exp]
extraCons = []
, envExp :: Exp
envExp = Exp
envE
, reqExp :: Exp
reqExp = Exp
reqE
}
let topEnv = Env { envSdc :: SDC
envSdc = SDC
sdc, envPhase :: DispatchPhase
envPhase = DispatchPhase
TopLevelPhase }
(childNames, clauses) <- mconcat <$> runReaderT (mapM go resources) topEnv
pure
( childNames
, Clause
[VarP envName, VarP reqName]
(NormalB $ helperE `AppE` pathInfo)
[FunD helperName $ clauses ++ [clause404']]
)
where
liftQ :: Q x -> DispatchM x
liftQ :: forall x. Q x -> DispatchM x
liftQ = Q x -> ReaderT Env Q x
forall (m :: * -> *) a. Monad m => m a -> ReaderT Env m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
askWrap :: DispatchM (Exp -> Exp)
askWrap :: DispatchM (Exp -> Exp)
askWrap = (Env -> Exp -> Exp) -> DispatchM (Exp -> Exp)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (DispatchPhase -> Exp -> Exp
wrapForPhase (DispatchPhase -> Exp -> Exp)
-> (Env -> DispatchPhase) -> Env -> Exp -> Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env -> DispatchPhase
envPhase)
withChildScope :: [Exp] -> Exp -> DispatchM r -> DispatchM r
withChildScope :: forall r. [Exp] -> Exp -> DispatchM r -> DispatchM r
withChildScope [Exp]
dyns Exp
constr = (Env -> Env) -> ReaderT Env Q r -> ReaderT Env Q r
forall a. (Env -> Env) -> ReaderT Env Q a -> ReaderT Env Q a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local ((Env -> Env) -> ReaderT Env Q r -> ReaderT Env Q r)
-> (Env -> Env) -> ReaderT Env Q r -> ReaderT Env Q r
forall a b. (a -> b) -> a -> b
$ \Env
e ->
let sdc :: SDC
sdc = Env -> SDC
envSdc Env
e
in Env
e { envSdc = sdc
{ extraParams = extraParams sdc ++ dyns
, extraCons = extraCons sdc ++ [constr]
}
, envPhase = NestedPhase
}
go :: ResourceTree a -> DispatchM ([String], [Clause])
go :: ResourceTree a -> ReaderT Env Q ([String], [Clause])
go (ResourceParent String
name Bool
_check Set String
_attrs [Piece a]
pieces [ResourceTree a]
children) = do
instanceExists <-
Q Bool -> DispatchM Bool
forall x. Q x -> DispatchM x
liftQ (Name -> RouteCon -> Q Bool
nestedInstanceExists (NestedTarget -> Name
nestedTargetClass NestedTarget
mdsNestedTarget) (RouteCon -> Q Bool) -> Q RouteCon -> Q Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> Q RouteCon
resolveRouteCon String
name)
let delegate =
Bool
instanceExists Bool -> Bool -> Bool
|| SameSpliceNestedInstances
mdsSameSpliceNestedInstances SameSpliceNestedInstances -> SameSpliceNestedInstances -> Bool
forall a. Eq a => a -> a -> Bool
== SameSpliceNestedInstances
GeneratesNestedInstances
(pats, dyns) <- liftQ $ handlePiecesM pieces
restName <- liftQ $ newName "_rest"
helperName <- liftQ $ newName ("helper" ++ name)
let helperCall = Name -> Exp
VarE Name
helperName Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
restName
constr = String -> [Exp] -> Exp
applyConPieces String
name [Exp]
dyns
helperClauses <-
if delegate
then do
expr <- delegateToNestedInstance name dyns
pure [Clause [VarP restName] (NormalB expr) []]
else do
childClauses <-
concatMap snd <$> withChildScope dyns constr (mapM go children)
pure $ childClauses ++ [Clause [WildP] (NormalB (ConE 'Nothing)) []]
body <- parentBody helperCall
pure
( [name | not instanceExists]
, [ Clause
[mkPathPat (EndRest restName) pats]
body
[FunD helperName helperClauses] ]
)
go (ResourceLeaf (Resource String
name [Piece a]
pieces Dispatch a
dispatch [String]
_ Bool
_check)) = do
(pats, dyns) <- Q ([Pat], [Exp]) -> DispatchM ([Pat], [Exp])
forall x. Q x -> DispatchM x
liftQ (Q ([Pat], [Exp]) -> DispatchM ([Pat], [Exp]))
-> Q ([Pat], [Exp]) -> DispatchM ([Pat], [Exp])
forall a b. (a -> b) -> a -> b
$ [Piece a] -> Q ([Pat], [Exp])
forall (m :: * -> *) a. Quote m => [Piece a] -> m ([Pat], [Exp])
handlePiecesM [Piece a]
pieces
(chooseMethod, finalPat) <- handleDispatch name dispatch dyns
wrap <- askWrap
let clauseBody = Exp -> Body
NormalB (Exp -> Exp
wrap Exp
chooseMethod)
pure ([], [Clause [mkPathPat finalPat pats] clauseBody []])
delegateToNestedInstance :: String -> [Exp] -> DispatchM Exp
delegateToNestedInstance :: String -> [Exp] -> DispatchM Exp
delegateToNestedInstance String
name [Exp]
dyns = do
sdc <- (Env -> SDC) -> ReaderT Env Q SDC
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Env -> SDC
envSdc
let thisRouteParentArgs = SDC -> [Exp]
extraParams SDC
sdc [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp]
dyns
liftQ (nestedDispatchCall (nestedTargetFn mdsNestedTarget) name dyns tyargs sdc thisRouteParentArgs)
parentBody :: Exp -> DispatchM Body
parentBody :: Exp -> DispatchM Body
parentBody Exp
helperCall = do
wrap <- DispatchM (Exp -> Exp)
askWrap
if mdsNestedRouteFallthrough
then liftQ $ mkGuardedBody helperCall (\Name
match' -> Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> Exp
wrap (Name -> Exp
VarE Name
match')))
else do
sdc <- asks envSdc
liftQ $ do
matchName <- newName "match"
baseNotFoundExp <-
[| $(mdsRunHandler) $(mds404) $(pure (envExp sdc)) Nothing $(pure (reqExp sdc)) |]
pure $ NormalB $ CaseE helperCall
[ Match (conPCompat 'Just [VarP matchName]) (NormalB (wrap (VarE matchName))) []
, Match (conPCompat 'Nothing []) (NormalB (wrap baseNotFoundExp)) []
]
handleDispatch :: String -> Dispatch a -> [Exp] -> DispatchM (Exp, PathTail)
handleDispatch :: String -> Dispatch a -> [Exp] -> DispatchM (Exp, PathTail)
handleDispatch String
name Dispatch a
dispatch' [Exp]
dyns = do
SDC {..} <- (Env -> SDC) -> ReaderT Env Q SDC
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Env -> SDC
envSdc
liftQ $ case dispatch' of
Methods Maybe a
multi [String]
methods -> do
(finalPat, mfinalE) <-
case Maybe a
multi of
Maybe a
Nothing -> (PathTail, Maybe Exp) -> Q (PathTail, Maybe Exp)
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return (PathTail
EndExact, Maybe Exp
forall a. Maybe a
Nothing)
Just a
_ -> do
multiName <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"multi"
return (EndMulti multiName, Just $ VarE multiName)
let dynsMulti =
case Maybe Exp
mfinalE of
Maybe Exp
Nothing -> [Exp]
dyns
Just Exp
e -> [Exp]
dyns [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp
e]
thisRoute = String -> [Exp] -> Exp
applyConPieces String
name [Exp]
dynsMulti
fullRoute = (Exp -> Exp -> Exp) -> Exp -> [Exp] -> Exp
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Exp -> Exp -> Exp
AppE Exp
thisRoute [Exp]
extraCons
jroute = Name -> Exp
ConE 'Just Exp -> Exp -> Exp
`AppE` Exp
fullRoute
allDyns = [Exp]
extraParams [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp]
dynsMulti
mkRunExp Maybe String
mmethod = do
runHandlerE <- Q Exp
mdsRunHandler
handlerE' <- mdsGetHandler mmethod name
handlerE <- mdsUnwrapper $ foldl' AppE handlerE' allDyns
return $ runHandlerE
`AppE` handlerE
`AppE` envExp
`AppE` jroute
`AppE` reqExp
func <-
case methods of
[] -> Maybe String -> Q Exp
mkRunExp Maybe String
forall a. Maybe a
Nothing
[String]
_ -> do
getMethod <- Q Exp
mdsMethod
let methodE = Exp
getMethod Exp -> Exp -> Exp
`AppE` Exp
reqExp
matches <- forM methods $ \String
method -> do
exp <- Maybe String -> Q Exp
mkRunExp (String -> Maybe String
forall a. a -> Maybe a
Just String
method)
return $ Match (LitP $ StringL method) (NormalB exp) []
match405 <- do
runHandlerE <- mdsRunHandler
handlerE <- mds405
let exp = Exp
runHandlerE
Exp -> Exp -> Exp
`AppE` Exp
handlerE
Exp -> Exp -> Exp
`AppE` Exp
envExp
Exp -> Exp -> Exp
`AppE` Exp
jroute
Exp -> Exp -> Exp
`AppE` Exp
reqExp
return $ Match WildP (NormalB exp) []
return $ CaseE methodE $ matches ++ [match405]
return (func, finalPat)
Subsite a
_ String
getSub -> do
restPath <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"restPath"
let allDyns = [Exp]
extraParams [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp]
dyns
sub2 <- mkLambda "sub" $ \Name
sub ->
Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ (Exp -> Exp -> Exp) -> Exp -> [Exp] -> Exp
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Exp
a Exp
b -> Exp
a Exp -> Exp -> Exp
`AppE` Exp
b) (Name -> Exp
VarE (String -> Name
mkName String
getSub) Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
sub) [Exp]
allDyns
routeBuilder <- mkLambda "sroute" $ \Name
sroute ->
Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ let thisRoute :: Exp
thisRoute = String -> [Exp] -> Exp
applyConPieces String
name [Exp]
dyns
in (Exp -> Exp -> Exp) -> Exp -> [Exp] -> Exp
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Exp -> Exp -> Exp
AppE (Exp -> Exp -> Exp
AppE Exp
thisRoute (Exp -> Exp) -> Exp -> Exp
forall a b. (a -> b) -> a -> b
$ Name -> Exp
VarE Name
sroute) [Exp]
extraCons
exp <-
[| $(mdsSubDispatcher)
$(mdsRunHandler)
$(pure sub2)
$(pure routeBuilder)
$(pure envExp)
($(mdsSetPathInfo) $(varE restPath) $(pure reqExp)) |]
return (exp, EndRest restPath)
mkClause404 :: Exp -> Exp -> Q Clause
mkClause404 Exp
envE Exp
reqE = do
exp <- [| $(Q Exp
mdsRunHandler) $(Q Exp
mds404) $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
envE) Nothing $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
reqE) |]
return $ Clause [WildP] (NormalB exp) []
nestedDispatchCall
:: Name
-> String
-> [Exp]
-> TyArgs
-> SDC
-> [Exp]
-> Q Exp
nestedDispatchCall :: Name -> String -> [Exp] -> TyArgs -> SDC -> [Exp] -> Q Exp
nestedDispatchCall Name
dispatchFn String
routeName [Exp]
routeDyns TyArgs
tyargs SDC
sdc [Exp]
parentDyns = do
routeType <- String -> TyArgs -> Q Type
appliedRouteTypeNamed String
routeName TyArgs
tyargs
let wrapper = (Exp -> Exp -> Exp) -> Exp -> [Exp] -> Exp
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Exp -> Exp -> Exp
composeE (String -> [Exp] -> Exp
applyConPieces String
routeName [Exp]
routeDyns) (SDC -> [Exp]
extraCons SDC
sdc)
[| $(varE dispatchFn)
(Proxy :: Proxy $(pure routeType))
$(pure $ parentArgsExprFromExps parentDyns)
$(pure wrapper)
$(pure $ envExp sdc)
$(pure $ reqExp sdc)
|]
mkGuardedBody
:: Exp
-> (Name -> Q Exp)
-> Q Body
mkGuardedBody :: Exp -> (Name -> Q Exp) -> Q Body
mkGuardedBody Exp
exp Name -> Q Exp
mkRhs = do
matchName <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"match"
result <- mkRhs matchName
let patGuard =
[Stmt] -> Guard
PatG [Pat -> Exp -> Stmt
BindS (Name -> [Pat] -> Pat
conPCompat 'Just [Name -> Pat
VarP Name
matchName]) Exp
exp]
pure $ GuardedB [(patGuard, result)]
defaultGetHandler :: Maybe String -> String -> Q Exp
defaultGetHandler :: Maybe String -> String -> Q Exp
defaultGetHandler Maybe String
Nothing String
s = Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ Name -> Exp
VarE (Name -> Exp) -> Name -> Exp
forall a b. (a -> b) -> a -> b
$ String -> Name
mkName (String -> Name) -> String -> Name
forall a b. (a -> b) -> a -> b
$ String
"handle" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s
defaultGetHandler (Just String
method) String
s = Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ Name -> Exp
VarE (Name -> Exp) -> Name -> Exp
forall a b. (a -> b) -> a -> b
$ String -> Name
mkName (String -> Name) -> String -> Name
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
method String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s
mkDispatchInstance
:: RouteOpts
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkDispatchInstance :: RouteOpts
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkDispatchInstance RouteOpts
routeOpts Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res =
case RouteOpts -> Maybe String
roFocusOnNestedRoute RouteOpts
routeOpts of
Just String
target ->
RouteOpts
-> String
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkNestedDispatchInstance RouteOpts
routeOpts String
target Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res
Maybe String
Nothing ->
RouteOpts
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkTopLevelDispatchInstance RouteOpts
routeOpts Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res
mkTopLevelDispatchInstance
:: RouteOpts
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkTopLevelDispatchInstance :: RouteOpts
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkTopLevelDispatchInstance RouteOpts
routeOpts Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res = do
let mds :: MkDispatchSettings a site b
mds =
(Exp -> Q Exp) -> Q Exp -> Q Exp -> MkDispatchSettings a site b
forall a site b.
(Exp -> Q Exp) -> Q Exp -> Q Exp -> MkDispatchSettings a site b
mkMDS
Exp -> Q Exp
unwrapper
[|yesodRunner|]
[|\parentRunner getSub toParent env -> yesodSubDispatch
YesodSubRunnerEnv
{ ysreParentRunner = parentRunner
, ysreGetSub = getSub
, ysreToParentRoute = toParent
, ysreParentEnv = env
}
|]
usesNestedDiscovery :: Bool
usesNestedDiscovery =
case RouteOpts -> TyArgs -> DiscoveryMode
discoveryMode RouteOpts
routeOpts TyArgs
tyargs of
DiscoveryMode
NestedDiscovery -> Bool
True
DiscoveryMode
InlineCompat -> Bool
False
mdsWithNestedDispatch :: MkDispatchSettings b site c
mdsWithNestedDispatch = MkDispatchSettings (ZonkAny 2) (ZonkAny 1) (ZonkAny 0)
forall {a} {site} {b}. MkDispatchSettings a site b
mds
{ mdsNestedRouteFallthrough = roNestedRouteFallthrough routeOpts
, mdsSameSpliceNestedInstances =
if usesNestedDiscovery
then GeneratesNestedInstances
else NoSameSpliceNestedInstances
}
(childNames, clause') <- TyArgs
-> MkDispatchSettings (ZonkAny 5) (ZonkAny 4) (ZonkAny 3)
-> [ResourceTree Type]
-> Q ([String], Clause)
forall a b site c.
TyArgs
-> MkDispatchSettings b site c
-> [ResourceTree a]
-> Q ([String], Clause)
mkDispatchClause TyArgs
tyargs MkDispatchSettings (ZonkAny 5) (ZonkAny 4) (ZonkAny 3)
forall {a} {site} {b}. MkDispatchSettings a site b
mdsWithNestedDispatch [ResourceTree Type]
res
let thisDispatch = Name -> [Clause] -> Dec
FunD 'yesodDispatch [Clause
clause']
childNamesToGenerate =
if Bool
usesNestedDiscovery then [String]
childNames else []
childInstances <-
fmap mconcat $ forM childNamesToGenerate $ \String
name -> do
RouteOpts
-> String
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkNestedDispatchInstance RouteOpts
routeOpts String
name Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res
return (instanceD cxt yDispatch [thisDispatch] : childInstances)
where
yDispatch :: Type
yDispatch = Name -> Type
ConT ''YesodDispatch Type -> Type -> Type
`AppT` Type
master
mkNestedDispatchInstance
:: RouteOpts
-> String
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> Q [Dec]
mkNestedDispatchInstance :: RouteOpts
-> String
-> Type
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkNestedDispatchInstance RouteOpts
routeOpts String
target Type
master Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res =
NestedTarget
-> Maybe Type
-> RouteOpts
-> String
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkNestedDispatchInstanceWith NestedTarget
TopLevelNested (Type -> Maybe Type
forall a. a -> Maybe a
Just Type
master)
RouteOpts
routeOpts String
target Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res
mkNestedDispatchInstanceWith
:: NestedTarget
-> Maybe Type
-> RouteOpts
-> String
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> Q [Dec]
mkNestedDispatchInstanceWith :: NestedTarget
-> Maybe Type
-> RouteOpts
-> String
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree Type]
-> DecsQ
mkNestedDispatchInstanceWith NestedTarget
nestedTarget Maybe Type
mmaster RouteOpts
routeOpts String
target Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree Type]
res = do
case String
-> [ResourceTree Type] -> Maybe ([Piece Type], [ResourceTree Type])
forall a.
String -> [ResourceTree a] -> Maybe ([Piece a], [ResourceTree a])
findNestedRoute String
target [ResourceTree Type]
res of
Maybe ([Piece Type], [ResourceTree Type])
Nothing ->
String -> DecsQ
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> DecsQ) -> String -> DecsQ
forall a b. (a -> b) -> a -> b
$ String
"Target '" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
target String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"' was not found in resources."
Just ([Piece Type]
prePieces, [ResourceTree Type]
subres) ->
String -> [Piece Type] -> [ResourceTree Type] -> DecsQ
go String
target [Piece Type]
prePieces [ResourceTree Type]
subres
where
go :: String -> [Piece Type] -> [ResourceTree Type] -> DecsQ
go String
curTarget [Piece Type]
prePieces [ResourceTree Type]
subres = do
let parentDepth :: Int
parentDepth = [Piece Type] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Piece Type]
prePieces
preDyns :: [()]
preDyns = [() | Dynamic Type
_ <- [Piece Type]
prePieces]
targetT :: Type
targetT = Type -> TyArgs -> Type
applyTyArgs (Name -> Type
ConT (String -> Name
mkName String
curTarget)) TyArgs
tyargs
urlToDispatchInstances <- case (Maybe Type
mmaster, [()]
preDyns) of
(Just Type
master, []) ->
Cxt -> String -> Type -> Type -> DecsQ
mkUrlToDispatchRedirectInstances Cxt
cxt String
curTarget Type
targetT Type
master
(Maybe Type, [()])
_ ->
[Dec] -> DecsQ
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
parentDynVars <- forM preDyns $ \()
_ -> String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"parentDyn"
let parentDynsP = [Name] -> Pat
parentArgsPat [Name]
parentDynVars
toParentN <- newName "toParentRoute"
envN <- newName "env"
reqN <- newName "req"
clauses <- genNestedDispatchClauses
nestedTarget
routeOpts
parentDynVars
(VarE toParentN)
(VarE envN)
(VarE reqN)
unwrapper
tyargs
subres
dropExp <- [| drop parentDepth (W.pathInfo $(varE reqN)) |]
let dispatchNestedT = Name -> Type
ConT (NestedTarget -> Name
nestedTargetClass NestedTarget
nestedTarget) Type -> Type -> Type
`AppT` Type
targetT
thisDispatch = Name -> [Clause] -> Dec
FunD (NestedTarget -> Name
nestedTargetFn NestedTarget
nestedTarget)
[[Pat] -> Body -> [Dec] -> Clause
Clause
[Pat
WildP, Pat
parentDynsP, Name -> Pat
VarP Name
toParentN, Name -> Pat
VarP Name
envN, Name -> Pat
VarP Name
reqN]
(Exp -> Body
NormalB (Exp -> Body) -> Exp -> Body
forall a b. (a -> b) -> a -> b
$ Exp -> [Match] -> Exp
CaseE Exp
dropExp [Match]
clauses)
[]
]
childInstances <-
fmap mconcat $ forM subres $ \ResourceTree Type
childRes -> do
case ResourceTree Type
childRes of
ResourceParent String
name Bool
_ Set String
_ [Piece Type]
childPieces [ResourceTree Type]
grandchildren -> do
rc <- String -> Q RouteCon
resolveRouteCon String
name
instanceExists <- nestedInstanceExists (nestedTargetClass nestedTarget) rc
if instanceExists
then pure []
else do
assertNestedSubArity
(nestedTargetCallSite nestedTarget)
(SubsiteName curTarget)
(SubsiteArity (tyArgsArity tyargs))
rc
go name (prePieces <> childPieces) grandchildren
ResourceTree Type
_ -> [Dec] -> DecsQ
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
return
( instanceD cxt dispatchNestedT
[ thisDispatch
]
: childInstances <> urlToDispatchInstances
)
mkUrlToDispatchRedirectInstances
:: Cxt
-> String
-> Type
-> Type
-> Q [Dec]
mkUrlToDispatchRedirectInstances :: Cxt -> String -> Type -> Type -> DecsQ
mkUrlToDispatchRedirectInstances Cxt
cxt String
target Type
targetT Type
master = do
urlToDispatchT <- [t| UrlToDispatch $(Type -> Q Type
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
targetT) $(Type -> Q Type
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
master) |]
urlToDispatchFn <- [e| toWaiAppYreNested (Proxy :: Proxy $(pure targetT)) () |]
mYesodConstraint <- do
hasYesodInstance <- isInstance ''Yesod [master]
if hasYesodInstance
then pure []
else do
yesodContext <- [t| Yesod $(pure master) |]
pure [yesodContext]
mToParentRouteConstraint <- do
mtypeName <- lookupTypeName target
parentRouteCxt <- [t| ToParentRoute $(pure targetT) |]
case mtypeName of
Maybe Name
Nothing -> do
Cxt -> Q Cxt
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Type
parentRouteCxt]
Just Name
_ -> do
hasToParentRouteInstance <- Name -> Cxt -> Q Bool
isInstance ''ToParentRoute [Type
targetT]
if hasToParentRouteInstance
then pure []
else do
pure [parentRouteCxt]
redirectT <- [t| RedirectUrl $(pure master) $(pure targetT) |]
redirectUrlFn <- [e| toTextUrl . WithParentArgs () |]
pure
[ instanceD (cxt <> mYesodConstraint <> mToParentRouteConstraint) urlToDispatchT
[ FunD 'urlToDispatch
[ Clause [ WildP ] (NormalB urlToDispatchFn) []
]
]
, InstanceD (Just Overlappable) (cxt <> mToParentRouteConstraint) redirectT
[ FunD 'toTextUrl
[ Clause [ ] (NormalB redirectUrlFn) [] ]
]
]
genNestedDispatchClauses
:: NestedTarget
-> RouteOpts
-> [Name]
-> Exp
-> Exp
-> Exp
-> (Exp -> Q Exp)
-> TyArgs
-> [ResourceTree Type]
-> Q [Match]
genNestedDispatchClauses :: NestedTarget
-> RouteOpts
-> [Name]
-> Exp
-> Exp
-> Exp
-> (Exp -> Q Exp)
-> TyArgs
-> [ResourceTree Type]
-> Q [Match]
genNestedDispatchClauses NestedTarget
nestedTarget RouteOpts
routeOpts [Name]
parentDynVars Exp
toParentE Exp
yreE Exp
reqE Exp -> Q Exp
unwrapper TyArgs
tyargs [ResourceTree Type]
resources = do
resourceClauses <- (ResourceTree Type -> Q [Match])
-> [ResourceTree Type] -> Q [[Match]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ResourceTree Type -> Q [Match]
genClauseForResource [ResourceTree Type]
resources
let fallbackClause = Pat -> Body -> [Dec] -> Match
Match Pat
WildP (Exp -> Body
NormalB (Name -> Exp
ConE 'Nothing)) []
return $ concat resourceClauses ++ [fallbackClause]
where
genClauseForResource :: ResourceTree Type -> Q [Match]
genClauseForResource :: ResourceTree Type -> Q [Match]
genClauseForResource (ResourceLeaf (Resource String
name [Piece Type]
pieces Dispatch Type
dispatch [String]
_ Bool
_check)) = do
(pats, dynVars) <- [Piece Type] -> Q ([Pat], [Name])
forall (m :: * -> *) a. Quote m => [Piece a] -> m ([Pat], [Name])
handlePiecesNames [Piece Type]
pieces
let routeCon = String -> [Exp] -> Exp
applyConPieces String
name ((Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
dynVars)
allDynVars = [Name]
parentDynVars [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
dynVars
case dispatch of
Methods Maybe Type
mmulti [String]
methods -> do
(finalPat, mMultiE) <- case Maybe Type
mmulti of
Maybe Type
Nothing -> (PathTail, Maybe Exp) -> Q (PathTail, Maybe Exp)
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PathTail
EndExact, Maybe Exp
forall a. Maybe a
Nothing)
Just Type
_ -> do
multiName <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"multi"
pure (EndMulti multiName, Just (VarE multiName))
let dynExpsMulti = case Maybe Exp
mMultiE of
Maybe Exp
Nothing -> (Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
dynVars
Just Exp
e -> (Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
dynVars [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp
e]
routeExp = Exp
toParentE Exp -> Exp -> Exp
`AppE` String -> [Exp] -> Exp
applyConPieces String
name [Exp]
dynExpsMulti
allDynExps = (Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
parentDynVars [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp]
dynExpsMulti
handlerExp <- genHandlerCase name methods allDynExps
body <-
[| Just ($(varE (nestedTargetRunner nestedTarget))
$(pure handlerExp)
$(pure yreE)
(Just $(pure routeExp))
$(pure reqE)) |]
return [Match (mkPathPat finalPat pats) (NormalB body) []]
Subsite Type
_ String
getSub -> do
restPath <- String -> Q Name
forall (m :: * -> *). Quote m => String -> m Name
newName String
"restPath"
sub2 <- mkLambda "sub" $ \Name
sub ->
Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ (Exp -> Exp -> Exp) -> Exp -> [Exp] -> Exp
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Exp -> Exp -> Exp
AppE (Name -> Exp
VarE (String -> Name
mkName String
getSub) Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
sub) ((Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
allDynVars)
routeLam <- mkLambda "sroute" $ \Name
srouteN ->
[e| $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
toParentE) ( $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
routeCon) $(Name -> Q Exp
forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
srouteN)) |]
let reqExp' = Exp -> [FieldExp] -> Exp
RecUpdE Exp
reqE [('W.pathInfo, Name -> Exp
VarE Name
restPath)]
body <- case nestedTarget of
NestedTarget
TopLevelNested ->
[| Just (yesodSubDispatch
YesodSubRunnerEnv
{ ysreParentRunner = yesodRunner
, ysreGetSub = $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
sub2)
, ysreToParentRoute = $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
routeLam)
, ysreParentEnv = $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
yreE)
}
$(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
reqExp')) |]
NestedTarget
SubsiteNested ->
[| Just (yesodSubDispatch
YesodSubRunnerEnv
{ ysreParentRunner = ysreParentRunner $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
yreE)
, ysreGetSub = $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
sub2) . ysreGetSub $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
yreE)
, ysreToParentRoute = ysreToParentRoute $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
yreE) . $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
routeLam)
, ysreParentEnv = ysreParentEnv $(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
yreE)
}
$(Exp -> Q Exp
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
reqExp')) |]
return [Match (mkPathPat (EndRest restPath) pats) (NormalB body) []]
genClauseForResource (ResourceParent String
name Bool
_check Set String
_attrs [Piece Type]
pieces [ResourceTree Type]
_children) = do
(pats, dynVars) <- [Piece Type] -> Q ([Pat], [Name])
forall (m :: * -> *) a. Quote m => [Piece a] -> m ([Pat], [Name])
handlePiecesNames [Piece Type]
pieces
let allDynVars = [Name]
parentDynVars [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
dynVars
parentArgsExp = [Name] -> Exp
parentArgsExpr [Name]
allDynVars
routeConWrapper = String -> [Exp] -> Exp
applyConPieces String
name ((Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
dynVars)
routeType <- appliedRouteTypeNamed name tyargs
resultName <- newName "k"
nestedCall <-
[| $(varE (nestedTargetFn nestedTarget))
(Proxy :: Proxy $(pure routeType))
$(pure parentArgsExp)
($(pure toParentE) . $(pure routeConWrapper))
$(pure yreE)
$(pure reqE) |]
if roNestedRouteFallthrough routeOpts
then do
let patGuard = Pat -> Exp -> Stmt
BindS (Name -> [Pat] -> Pat
conPCompat 'Just [Name -> Pat
VarP Name
resultName]) Exp
nestedCall
guardedBody = [(Guard, Exp)] -> Body
GuardedB [([Stmt] -> Guard
PatG [Stmt
patGuard], Name -> Exp
ConE 'Just Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
resultName)]
return [Match (mkPathPat EndWild pats) guardedBody []]
else do
committedBody <-
[| Just (fromMaybe
($(varE (nestedTargetRunner nestedTarget))
(void notFound) $(pure yreE) Nothing $(pure reqE))
$(pure nestedCall)) |]
return [Match (mkPathPat EndWild pats) (NormalB committedBody) []]
genHandlerCase :: String -> [String] -> [Exp] -> Q Exp
genHandlerCase :: String -> [String] -> [Exp] -> Q Exp
genHandlerCase String
name [String]
methods [Exp]
allDynExps = do
let handlerExpFor :: Maybe String -> Q Exp
handlerExpFor Maybe String
mmethod = do
handlerName <- Maybe String -> String -> Q Exp
defaultGetHandler Maybe String
mmethod String
name
unwrapper $ foldl' AppE handlerName allDynExps
if [String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
methods
then do
handlerE <- Maybe String -> Q Exp
handlerExpFor Maybe String
forall a. Maybe a
Nothing
[| fmap toTypedContent $(pure handlerE) |]
else do
methodMatches <- [String] -> (String -> Q Match) -> Q [Match]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [String]
methods ((String -> Q Match) -> Q [Match])
-> (String -> Q Match) -> Q [Match]
forall a b. (a -> b) -> a -> b
$ \String
method -> do
handlerE <- Maybe String -> Q Exp
handlerExpFor (String -> Maybe String
forall a. a -> Maybe a
Just String
method)
wrappedHandler <- [| fmap toTypedContent $(pure handlerE) |]
return $ Match (LitP $ StringL method) (NormalB wrappedHandler) []
badMethodHandler <- unwrapper (VarE 'badMethod)
let badMethodMatch = Pat -> Body -> [Dec] -> Match
Match Pat
WildP (Exp -> Body
NormalB Exp
badMethodHandler) []
scrutinee <- [| W.requestMethod $(pure reqE) |]
return $ CaseE scrutinee (methodMatches ++ [badMethodMatch])
mkYesodSubDispatch :: [ResourceTree a] -> Q Exp
mkYesodSubDispatch :: forall a. [ResourceTree a] -> Q Exp
mkYesodSubDispatch = RouteOpts -> [ResourceTree a] -> Q Exp
forall a. RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWith RouteOpts
defaultOpts
mkYesodSubDispatchWith :: RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWith :: forall a. RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWith = SameSpliceNestedInstances -> RouteOpts -> [ResourceTree a] -> Q Exp
forall a.
SameSpliceNestedInstances -> RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWithDelegate SameSpliceNestedInstances
NoSameSpliceNestedInstances
mkYesodSubDispatchWithDelegate
:: SameSpliceNestedInstances -> RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWithDelegate :: forall a.
SameSpliceNestedInstances -> RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWithDelegate SameSpliceNestedInstances
sameSplice RouteOpts
routeOpts [ResourceTree a]
res = do
let mds :: MkDispatchSettings b site c
mds = ((Exp -> Q Exp)
-> Q Exp
-> Q Exp
-> MkDispatchSettings (ZonkAny 8) (ZonkAny 7) (ZonkAny 6)
forall a site b.
(Exp -> Q Exp) -> Q Exp -> Q Exp -> MkDispatchSettings a site b
mkMDS
Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return
[|subHelper|]
[|subTopDispatch|])
{ mdsNestedTarget = SubsiteNested
, mdsNestedRouteFallthrough = roNestedRouteFallthrough routeOpts
, mdsSameSpliceNestedInstances = sameSplice
}
(_childNames, clause') <-
TyArgs
-> MkDispatchSettings (ZonkAny 11) (ZonkAny 10) (ZonkAny 9)
-> [ResourceTree a]
-> Q ([String], Clause)
forall a b site c.
TyArgs
-> MkDispatchSettings b site c
-> [ResourceTree a]
-> Q ([String], Clause)
mkDispatchClause
TyArgs
NoTyArgs
MkDispatchSettings (ZonkAny 11) (ZonkAny 10) (ZonkAny 9)
forall {a} {site} {b}. MkDispatchSettings a site b
mds
[ResourceTree a]
res
inner <- newName "inner"
let innerFun = Name -> [Clause] -> Dec
FunD Name
inner [Clause
clause']
helper <- newName "helper"
let fun = Name -> [Clause] -> Dec
FunD Name
helper
[ [Pat] -> Body -> [Dec] -> Clause
Clause
[]
(Exp -> Body
NormalB (Exp -> Body) -> Exp -> Body
forall a b. (a -> b) -> a -> b
$ Name -> Exp
VarE Name
inner)
[Dec
innerFun]
]
return $ LetE [fun] (VarE helper)
subTopDispatch ::
(YesodSubDispatch sub master) =>
(forall content. ToTypedContent content =>
SubHandlerFor child master content ->
YesodSubRunnerEnv child master ->
Maybe (Route child) ->
W.Application
) ->
(mid -> sub) ->
(Route sub -> Route mid) ->
YesodSubRunnerEnv mid master ->
W.Application
subTopDispatch :: forall sub master child mid.
YesodSubDispatch sub master =>
(forall content.
ToTypedContent content =>
SubHandlerFor child master content
-> YesodSubRunnerEnv child master
-> Maybe (Route child)
-> Application)
-> (mid -> sub)
-> (Route sub -> Route mid)
-> YesodSubRunnerEnv mid master
-> Application
subTopDispatch forall content.
ToTypedContent content =>
SubHandlerFor child master content
-> YesodSubRunnerEnv child master
-> Maybe (Route child)
-> Application
_ mid -> sub
getSub Route sub -> Route mid
toParent YesodSubRunnerEnv mid master
env =
YesodSubRunnerEnv sub master -> Application
forall sub master.
YesodSubDispatch sub master =>
YesodSubRunnerEnv sub master -> Application
yesodSubDispatch
( YesodSubRunnerEnv
{ ysreParentRunner :: ParentRunner master
ysreParentRunner = YesodSubRunnerEnv mid master -> ParentRunner master
forall sub parent.
YesodSubRunnerEnv sub parent -> ParentRunner parent
ysreParentRunner YesodSubRunnerEnv mid master
env
, ysreGetSub :: master -> sub
ysreGetSub = mid -> sub
getSub (mid -> sub) -> (master -> mid) -> master -> sub
forall b c a. (b -> c) -> (a -> b) -> a -> c
. YesodSubRunnerEnv mid master -> master -> mid
forall sub parent. YesodSubRunnerEnv sub parent -> parent -> sub
ysreGetSub YesodSubRunnerEnv mid master
env
, ysreToParentRoute :: Route sub -> Route master
ysreToParentRoute = YesodSubRunnerEnv mid master -> Route mid -> Route master
forall sub parent.
YesodSubRunnerEnv sub parent -> Route sub -> Route parent
ysreToParentRoute YesodSubRunnerEnv mid master
env (Route mid -> Route master)
-> (Route sub -> Route mid) -> Route sub -> Route master
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Route sub -> Route mid
toParent
, ysreParentEnv :: YesodRunnerEnv master
ysreParentEnv = YesodSubRunnerEnv mid master -> YesodRunnerEnv master
forall sub parent.
YesodSubRunnerEnv sub parent -> YesodRunnerEnv parent
ysreParentEnv YesodSubRunnerEnv mid master
env
}
)
mkMDS :: (Exp -> Q Exp) -> Q Exp -> Q Exp -> MkDispatchSettings a site b
mkMDS :: forall a site b.
(Exp -> Q Exp) -> Q Exp -> Q Exp -> MkDispatchSettings a site b
mkMDS Exp -> Q Exp
unwrapper Q Exp
runHandlerE Q Exp
subDispatcher = MkDispatchSettings
{ mdsRunHandler :: Q Exp
mdsRunHandler = Q Exp
runHandlerE
, mdsSubDispatcher :: Q Exp
mdsSubDispatcher = Q Exp
subDispatcher
, mdsGetPathInfo :: Q Exp
mdsGetPathInfo = [|W.pathInfo|]
, mdsSetPathInfo :: Q Exp
mdsSetPathInfo = [|\p r -> r { W.pathInfo = p }|]
, mdsMethod :: Q Exp
mdsMethod = [|W.requestMethod|]
, mds404 :: Q Exp
mds404 = [|void notFound|]
, mds405 :: Q Exp
mds405 = [|void badMethod|]
, mdsGetHandler :: Maybe String -> String -> Q Exp
mdsGetHandler = Maybe String -> String -> Q Exp
defaultGetHandler
, mdsUnwrapper :: Exp -> Q Exp
mdsUnwrapper = Exp -> Q Exp
unwrapper
, mdsNestedRouteFallthrough :: Bool
mdsNestedRouteFallthrough = Bool
False
, mdsNestedTarget :: NestedTarget
mdsNestedTarget = NestedTarget
TopLevelNested
, mdsSameSpliceNestedInstances :: SameSpliceNestedInstances
mdsSameSpliceNestedInstances = SameSpliceNestedInstances
NoSameSpliceNestedInstances
}
parseYesodName :: String -> Either String (String, [String], [[String]])
parseYesodName :: String -> Either String (String, [String], [[String]])
parseYesodName String
name = do
(ParseError -> Either String (String, [String], [[String]]))
-> ((String, [String], [[String]])
-> Either String (String, [String], [[String]]))
-> Either ParseError (String, [String], [[String]])
-> Either String (String, [String], [[String]])
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Either String (String, [String], [[String]])
forall a b. a -> Either a b
Left (String -> Either String (String, [String], [[String]]))
-> (ParseError -> String)
-> ParseError
-> Either String (String, [String], [[String]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> String
forall a. Show a => a -> String
show) (String, [String], [[String]])
-> Either String (String, [String], [[String]])
forall a b. b -> Either a b
Right (Either ParseError (String, [String], [[String]])
-> Either String (String, [String], [[String]]))
-> Either ParseError (String, [String], [[String]])
-> Either String (String, [String], [[String]])
forall a b. (a -> b) -> a -> b
$ Parsec String () (String, [String], [[String]])
-> String
-> String
-> Either ParseError (String, [String], [[String]])
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () (String, [String], [[String]])
forall {u}.
ParsecT String u Identity (String, [String], [[String]])
parseName String
"" String
name
where
parseName :: ParsecT String u Identity (String, [String], [[String]])
parseName = do
cxt <- [[String]]
-> ParsecT String u Identity [[String]]
-> ParsecT String u Identity [[String]]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [] ParsecT String u Identity [[String]]
forall {u}. ParsecT String u Identity [[String]]
parseContext
name' <- parseWord
args <- many parseWord
spaces
eof
return ( name', args, cxt)
parseWord :: ParsecT String u Identity String
parseWord = do
ParsecT String u Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
ParsecT String u Identity Char -> ParsecT String u Identity String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT String u Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
alphaNum
parseContext :: ParsecT String u Identity [[String]]
parseContext = ParsecT String u Identity [[String]]
-> ParsecT String u Identity [[String]]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT String u Identity [[String]]
-> ParsecT String u Identity [[String]])
-> ParsecT String u Identity [[String]]
-> ParsecT String u Identity [[String]]
forall a b. (a -> b) -> a -> b
$ do
cxts <- ParsecT String u Identity [[String]]
-> ParsecT String u Identity [[String]]
forall {s} {m :: * -> *} {u} {b}.
Stream s m Char =>
ParsecT s u m b -> ParsecT s u m b
parseParen ParsecT String u Identity [[String]]
forall {u}. ParsecT String u Identity [[String]]
parseContexts
spaces
_ <- string "=>"
return cxts
parseParen :: ParsecT s u m b -> ParsecT s u m b
parseParen ParsecT s u m b
p = do
ParsecT s u m ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
_ <- Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'('
r <- p
spaces
_ <- char ')'
return r
parseContexts :: ParsecT String u Identity [[String]]
parseContexts =
ParsecT String u Identity [String]
-> ParsecT String u Identity ()
-> ParsecT String u Identity [[String]]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepBy1 (ParsecT String u Identity String
-> ParsecT String u Identity [String]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT String u Identity String
forall {u}. ParsecT String u Identity String
parseWord) (ParsecT String u Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces ParsecT String u Identity ()
-> ParsecT String u Identity Char -> ParsecT String u Identity Char
forall a b.
ParsecT String u Identity a
-> ParsecT String u Identity b -> ParsecT String u Identity b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> ParsecT String u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',' ParsecT String u Identity Char
-> ParsecT String u Identity () -> ParsecT String u Identity ()
forall a b.
ParsecT String u Identity a
-> ParsecT String u Identity b -> ParsecT String u Identity b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> ParsecT String u Identity ()
forall a. a -> ParsecT String u Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return ())