{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Yesod.Core.Internal.TH
( mkYesod
, mkYesodOpts
, mkYesodWith
, mkYesodData
, mkYesodDataOpts
, mkYesodSubData
, mkYesodSubDataOpts
, mkYesodWithParser
, mkYesodWithParserOpts
, mkYesodDispatch
, mkYesodDispatchOpts
, masterTypeSyns
, mkYesodGeneral
, mkYesodGeneralOpts
, mkMDS
, mkDispatchInstance
, mkYesodSubDispatch
, mkYesodSubDispatchInstance
, mkYesodSubDispatchInstanceOpts
, mkNestedSubDispatchInstance
, subTopDispatch
, RouteOpts
, defaultOpts
, setEqDerived
, setShowDerived
, setReadDerived
, setCreateResources
, setFocusOnNestedRoute
, unsetFocusOnNestedRoute
, setParameterizedSubroute
, setNestedRouteFallthrough
)
where
import Prelude hiding (exp)
import Yesod.Core.Handler
import Language.Haskell.TH hiding (cxt, instanceD)
import Language.Haskell.TH.Syntax
import Data.ByteString.Lazy.Char8 ()
import Data.List (foldl')
import Data.Maybe
import Control.Monad
import Yesod.Routes.TH
import Yesod.Routes.TH.Dispatch
(parseYesodName, mkYesodSubDispatchWithDelegate, SameSpliceNestedInstances (..))
import Yesod.Routes.TH.Internal (instanceD, typeArity, assertNestedSubArity, ArityCallSite(..), SubsiteName(..), SubsiteArity(..), resolveRouteCon, nestedInstanceExists)
import Yesod.Routes.Parse
import Yesod.Core.Types
import Yesod.Core.Class.Dispatch (YesodSubDispatch(..), YesodSubDispatchNested(..))
mkYesod :: String
-> [ResourceTree String]
-> Q [Dec]
mkYesod :: String -> [ResourceTree String] -> Q [Dec]
mkYesod = RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodOpts RouteOpts
defaultOpts
mkYesodOpts :: RouteOpts
-> String
-> [ResourceTree String]
-> Q [Dec]
mkYesodOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodOpts RouteOpts
opts String
name = (([Dec], [Dec]) -> [Dec]) -> Q ([Dec], [Dec]) -> Q [Dec]
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Dec] -> [Dec] -> [Dec]) -> ([Dec], [Dec]) -> [Dec]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Dec] -> [Dec] -> [Dec]
forall a. [a] -> [a] -> [a]
(++)) (Q ([Dec], [Dec]) -> Q [Dec])
-> ([ResourceTree String] -> Q ([Dec], [Dec]))
-> [ResourceTree String]
-> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
opts String
name Bool
False Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# DEPRECATED mkYesodWith "Contexts and type variables are now parsed from the name in `mkYesod`. <https://github.com/yesodweb/yesod/pull/1366>" #-}
mkYesodWith :: [[String]]
-> String
-> [String]
-> [ResourceTree String]
-> Q [Dec]
mkYesodWith :: [[String]]
-> String -> [String] -> [ResourceTree String] -> Q [Dec]
mkYesodWith [[String]]
cxts String
name [String]
args = (([Dec], [Dec]) -> [Dec]) -> Q ([Dec], [Dec]) -> Q [Dec]
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Dec] -> [Dec] -> [Dec]) -> ([Dec], [Dec]) -> [Dec]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Dec] -> [Dec] -> [Dec]
forall a. [a] -> [a] -> [a]
(++)) (Q ([Dec], [Dec]) -> Q [Dec])
-> ([ResourceTree String] -> Q ([Dec], [Dec]))
-> [ResourceTree String]
-> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodGeneral [[String]]
cxts String
name [String]
args Bool
False Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return
mkYesodData :: String -> [ResourceTree String] -> Q [Dec]
mkYesodData :: String -> [ResourceTree String] -> Q [Dec]
mkYesodData = RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDataOpts RouteOpts
defaultOpts
mkYesodDataOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDataOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDataOpts RouteOpts
opts String
name [ResourceTree String]
resS = ([Dec], [Dec]) -> [Dec]
forall a b. (a, b) -> a
fst (([Dec], [Dec]) -> [Dec]) -> Q ([Dec], [Dec]) -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
opts String
name Bool
False Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return [ResourceTree String]
resS
mkYesodSubData :: String -> [ResourceTree String] -> Q [Dec]
mkYesodSubData :: String -> [ResourceTree String] -> Q [Dec]
mkYesodSubData = RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDataOpts RouteOpts
defaultOpts
mkYesodSubDataOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDataOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDataOpts RouteOpts
opts String
name [ResourceTree String]
resS = ([Dec], [Dec]) -> [Dec]
forall a b. (a, b) -> a
fst (([Dec], [Dec]) -> [Dec]) -> Q ([Dec], [Dec]) -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
opts String
name Bool
True Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return [ResourceTree String]
resS
parseYesodNameQ :: String -> Q (String, [String], [[String]])
parseYesodNameQ :: String -> Q (String, [String], [[String]])
parseYesodNameQ String
name =
case String -> Either String (String, [String], [[String]])
parseYesodName String
name of
Left String
err -> String -> Q (String, [String], [[String]])
forall a. HasCallStack => String -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
String -> m a
fail String
err
Right (String, [String], [[String]])
a -> (String, [String], [[String]]) -> Q (String, [String], [[String]])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String, [String], [[String]])
a
mkYesodWithParser :: String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q([Dec],[Dec])
mkYesodWithParser :: String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParser = RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
defaultOpts
mkYesodWithParserOpts :: RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q([Dec],[Dec])
mkYesodWithParserOpts :: RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
opts String
name Bool
isSub Exp -> Q Exp
f [ResourceTree String]
resS = do
(name', rest, cxt) <- String -> Q (String, [String], [[String]])
parseYesodNameQ String
name
mkYesodGeneralOpts opts cxt name' rest isSub f resS
mkYesodDispatch :: String -> [ResourceTree String] -> Q [Dec]
mkYesodDispatch :: String -> [ResourceTree String] -> Q [Dec]
mkYesodDispatch = RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDispatchOpts RouteOpts
defaultOpts
mkYesodDispatchOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDispatchOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodDispatchOpts RouteOpts
opts String
name = (([Dec], [Dec]) -> [Dec]) -> Q ([Dec], [Dec]) -> Q [Dec]
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Dec], [Dec]) -> [Dec]
forall a b. (a, b) -> b
snd (Q ([Dec], [Dec]) -> Q [Dec])
-> ([ResourceTree String] -> Q ([Dec], [Dec]))
-> [ResourceTree String]
-> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RouteOpts
-> String
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodWithParserOpts RouteOpts
opts String
name Bool
False Exp -> Q Exp
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return
buildAppCxt :: [[String]] -> Q Cxt
buildAppCxt :: [[String]] -> Q Cxt
buildAppCxt = ([String] -> Q Pred) -> [[String]] -> Q Cxt
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 (([String] -> Q Pred) -> [[String]] -> Q Cxt)
-> ([String] -> Q Pred) -> [[String]] -> Q Cxt
forall a b. (a -> b) -> a -> b
$ \[String]
ctxs ->
case [String]
ctxs of
String
c:[String]
rest ->
Pred -> Q Pred
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Pred -> Q Pred) -> Pred -> Q Pred
forall a b. (a -> b) -> a -> b
$ (Pred -> String -> Pred) -> Pred -> [String] -> Pred
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Pred
acc String
v -> Pred
acc Pred -> Pred -> Pred
`AppT` (Pred, Name) -> Pred
forall a b. (a, b) -> a
fst (String -> (Pred, Name)
nameToType String
v)) (Name -> Pred
ConT (Name -> Pred) -> Name -> Pred
forall a b. (a -> b) -> a -> b
$ String -> Name
mkName String
c) [String]
rest
[] -> String -> Q Pred
forall a. HasCallStack => String -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
String -> m a
fail (String -> Q Pred) -> String -> Q Pred
forall a b. (a -> b) -> a -> b
$ String
"mkYesod: empty type-class context in route definition: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
ctxs
masterTypeSyns :: [Name] -> Type -> [Dec]
masterTypeSyns :: [Name] -> Pred -> [Dec]
masterTypeSyns [Name]
vs Pred
site =
[ Name -> [TyVarBndr BndrVis] -> Pred -> Dec
TySynD (String -> Name
mkName String
"Handler") ((Name -> TyVarBndr BndrVis) -> [Name] -> [TyVarBndr BndrVis]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> TyVarBndr BndrVis
forall flag. DefaultBndrFlag flag => Name -> TyVarBndr flag
plainTV [Name]
vs)
(Pred -> Dec) -> Pred -> Dec
forall a b. (a -> b) -> a -> b
$ Name -> Pred
ConT ''HandlerFor Pred -> Pred -> Pred
`AppT` Pred
site
, Name -> [TyVarBndr BndrVis] -> Pred -> Dec
TySynD (String -> Name
mkName String
"Widget") ((Name -> TyVarBndr BndrVis) -> [Name] -> [TyVarBndr BndrVis]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> TyVarBndr BndrVis
forall flag. DefaultBndrFlag flag => Name -> TyVarBndr flag
plainTV [Name]
vs)
(Pred -> Dec) -> Pred -> Dec
forall a b. (a -> b) -> a -> b
$ Name -> Pred
ConT ''WidgetFor Pred -> Pred -> Pred
`AppT` Pred
site Pred -> Pred -> Pred
`AppT` Name -> Pred
ConT ''()
]
mkYesodGeneral :: [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q([Dec],[Dec])
mkYesodGeneral :: [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodGeneral = RouteOpts
-> [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodGeneralOpts RouteOpts
defaultOpts
parseResourceTypes :: [ResourceTree String] -> Q [ResourceTree Type]
parseResourceTypes :: [ResourceTree String] -> Q [ResourceTree Pred]
parseResourceTypes = (ResourceTree String -> Q (ResourceTree Pred))
-> [ResourceTree String] -> Q [ResourceTree Pred]
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 ((String -> Q Pred) -> ResourceTree String -> Q (ResourceTree Pred)
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) -> ResourceTree a -> f (ResourceTree b)
traverse (\String
s -> String -> Q String
forall (m :: * -> *). MonadFail m => String -> m String
dropBracketM String
s Q String -> (String -> Q Pred) -> Q Pred
forall a b. Q a -> (a -> Q b) -> Q b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Q Pred
forall (m :: * -> *). MonadFail m => String -> m Pred
parseTypeM))
mkNestedSubDispatchInstance
:: RouteOpts
-> String
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q [Dec]
mkNestedSubDispatchInstance :: RouteOpts
-> String
-> Cxt
-> TyArgs
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q [Dec]
mkNestedSubDispatchInstance RouteOpts
routeOpts String
target Cxt
cxt TyArgs
tyargs Exp -> Q Exp
unwrapper [ResourceTree String]
resS = do
res <- [ResourceTree String] -> Q [ResourceTree Pred]
parseResourceTypes [ResourceTree String]
resS
rc <- resolveRouteCon target
assertNestedSubArity SubsiteCall (SubsiteName target)
(SubsiteArity (tyArgsArity tyargs)) rc
mkNestedDispatchInstanceWith SubsiteNested Nothing
routeOpts target cxt tyargs unwrapper res
data ResolvedFoundation = ResolvedFoundation
{ ResolvedFoundation -> [(Pred, Name)]
rfBoundNames :: [(Type, Name)]
, ResolvedFoundation -> [Name]
rfFillVars :: [Name]
, ResolvedFoundation -> Pred
rfSite :: Type
, ResolvedFoundation -> [ResourceTree Pred]
rfResources :: [ResourceTree Type]
}
resolveFoundation :: String -> [String] -> [ResourceTree String] -> Q ResolvedFoundation
resolveFoundation :: String -> [String] -> [ResourceTree String] -> Q ResolvedFoundation
resolveFoundation String
namestr [String]
mtys [ResourceTree String]
resS = do
mname <- String -> Q (Maybe Name)
lookupTypeName String
namestr
arity <- maybe (pure 0) typeArity mname
let name = String -> Name
mkName String
namestr
vns <- replicateM (arity - length mtys) $ newName "t"
let boundNames = (String -> (Pred, Name)) -> [String] -> [(Pred, Name)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> (Pred, Name)
nameToType [String]
mtys
argtypes = ((Pred, Name) -> Pred) -> [(Pred, Name)] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Pred, Name) -> Pred
forall a b. (a, b) -> a
fst [(Pred, Name)]
boundNames Cxt -> Cxt -> Cxt
forall a. [a] -> [a] -> [a]
++ (Name -> Pred) -> [Name] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Pred
VarT [Name]
vns
site = (Pred -> Pred -> Pred) -> Pred -> Cxt -> Pred
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Pred -> Pred -> Pred
AppT (Name -> Pred
ConT Name
name) Cxt
argtypes
res <- parseResourceTypes resS
pure ResolvedFoundation
{ rfBoundNames = boundNames
, rfFillVars = vns
, rfSite = site
, rfResources = res
}
mkYesodGeneralOpts :: RouteOpts
-> [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q([Dec],[Dec])
mkYesodGeneralOpts :: RouteOpts
-> [[String]]
-> String
-> [String]
-> Bool
-> (Exp -> Q Exp)
-> [ResourceTree String]
-> Q ([Dec], [Dec])
mkYesodGeneralOpts RouteOpts
opts [[String]]
appCxt' String
namestr [String]
mtys Bool
isSub Exp -> Q Exp
f [ResourceTree String]
resS = do
appCxt <- [[String]] -> Q Cxt
buildAppCxt [[String]]
appCxt'
foundation <- resolveFoundation namestr mtys resS
let fillNames = (Name -> (Pred, Name)) -> [Name] -> [(Pred, Name)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Name
v -> (Name -> Pred
VarT Name
v, Name
v)) (ResolvedFoundation -> [Name]
rfFillVars ResolvedFoundation
foundation)
tyArgs = [(Pred, Name)] -> TyArgs
toTyArgs (ResolvedFoundation -> [(Pred, Name)]
rfBoundNames ResolvedFoundation
foundation [(Pred, Name)] -> [(Pred, Name)] -> [(Pred, Name)]
forall a. [a] -> [a] -> [a]
++ [(Pred, Name)]
fillNames)
let argvars = ((String -> Name) -> [String] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Name
mkName ([String] -> [Name])
-> ([String] -> [String]) -> [String] -> [Name]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
isTvar) [String]
mtys [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ ResolvedFoundation -> [Name]
rfFillVars ResolvedFoundation
foundation
renderRouteDec <-
mkRenderRouteInstanceOpts opts appCxt tyArgs (rfSite foundation) (rfResources foundation)
routeAttrsDec <-
case roFocusOnNestedRoute opts of
Maybe String
Nothing -> do
flatAttrs <- Cxt -> Pred -> [ResourceTree Pred] -> Q Dec
forall a. Cxt -> Pred -> [ResourceTree a] -> Q Dec
mkRouteAttrsInstance Cxt
appCxt (ResolvedFoundation -> Pred
rfSite ResolvedFoundation
foundation) (ResolvedFoundation -> [ResourceTree Pred]
rfResources ResolvedFoundation
foundation)
nestedAttrs <-
case discoveryMode opts tyArgs of
DiscoveryMode
NestedDiscovery ->
Cxt -> TyArgs -> [ResourceTree Pred] -> Q [Dec]
forall a. Cxt -> TyArgs -> [ResourceTree a] -> Q [Dec]
mkRouteAttrsNestedInstances Cxt
appCxt TyArgs
tyArgs (ResolvedFoundation -> [ResourceTree Pred]
rfResources ResolvedFoundation
foundation)
DiscoveryMode
InlineCompat -> [Dec] -> Q [Dec]
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
pure (flatAttrs : nestedAttrs)
Just String
target ->
Cxt -> Pred -> String -> [ResourceTree Pred] -> Q [Dec]
forall a. Cxt -> Pred -> String -> [ResourceTree a] -> Q [Dec]
mkRouteAttrsInstanceFor
Cxt
appCxt
(Pred -> TyArgs -> Pred
applyTyArgs (Name -> Pred
ConT (String -> Name
mkName String
target)) TyArgs
tyArgs)
String
target
(ResolvedFoundation -> [ResourceTree Pred]
rfResources ResolvedFoundation
foundation)
dispatchDec <-
mkDispatchInstance opts (rfSite foundation) appCxt tyArgs f (rfResources foundation)
parseRouteDec <-
mkParseRouteInstanceOpts opts tyArgs appCxt (rfSite foundation) (rfResources foundation)
let rname = String -> Name
mkName (String -> Name) -> String -> Name
forall a b. (a -> b) -> a -> b
$ String
"resources" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
namestr
resourcesDec <-
if shouldCreateResources opts
then do
eres <- lift resS
pure
[ SigD rname $ ListT `AppT` (ConT ''ResourceTree `AppT` ConT ''String)
, FunD rname [Clause [] (NormalB eres) []]
]
else do
pure []
let dataDec = [[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Dec]
parseRouteDec
, [Dec]
renderRouteDec
, [Dec]
routeAttrsDec
, if Maybe String -> Bool
forall a. Maybe a -> Bool
isJust (RouteOpts -> Maybe String
roFocusOnNestedRoute RouteOpts
opts) then [] else [Dec]
resourcesDec
, if Bool
isSub Bool -> Bool -> Bool
|| Maybe String -> Bool
forall a. Maybe a -> Bool
isJust (RouteOpts -> Maybe String
roFocusOnNestedRoute RouteOpts
opts)
then []
else [Name] -> Pred -> [Dec]
masterTypeSyns [Name]
argvars (ResolvedFoundation -> Pred
rfSite ResolvedFoundation
foundation)
]
return (dataDec, dispatchDec)
mkYesodSubDispatchInstance
:: String
-> [ResourceTree String]
-> Q [Dec]
mkYesodSubDispatchInstance :: String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDispatchInstance = RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDispatchInstanceOpts RouteOpts
defaultOpts
mkYesodSubDispatchInstanceOpts
:: RouteOpts
-> String
-> [ResourceTree String]
-> Q [Dec]
mkYesodSubDispatchInstanceOpts :: RouteOpts -> String -> [ResourceTree String] -> Q [Dec]
mkYesodSubDispatchInstanceOpts RouteOpts
opts String
nameStr [ResourceTree String]
resS = do
(namestr, mtys, appCxt') <- String -> Q (String, [String], [[String]])
parseYesodNameQ String
nameStr
appCxt <- buildAppCxt appCxt'
foundation <- resolveFoundation namestr mtys resS
let fillNames = (Name -> (Pred, Name)) -> [Name] -> [(Pred, Name)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Name
v -> (Name -> Pred
VarT Name
v, Name
v)) (ResolvedFoundation -> [Name]
rfFillVars ResolvedFoundation
foundation)
tyArgs = [(Pred, Name)] -> TyArgs
toTyArgs (ResolvedFoundation -> [(Pred, Name)]
rfBoundNames ResolvedFoundation
foundation [(Pred, Name)] -> [(Pred, Name)] -> [(Pred, Name)]
forall a. [a] -> [a] -> [a]
++ [(Pred, Name)]
fillNames)
masterN <- newName "master"
let masterT = Name -> Pred
VarT Name
masterN
subDispatchExp =
SameSpliceNestedInstances
-> RouteOpts -> [ResourceTree Pred] -> Q Exp
forall a.
SameSpliceNestedInstances -> RouteOpts -> [ResourceTree a] -> Q Exp
mkYesodSubDispatchWithDelegate
SameSpliceNestedInstances
GeneratesNestedInstances
RouteOpts
opts
(ResolvedFoundation -> [ResourceTree Pred]
rfResources ResolvedFoundation
foundation)
subDispatchBody <- subDispatchExp
let yesodSubDispatchInst = Cxt -> Pred -> [Dec] -> Dec
instanceD
Cxt
appCxt
(Name -> Pred
ConT ''YesodSubDispatch Pred -> Pred -> Pred
`AppT` ResolvedFoundation -> Pred
rfSite ResolvedFoundation
foundation Pred -> Pred -> Pred
`AppT` Pred
masterT)
[ Name -> [Clause] -> Dec
FunD 'yesodSubDispatch
[ [Pat] -> Body -> [Dec] -> Clause
Clause [] (Exp -> Body
NormalB Exp
subDispatchBody) [] ]
]
let nestedNames = [ String
n | ResourceParent String
n Bool
_ Set String
_ [Piece Pred]
_ [ResourceTree Pred]
_ <- ResolvedFoundation -> [ResourceTree Pred]
rfResources ResolvedFoundation
foundation ]
nestedInstances <- fmap mconcat $ forM nestedNames $ \String
nestedName -> do
rc <- String -> Q RouteCon
resolveRouteCon String
nestedName
instanceExists <- nestedInstanceExists ''YesodSubDispatchNested rc
if instanceExists
then pure []
else do
assertNestedSubArity
SubsiteCall
(SubsiteName namestr)
(SubsiteArity (tyArgsArity tyArgs))
rc
mkNestedDispatchInstanceWith SubsiteNested Nothing
opts
nestedName appCxt tyArgs return (rfResources foundation)
return $ yesodSubDispatchInst : nestedInstances