@@ -1,8 +1,14 @@
1+{-# language ScopedTypeVariables #-}
2+{-# language TypeApplications #-}
3+{-# language ViewPatterns #-}
1module Rel8.Type.ReadShow ( ReadShow(..) ) where
5module Rel8.Type.ReadShow ( ReadShow(..) ) where
8+import Data.Proxy ( Proxy( Proxy ) )
9+import Data.Typeable ( Typeable, typeRep )
4import Prelude
10import Prelude
5−import Text.Read ( readEither )
11+import Text.Read ( readMaybe )
8import Rel8.Type ( DBType( typeInformation ) )
14import Rel8.Type ( DBType( typeInformation ) )
@@ -17,8 +23,10 @@ import qualified Data.Text as Text
17newtype ReadShow a = ReadShow { fromReadShow :: a }
23newtype ReadShow a = ReadShow { fromReadShow :: a }
20−instance (Read a, Show a) => DBType (ReadShow a) where
26+instance (Read a, Show a, Typeable a) => DBType (ReadShow a) where
21 typeInformation = parseTypeInformation parser printer typeInformation
27 typeInformation = parseTypeInformation parser printer typeInformation
23− parser = fmap ReadShow . readEither . Text.unpack
29+ parser (Text.unpack -> t) = case readMaybe t of
30+ Just ok -> Right $ ReadShow ok
31+ Nothing -> Left $ "Could not read " <> t <> " as a " <> show (typeRep (Proxy @a))
24 printer = Text.pack . show . fromReadShow
32 printer = Text.pack . show . fromReadShow