From fc6cf6169868e60c189e4b243330c3717ff159f3 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 26 May 2022 13:58:07 +0200 Subject: ... --- overlays/spm/server/Data/UUID/Instances.hs | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 overlays/spm/server/Data/UUID/Instances.hs (limited to 'overlays/spm/server/Data/UUID') diff --git a/overlays/spm/server/Data/UUID/Instances.hs b/overlays/spm/server/Data/UUID/Instances.hs new file mode 100644 index 00000000..b2268c96 --- /dev/null +++ b/overlays/spm/server/Data/UUID/Instances.hs @@ -0,0 +1,31 @@ +{-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE OverloadedStrings #-} + +module Data.UUID.Instances () where + +import Prelude +import Database.Persist +import Database.Persist.Sql +import Data.UUID (UUID) +import qualified Data.UUID as UUID + +import qualified Data.ByteString.Char8 as CBS +import qualified Data.Text as Text + +import Web.PathPieces + + +instance PersistField UUID where + toPersistValue = PersistLiteralEscaped . CBS.pack . UUID.toString + fromPersistValue (PersistLiteralEscaped uuidB8) = + case UUID.fromString $ CBS.unpack uuidB8 of + Just uuid -> Right uuid + Nothing -> Left "Invalid UUID" + fromPersistValue v = Left $ "Expected PersistLiteral but got ‘" <> Text.pack (show v) <> "’" + +instance PersistFieldSql UUID where + sqlType _ = SqlOther "uuid" + +instance PathPiece UUID where + toPathPiece = Text.pack . UUID.toString + fromPathPiece = UUID.fromString . Text.unpack -- cgit v1.2.3