Andrew Myers | 8 Nov 03:38 2013

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:

    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?

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)

(Continue reading)

Andrew | 8 Nov 15:09 2013

Re: Help using Groundhog Template Haskell.

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"