Skip to content

Commit

Permalink
Optionally derive PathMultiPiece for composite keys (#1509)
Browse files Browse the repository at this point in the history
* Move derivation strategy choice to `where` clause

* Create PersistPathMultiPiece typeclass

* Create spec and derive PersistPathMultiPiece instance in dataTypeDec

* Revert changes to mkKeyTypeDec

This reverts part of commit 500feba.

* Fix and test instance derivation using mpsDeriveInstances

This reverts part of commit 7b9e76f.

* Move PersistPathMultiPiece into PersistEntity module

This avoids creating an orphan instance of PathMultiPiece.

* Document feature in Database.Persist.Quasi

* Add top-level `describe` to CompositeKeyPathMultiPieceSpec

* Only derive PathMultiPiece for composite keys

As mentioned in the MkPersistSettings Haddock change, specifying
PersistPathMultiPiece directly overrides this behaviour.

* Fix PersistPathMultiPiece override and add tests for it

* Use DerivingVia with newtype wrapper

This reverts commit 5bc5799.

* Only use DerivingVia on GHC versions with support for it

* Add CPP language pragma to spec

* Revert changes to Database.Persist.TH and remove associated spec
  • Loading branch information
blujupiter32 authored Oct 3, 2023
1 parent 7a05b48 commit 6ec4de1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions persistent/Database/Persist/Class/PersistEntity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Database.Persist.Class.PersistEntity
, FilterValue (..)
, BackendSpecificFilter
, Entity (.., Entity, entityKey, entityVal)
, ViaPersistEntity (..)

, recordName
, entityValues
Expand Down Expand Up @@ -56,6 +57,7 @@ import Data.Aeson.Text (encodeToTextBuilder)
import Data.Aeson.Types (Parser, Result(Error, Success))
import Data.Attoparsec.ByteString (parseOnly)
import Data.Functor.Identity
import Web.PathPieces (PathMultiPiece(..), PathPiece(..))

#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.KeyMap as AM
Expand Down Expand Up @@ -180,6 +182,18 @@ class ( PersistField (Key record), ToJSON (Key record), FromJSON (Key record)
keyFromRecordM :: Maybe (record -> Key record)
keyFromRecordM = Nothing

-- | Newtype wrapper for optionally deriving typeclass instances on
-- 'PersistEntity' keys.
--
-- @since 2.14.6.0
newtype ViaPersistEntity record = ViaPersistEntity (Key record)

instance PersistEntity record => PathMultiPiece (ViaPersistEntity record) where
fromPathMultiPiece pieces = do
Right key <- keyFromValues <$> mapM fromPathPiece pieces
pure $ ViaPersistEntity key
toPathMultiPiece (ViaPersistEntity key) = map toPathPiece $ keyToValues key

-- | Construct an @'Entity' record@ by providing a value for each of the
-- record's fields.
--
Expand Down

0 comments on commit 6ec4de1

Please sign in to comment.