on master help sign in
Have ReadShow describe what it's trying to read
commit444bf051c43ae18b7a540edbb8a9c36d7f2e65a7 authorOllie Charles <ollie@ocharles.org.uk> dateWed 7 Apr 2021, 11:02 (6 years ago) parent926e5d4
1 file changed+11−3
src/Rel8/Type/ReadShow.hs+11−3
src/Rel8/Type/ReadShow.hs+11−3
@@ -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