on 06f67cf help sign in
rel8/src/Rel8/Type/Information.hs
48 lines 1.1 kB history raw
1{-# language NamedFieldPuns #-}
2{-# language StandaloneKindSignatures #-}
4module Rel8.Type.Information
5 ( TypeInformation(..)
6 , mapTypeInformation
7 , parseTypeInformation
8 )
9where
11-- base
12import Data.Bifunctor ( first )
13import Data.Kind ( Type )
14import Prelude
16-- hasql
17import qualified Hasql.Decoders as Hasql
19-- opaleye
20import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
22-- text
23import qualified Data.Text as Text
26type TypeInformation :: Type -> Type
27data TypeInformation a = TypeInformation
28 { encode :: a -> Opaleye.PrimExpr
29 , decode :: Hasql.Value a
30 , typeName :: String
31 }
34mapTypeInformation :: ()
35 => (a -> b) -> (b -> a)
36 -> TypeInformation a -> TypeInformation b
37mapTypeInformation = parseTypeInformation . fmap pure
40parseTypeInformation :: ()
41 => (a -> Either String b) -> (b -> a)
42 -> TypeInformation a -> TypeInformation b
43parseTypeInformation to from TypeInformation {encode, decode, typeName} =
44 TypeInformation
45 { encode = encode . from
46 , decode = Hasql.refine (first Text.pack . to) decode
47 , typeName
48 }