-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derive Generic instances for non-generic newtype keys #990
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ module Database.Persist.TH | |
, mpsPrefixFields | ||
, mpsEntityJSON | ||
, mpsGenerateLenses | ||
, mpsDeriveInstances | ||
, EntityJSON(..) | ||
, mkPersistSettings | ||
, sqlSettings | ||
|
@@ -435,6 +436,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 | ||
|
@@ -457,6 +464,7 @@ mkPersistSettings t = MkPersistSettings | |
, entityFromJSON = 'entityIdFromJSON | ||
} | ||
, mpsGenerateLenses = False | ||
, mpsDeriveInstances = [] | ||
} | ||
|
||
-- | Use the 'SqlPersist' backend. | ||
|
@@ -492,7 +500,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 | ||
#if MIN_VERSION_template_haskell(2,12,0) | ||
DataD [] nameFinal paramsFinal | ||
Nothing | ||
|
@@ -792,14 +802,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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Point for future upgrade: remove the unnecessary instances here like PathPiece, ToHttpApiData, etc that don't make sense for non-web-apps. |
||
if customKeyType | ||
then return ([], allInstances) | ||
else do | ||
|
@@ -902,6 +912,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel like these somewhat awkward |
||
|
||
keyIdName :: EntityDef -> Name | ||
keyIdName = mkName . unpack . keyIdText | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE ExistentialQuantification #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
|
@@ -22,13 +23,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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. love it, awesome. |
||
Person json | ||
name Text | ||
age Int Maybe | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh hell yes