Help using Groundhog Template Haskell.

Hi Cafe, I'm trying to experiment with groundhog because I want something that I can map between arbitrary ADTs and arbitrary Schema. Groundhog looks like exactly what I want but I'm hung up on an error coming from the mkPersist TH function. When I compile the code below with ghc --make groundhog.hs I get this error: groundhog.hs:23:1: Function binding for `Database.Groundhog.Core.entityFieldChain' has no equations When splicing a TH declaration: I'm pretty sure it has something to do with the TransactionType not being made up of primitive types but I'm not sure what to do about it. Can someone familiar with the library point me in the right direction? Thanks, Andrew Myers {-# LANGUAGE TemplateHaskell, QuasiQuotes #-} module Checking.Database.Groundhog where import qualified Database.Groundhog.TH as TH import qualified Data.Text as T import qualified Data.Time as D import qualified Control.Lens as L data TransactionType = Deposit | Debt | Informational deriving (Show, Eq, Read) data Transaction = Transaction { _transactionDate :: D.UTCTime , _transactionAmount :: Double , _transactionDescription :: T.Text , _transactionBalanced :: Bool , _transactionCheckNumber :: Maybe Int , _transactionTransactionType :: TransactionType } deriving (Show, Read, Eq) TH.mkPersist TH.defaultCodegenConfig [TH.groundhog| - entity: Transaction constructors: - name: Transaction fields: - name: _transactionDate dbName: date exprName: DateField - name: _transactionAmount dbName: amount exprName: AmountField - name: _transactionDescription dbName: description exprName: DescriptionField - name: _transactionBalanced dbName: balanced exprName: BalancedField - name: _transactionCheckNumber dbName: check_number exprName: CheckNumberField - name: _transactionTransactionType dbName: transaction_type exprName: TransactionTypeField - entity: TransactionType |]

I seem to have worked around this by hand rolling instances for TransactionType as below. I would still like to understand how to make this work with mkPersist though. instance G.PersistField TransactionType where persistName _ = "TransactionType" toPersistValues = GG.primToPersistValue fromPersistValues = GG.primFromPersistValue dbType _ = G.DbTypePrimitive G.DbString False Nothing Nothing instance G.PrimitivePersistField TransactionType where toPrimitivePersistValue _ a = G.PersistString (show a) fromPrimitivePersistValue _ (G.PersistString s) = read s fromPrimitivePersistValue _ _ = error "Expected PersistString"
participants (2)
-
Andrew
-
Andrew Myers