@@ -1,8 +1,14 @@
1+{-# language ScopedTypeVariables #-}
2+{-# language TypeApplications #-}
3+{-# language ViewPatterns #-}
4+
15module Rel8.Type.ReadShow ( ReadShow(..) ) where
26
37-- base
8+import Data.Proxy ( Proxy( Proxy ) )
9+import Data.Typeable ( Typeable, typeRep )
410import Prelude
5−import Text.Read ( readEither )
11+import Text.Read ( readMaybe )
612
713-- rel8
814import Rel8.Type ( DBType( typeInformation ) )
@@ -17,8 +23,10 @@ import qualified Data.Text as Text
1723newtype ReadShow a = ReadShow { fromReadShow :: a }
1824
1925
20−instance (Read a, Show a) => DBType (ReadShow a) where
26+instance (Read a, Show a, Typeable a) => DBType (ReadShow a) where
2127 typeInformation = parseTypeInformation parser printer typeInformation
2228 where
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))
2432 printer = Text.pack . show . fromReadShow