git.iko.soy/typeable/postgresql-simple
on master help sign in
Mention GHC generics support in documentation (#6)

* Mention GHC generics support in documentation

commitb02bcf76e02ddd2890ce0b9ac04467063a8b9c3d authorGabriel Gonzalez <Gabriel439@gmail.com> dateThu 7 Feb 2019, 02:57 (8 years ago) parent466c469
2 files changed+33
src/Database/PostgreSQL/Simple/FromRow.hs+17 src/Database/PostgreSQL/Simple/ToRow.hs+16
src/Database/PostgreSQL/Simple/FromRow.hs+17
@@ -67,6 +67,23 @@ import GHC.Generics
6767-- in a single row of the query result. Otherwise, a 'ConversionFailed'
6868-- exception will be thrown.
6969--
70+-- You can also derive 'FromRow' for your data type using GHC generics, like
71+-- this:
72+--
73+-- @
74+-- \{-# LANGUAGE DeriveAnyClass \#-}
75+-- \{-# LANGUAGE DeriveGeneric \#-}
76+--
77+-- import "GHC.Generics" ('GHC.Generics.Generic')
78+-- import "Database.PostgreSQL.Simple" ('FromRow')
79+--
80+-- data User = User { name :: String, fileQuota :: Int }
81+-- deriving ('GHC.Generics.Generic', 'FromRow')
82+-- @
83+--
84+-- Note that this only works for product types (e.g. records) and does not
85+-- support sum types or recursive types.
86+--
7087-- Note that 'field' evaluates its result to WHNF, so the caveats listed in
7188-- mysql-simple and very early versions of postgresql-simple no longer apply.
7289-- Instead, look at the caveats associated with user-defined implementations
src/Database/PostgreSQL/Simple/ToRow.hs+16
@@ -30,6 +30,22 @@ import GHC.Generics
3030--
3131-- Instances should use the 'toField' method of the 'ToField' class
3232-- to perform conversion of each element of the collection.
33+--
34+-- You can derive 'ToRow' for your data type using GHC generics, like this:
35+--
36+-- @
37+-- \{-# LANGUAGE DeriveAnyClass \#-}
38+-- \{-# LANGUAGE DeriveGeneric \#-}
39+--
40+-- import "GHC.Generics" ('GHC.Generics.Generic')
41+-- import "Database.PostgreSQL.Simple" ('ToRow')
42+--
43+-- data User = User { name :: String, fileQuota :: Int }
44+-- deriving ('GHC.Generics.Generic', 'ToRow')
45+-- @
46+--
47+-- Note that this only works for product types (e.g. records) and does not
48+-- support sum types or recursive types.
3349class ToRow a where
3450 toRow :: a -> [Action]
3551 default toRow :: (Generic a, GToRow (Rep a)) => a -> [Action]