Skip to content

Commit

Permalink
Merge pull request #1664 from RyanGlScott/master
Browse files Browse the repository at this point in the history
Use DeriveLift to generate yesod-core's Lift instances
  • Loading branch information
snoyberg committed Mar 31, 2020
2 parents 59f601a + 29a0842 commit 7f37d2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
5 changes: 5 additions & 0 deletions yesod-core/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

* Add functions for setting description and OG meta [#1663](https://github.com/yesodweb/yesod/pull/1663)

* Use `DeriveLift` to implement the `Lift` instances for `ResourceTree`,
`Resource`, `Piece`, and `Dispatch`. Among other benefits, this provides
implementations of `liftTyped` on `template-haskell-2.16` (GHC 8.10) or
later. [#1664](https://github.com/yesodweb/yesod/pull/1664)

## 1.6.17.3

* Support for `unliftio-core` 0.2
Expand Down
26 changes: 5 additions & 21 deletions yesod-core/src/Yesod/Routes/TH/Types.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveLift #-}
-- | Warning! This module is considered internal and may have breaking changes
module Yesod.Routes.TH.Types
( -- * Data types
Expand All @@ -21,7 +21,7 @@ import Language.Haskell.TH.Syntax
data ResourceTree typ
= ResourceLeaf (Resource typ)
| ResourceParent String CheckOverlap [Piece typ] [ResourceTree typ]
deriving (Show, Functor)
deriving (Lift, Show, Functor)

resourceTreePieces :: ResourceTree typ -> [Piece typ]
resourceTreePieces (ResourceLeaf r) = resourcePieces r
Expand All @@ -31,35 +31,24 @@ resourceTreeName :: ResourceTree typ -> String
resourceTreeName (ResourceLeaf r) = resourceName r
resourceTreeName (ResourceParent x _ _ _) = x

instance Lift t => Lift (ResourceTree t) where
lift (ResourceLeaf r) = [|ResourceLeaf $(lift r)|]
lift (ResourceParent a b c d) = [|ResourceParent $(lift a) $(lift b) $(lift c) $(lift d)|]

data Resource typ = Resource
{ resourceName :: String
, resourcePieces :: [Piece typ]
, resourceDispatch :: Dispatch typ
, resourceAttrs :: [String]
, resourceCheck :: CheckOverlap
}
deriving (Show, Functor)
deriving (Lift, Show, Functor)

type CheckOverlap = Bool

instance Lift t => Lift (Resource t) where
lift (Resource a b c d e) = [|Resource a b c d e|]

data Piece typ = Static String | Dynamic typ
deriving Show
deriving (Lift, Show)

instance Functor Piece where
fmap _ (Static s) = Static s
fmap f (Dynamic t) = Dynamic (f t)

instance Lift t => Lift (Piece t) where
lift (Static s) = [|Static $(lift s)|]
lift (Dynamic t) = [|Dynamic $(lift t)|]

data Dispatch typ =
Methods
{ methodsMulti :: Maybe typ -- ^ type of the multi piece at the end
Expand All @@ -69,17 +58,12 @@ data Dispatch typ =
{ subsiteType :: typ
, subsiteFunc :: String
}
deriving Show
deriving (Lift, Show)

instance Functor Dispatch where
fmap f (Methods a b) = Methods (fmap f a) b
fmap f (Subsite a b) = Subsite (f a) b

instance Lift t => Lift (Dispatch t) where
lift (Methods Nothing b) = [|Methods Nothing $(lift b)|]
lift (Methods (Just t) b) = [|Methods (Just $(lift t)) $(lift b)|]
lift (Subsite t b) = [|Subsite $(lift t) $(lift b)|]

resourceMulti :: Resource typ -> Maybe typ
resourceMulti Resource { resourceDispatch = Methods (Just t) _ } = Just t
resourceMulti _ = Nothing
Expand Down

0 comments on commit 7f37d2b

Please sign in to comment.