-
Notifications
You must be signed in to change notification settings - Fork 301
Open
Description
Should support for UUIDs be built in to persistent
or should it be its own package? FYI, the own package version might look something like
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Instances where
import Control.Error.Util
import Data.UUID (UUID)
import qualified Data.UUID as UUID
import qualified Data.ByteString.Lazy as BSL
import Database.Persist.Sql
import Web.HttpApiData
instance PersistField UUID where
toPersistValue = PersistDbSpecific . BSL.toStrict . UUID.toByteString
fromPersistValue (PersistDbSpecific t) =
case UUID.fromByteString $ BSL.fromStrict t of
Just x -> Right x
Nothing -> Left "Invalid UUID"
fromPersistValue _ = Left "Not PersistDBSpecific"
instance PersistFieldSql UUID where
sqlType _ = SqlOther "uuid"
instance PathPiece UUID where
toPathPiece = toUrlPiece
fromPathPiece = hush . parseUrlPiece
instance ToHttpApiData UUID where
toUrlPiece = UUID.toText
instance FromHttpApiData UUID where
parseUrlPiece uuid = note ("Error parsing UUID " <> uuid) . UUID.fromText $ uuid
instance ToJSON UUID where
toJSON = toJSON . UUID.toText
instance FromJSON UUID where
parseJSON uuid = maybe (fail $ "Error parsing UUID " <> show uuid) pure . UUID.fromText <=< parseJSON $ uuid
fabianhjr and xiaoshihou514
Metadata
Metadata
Assignees
Labels
No labels