{-# LANGUAGE CPP #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Yesod.Routes.TH.RenderRoute
(
mkRenderRouteInstanceOpts
, mkRouteConsOpts
, shouldCreateResources
, RouteOpts
, defaultOpts
, setEqDerived
, setShowDerived
, setReadDerived
, setCreateResources
, setFocusOnNestedRoute
, unsetFocusOnNestedRoute
, roFocusOnNestedRoute
, roNestedRouteFallthrough
, setParameterizedSubroute
, setNestedRouteFallthrough
, DiscoveryMode(..)
, discoveryMode
) where
import Data.Maybe
import Yesod.Routes.TH.Types
import Language.Haskell.TH (varE, varP)
import Language.Haskell.TH.Syntax
import Control.Monad
import Data.Text (pack)
import Web.PathPieces (PathPiece (..), PathMultiPiece (..))
import Yesod.Routes.Class
import Data.Foldable
import Yesod.Routes.TH.Internal
import Data.Char
import Yesod.Core.Class.Dispatch.ToParentRoute
data RouteOpts = MkRouteOpts
{ RouteOpts -> Bool
roDerivedEq :: Bool
, RouteOpts -> Bool
roDerivedShow :: Bool
, RouteOpts -> Bool
roDerivedRead :: Bool
, RouteOpts -> Bool
roCreateResources :: Bool
, RouteOpts -> Maybe [Char]
roFocusOnNestedRoute :: Maybe String
, RouteOpts -> Bool
roParameterizedSubroute :: Bool
, RouteOpts -> Bool
roNestedRouteFallthrough :: Bool
}
defaultOpts :: RouteOpts
defaultOpts :: RouteOpts
defaultOpts = MkRouteOpts
{ roDerivedEq :: Bool
roDerivedEq = Bool
True
, roDerivedShow :: Bool
roDerivedShow = Bool
True
, roDerivedRead :: Bool
roDerivedRead = Bool
True
, roCreateResources :: Bool
roCreateResources = Bool
True
, roParameterizedSubroute :: Bool
roParameterizedSubroute = Bool
False
, roFocusOnNestedRoute :: Maybe [Char]
roFocusOnNestedRoute = Maybe [Char]
forall a. Maybe a
Nothing
, roNestedRouteFallthrough :: Bool
roNestedRouteFallthrough = Bool
False
}
setFocusOnNestedRoute :: String -> RouteOpts -> RouteOpts
setFocusOnNestedRoute :: [Char] -> RouteOpts -> RouteOpts
setFocusOnNestedRoute [Char]
str RouteOpts
rdo = RouteOpts
rdo { roFocusOnNestedRoute = Just str }
unsetFocusOnNestedRoute :: RouteOpts -> RouteOpts
unsetFocusOnNestedRoute :: RouteOpts -> RouteOpts
unsetFocusOnNestedRoute RouteOpts
rdo = RouteOpts
rdo { roFocusOnNestedRoute = Nothing }
setNestedRouteFallthrough :: Bool -> RouteOpts -> RouteOpts
setNestedRouteFallthrough :: Bool -> RouteOpts -> RouteOpts
setNestedRouteFallthrough Bool
b RouteOpts
rdo = RouteOpts
rdo { roNestedRouteFallthrough = b }
setEqDerived :: Bool -> RouteOpts -> RouteOpts
setEqDerived :: Bool -> RouteOpts -> RouteOpts
setEqDerived Bool
b RouteOpts
rdo = RouteOpts
rdo { roDerivedEq = b }
setShowDerived :: Bool -> RouteOpts -> RouteOpts
setShowDerived :: Bool -> RouteOpts -> RouteOpts
setShowDerived Bool
b RouteOpts
rdo = RouteOpts
rdo { roDerivedShow = b }
setReadDerived :: Bool -> RouteOpts -> RouteOpts
setReadDerived :: Bool -> RouteOpts -> RouteOpts
setReadDerived Bool
b RouteOpts
rdo = RouteOpts
rdo { roDerivedRead = b }
setCreateResources :: Bool -> RouteOpts -> RouteOpts
setCreateResources :: Bool -> RouteOpts -> RouteOpts
setCreateResources Bool
b RouteOpts
rdo = RouteOpts
rdo { roCreateResources = b }
shouldCreateResources :: RouteOpts -> Bool
shouldCreateResources :: RouteOpts -> Bool
shouldCreateResources = RouteOpts -> Bool
roCreateResources
setParameterizedSubroute :: Bool -> RouteOpts -> RouteOpts
setParameterizedSubroute :: Bool -> RouteOpts -> RouteOpts
setParameterizedSubroute Bool
b RouteOpts
rdo = RouteOpts
rdo { roParameterizedSubroute = b }
instanceNamesFromOpts :: RouteOpts -> [Name]
instanceNamesFromOpts :: RouteOpts -> [Name]
instanceNamesFromOpts MkRouteOpts {Bool
Maybe [Char]
roFocusOnNestedRoute :: RouteOpts -> Maybe [Char]
roNestedRouteFallthrough :: RouteOpts -> Bool
roDerivedEq :: RouteOpts -> Bool
roDerivedShow :: RouteOpts -> Bool
roDerivedRead :: RouteOpts -> Bool
roCreateResources :: RouteOpts -> Bool
roParameterizedSubroute :: RouteOpts -> Bool
roDerivedEq :: Bool
roDerivedShow :: Bool
roDerivedRead :: Bool
roCreateResources :: Bool
roFocusOnNestedRoute :: Maybe [Char]
roParameterizedSubroute :: Bool
roNestedRouteFallthrough :: Bool
..} = Bool -> Name -> [Name] -> [Name]
forall {a}. Bool -> a -> [a] -> [a]
prependIf Bool
roDerivedEq ''Eq ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ Bool -> Name -> [Name] -> [Name]
forall {a}. Bool -> a -> [a] -> [a]
prependIf Bool
roDerivedShow ''Show ([Name] -> [Name]) -> [Name] -> [Name]
forall a b. (a -> b) -> a -> b
$ Bool -> Name -> [Name] -> [Name]
forall {a}. Bool -> a -> [a] -> [a]
prependIf Bool
roDerivedRead ''Read []
where prependIf :: Bool -> a -> [a] -> [a]
prependIf Bool
b = if Bool
b then (:) else ([a] -> [a]) -> a -> [a] -> [a]
forall a b. a -> b -> a
const [a] -> [a]
forall a. a -> a
id
data DiscoveryMode
=
InlineCompat
|
NestedDiscovery
deriving (DiscoveryMode -> DiscoveryMode -> Bool
(DiscoveryMode -> DiscoveryMode -> Bool)
-> (DiscoveryMode -> DiscoveryMode -> Bool) -> Eq DiscoveryMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DiscoveryMode -> DiscoveryMode -> Bool
== :: DiscoveryMode -> DiscoveryMode -> Bool
$c/= :: DiscoveryMode -> DiscoveryMode -> Bool
/= :: DiscoveryMode -> DiscoveryMode -> Bool
Eq, Int -> DiscoveryMode -> ShowS
[DiscoveryMode] -> ShowS
DiscoveryMode -> [Char]
(Int -> DiscoveryMode -> ShowS)
-> (DiscoveryMode -> [Char])
-> ([DiscoveryMode] -> ShowS)
-> Show DiscoveryMode
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DiscoveryMode -> ShowS
showsPrec :: Int -> DiscoveryMode -> ShowS
$cshow :: DiscoveryMode -> [Char]
show :: DiscoveryMode -> [Char]
$cshowList :: [DiscoveryMode] -> ShowS
showList :: [DiscoveryMode] -> ShowS
Show)
discoveryMode :: RouteOpts -> TyArgs -> DiscoveryMode
discoveryMode :: RouteOpts -> TyArgs -> DiscoveryMode
discoveryMode RouteOpts
opts TyArgs
tyargs
| Bool -> Bool
not (TyArgs -> Bool
hasTyArgs TyArgs
tyargs) = DiscoveryMode
NestedDiscovery
| RouteOpts -> Bool
roParameterizedSubroute RouteOpts
opts = DiscoveryMode
NestedDiscovery
| Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isJust (RouteOpts -> Maybe [Char]
roFocusOnNestedRoute RouteOpts
opts) = DiscoveryMode
NestedDiscovery
| Bool
otherwise = DiscoveryMode
InlineCompat
plainTVCompat :: Name ->
#if MIN_VERSION_template_haskell(2,21,0)
TyVarBndr BndrVis
#elif MIN_VERSION_template_haskell(2,17,0)
TyVarBndr ()
#else
TyVarBndr
#endif
plainTVCompat :: Name -> TyVarBndr BndrVis
plainTVCompat =
#if MIN_VERSION_template_haskell(2,21,0)
(Name -> BndrVis -> TyVarBndr BndrVis
forall flag. Name -> flag -> TyVarBndr flag
`PlainTV` BndrVis
BndrReq)
#elif MIN_VERSION_template_haskell(2,17,0)
(`PlainTV` ())
#else
PlainTV
#endif
leafRouteCon :: Resource Type -> Con
leafRouteCon :: Resource Type -> Con
leafRouteCon Resource Type
res =
Name -> [BangType] -> Con
NormalC ([Char] -> Name
mkName ([Char] -> Name) -> [Char] -> Name
forall a b. (a -> b) -> a -> b
$ Resource Type -> [Char]
forall typ. Resource typ -> [Char]
resourceName Resource Type
res)
([BangType] -> Con) -> [BangType] -> Con
forall a b. (a -> b) -> a -> b
$ (Type -> BangType) -> [Type] -> [BangType]
forall a b. (a -> b) -> [a] -> [b]
map (Bang
notStrict,)
([Type] -> [BangType]) -> [Type] -> [BangType]
forall a b. (a -> b) -> a -> b
$ [[Type]] -> [Type]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Type]
singles, [Type]
multi, [Type]
sub]
where
singles :: [Type]
singles = (Piece Type -> [Type]) -> [Piece Type] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Piece Type -> [Type]
forall {a}. Piece a -> [a]
toSingle ([Piece Type] -> [Type]) -> [Piece Type] -> [Type]
forall a b. (a -> b) -> a -> b
$ Resource Type -> [Piece Type]
forall typ. Resource typ -> [Piece typ]
resourcePieces Resource Type
res
toSingle :: Piece a -> [a]
toSingle Static{} = []
toSingle (Dynamic a
typ) = [a
typ]
multi :: [Type]
multi = Maybe Type -> [Type]
forall a. Maybe a -> [a]
maybeToList (Maybe Type -> [Type]) -> Maybe Type -> [Type]
forall a b. (a -> b) -> a -> b
$ Resource Type -> Maybe Type
forall typ. Resource typ -> Maybe typ
resourceMulti Resource Type
res
sub :: [Type]
sub =
case Resource Type -> Dispatch Type
forall typ. Resource typ -> Dispatch typ
resourceDispatch Resource Type
res of
Subsite { subsiteType :: forall typ. Dispatch typ -> typ
subsiteType = Type
typ } -> [Name -> Type
ConT ''Route Type -> Type -> Type
`AppT` Type
typ]
Dispatch Type
_ -> []
parentRouteCon :: String -> [Piece Type] -> TyArgs -> Con
parentRouteCon :: [Char] -> [Piece Type] -> TyArgs -> Con
parentRouteCon [Char]
name [Piece Type]
pieces TyArgs
tyargs =
Name -> [BangType] -> Con
NormalC ([Char] -> Name
mkName [Char]
name)
([BangType] -> Con) -> [BangType] -> Con
forall a b. (a -> b) -> a -> b
$ (Type -> BangType) -> [Type] -> [BangType]
forall a b. (a -> b) -> [a] -> [b]
map (Bang
notStrict,)
([Type] -> [BangType]) -> [Type] -> [BangType]
forall a b. (a -> b) -> a -> b
$ [Type]
singles [Type] -> [Type] -> [Type]
forall a. [a] -> [a] -> [a]
++ [Type -> TyArgs -> Type
applyTyArgs (Name -> Type
ConT ([Char] -> Name
mkName [Char]
name)) TyArgs
tyargs]
where
singles :: [Type]
singles = (Piece Type -> [Type]) -> [Piece Type] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Piece Type -> [Type]
forall {a}. Piece a -> [a]
toSingle [Piece Type]
pieces
toSingle :: Piece a -> [a]
toSingle Static{} = []
toSingle (Dynamic a
typ) = [a
typ]
childDerivCxt :: TyArgs -> Cxt -> Cxt
childDerivCxt :: TyArgs -> [Type] -> [Type]
childDerivCxt TyArgs
tyargs [Type]
cxt
| TyArgs -> Bool
hasTyArgs TyArgs
tyargs = [Type]
cxt
| Bool
otherwise = []
mkRouteConsOpts :: RouteOpts -> Cxt -> TyArgs -> Type -> [ResourceTree Type] -> Q ([Con], [Dec])
mkRouteConsOpts :: RouteOpts
-> [Type]
-> TyArgs
-> Type
-> [ResourceTree Type]
-> Q ([Con], [Dec])
mkRouteConsOpts RouteOpts
opts [Type]
cxt TyArgs
origTyargs Type
master [ResourceTree Type]
resourceTrees = do
(prePieces, focusedTrees) <-
case RouteOpts -> Maybe [Char]
roFocusOnNestedRoute RouteOpts
opts of
Maybe [Char]
Nothing ->
([Piece Type], [ResourceTree Type])
-> Q ([Piece Type], [ResourceTree Type])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [ResourceTree Type]
resourceTrees)
Just [Char]
target -> do
let mnestedRoute :: Maybe ([Piece Type], [ResourceTree Type])
mnestedRoute = [Char]
-> [ResourceTree Type] -> Maybe ([Piece Type], [ResourceTree Type])
forall a.
[Char] -> [ResourceTree a] -> Maybe ([Piece a], [ResourceTree a])
findNestedRoute [Char]
target [ResourceTree Type]
resourceTrees
case Maybe ([Piece Type], [ResourceTree Type])
mnestedRoute of
Maybe ([Piece Type], [ResourceTree Type])
Nothing ->
[Char] -> Q ([Piece Type], [ResourceTree Type])
forall a. HasCallStack => [Char] -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail ([Char] -> Q ([Piece Type], [ResourceTree Type]))
-> [Char] -> Q ([Piece Type], [ResourceTree Type])
forall a b. (a -> b) -> a -> b
$ [Char]
"Target '" [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
target [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
"' was not found in resources."
Just ([Piece Type], [ResourceTree Type])
r ->
([Piece Type], [ResourceTree Type])
-> Q ([Piece Type], [ResourceTree Type])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Piece Type], [ResourceTree Type])
r
mkRouteConsOpts' prePieces focusedTrees
where
mode :: DiscoveryMode
mode = RouteOpts -> TyArgs -> DiscoveryMode
discoveryMode RouteOpts
opts TyArgs
origTyargs
tyargs :: TyArgs
tyargs =
case DiscoveryMode
mode of
DiscoveryMode
NestedDiscovery -> TyArgs
origTyargs
DiscoveryMode
InlineCompat -> TyArgs
NoTyArgs
subrouteDecTypeArgs :: [TyVarBndr BndrVis]
subrouteDecTypeArgs = (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
plainTVCompat (TyArgs -> [Name]
tyArgsBinders TyArgs
tyargs)
childCxt :: [Type]
childCxt = TyArgs -> [Type] -> [Type]
childDerivCxt TyArgs
tyargs [Type]
cxt
([DerivClause]
inlineDerives, Type -> [Dec]
mkStandaloneDerives) = RouteOpts -> [Type] -> ([DerivClause], Type -> [Dec])
getDerivesFor RouteOpts
opts [Type]
childCxt
mkRouteConsOpts' :: [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkRouteConsOpts' :: [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkRouteConsOpts' [Piece Type]
prePieces [ResourceTree Type]
trees = do
results <- (ResourceTree Type -> Q ([Con], [Dec]))
-> [ResourceTree Type] -> Q [([Con], [Dec])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkRouteCon [Piece Type]
prePieces) [ResourceTree Type]
trees
pure (mconcat results)
mkRouteCon :: [Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkRouteCon :: [Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkRouteCon [Piece Type]
_ (ResourceLeaf Resource Type
res) =
([Con], [Dec]) -> Q ([Con], [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Resource Type -> Con
leafRouteCon Resource Type
res], [])
mkRouteCon [Piece Type]
prePieces (ResourceParent [Char]
name Bool
_check Set [Char]
_attrs [Piece Type]
pieces [ResourceTree Type]
children) = do
let accumulatedPieces :: [Piece Type]
accumulatedPieces = [Piece Type]
prePieces [Piece Type] -> [Piece Type] -> [Piece Type]
forall a. Semigroup a => a -> a -> a
<> [Piece Type]
pieces
(cons, decs) <- [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkRouteConsOpts' [Piece Type]
accumulatedPieces [ResourceTree Type]
children
let childDataName = [Char] -> Name
mkName [Char]
name
mname' <- lookupTypeName name
mdec <- case mname' of
Just Name
_ -> do
Maybe [Dec] -> Q (Maybe [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [Dec]
forall a. Maybe a
Nothing
Maybe Name
Nothing -> do
let childData :: Dec
childData = Name -> [Con] -> [DerivClause] -> Dec
mkChildDataGen Name
childDataName [Con]
cons [DerivClause]
inlineDerives
childDataType :: Type
childDataType = Type -> TyArgs -> Type
applyTyArgs (Name -> Type
ConT Name
childDataName) TyArgs
tyargs
case RouteOpts -> Maybe [Char]
roFocusOnNestedRoute RouteOpts
opts of
Just [Char]
target | [Char]
target [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char]
name ->
Maybe [Dec] -> Q (Maybe [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [Dec]
forall a. Maybe a
Nothing
Maybe [Char]
_ | DiscoveryMode
InlineCompat <- DiscoveryMode
mode ->
Maybe [Dec] -> Q (Maybe [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe [Dec] -> Q (Maybe [Dec])) -> Maybe [Dec] -> Q (Maybe [Dec])
forall a b. (a -> b) -> a -> b
$ [Dec] -> Maybe [Dec]
forall a. a -> Maybe a
Just (Dec
childData Dec -> [Dec] -> [Dec]
forall a. a -> [a] -> [a]
: Type -> [Dec]
mkStandaloneDerives Type
childDataType)
Maybe [Char]
_ ->
[Dec] -> Maybe [Dec]
forall a. a -> Maybe a
Just ([Dec] -> Maybe [Dec]) -> Q [Dec] -> Q (Maybe [Dec])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
RouteOpts
-> [Type]
-> [Type]
-> TyArgs
-> Type
-> Name
-> [Con]
-> [Piece Type]
-> [ResourceTree Type]
-> Q [Dec]
nestedChildDataAndInstance
RouteOpts
opts [Type]
cxt [Type]
childCxt TyArgs
tyargs Type
master Name
childDataName [Con]
cons
([Piece Type]
prePieces [Piece Type] -> [Piece Type] -> [Piece Type]
forall a. Semigroup a => a -> a -> a
<> [Piece Type]
pieces) [ResourceTree Type]
children
return ([parentRouteCon name pieces tyargs], maybe id (<>) mdec decs)
where
mkChildDataGen :: Name -> [Con] -> [DerivClause] -> Dec
mkChildDataGen :: Name -> [Con] -> [DerivClause] -> Dec
mkChildDataGen Name
childDataName [Con]
cons [DerivClause]
conts =
[Type]
-> Name
-> [TyVarBndr BndrVis]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataD [] Name
childDataName [TyVarBndr BndrVis]
subrouteDecTypeArgs Maybe Type
forall a. Maybe a
Nothing [Con]
cons [DerivClause]
conts
parentConPat :: String -> [Piece a] -> Q ([Name], Name, Pat)
parentConPat :: forall a. [Char] -> [Piece a] -> Q ([Name], Name, Pat)
parentConPat [Char]
name [Piece a]
pieces = do
let cnt :: Int
cnt = [()] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [() | Dynamic{} <- [Piece a]
pieces]
dyns <- Int -> Q Name -> Q [Name]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
cnt (Q Name -> Q [Name]) -> Q Name -> Q [Name]
forall a b. (a -> b) -> a -> b
$ [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"dyn"
child <- newName "child"
pure (dyns, child, conPCompat (mkName name) $ map VarP $ dyns ++ [child])
isDynamic :: Piece t -> Bool
isDynamic :: forall t. Piece t -> Bool
isDynamic Dynamic{} = Bool
True
isDynamic Piece t
_ = Bool
False
mkPieces :: (String -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
mkPieces :: forall t. ([Char] -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
mkPieces [Char] -> Exp
_ Exp
_ [] [Name]
_ = [Exp] -> Q [Exp]
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
mkPieces [Char] -> Exp
toText Exp
tsp (Static [Char]
s:[Piece t]
ps) [Name]
dyns = ([Char] -> Exp
toText [Char]
s Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
:) ([Exp] -> [Exp]) -> Q [Exp] -> Q [Exp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Char] -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
forall t. ([Char] -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
mkPieces [Char] -> Exp
toText Exp
tsp [Piece t]
ps [Name]
dyns
mkPieces [Char] -> Exp
toText Exp
tsp (Dynamic{}:[Piece t]
ps) (Name
d:[Name]
dyns) = (Exp
tsp Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
d Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
:) ([Exp] -> [Exp]) -> Q [Exp] -> Q [Exp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Char] -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
forall t. ([Char] -> Exp) -> Exp -> [Piece t] -> [Name] -> Q [Exp]
mkPieces [Char] -> Exp
toText Exp
tsp [Piece t]
ps [Name]
dyns
mkPieces [Char] -> Exp
_ Exp
_ (Dynamic t
_ : [Piece t]
_) [] =
[Char] -> Q [Exp]
forall a. HasCallStack => [Char] -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail [Char]
"RenderRoute.mkPieces: a dynamic path piece has no corresponding bound variable (route definition and piece-binder list are out of sync)"
mkRenderRouteClauses :: RouteOpts -> TyArgs -> [ResourceTree Type] -> Q [Clause]
mkRenderRouteClauses :: RouteOpts -> TyArgs -> [ResourceTree Type] -> Q [Clause]
mkRenderRouteClauses RouteOpts
opts TyArgs
origTyargs =
[ResourceTree Type] -> Q [Clause]
forall {typ}. [ResourceTree typ] -> Q [Clause]
goList
where
goList :: [ResourceTree typ] -> Q [Clause]
goList = ([[Clause]] -> [Clause]) -> Q [[Clause]] -> Q [Clause]
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Clause]] -> [Clause]
forall a. Monoid a => [a] -> a
mconcat (Q [[Clause]] -> Q [Clause])
-> ([ResourceTree typ] -> Q [[Clause]])
-> [ResourceTree typ]
-> Q [Clause]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ResourceTree typ -> Q [Clause])
-> [ResourceTree typ] -> Q [[Clause]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ResourceTree typ -> Q [Clause]
go
mode :: DiscoveryMode
mode = RouteOpts -> TyArgs -> DiscoveryMode
discoveryMode RouteOpts
opts TyArgs
origTyargs
go :: ResourceTree typ -> Q [Clause]
go (ResourceParent [Char]
name Bool
_check Set [Char]
_attrs [Piece typ]
pieces [ResourceTree typ]
children) =
case DiscoveryMode
mode of
DiscoveryMode
NestedDiscovery -> Q [Clause]
goNested
DiscoveryMode
InlineCompat -> Q [Clause]
goInline
where
goNested :: Q [Clause]
goNested = do
(dyns, child, pat) <- [Char] -> [Piece typ] -> Q ([Name], Name, Pat)
forall a. [Char] -> [Piece a] -> Q ([Name], Name, Pat)
parentConPat [Char]
name [Piece typ]
pieces
body <- [| renderRouteNested $(pure (parentArgsExpr dyns)) $(pure (VarE child)) |]
pure [Clause [pat] (NormalB body) []]
goInline :: Q [Clause]
goInline = do
(dyns, child, pat) <- [Char] -> [Piece typ] -> Q ([Name], Name, Pat)
forall a. [Char] -> [Piece a] -> Q ([Name], Name, Pat)
parentConPat [Char]
name [Piece typ]
pieces
pack' <- [|pack|]
tsp <- [|toPathPiece|]
piecesSingle <- mkPieces (AppE pack' . LitE . StringL) tsp pieces dyns
childRender <- newName "childRender"
childClauses <- goList children
body <- delegatingBody piecesSingle (VarE childRender) (VarE child)
pure [Clause [pat] (NormalB body) [FunD childRender childClauses]]
go (ResourceLeaf Resource typ
res) = do
let cnt :: Int
cnt = [Piece typ] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((Piece typ -> Bool) -> [Piece typ] -> [Piece typ]
forall a. (a -> Bool) -> [a] -> [a]
filter Piece typ -> Bool
forall t. Piece t -> Bool
isDynamic ([Piece typ] -> [Piece typ]) -> [Piece typ] -> [Piece typ]
forall a b. (a -> b) -> a -> b
$ Resource typ -> [Piece typ]
forall typ. Resource typ -> [Piece typ]
resourcePieces Resource typ
res) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> (typ -> Int) -> Maybe typ -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 (Int -> typ -> Int
forall a b. a -> b -> a
const Int
1) (Resource typ -> Maybe typ
forall typ. Resource typ -> Maybe typ
resourceMulti Resource typ
res)
dyns <- Int -> Q Name -> Q [Name]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
cnt (Q Name -> Q [Name]) -> Q Name -> Q [Name]
forall a b. (a -> b) -> a -> b
$ [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"dyn"
sub <-
case resourceDispatch res of
Subsite{} -> Name -> [Name]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [Name]) -> Q Name -> Q [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"sub"
Dispatch typ
_ -> [Name] -> Q [Name]
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return []
let pat = Name -> [Pat] -> Pat
conPCompat ([Char] -> Name
mkName ([Char] -> Name) -> [Char] -> Name
forall a b. (a -> b) -> a -> b
$ Resource typ -> [Char]
forall typ. Resource typ -> [Char]
resourceName Resource typ
res) ([Pat] -> Pat) -> [Pat] -> Pat
forall a b. (a -> b) -> a -> b
$ (Name -> Pat) -> [Name] -> [Pat]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Pat
VarP ([Name] -> [Pat]) -> [Name] -> [Pat]
forall a b. (a -> b) -> a -> b
$ [Name]
dyns [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
sub
pack' <- [|pack|]
tsp <- [|toPathPiece|]
piecesSingle <- mkPieces (AppE pack' . LitE . StringL) tsp (resourcePieces res) dyns
piecesMulti <-
case resourceMulti res of
Maybe typ
Nothing -> 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
$ [Exp] -> Exp
ListE []
Just{} -> do
tmp <- [|toPathMultiPiece|]
return $ tmp `AppE` VarE (last dyns)
body <-
case sub of
[Name
x] -> do
rr <- [|renderRoute|]
delegatingBody piecesSingle rr (VarE x)
[Name]
_ ->
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
$ [Exp] -> Exp
mkTupE [(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
consE Exp
piecesMulti [Exp]
piecesSingle, [Exp] -> Exp
ListE []]
return [Clause [pat] (NormalB body) []]
delegatingBody
:: [Exp]
-> Exp
-> Exp
-> Q Exp
delegatingBody :: [Exp] -> Exp -> Exp -> Q Exp
delegatingBody [Exp]
piecesSingle Exp
rr Exp
childArg = do
a <- [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"a"
b <- newName "b"
let pieces' = (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
consE (Name -> Exp
VarE Name
a) [Exp]
piecesSingle
[e| ( \ ( $(varP a), $(varP b) ) -> ( $(pure pieces'), $(varE b) ) )
( $(pure rr) $(pure childArg) )
|]
mkRenderRouteNestedClauses
:: [Either String Name]
-> [ResourceTree Type]
-> Q [Clause]
mkRenderRouteNestedClauses :: [Either [Char] Name] -> [ResourceTree Type] -> Q [Clause]
mkRenderRouteNestedClauses [Either [Char] Name]
parentArgsNames [ResourceTree Type]
resources = do
([[Clause]] -> [Clause]) -> Q [[Clause]] -> Q [Clause]
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Clause]] -> [Clause]
forall a. Monoid a => [a] -> a
mconcat (Q [[Clause]] -> Q [Clause])
-> ([ResourceTree Type] -> Q [[Clause]])
-> [ResourceTree Type]
-> Q [Clause]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ResourceTree Type -> Q [Clause])
-> [ResourceTree Type] -> Q [[Clause]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ResourceTree Type -> Q [Clause]
forall {typ}. ResourceTree typ -> Q [Clause]
go ([ResourceTree Type] -> Q [Clause])
-> [ResourceTree Type] -> Q [Clause]
forall a b. (a -> b) -> a -> b
$ [ResourceTree Type]
resources
where
go :: ResourceTree typ -> Q [Clause]
go (ResourceParent [Char]
name Bool
_check Set [Char]
_attrs [Piece typ]
pieces [ResourceTree typ]
_children) = do
(dyns, child, pat) <- [Char] -> [Piece typ] -> Q ([Name], Name, Pat)
forall a. [Char] -> [Piece a] -> Q ([Name], Name, Pat)
parentConPat [Char]
name [Piece typ]
pieces
let parentDyns = (Either [Char] Name -> Maybe Name)
-> [Either [Char] Name] -> [Name]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (([Char] -> Maybe Name)
-> (Name -> Maybe Name) -> Either [Char] Name -> Maybe Name
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Name -> [Char] -> Maybe Name
forall a b. a -> b -> a
const Maybe Name
forall a. Maybe a
Nothing) Name -> Maybe Name
forall a. a -> Maybe a
Just) [Either [Char] Name]
parentArgsNames
let allDyns = [Name]
parentDyns [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
dyns
accumulatedParentArgsExp = [Name] -> Exp
parentArgsExpr [Name]
allDyns
body <- [| renderRouteNested $(pure accumulatedParentArgsExp) $(pure (VarE child)) |]
pure [Clause [parentArgsPat parentDyns, pat] (NormalB body) []]
go (ResourceLeaf Resource typ
res) = do
let cnt :: Int
cnt = [Piece typ] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((Piece typ -> Bool) -> [Piece typ] -> [Piece typ]
forall a. (a -> Bool) -> [a] -> [a]
filter Piece typ -> Bool
forall t. Piece t -> Bool
isDynamic ([Piece typ] -> [Piece typ]) -> [Piece typ] -> [Piece typ]
forall a b. (a -> b) -> a -> b
$ Resource typ -> [Piece typ]
forall typ. Resource typ -> [Piece typ]
resourcePieces Resource typ
res) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> (typ -> Int) -> Maybe typ -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 (Int -> typ -> Int
forall a b. a -> b -> a
const Int
1) (Resource typ -> Maybe typ
forall typ. Resource typ -> Maybe typ
resourceMulti Resource typ
res)
dyns <- Int -> Q Name -> Q [Name]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
cnt (Q Name -> Q [Name]) -> Q Name -> Q [Name]
forall a b. (a -> b) -> a -> b
$ [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"dyn"
sub <-
case resourceDispatch res of
Subsite{} -> Name -> [Name]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [Name]) -> Q Name -> Q [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"sub"
Dispatch typ
_ -> [Name] -> Q [Name]
forall a. a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return []
let pat = Name -> [Pat] -> Pat
conPCompat ([Char] -> Name
mkName ([Char] -> Name) -> [Char] -> Name
forall a b. (a -> b) -> a -> b
$ Resource typ -> [Char]
forall typ. Resource typ -> [Char]
resourceName Resource typ
res) ([Pat] -> Pat) -> [Pat] -> Pat
forall a b. (a -> b) -> a -> b
$ (Name -> Pat) -> [Name] -> [Pat]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Pat
VarP ([Name] -> [Pat]) -> [Name] -> [Pat]
forall a b. (a -> b) -> a -> b
$ [Name]
dyns [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
sub
pack' <- [|pack|]
tsp <- [|toPathPiece|]
let parentDyns = (Either [Char] Name -> Maybe Name)
-> [Either [Char] Name] -> [Name]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (([Char] -> Maybe Name)
-> (Name -> Maybe Name) -> Either [Char] Name -> Maybe Name
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Name -> [Char] -> Maybe Name
forall a b. a -> b -> a
const Maybe Name
forall a. Maybe a
Nothing) Name -> Maybe Name
forall a. a -> Maybe a
Just) [Either [Char] Name]
parentArgsNames
parentPieces <- mkParentPieces pack' tsp parentArgsNames parentDyns
thisResourcePieces <- mkPieces (AppE pack' . LitE . StringL) tsp (resourcePieces res) dyns
piecesMulti <-
case resourceMulti res of
Maybe typ
Nothing -> 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
$ [Exp] -> Exp
ListE []
Just{} -> do
tmp <- [|toPathMultiPiece|]
return $ tmp `AppE` VarE (last dyns)
body <-
case sub of
[Name
x] -> do
rr <- [|renderRoute|]
delegatingBody (parentPieces ++ thisResourcePieces) rr (VarE x)
[Name]
_ -> do
colon <- [|(:)|]
let cons Exp
a Exp
b = Maybe Exp -> Exp -> Maybe Exp -> Exp
InfixE (Exp -> Maybe Exp
forall a. a -> Maybe a
Just Exp
a) Exp
colon (Exp -> Maybe Exp
forall a. a -> Maybe a
Just Exp
b)
let allPieces = [Exp]
parentPieces [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp]
thisResourcePieces
return $ mkTupE [foldr cons piecesMulti allPieces, ListE []]
return [Clause [parentArgsPat parentDyns, pat] (NormalB body) []]
mkParentPieces :: Exp -> Exp -> [Either String Name] -> [Name] -> Q [Exp]
mkParentPieces :: Exp -> Exp -> [Either [Char] Name] -> [Name] -> Q [Exp]
mkParentPieces Exp
pack' Exp
tsp [Either [Char] Name]
parentArgsNames' [Name]
parentDyns = [Either [Char] Name] -> [Name] -> Q [Exp]
forall {f :: * -> *} {b}.
MonadFail f =>
[Either [Char] b] -> [Name] -> f [Exp]
goParentPieces [Either [Char] Name]
parentArgsNames' [Name]
parentDyns
where
goParentPieces :: [Either [Char] b] -> [Name] -> f [Exp]
goParentPieces [] [Name]
_ = [Exp] -> f [Exp]
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
goParentPieces (Left [Char]
staticStr : [Either [Char] b]
rest) [Name]
dyns =
((Exp
pack' Exp -> Exp -> Exp
`AppE` Lit -> Exp
LitE ([Char] -> Lit
StringL [Char]
staticStr)) Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
:) ([Exp] -> [Exp]) -> f [Exp] -> f [Exp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Either [Char] b] -> [Name] -> f [Exp]
goParentPieces [Either [Char] b]
rest [Name]
dyns
goParentPieces (Right b
_ : [Either [Char] b]
rest) (Name
dyn : [Name]
dyns) =
((Exp
tsp Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
dyn) Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
:) ([Exp] -> [Exp]) -> f [Exp] -> f [Exp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Either [Char] b] -> [Name] -> f [Exp]
goParentPieces [Either [Char] b]
rest [Name]
dyns
goParentPieces (Right b
_ : [Either [Char] b]
_) [] =
[Char] -> f [Exp]
forall a. HasCallStack => [Char] -> f a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail [Char]
"RenderRoute.mkParentPieces: a dynamic parent path piece has no corresponding bound variable"
renderRouteNestedBody
:: [Piece Type]
-> [ResourceTree Type]
-> Q (Type, [Clause])
renderRouteNestedBody :: [Piece Type] -> [ResourceTree Type] -> Q (Type, [Clause])
renderRouteNestedBody [Piece Type]
prepieces [ResourceTree Type]
children = do
let piecesAndNames :: [Either [Char] Type]
piecesAndNames =
(Piece Type -> Either [Char] Type)
-> [Piece Type] -> [Either [Char] Type]
forall a b. (a -> b) -> [a] -> [b]
map (\Piece Type
p -> case Piece Type
p of
Static [Char]
str -> [Char] -> Either [Char] Type
forall a b. a -> Either a b
Left [Char]
str
Dynamic Type
a -> Type -> Either [Char] Type
forall a b. b -> Either a b
Right Type
a)
[Piece Type]
prepieces
preDyns :: [Type]
preDyns = (Either [Char] Type -> Maybe Type)
-> [Either [Char] Type] -> [Type]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (([Char] -> Maybe Type)
-> (Type -> Maybe Type) -> Either [Char] Type -> Maybe Type
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Type -> [Char] -> Maybe Type
forall a b. a -> b -> a
const Maybe Type
forall a. Maybe a
Nothing) Type -> Maybe Type
forall a. a -> Maybe a
Just) [Either [Char] Type]
piecesAndNames
let parentDynT :: Type
parentDynT = [Type] -> Type
parentArgsType [Type]
preDyns
parentNames <- [Either [Char] Type]
-> (Either [Char] Type -> Q (Either [Char] Name))
-> Q [Either [Char] Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Either [Char] Type]
piecesAndNames ((Either [Char] Type -> Q (Either [Char] Name))
-> Q [Either [Char] Name])
-> (Either [Char] Type -> Q (Either [Char] Name))
-> Q [Either [Char] Name]
forall a b. (a -> b) -> a -> b
$ \Either [Char] Type
epiece'name ->
case Either [Char] Type
epiece'name of
Left [Char]
piece -> Either [Char] Name -> Q (Either [Char] Name)
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> Either [Char] Name
forall a b. a -> Either a b
Left [Char]
piece)
Right Type
t ->
case (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
filter Char -> Bool
isAlphaNum (Type -> [Char]
forall a. Show a => a -> [Char]
show Type
t) of
(Char
a : [Char]
as) -> Name -> Either [Char] Name
forall a b. b -> Either a b
Right (Name -> Either [Char] Name) -> Q Name -> Q (Either [Char] Name)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> Q Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName (Char -> Char
toLower Char
a Char -> ShowS
forall a. a -> [a] -> [a]
: [Char]
as)
[] -> [Char] -> Q (Either [Char] Name)
forall a. HasCallStack => [Char] -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail
[Char]
"renderRouteNested: a dynamic piece's type renders with no alphanumeric characters, so no binder name can be derived from it"
childClauses <- mkRenderRouteNestedClauses parentNames children
pure (parentDynT, childClauses)
mkRenderRouteInstanceOpts
:: RouteOpts
-> Cxt
-> TyArgs
-> Type
-> [ResourceTree Type]
-> Q [Dec]
mkRenderRouteInstanceOpts :: RouteOpts
-> [Type] -> TyArgs -> Type -> [ResourceTree Type] -> Q [Dec]
mkRenderRouteInstanceOpts RouteOpts
opts [Type]
cxt TyArgs
tyargs Type
typ [ResourceTree Type]
ress = do
case RouteOpts -> Maybe [Char]
roFocusOnNestedRoute RouteOpts
opts of
Maybe [Char]
Nothing -> do
cls <- RouteOpts -> TyArgs -> [ResourceTree Type] -> Q [Clause]
mkRenderRouteClauses RouteOpts
opts TyArgs
tyargs [ResourceTree Type]
ress
(cons, decs) <- mkRouteConsOpts opts cxt tyargs typ ress
let did = [Type]
-> Maybe [TyVarBndr ()]
-> Type
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataInstD []
#if MIN_VERSION_template_haskell(2,15,0)
Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing Type
routeDataName
#else
''Route [typ]
#endif
Maybe Type
forall a. Maybe a
Nothing [Con]
cons [DerivClause]
inlineDerives
parentRouteInstancesDecs <-
case discoveryMode opts tyargs of
DiscoveryMode
NestedDiscovery -> [Type] -> TyArgs -> [ResourceTree Type] -> Q [Dec]
mkToParentRouteInstances [Type]
cxt TyArgs
tyargs [ResourceTree Type]
ress
DiscoveryMode
InlineCompat -> [Dec] -> Q [Dec]
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
pure $ mconcat
[ pure $ instanceD cxt (ConT ''RenderRoute `AppT` typ)
[ did
, FunD 'renderRoute cls
]
, mkStandaloneDerives routeDataName
, decs
, parentRouteInstancesDecs
]
Just [Char]
target ->
case [Char]
-> [ResourceTree Type] -> Maybe ([Piece Type], [ResourceTree Type])
forall a.
[Char] -> [ResourceTree a] -> Maybe ([Piece a], [ResourceTree a])
findNestedRoute [Char]
target [ResourceTree Type]
ress of
Maybe ([Piece Type], [ResourceTree Type])
Nothing ->
[Char] -> Q [Dec]
forall a. HasCallStack => [Char] -> Q a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail ([Char] -> Q [Dec]) -> [Char] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ [Char]
"Target '" [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
target [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
"' was not found in resources."
Just ([Piece Type]
prepieces, [ResourceTree Type]
ress') ->
RouteOpts
-> [Type]
-> TyArgs
-> Type
-> [Piece Type]
-> [Char]
-> [ResourceTree Type]
-> Q [Dec]
mkRenderRouteNestedInstanceOpts RouteOpts
opts [Type]
cxt TyArgs
tyargs Type
typ [Piece Type]
prepieces [Char]
target [ResourceTree Type]
ress'
where
routeDataName :: Type
routeDataName = Name -> Type
ConT ''Route Type -> Type -> Type
`AppT` Type
typ
([DerivClause]
inlineDerives, Type -> [Dec]
mkStandaloneDerives) = RouteOpts -> [Type] -> ([DerivClause], Type -> [Dec])
getDerivesFor RouteOpts
opts [Type]
cxt
getDerivesFor :: RouteOpts -> Cxt -> ([DerivClause], Type -> [Dec])
getDerivesFor :: RouteOpts -> [Type] -> ([DerivClause], Type -> [Dec])
getDerivesFor RouteOpts
opts [Type]
cxt
| [Type] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
cxt =
( [Maybe DerivStrategy -> [Type] -> DerivClause
DerivClause Maybe DerivStrategy
forall a. Maybe a
Nothing [Type]
clazzes']
, [Dec] -> Type -> [Dec]
forall a b. a -> b -> a
const []
)
| Bool
otherwise =
( []
, \Type
typ -> (Type -> Dec) -> [Type] -> [Dec]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe DerivStrategy -> [Type] -> Type -> Dec
StandaloneDerivD Maybe DerivStrategy
forall a. Maybe a
Nothing [Type]
cxt (Type -> Dec) -> (Type -> Type) -> Type -> Dec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Type -> Type -> Type
`AppT` Type
typ)) [Type]
clazzes'
)
where
clazzes' :: [Type]
clazzes' = Name -> Type
ConT (Name -> Type) -> [Name] -> [Type]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RouteOpts -> [Name]
instanceNamesFromOpts RouteOpts
opts
mkToParentRouteInstances :: Cxt -> TyArgs -> [ResourceTree Type] -> Q [Dec]
mkToParentRouteInstances :: [Type] -> TyArgs -> [ResourceTree Type] -> Q [Dec]
mkToParentRouteInstances [Type]
cxt TyArgs
origTyargs [ResourceTree Type]
ress = do
[[Dec]] -> [Dec]
forall a. Monoid a => [a] -> a
mconcat ([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ResourceTree Type -> Q [Dec]) -> [ResourceTree Type] -> Q [[Dec]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (([Piece Type], [(Name, Int)]) -> ResourceTree Type -> Q [Dec]
forall {f :: * -> *} {a}.
Quote f =>
([Piece a], [(Name, Int)]) -> ResourceTree a -> f [Dec]
go ([], [])) [ResourceTree Type]
ress
where
go :: ([Piece a], [(Name, Int)]) -> ResourceTree a -> f [Dec]
go ([Piece a], [(Name, Int)])
_ (ResourceLeaf Resource a
_) =
[Dec] -> f [Dec]
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
go ([Piece a]
accPieces, [(Name, Int)]
parentConstructors) (ResourceParent [Char]
name Bool
_check Set [Char]
_attrs [Piece a]
pieces [ResourceTree a]
children) = do
let accDynTypes :: [a]
accDynTypes = [a
t | Dynamic a
t <- [Piece a]
accPieces]
accDynVars <- (a -> f Name) -> [a] -> f [Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (\a
_ -> [Char] -> f Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"parent") [a]
accDynTypes
let piecesDynTypes = [a
t | Dynamic a
t <- [Piece a]
pieces]
piecesDynVars <- mapM (\a
_ -> [Char] -> f Name
forall (m :: * -> *). Quote m => [Char] -> m Name
newName [Char]
"piece") piecesDynTypes
child <- newName "child"
let allParentDynVars = [Name]
accDynVars [Name] -> [Name] -> [Name]
forall a. [a] -> [a] -> [a]
++ [Name]
piecesDynVars
let applyConToParentArgs = [(Name, Int)] -> Name -> [Name] -> Exp -> Exp
buildRouteExpr [(Name, Int)]
parentConstructors ([Char] -> Name
mkName [Char]
name) [Name]
allParentDynVars (Name -> Exp
VarE Name
child)
let toParentRouteD =
Name -> [Clause] -> Dec
FunD 'toParentRoute
[ [Pat] -> Body -> [Dec] -> Clause
Clause
[[Name] -> Pat
parentArgsPat [Name]
allParentDynVars, Name -> Pat
VarP Name
child]
(Exp -> Body
NormalB Exp
applyConToParentArgs)
[]
]
let thisInstance =
[Type] -> Type -> [Dec] -> Dec
instanceD [Type]
cxt (Name -> Type
ConT ''ToParentRoute Type -> Type -> Type
`AppT` [Char] -> Type
applyTypeVariables [Char]
name) [Dec
toParentRouteD]
let thisPieceCount = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
piecesDynTypes
acc' =
( [Piece a]
accPieces [Piece a] -> [Piece a] -> [Piece a]
forall a. Semigroup a => a -> a -> a
<> [Piece a]
pieces
, [(Name, Int)]
parentConstructors [(Name, Int)] -> [(Name, Int)] -> [(Name, Int)]
forall a. [a] -> [a] -> [a]
++ [([Char] -> Name
mkName [Char]
name, Int
thisPieceCount)]
)
childrenInstances <- mconcat <$> mapM (go acc') children
pure $ thisInstance : childrenInstances
applyTypeVariables :: [Char] -> Type
applyTypeVariables [Char]
name =
Type -> TyArgs -> Type
applyTyArgs (Name -> Type
ConT ([Char] -> Name
mkName [Char]
name)) TyArgs
origTyargs
buildRouteExpr :: [(Name, Int)] -> Name -> [Name] -> Exp -> Exp
buildRouteExpr :: [(Name, Int)] -> Name -> [Name] -> Exp -> Exp
buildRouteExpr [(Name, Int)]
parentCtors Name
thisCtor [Name]
allDynVars Exp
childExpr =
let
thisPieceCount :: Int
thisPieceCount = [Name] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Name]
allDynVars Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((Name, Int) -> Int) -> [(Name, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Int) -> Int
forall a b. (a, b) -> b
snd [(Name, Int)]
parentCtors)
allCtors :: [(Name, Int)]
allCtors = [(Name, Int)]
parentCtors [(Name, Int)] -> [(Name, Int)] -> [(Name, Int)]
forall a. [a] -> [a] -> [a]
++ [(Name
thisCtor, Int
thisPieceCount)]
partitionedVars :: [[Name]]
partitionedVars = [Name] -> [(Name, Int)] -> [[Name]]
forall {a} {a}. [a] -> [(a, Int)] -> [[a]]
go' [Name]
allDynVars [(Name, Int)]
allCtors
where
go' :: [a] -> [(a, Int)] -> [[a]]
go' [a]
_ [] = []
go' [a]
vars ((a
_, Int
count) : [(a, Int)]
rest) =
let ([a]
varsForThis, [a]
remaining) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
count [a]
vars
in [a]
varsForThis [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> [(a, Int)] -> [[a]]
go' [a]
remaining [(a, Int)]
rest
finalExpr :: Exp
finalExpr = ((Name, [Name]) -> Exp -> Exp) -> Exp -> [(Name, [Name])] -> Exp
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\(Name
ctorName, [Name]
varsForThis) Exp
innerExpr ->
(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
ConE Name
ctorName) ((Name -> Exp) -> [Name] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Name -> Exp
VarE [Name]
varsForThis [Exp] -> [Exp] -> [Exp]
forall a. [a] -> [a] -> [a]
++ [Exp
innerExpr]))
Exp
childExpr
([Name] -> [[Name]] -> [(Name, [Name])]
forall a b. [a] -> [b] -> [(a, b)]
zip (((Name, Int) -> Name) -> [(Name, Int)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, Int) -> Name
forall a b. (a, b) -> a
fst [(Name, Int)]
allCtors) [[Name]]
partitionedVars)
in Exp
finalExpr
notStrict :: Bang
notStrict :: Bang
notStrict = SourceUnpackedness -> SourceStrictness -> Bang
Bang SourceUnpackedness
NoSourceUnpackedness SourceStrictness
NoSourceStrictness
mkRenderRouteNestedInstanceD
:: Cxt
-> Type
-> Type
-> Type
-> [Clause]
-> Dec
mkRenderRouteNestedInstanceD :: [Type] -> Type -> Type -> Type -> [Clause] -> Dec
mkRenderRouteNestedInstanceD [Type]
instCxt Type
childRouteType Type
parentSiteRHS Type
parentArgsT [Clause]
clauses =
[Type] -> Type -> [Dec] -> Dec
instanceD
[Type]
instCxt
(Name -> Type
ConT ''RenderRouteNested Type -> Type -> Type
`AppT` Type
childRouteType)
[ TySynEqn -> Dec
TySynInstD (TySynEqn -> Dec) -> TySynEqn -> Dec
forall a b. (a -> b) -> a -> b
$ Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TySynEqn Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing (Name -> Type
ConT ''ParentSite Type -> Type -> Type
`AppT` Type
childRouteType) Type
parentSiteRHS
, TySynEqn -> Dec
TySynInstD (TySynEqn -> Dec) -> TySynEqn -> Dec
forall a b. (a -> b) -> a -> b
$ Maybe [TyVarBndr ()] -> Type -> Type -> TySynEqn
TySynEqn Maybe [TyVarBndr ()]
forall a. Maybe a
Nothing (Name -> Type
ConT ''ParentArgs Type -> Type -> Type
`AppT` Type
childRouteType) Type
parentArgsT
, Name -> [Clause] -> Dec
FunD 'renderRouteNested [Clause]
clauses
]
nestedChildDataAndInstance
:: RouteOpts
-> Cxt
-> Cxt
-> TyArgs
-> Type
-> Name
-> [Con]
-> [Piece Type]
-> [ResourceTree Type]
-> Q [Dec]
nestedChildDataAndInstance :: RouteOpts
-> [Type]
-> [Type]
-> TyArgs
-> Type
-> Name
-> [Con]
-> [Piece Type]
-> [ResourceTree Type]
-> Q [Dec]
nestedChildDataAndInstance RouteOpts
opts [Type]
instCxt [Type]
derivCxt TyArgs
tyargs Type
parentSiteRHS Name
childDataName [Con]
cons [Piece Type]
accPieces [ResourceTree Type]
children = do
let subrouteDecTypeArgs :: [TyVarBndr BndrVis]
subrouteDecTypeArgs = (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
plainTVCompat (TyArgs -> [Name]
tyArgsBinders TyArgs
tyargs)
([DerivClause]
inlineDerives, Type -> [Dec]
mkStandaloneDerives) = RouteOpts -> [Type] -> ([DerivClause], Type -> [Dec])
getDerivesFor RouteOpts
opts [Type]
derivCxt
childData :: Dec
childData = [Type]
-> Name
-> [TyVarBndr BndrVis]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataD [] Name
childDataName [TyVarBndr BndrVis]
subrouteDecTypeArgs Maybe Type
forall a. Maybe a
Nothing [Con]
cons [DerivClause]
inlineDerives
childDataType :: Type
childDataType = Type -> TyArgs -> Type
applyTyArgs (Name -> Type
ConT Name
childDataName) TyArgs
tyargs
(parentDynT, childClauses) <- [Piece Type] -> [ResourceTree Type] -> Q (Type, [Clause])
renderRouteNestedBody [Piece Type]
accPieces [ResourceTree Type]
children
let childInstance =
[Type] -> Type -> Type -> Type -> [Clause] -> Dec
mkRenderRouteNestedInstanceD [Type]
instCxt Type
childDataType Type
parentSiteRHS Type
parentDynT [Clause]
childClauses
pure (childData : childInstance : mkStandaloneDerives childDataType)
mkRenderRouteNestedInstanceOpts
:: RouteOpts
-> Cxt
-> TyArgs
-> Type
-> [Piece Type]
-> String
-> [ResourceTree Type]
-> Q [Dec]
mkRenderRouteNestedInstanceOpts :: RouteOpts
-> [Type]
-> TyArgs
-> Type
-> [Piece Type]
-> [Char]
-> [ResourceTree Type]
-> Q [Dec]
mkRenderRouteNestedInstanceOpts RouteOpts
routeOpts [Type]
cxt TyArgs
tyargs Type
typ [Piece Type]
prepieces [Char]
target [ResourceTree Type]
ress = do
(cons, childDecs) <- [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkFocusedChildCons [Piece Type]
prepieces [ResourceTree Type]
ress
let targetName = [Char] -> Name
mkName [Char]
target
targetDecs =
RouteOpts
-> [Type]
-> [Type]
-> TyArgs
-> Type
-> Name
-> [Con]
-> [Piece Type]
-> [ResourceTree Type]
-> Q [Dec]
nestedChildDataAndInstance
RouteOpts
routeOpts [Type]
cxt [Type]
childCxt TyArgs
tyargs Type
typ Name
targetName [Con]
cons [Piece Type]
prepieces [ResourceTree Type]
ress
(<> childDecs) <$> targetDecs
where
childCxt :: [Type]
childCxt = TyArgs -> [Type] -> [Type]
childDerivCxt TyArgs
tyargs [Type]
cxt
mkFocusedChildCons :: [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkFocusedChildCons :: [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkFocusedChildCons [Piece Type]
prePieces [ResourceTree Type]
trees =
[([Con], [Dec])] -> ([Con], [Dec])
forall a. Monoid a => [a] -> a
mconcat ([([Con], [Dec])] -> ([Con], [Dec]))
-> Q [([Con], [Dec])] -> Q ([Con], [Dec])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ResourceTree Type -> Q ([Con], [Dec]))
-> [ResourceTree Type] -> Q [([Con], [Dec])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkFocusedChildCon [Piece Type]
prePieces) [ResourceTree Type]
trees
mkFocusedChildCon :: [Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkFocusedChildCon :: [Piece Type] -> ResourceTree Type -> Q ([Con], [Dec])
mkFocusedChildCon [Piece Type]
_ (ResourceLeaf Resource Type
res) =
([Con], [Dec]) -> Q ([Con], [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Resource Type -> Con
leafRouteCon Resource Type
res], [])
mkFocusedChildCon [Piece Type]
prePieces (ResourceParent [Char]
name Bool
_check Set [Char]
_attrs [Piece Type]
pieces [ResourceTree Type]
children) = do
let accumulatedPieces :: [Piece Type]
accumulatedPieces = [Piece Type]
prePieces [Piece Type] -> [Piece Type] -> [Piece Type]
forall a. Semigroup a => a -> a -> a
<> [Piece Type]
pieces
(cons, decs) <- [Piece Type] -> [ResourceTree Type] -> Q ([Con], [Dec])
mkFocusedChildCons [Piece Type]
accumulatedPieces [ResourceTree Type]
children
let childDataName = [Char] -> Name
mkName [Char]
name
mname' <- lookupTypeName name
mdec <- case mname' of
Just Name
_ -> Maybe [Dec] -> Q (Maybe [Dec])
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [Dec]
forall a. Maybe a
Nothing
Maybe Name
Nothing ->
[Dec] -> Maybe [Dec]
forall a. a -> Maybe a
Just ([Dec] -> Maybe [Dec]) -> Q [Dec] -> Q (Maybe [Dec])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
RouteOpts
-> [Type]
-> [Type]
-> TyArgs
-> Type
-> Name
-> [Con]
-> [Piece Type]
-> [ResourceTree Type]
-> Q [Dec]
nestedChildDataAndInstance
RouteOpts
routeOpts [Type]
cxt [Type]
childCxt TyArgs
tyargs Type
typ Name
childDataName [Con]
cons
[Piece Type]
accumulatedPieces [ResourceTree Type]
children
return ([parentRouteCon name pieces tyargs], maybe id (<>) mdec decs)