git.iko.soy/typeable/postgresql-simple
on master help sign in
Merge pull request #53 from haskellari/pr-46-const-identity-instances
fa9c19aOleg Grenrus
CHANGES.md+4−1
@@ -2,10 +2,13 @@
2
2
3 * Add `fromFieldJSONByteString`
3 * Add `fromFieldJSONByteString`
4 Thanks to tomjaguarpaw for the implementation
4 Thanks to tomjaguarpaw for the implementation
5− https://github.com/lpsmith/postgresql-simple/pull/222
5+ https://github.com/haskellari/postgresql-simple/pull/47
6 * Add `attoFieldParser`
6 * Add `attoFieldParser`
7 Thanks to Victor Nawothnig for the implementation
7 Thanks to Victor Nawothnig for the implementation
8 https://github.com/haskellari/postgresql-simple/pull/45
8 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
9
12
10### Version 0.6.2 (2019-04-26)
13### Version 0.6.2 (2019-04-26)
11
14
src/Database/PostgreSQL/Simple/FromField.hs+9−1
@@ -2,6 +2,7 @@
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE PolyKinds #-}
5
6
6{- |
7{- |
7Module: Database.PostgreSQL.Simple.FromField
8Module: Database.PostgreSQL.Simple.FromField
@@ -115,7 +116,7 @@ module Database.PostgreSQL.Simple.FromField
115
116
116#include "MachDeps.h"
117#include "MachDeps.h"
117
118
118−import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
119+import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
119import Control.Concurrent.MVar (MVar, newMVar)
120import Control.Concurrent.MVar (MVar, newMVar)
120import Control.Exception (Exception)
121import Control.Exception (Exception)
121import qualified Data.Aeson as JSON
122import qualified Data.Aeson as JSON
@@ -124,6 +125,7 @@ import qualified Data.Aeson.Parser as JSON (value')
124import Data.Attoparsec.ByteString.Char8 hiding (Result)
125import Data.Attoparsec.ByteString.Char8 hiding (Result)
125import Data.ByteString (ByteString)
126import Data.ByteString (ByteString)
126import qualified Data.ByteString.Char8 as B
127import qualified Data.ByteString.Char8 as B
128+import Data.Functor.Identity (Identity(Identity))
127import Data.Int (Int16, Int32, Int64)
129import Data.Int (Int16, Int32, Int64)
128import Data.IORef (IORef, newIORef)
130import Data.IORef (IORef, newIORef)
129import Data.Ratio (Ratio)
131import Data.Ratio (Ratio)
@@ -269,6 +271,12 @@ instance FromField () where
269 | typeOid f /= TI.voidOid = returnError Incompatible f ""
271 | typeOid f /= TI.voidOid = returnError Incompatible f ""
270 | otherwise = pure ()
272 | otherwise = pure ()
271
273
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+
272-- | For dealing with null values. Compatible with any postgresql type
280-- | For dealing with null values. Compatible with any postgresql type
273-- compatible with type @a@. Note that the type is not checked if
281-- compatible with type @a@. Note that the type is not checked if
274-- the value is null, although it is inadvisable to rely on this
282-- the value is null, although it is inadvisable to rely on this
src/Database/PostgreSQL/Simple/ToField.hs+11
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3+{-# LANGUAGE PolyKinds #-}
3
4
4------------------------------------------------------------------------------
5------------------------------------------------------------------------------
5-- |
6-- |
@@ -22,6 +23,7 @@ module Database.PostgreSQL.Simple.ToField
22 , inQuotes
23 , inQuotes
23 ) where
24 ) where
24
25
26+import Control.Applicative (Const(Const))
25import qualified Data.Aeson as JSON
27import qualified Data.Aeson as JSON
26import Data.ByteString (ByteString)
28import Data.ByteString (ByteString)
27import Data.ByteString.Builder
29import Data.ByteString.Builder
@@ -30,6 +32,7 @@ import Data.ByteString.Builder
30 , wordDec, word8Dec, word16Dec, word32Dec, word64Dec
32 , wordDec, word8Dec, word16Dec, word32Dec, word64Dec
31 , floatDec, doubleDec
33 , floatDec, doubleDec
32 )
34 )
35+import Data.Functor.Identity (Identity(Identity))
33import Data.Int (Int8, Int16, Int32, Int64)
36import Data.Int (Int8, Int16, Int32, Int64)
34import Data.List (intersperse)
37import Data.List (intersperse)
35import Data.Monoid (mappend)
38import Data.Monoid (mappend)
@@ -100,6 +103,14 @@ instance ToField Action where
100 toField a = a
103 toField a = a
101 {-# INLINE toField #-}
104 {-# INLINE toField #-}
102
105
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+
103instance (ToField a) => ToField (Maybe a) where
114instance (ToField a) => ToField (Maybe a) where
104 toField Nothing = renderNull
115 toField Nothing = renderNull
105 toField (Just a) = toField a
116 toField (Just a) = toField a