Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.7.4

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

## 2.7.3

* Update module documentation for `Database.Persist.TH` to better describe the purpose of the module [#968](https://github.com/yesodweb/persistent/pull/968)
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 @@ -26,6 +26,7 @@ module Database.Persist.TH
, mpsPrefixFields
, mpsEntityJSON
, mpsGenerateLenses
, mpsDeriveInstances
, EntityJSON(..)
, mkPersistSettings
, sqlSettings
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh hell yes

}

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

-- | Use the 'SqlPersist' backend.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like these somewhat awkward elem dances could be made easier by a gentle use of Set


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.7.3
version: 2.7.4
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 All @@ -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|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it, awesome.

Person json
name Text
age Int Maybe
Expand Down