Skip to content

Commit

Permalink
Allow the user to provide instances that will be derived for all types (
Browse files Browse the repository at this point in the history
#990)

Co-authored-by: Matt Parsons <parsonsmatt@gmail.com>
  • Loading branch information
jessekempf and parsonsmatt committed Jan 28, 2020
1 parent 6665283 commit 0cb5c45
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions persistent-template/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased changes

## 2.8.1

* Let the user pass instances that will be derived for record and for key types (https://github.com/yesodweb/persistent/pull/990

## 2.8.0.1

* Small optimization/code cleanup to generated Template Haskell code size, by slimming the implementation of to/fromPersistValue for Entities. [#1014](https://github.com/yesodweb/persistent/pull/1014)
Expand Down
22 changes: 17 additions & 5 deletions persistent-template/Database/Persist/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Database.Persist.TH
, mpsPrefixFields
, mpsEntityJSON
, mpsGenerateLenses
, mpsDeriveInstances
, EntityJSON(..)
, mkPersistSettings
, sqlSettings
Expand Down Expand Up @@ -445,6 +446,12 @@ data MkPersistSettings = MkPersistSettings
-- Default: False
--
-- @since 1.3.1
, mpsDeriveInstances :: ![Name]
-- ^ Automatically derive these typeclass instances for all record and key types.
--
-- Default: []
--
-- @since 2.7.4
}

data EntityJSON = EntityJSON
Expand All @@ -467,6 +474,7 @@ mkPersistSettings t = MkPersistSettings
, entityFromJSON = 'entityIdFromJSON
}
, mpsGenerateLenses = False
, mpsDeriveInstances = []
}

-- | Use the 'SqlPersist' backend.
Expand Down Expand Up @@ -502,7 +510,9 @@ upperFirst t =

dataTypeDec :: MkPersistSettings -> EntityDef -> Q Dec
dataTypeDec mps t = do
let names = map (mkName . unpack) $ entityDerives t
let entityInstances = map (mkName . unpack) $ entityDerives t
additionalInstances = filter (`notElem` entityInstances) $ mpsDeriveInstances mps
names = entityInstances <> additionalInstances
DataD [] nameFinal paramsFinal
Nothing
constrs
Expand Down Expand Up @@ -791,14 +801,14 @@ mkKeyTypeDec mps t = do
if mpsGeneric mps
then if not useNewtype
then do pfDec <- pfInstD
return (pfDec, [''Generic])
return (pfDec, supplement [''Generic])
else do gi <- genericNewtypeInstances
return (gi, [])
return (gi, supplement [])
else if not useNewtype
then do pfDec <- pfInstD
return (pfDec, [''Show, ''Read, ''Eq, ''Ord, ''Generic])
return (pfDec, supplement [''Show, ''Read, ''Eq, ''Ord, ''Generic])
else do
let allInstances = [''Show, ''Read, ''Eq, ''Ord, ''PathPiece, ''ToHttpApiData, ''FromHttpApiData, ''PersistField, ''PersistFieldSql, ''ToJSON, ''FromJSON]
let allInstances = supplement [''Show, ''Read, ''Eq, ''Ord, ''PathPiece, ''ToHttpApiData, ''FromHttpApiData, ''PersistField, ''PersistFieldSql, ''ToJSON, ''FromJSON]
if customKeyType
then return ([], allInstances)
else do
Expand Down Expand Up @@ -873,6 +883,8 @@ mkKeyTypeDec mps t = do
useNewtype = pkNewtype mps t
customKeyType = not (defaultIdType t) || not useNewtype || isJust (entityPrimary t)

supplement :: [Name] -> [Name]
supplement names = names <> (filter (`notElem` names) $ mpsDeriveInstances mps)

keyIdName :: EntityDef -> Name
keyIdName = mkName . unpack . keyIdText
Expand Down
2 changes: 1 addition & 1 deletion persistent-template/persistent-template.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-template
version: 2.8.0.1
version: 2.8.1
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down
4 changes: 3 additions & 1 deletion persistent-template/test/main.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
Expand Down Expand Up @@ -31,13 +32,14 @@ import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Gen (Gen)
import GHC.Generics (Generic)

import Database.Persist
import Database.Persist.TH
import TemplateTestImports


share [mkPersist sqlSettings { mpsGeneric = False }, mkDeleteCascade sqlSettings { mpsGeneric = False }] [persistUpperCase|
share [mkPersist sqlSettings { mpsGeneric = False, mpsDeriveInstances = [''Generic] }, mkDeleteCascade sqlSettings { mpsGeneric = False }] [persistUpperCase|
Person json
name Text
age Int Maybe
Expand Down

0 comments on commit 0cb5c45

Please sign in to comment.