git.iko.soy/typeable/postgresql-simple
on master help sign in
Add To/FromField instances for Const
commit2c0e6a5c656299af5d1457c7bbd74c5a50e23f21 authorCary Robbins <carymrobbins@gmail.com> dateFri 7 Aug 2020, 08:27 (6 years ago) parent265a519
3 files changed+24−2
CHANGES.md+4−1 src/Database/PostgreSQL/Simple/FromField.hs+9−1 src/Database/PostgreSQL/Simple/ToField.hs+11
CHANGES.md+4−1
@@ -2,10 +2,13 @@
22
33 * Add `fromFieldJSONByteString`
44 Thanks to tomjaguarpaw for the implementation
5− https://github.com/lpsmith/postgresql-simple/pull/222
5+ https://github.com/haskellari/postgresql-simple/pull/47
66 * Add `attoFieldParser`
77 Thanks to Victor Nawothnig for the implementation
88 https://github.com/haskellari/postgresql-simple/pull/45
9+ * Add `Identity` and `Const` instance
10+ Thanks to Cary Robbins for the implementation
11+ https://github.com/haskellari/postgresql-simple/pull/46
912
1013### Version 0.6.2 (2019-04-26)
1114
src/Database/PostgreSQL/Simple/FromField.hs+9−1
@@ -2,6 +2,7 @@
22{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
33{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
44{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE PolyKinds #-}
56
67{- |
78Module: Database.PostgreSQL.Simple.FromField
@@ -115,7 +116,7 @@ module Database.PostgreSQL.Simple.FromField
115116
116117#include "MachDeps.h"
117118
118−import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
119+import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
119120import Control.Concurrent.MVar (MVar, newMVar)
120121import Control.Exception (Exception)
121122import qualified Data.Aeson as JSON
@@ -124,6 +125,7 @@ import qualified Data.Aeson.Parser as JSON (value')
124125import Data.Attoparsec.ByteString.Char8 hiding (Result)
125126import Data.ByteString (ByteString)
126127import qualified Data.ByteString.Char8 as B
128+import Data.Functor.Identity (Identity(Identity))
127129import Data.Int (Int16, Int32, Int64)
128130import Data.IORef (IORef, newIORef)
129131import Data.Ratio (Ratio)
@@ -269,6 +271,12 @@ instance FromField () where
269271 | typeOid f /= TI.voidOid = returnError Incompatible f ""
270272 | otherwise = pure ()
271273
274+instance (FromField a) => FromField (Const a b) where
275+ fromField f bs = Const <$> fromField f bs
276+
277+instance (FromField a) => FromField (Identity a) where
278+ fromField f bs = Identity <$> fromField f bs
279+
272280-- | For dealing with null values. Compatible with any postgresql type
273281-- compatible with type @a@. Note that the type is not checked if
274282-- the value is null, although it is inadvisable to rely on this
src/Database/PostgreSQL/Simple/ToField.hs+11
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
22{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3+{-# LANGUAGE PolyKinds #-}
34
45------------------------------------------------------------------------------
56-- |
@@ -22,6 +23,7 @@ module Database.PostgreSQL.Simple.ToField
2223 , inQuotes
2324 ) where
2425
26+import Control.Applicative (Const(Const))
2527import qualified Data.Aeson as JSON
2628import Data.ByteString (ByteString)
2729import Data.ByteString.Builder
@@ -30,6 +32,7 @@ import Data.ByteString.Builder
3032 , wordDec, word8Dec, word16Dec, word32Dec, word64Dec
3133 , floatDec, doubleDec
3234 )
35+import Data.Functor.Identity (Identity(Identity))
3336import Data.Int (Int8, Int16, Int32, Int64)
3437import Data.List (intersperse)
3538import Data.Monoid (mappend)
@@ -100,6 +103,14 @@ instance ToField Action where
100103 toField a = a
101104 {-# INLINE toField #-}
102105
106+instance (ToField a) => ToField (Const a b) where
107+ toField (Const a) = toField a
108+ {-# INLINE toField #-}
109+
110+instance (ToField a) => ToField (Identity a) where
111+ toField (Identity a) = toField a
112+ {-# INLINE toField #-}
113+
103114instance (ToField a) => ToField (Maybe a) where
104115 toField Nothing = renderNull
105116 toField (Just a) = toField a