on da76712 help sign in
rel8/src/Rel8/Type/ReadShow.hs
35 lines 1.1 kB history raw
1{-# language ScopedTypeVariables #-}
2{-# language StandaloneKindSignatures #-}
3{-# language TypeApplications #-}
4{-# language ViewPatterns #-}
6module Rel8.Type.ReadShow ( ReadShow(..) ) where
8-- base
9import Data.Kind ( Type )
10import Data.Proxy ( Proxy( Proxy ) )
11import Data.Typeable ( Typeable, typeRep )
12import Prelude
13import Text.Read ( readMaybe )
15-- rel8
16import Rel8.Type ( DBType( typeInformation ) )
17import Rel8.Type.Information ( parseTypeInformation )
19-- text
20import qualified Data.Text as Text
23-- | A deriving-via helper type for column types that store a Haskell value
24-- using a Haskell's 'Read' and 'Show' type classes.
25type ReadShow :: Type -> Type
26newtype ReadShow a = ReadShow { fromReadShow :: a }
29instance (Read a, Show a, Typeable a) => DBType (ReadShow a) where
30 typeInformation = parseTypeInformation parser printer typeInformation
31 where
32 parser (Text.unpack -> t) = case readMaybe t of
33 Just ok -> Right $ ReadShow ok
34 Nothing -> Left $ "Could not read " <> t <> " as a " <> show (typeRep (Proxy @a))
35 printer = Text.pack . show . fromReadShow