Skip to content

Add To/FromField instances for Const #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

* Add `fromFieldJSONByteString`
Thanks to tomjaguarpaw for the implementation
https://github.com/lpsmith/postgresql-simple/pull/222
https://github.com/haskellari/postgresql-simple/pull/47
* Add `attoFieldParser`
Thanks to Victor Nawothnig for the implementation
https://github.com/haskellari/postgresql-simple/pull/45
* Add `Identity` and `Const` instance
Thanks to Cary Robbins for the implementation
https://github.com/haskellari/postgresql-simple/pull/46

### Version 0.6.2 (2019-04-26)

Expand Down
10 changes: 9 additions & 1 deletion src/Database/PostgreSQL/Simple/FromField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PolyKinds #-}

{- |
Module: Database.PostgreSQL.Simple.FromField
Expand Down Expand Up @@ -115,7 +116,7 @@ module Database.PostgreSQL.Simple.FromField

#include "MachDeps.h"

import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
import Control.Applicative ( Const(Const), (<|>), (<$>), pure, (*>), (<*) )
import Control.Concurrent.MVar (MVar, newMVar)
import Control.Exception (Exception)
import qualified Data.Aeson as JSON
Expand All @@ -124,6 +125,7 @@ import qualified Data.Aeson.Parser as JSON (value')
import Data.Attoparsec.ByteString.Char8 hiding (Result)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Functor.Identity (Identity(Identity))
import Data.Int (Int16, Int32, Int64)
import Data.IORef (IORef, newIORef)
import Data.Ratio (Ratio)
Expand Down Expand Up @@ -269,6 +271,12 @@ instance FromField () where
| typeOid f /= TI.voidOid = returnError Incompatible f ""
| otherwise = pure ()

instance (FromField a) => FromField (Const a b) where
fromField f bs = Const <$> fromField f bs

instance (FromField a) => FromField (Identity a) where
fromField f bs = Identity <$> fromField f bs

-- | For dealing with null values. Compatible with any postgresql type
-- compatible with type @a@. Note that the type is not checked if
-- the value is null, although it is inadvisable to rely on this
Expand Down
11 changes: 11 additions & 0 deletions src/Database/PostgreSQL/Simple/ToField.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE PolyKinds #-}

------------------------------------------------------------------------------
-- |
Expand All @@ -22,6 +23,7 @@ module Database.PostgreSQL.Simple.ToField
, inQuotes
) where

import Control.Applicative (Const(Const))
import qualified Data.Aeson as JSON
import Data.ByteString (ByteString)
import Data.ByteString.Builder
Expand All @@ -30,6 +32,7 @@ import Data.ByteString.Builder
, wordDec, word8Dec, word16Dec, word32Dec, word64Dec
, floatDec, doubleDec
)
import Data.Functor.Identity (Identity(Identity))
import Data.Int (Int8, Int16, Int32, Int64)
import Data.List (intersperse)
import Data.Monoid (mappend)
Expand Down Expand Up @@ -100,6 +103,14 @@ instance ToField Action where
toField a = a
{-# INLINE toField #-}

instance (ToField a) => ToField (Const a b) where
toField (Const a) = toField a
{-# INLINE toField #-}

instance (ToField a) => ToField (Identity a) where
toField (Identity a) = toField a
{-# INLINE toField #-}

instance (ToField a) => ToField (Maybe a) where
toField Nothing = renderNull
toField (Just a) = toField a
Expand Down