Skip to content

Commit fa9c19a

Browse files
authored
Merge pull request #53 from haskellari/pr-46-const-identity-instances
Add To/FromField instances for Const
2 parents 265a519 + 2c0e6a5 commit fa9c19a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
33
{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
44
{-# LANGUAGE RecordWildCards #-}
5+
{-# LANGUAGE PolyKinds #-}
56

67
{- |
78
Module: 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, (*>), (<*) )
119120
import Control.Concurrent.MVar (MVar, newMVar)
120121
import Control.Exception (Exception)
121122
import qualified Data.Aeson as JSON
@@ -124,6 +125,7 @@ import qualified Data.Aeson.Parser as JSON (value')
124125
import Data.Attoparsec.ByteString.Char8 hiding (Result)
125126
import Data.ByteString (ByteString)
126127
import qualified Data.ByteString.Char8 as B
128+
import Data.Functor.Identity (Identity(Identity))
127129
import Data.Int (Int16, Int32, Int64)
128130
import Data.IORef (IORef, newIORef)
129131
import 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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
2527
import qualified Data.Aeson as JSON
2628
import Data.ByteString (ByteString)
2729
import 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))
3336
import Data.Int (Int8, Int16, Int32, Int64)
3437
import Data.List (intersperse)
3538
import 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+
103114
instance (ToField a) => ToField (Maybe a) where
104115
toField Nothing = renderNull
105116
toField (Just a) = toField a

0 commit comments

Comments
 (0)