Fixed some TH code for subsites
This commit is contained in:
parent
93c724ba7d
commit
7f51c7fd20
@ -102,6 +102,14 @@ class Eq (Route s) => YesodSubSite s y where
|
|||||||
-> (Route s -> Route y)
|
-> (Route s -> Route y)
|
||||||
-> s
|
-> s
|
||||||
-> W.Application
|
-> W.Application
|
||||||
|
dispatchToSubSubsite
|
||||||
|
:: (Yesod y, YesodSite y)
|
||||||
|
=> y
|
||||||
|
-> Maybe CS.Key
|
||||||
|
-> [String]
|
||||||
|
-> (Route s -> Route y)
|
||||||
|
-> s
|
||||||
|
-> Maybe W.Application
|
||||||
|
|
||||||
-- | Define settings for a Yesod applications. The only required setting is
|
-- | Define settings for a Yesod applications. The only required setting is
|
||||||
-- 'approot'; other than that, there are intelligent defaults.
|
-- 'approot'; other than that, there are intelligent defaults.
|
||||||
|
|||||||
@ -172,11 +172,15 @@ mkYesodGeneral name args clazzes isSub res = do
|
|||||||
then (clazzes, ConT ''YesodSubSite `AppT` arg `AppT` VarT (mkName "master"), "getSubSite")
|
then (clazzes, ConT ''YesodSubSite `AppT` arg `AppT` VarT (mkName "master"), "getSubSite")
|
||||||
else ([], ConT ''YesodSite `AppT` arg, "getSite")
|
else ([], ConT ''YesodSite `AppT` arg, "getSite")
|
||||||
subsiteClauses <- catMaybes <$> mapM sc th'
|
subsiteClauses <- catMaybes <$> mapM sc th'
|
||||||
|
let subSubsiteClauses = [] -- FIXME subSubsiteClauses
|
||||||
nothing <- [|Nothing|]
|
nothing <- [|Nothing|]
|
||||||
dds <- [|defaultDispatchSubsite|]
|
dds <- [|defaultDispatchSubsite|]
|
||||||
let otherMethods =
|
let otherMethods =
|
||||||
if isSub
|
if isSub
|
||||||
then [ FunD (mkName "dispatchSubsite") [Clause [] (NormalB dds) []]]
|
then [ FunD (mkName "dispatchSubsite") [Clause [] (NormalB dds) []]
|
||||||
|
, FunD (mkName "dispatchToSubSubsite")
|
||||||
|
(subSubsiteClauses ++ [Clause [WildP, WildP, WildP, WildP, WildP] (NormalB nothing) []])
|
||||||
|
]
|
||||||
else [ FunD (mkName "dispatchToSubsite")
|
else [ FunD (mkName "dispatchToSubsite")
|
||||||
(subsiteClauses ++ [Clause [WildP, WildP, WildP] (NormalB nothing) []])
|
(subsiteClauses ++ [Clause [WildP, WildP, WildP] (NormalB nothing) []])
|
||||||
]
|
]
|
||||||
@ -189,10 +193,13 @@ mkYesodGeneral name args clazzes isSub res = do
|
|||||||
master <- newName "master"
|
master <- newName "master"
|
||||||
mkey <- newName "mkey"
|
mkey <- newName "mkey"
|
||||||
just <- [|Just|]
|
just <- [|Just|]
|
||||||
(pat', tma', rest) <- mkPat' pieces $ just `AppE` (VarE (mkName toSub) `AppE` VarE master)
|
(pat', tma', rest, toMaster)
|
||||||
|
<- mkPat' pieces
|
||||||
|
(ConE $ mkName constr)
|
||||||
|
$ just `AppE` (VarE (mkName toSub) `AppE` VarE master)
|
||||||
ds <- [|dispatchSubsite|]
|
ds <- [|dispatchSubsite|]
|
||||||
-- let toMaster = ConE (mkName "SubsiteR")
|
goodParse <- (`AppE` tma') <$> [|isJust|]
|
||||||
toMaster <- [|error "FIXME toMaster"|]
|
tma'' <- (`AppE` tma') <$> [|fromJust|]
|
||||||
let body' = ds `AppE` VarE master `AppE` VarE mkey `AppE` rest `AppE` toMaster
|
let body' = ds `AppE` VarE master `AppE` VarE mkey `AppE` rest `AppE` toMaster
|
||||||
fmap' <- [|(<$>)|]
|
fmap' <- [|(<$>)|]
|
||||||
let body = InfixE (Just body') fmap' $ Just tma'
|
let body = InfixE (Just body') fmap' $ Just tma'
|
||||||
@ -200,24 +207,25 @@ mkYesodGeneral name args clazzes isSub res = do
|
|||||||
[ VarP master
|
[ VarP master
|
||||||
, VarP mkey
|
, VarP mkey
|
||||||
, pat'
|
, pat'
|
||||||
] (NormalB body) []
|
] (GuardedB [(NormalG goodParse, body)]) []
|
||||||
sc _ = return Nothing
|
sc _ = return Nothing
|
||||||
mkPat' :: [Piece] -> Exp -> Q (Pat, Exp, Exp)
|
mkPat' :: [Piece] -> Exp -> Exp -> Q (Pat, Exp, Exp, Exp)
|
||||||
mkPat' (MultiPiece _:_) _ = error "MultiPiece not allowed as part of a subsite"
|
mkPat' (MultiPiece _:_) _ _ = error "MultiPiece not allowed as part of a subsite"
|
||||||
mkPat' (StaticPiece s:rest) tma = do
|
mkPat' (StaticPiece s:rest) toMaster tma = do
|
||||||
(x, tma, rest') <- mkPat' rest tma
|
(x, tma, rest', toMaster') <- mkPat' rest toMaster tma
|
||||||
let sp = LitP $ StringL s
|
let sp = LitP $ StringL s
|
||||||
return (InfixP sp (mkName ":") x, tma, rest')
|
return (InfixP sp (mkName ":") x, tma, rest', toMaster')
|
||||||
mkPat' (SinglePiece s:rest) tma = do
|
mkPat' (SinglePiece s:rest) toMaster tma = do
|
||||||
fsp <- [|either (const Nothing) Just . fromSinglePiece|]
|
fsp <- [|either (const Nothing) Just . fromSinglePiece|]
|
||||||
v <- newName $ "var" ++ s
|
v <- newName $ "var" ++ s
|
||||||
be <- [|(<*>)|]
|
be <- [|(<*>)|]
|
||||||
let tma' = InfixE (Just tma) be $ Just $ fsp `AppE` VarE v
|
let tma' = InfixE (Just tma) be $ Just $ fsp `AppE` VarE v
|
||||||
(x, tma'', rest) <- mkPat' rest tma'
|
let toMaster' = toMaster `AppE` VarE v
|
||||||
return (InfixP (VarP v) (mkName ":") x, tma'', rest)
|
(x, tma'', rest, toMaster'') <- mkPat' rest toMaster' tma'
|
||||||
mkPat' [] parse = do
|
return (InfixP (VarP v) (mkName ":") x, tma'', rest, toMaster'')
|
||||||
|
mkPat' [] toMaster parse = do
|
||||||
rest <- newName "rest"
|
rest <- newName "rest"
|
||||||
return (VarP rest, parse, VarE rest)
|
return (VarP rest, parse, VarE rest, toMaster)
|
||||||
|
|
||||||
isStatic :: Piece -> Bool
|
isStatic :: Piece -> Bool
|
||||||
isStatic StaticPiece{} = True
|
isStatic StaticPiece{} = True
|
||||||
@ -334,7 +342,6 @@ normalDispatch y key' segments env =
|
|||||||
Nothing -> badMethod
|
Nothing -> badMethod
|
||||||
Just h -> h
|
Just h -> h
|
||||||
|
|
||||||
-- FIXME address sub-subsites
|
|
||||||
defaultDispatchSubsite
|
defaultDispatchSubsite
|
||||||
:: (Yesod m, YesodSite m, YesodSubSite s m)
|
:: (Yesod m, YesodSite m, YesodSubSite s m)
|
||||||
=> m -> Maybe Key -> [String]
|
=> m -> Maybe Key -> [String]
|
||||||
@ -342,7 +349,9 @@ defaultDispatchSubsite
|
|||||||
-> s
|
-> s
|
||||||
-> W.Application
|
-> W.Application
|
||||||
defaultDispatchSubsite y key' segments toMasterRoute s env =
|
defaultDispatchSubsite y key' segments toMasterRoute s env =
|
||||||
yesodRunner y key' (fmap toMasterRoute murl) handler env
|
case dispatchToSubSubsite y key' segments toMasterRoute s of
|
||||||
|
Just app -> app env
|
||||||
|
Nothing -> yesodRunner y key' (fmap toMasterRoute murl) handler env
|
||||||
where
|
where
|
||||||
method = B.unpack $ W.requestMethod env
|
method = B.unpack $ W.requestMethod env
|
||||||
murl = either (const Nothing) Just $ parsePathSegments (getSubSite' s y) segments
|
murl = either (const Nothing) Just $ parsePathSegments (getSubSite' s y) segments
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
{-# LANGUAGE QuasiQuotes, TypeFamilies, OverloadedStrings #-}
|
{-# LANGUAGE QuasiQuotes, TypeFamilies, OverloadedStrings #-}
|
||||||
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
|
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
|
||||||
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
import Yesod.Core
|
import Yesod.Core
|
||||||
import Yesod.Dispatch
|
import Yesod.Dispatch
|
||||||
import Yesod.Content
|
import Yesod.Content
|
||||||
@ -15,7 +16,9 @@ mkYesodSub "Subsite" [] [$parseRoutes|
|
|||||||
getSubRootR :: GHandler Subsite m RepPlain
|
getSubRootR :: GHandler Subsite m RepPlain
|
||||||
getSubRootR = do
|
getSubRootR = do
|
||||||
Subsite s <- getYesodSub
|
Subsite s <- getYesodSub
|
||||||
return $ RepPlain $ toContent $ "Hello Sub World: " ++ s
|
tm <- getRouteToMaster
|
||||||
|
render <- getUrlRender
|
||||||
|
return $ RepPlain $ toContent $ "Hello Sub World: " ++ s ++ ". " ++ render (tm SubRootR)
|
||||||
|
|
||||||
data HelloWorld = HelloWorld { getSubsite :: String -> Subsite }
|
data HelloWorld = HelloWorld { getSubsite :: String -> Subsite }
|
||||||
mkYesod "HelloWorld" [$parseRoutes|
|
mkYesod "HelloWorld" [$parseRoutes|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user