@@ -0,0 +1,43 @@
1+module Rel8.Type.ReadShow ( ReadShow(..) ) where
5+import Text.Read ( readEither )
8+import Rel8.Type ( DBType( typeInformation ) )
9+import Rel8.Type.Information ( parseTypeInformation )
12+import qualified Data.Text as Text
15+-- | A deriving-via helper type for column types that store a Haskell value
16+-- using a Haskell's 'Read' and 'Show' type classes.
21+-- data Color = Red | Green | Blue
22+-- deriving (Read, Show)
23+-- deriving PrimitiveType via ReadShow Color
26+-- will allow you to store @Color@ values in a single SQL column (stored as
28+newtype ReadShow a = ReadShow { fromReadShow :: a }
31+-- | The 'PrimitiveType' instance for 'ReadShow' allows you to serialize a type using
32+-- Haskell's 'Read' and 'Show' instances:
35+-- data Color = Red | Green | Blue
36+-- deriving (Read, Show)
37+-- deriving PrimitiveType via ReadShow Color
39+instance (Read a, Show a) => DBType (ReadShow a) where
40+ typeInformation = parseTypeInformation parser printer typeInformation
42+ parser = fmap ReadShow . readEither . Text.unpack
43+ printer = Text.pack . show . fromReadShow