lykahb / groundhog

This library maps datatypes to a relational model, in a way similar to what ORM libraries do in OOP. See the tutorial https://www.schoolofhaskell.com/user/lykahb/groundhog for introduction
http://hackage.haskell.org/package/groundhog
176 stars 39 forks source link

UUID PrimitivePersistField #87

Closed rsoeldner closed 5 years ago

rsoeldner commented 5 years ago

Using the groundhog-postgresql backend and struggle with this setup, pattern matching fromPrimitivePersistValue (PersistText d) will fail and I do not understand why.

import qualified Data.UUID as UUID
newtype DeviceId = DeviceId { unDeviceId :: UUID.UUID } deriving Show

instance PrimitivePersistField DeviceId where
  toPrimitivePersistValue d = PersistText . UUID.toText $ unDeviceId d
  fromPrimitivePersistValue (PersistText d) = case UUID.fromText d of
    Just uuid -> DeviceId uuid
    Nothing -> error "not able to parse"

instance PersistField DeviceId where
  persistName _ = "UUID"
  toPersistValues = primToPersistValue
  fromPersistValues = primFromPersistValue
  dbType _ _ = DbTypePrimitive (DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
lykahb commented 5 years ago

Likely the instance receives a binary string. What the function receives may depend on the database engine. Try rewriting it as

  fromPrimitivePersistValue d = case UUID.fromText (fromPrimitivePersistValue d) of
    Just uuid -> DeviceId uuid
    Nothing -> error "not able to parse"