Normal and subsite dispatch code completely unified

This commit is contained in:
Michael Snoyman 2011-01-28 10:45:12 +02:00
parent 09e93e96a1
commit c571aac930
2 changed files with 18 additions and 38 deletions

View File

@ -10,7 +10,6 @@ module Yesod.Core
( -- * Type classes ( -- * Type classes
Yesod (..) Yesod (..)
, YesodDispatch (..) , YesodDispatch (..)
, YesodSubSite (..)
, RenderRoute (..) , RenderRoute (..)
-- ** Breadcrumbs -- ** Breadcrumbs
, YesodBreadcrumbs (..) , YesodBreadcrumbs (..)
@ -92,25 +91,11 @@ class Yesod master => YesodDispatch a master where
-> (Route a -> Route master) -> (Route a -> Route master)
-> Maybe W.Application -> Maybe W.Application
-- | Same as 'YesodSite', but for subsites. Once again, users should not need yesodRunner :: a
-- to deal with it directly, as mkYesodSub creates instances appropriately. -> master
class (RenderRoute (Route s)) => YesodSubSite s y where -> (Route a -> Route master)
dispatchSubsite :: (Yesod y) -> Maybe CS.Key -> Maybe (Route a) -> GHandler a master ChooseRep -> W.Application
=> y yesodRunner = defaultYesodRunner
-> Maybe CS.Key
-> [String]
-> (Route s -> Route y)
-> s
-> W.Application
dispatchToSubSubsite
:: (Yesod y)
=> y
-> Maybe CS.Key
-> [String]
-> (Route s -> Route y)
-> s
-> Maybe W.Application
dispatchSubLocal :: 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.
@ -252,14 +237,6 @@ class RenderRoute (Route a) => Yesod a where
sessionIpAddress :: a -> Bool sessionIpAddress :: a -> Bool
sessionIpAddress _ = True sessionIpAddress _ = True
-- FIXME this probably needs to be a part of YesodDispatch
yesodRunner :: Yesod master
=> a
-> master
-> (Route a -> Route master)
-> Maybe CS.Key -> Maybe (Route a) -> GHandler a master ChooseRep -> W.Application
yesodRunner = defaultYesodRunner
defaultYesodRunner :: Yesod master defaultYesodRunner :: Yesod master
=> a => a
-> master -> master

View File

@ -173,12 +173,11 @@ mkYesodGeneral name args clazzes isSub res = do
-} -}
let sortedRes = filter (not . isSubSite) th' ++ filter isSubSite th' let sortedRes = filter (not . isSubSite) th' ++ filter isSubSite th'
yd <- mkYesodDispatch' sortedRes yd <- mkYesodDispatch' sortedRes
localClauses <- catMaybes <$> mapM mkDispatchLocal th'
subsiteClauses <- catMaybes <$> mapM mkDispatchToSubsite th'
let subSubsiteClauses = [] -- FIXME subSubsiteClauses
nothing <- [|Nothing|] nothing <- [|Nothing|]
let mkYSS = InstanceD clazzes (ConT ''YesodSubSite `AppT` arg `AppT` VarT (mkName "master")) let master = mkName "master"
[ let ctx = ClassP (mkName "Yesod") [VarT master] : clazzes
let mkYSS = InstanceD ctx (ConT ''YesodDispatch `AppT` arg `AppT` VarT master)
[ FunD (mkName "yesodDispatch") [yd]
] ]
mkYS = InstanceD [] (ConT ''YesodDispatch `AppT` arg `AppT` arg) [FunD (mkName "yesodDispatch") [yd]] mkYS = InstanceD [] (ConT ''YesodDispatch `AppT` arg `AppT` arg) [FunD (mkName "yesodDispatch") [yd]]
let y = if isSub then mkYSS else mkYS {-InstanceD ctx ytyp let y = if isSub then mkYSS else mkYS {-InstanceD ctx ytyp
@ -198,7 +197,7 @@ mkYesodDispatch' sortedRes = do
nothing <- [|Nothing|] nothing <- [|Nothing|]
body <- foldM (go master sub toMasterRoute mkey segments) nothing sortedRes body <- foldM (go master sub toMasterRoute mkey segments) nothing sortedRes
return $ Clause return $ Clause
[VarP master, VarP mkey, VarP segments, VarP sub, VarP toMasterRoute] [VarP sub, VarP mkey, VarP segments, VarP master, VarP toMasterRoute]
(NormalB body) (NormalB body)
[] []
where where
@ -208,7 +207,7 @@ mkYesodDispatch' sortedRes = do
app <- newName "app" app <- newName "app"
return $ CaseE test return $ CaseE test
[ Match (ConP (mkName "Nothing") []) (NormalB onFail) [] [ Match (ConP (mkName "Nothing") []) (NormalB onFail) []
, Match (ConP (mkName "Just") [VarP app]) (NormalB $ just `AppE` VarE app) [] , Match (ConP (mkName "Just") [VarP app]) (NormalB $ VarE app) []
] ]
go master sub toMasterRoute mkey segments onFail ((constr, Simple pieces methods), Nothing) = do go master sub toMasterRoute mkey segments onFail ((constr, Simple pieces methods), Nothing) = do
test <- mkSimpleExp segments pieces id (master, sub, toMasterRoute, mkey, constr, methods) test <- mkSimpleExp segments pieces id (master, sub, toMasterRoute, mkey, constr, methods)
@ -294,11 +293,15 @@ mkSimpleExp segments (SinglePiece s:pieces) frontVars x = do
return exp return exp
mkSubsiteExp segments [] frontVars (master, sub, toMasterRoute, mkey, constr, toSub) = do mkSubsiteExp segments [] frontVars (master, sub, toMasterRoute, mkey, constr, toSub) = do
ds <- [|dispatchSubsite|] yd <- [|yesodDispatch|]
let con = foldl' AppE (ConE $ mkName constr) $ frontVars [] let con = foldl' AppE (ConE $ mkName constr) $ frontVars []
let s' = VarE (mkName toSub) `AppE` VarE master let s' = VarE (mkName toSub) `AppE` VarE master
let s = foldl' AppE s' $ frontVars [] let s = foldl' AppE s' $ frontVars []
let app = ds `AppE` VarE master `AppE` VarE mkey `AppE` VarE segments `AppE` con `AppE` s let app = yd `AppE` s
`AppE` VarE mkey
`AppE` VarE segments
`AppE` VarE master
`AppE` con
just <- [|Just|] just <- [|Just|]
return $ just `AppE` app return $ just `AppE` app
mkSubsiteExp segments (StaticPiece s:pieces) frontVars x = do mkSubsiteExp segments (StaticPiece s:pieces) frontVars x = do
@ -400,7 +403,7 @@ mkDispatchToSubsite ((constr, SubSite { ssPieces = pieces }), Just toSub) = do
<- mkPat' pieces <- mkPat' pieces
(ConE $ mkName constr) (ConE $ mkName constr)
$ just `AppE` (VarE (mkName toSub) `AppE` VarE master) $ just `AppE` (VarE (mkName toSub) `AppE` VarE master)
ds <- [|dispatchSubsite|] ds <- error "FIXME" -- [|dispatchSubsite|]
goodParse <- (`AppE` tma') <$> [|isJust|] goodParse <- (`AppE` tma') <$> [|isJust|]
tma'' <- (`AppE` tma') <$> [|fromJust|] 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