1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language FlexibleInstances #-}
5{-# language ScopedTypeVariables #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeApplications #-}
8{-# language TypeOperators #-}
9{-# language UndecidableInstances #-}
17import Data.Kind ( Constraint, Type )
18import Data.Proxy ( Proxy( Proxy ) )
20 ( Generic, Rep, from, to
21 , (:+:)( L1, R1 ), M1( M1 ), U1( U1 ), Meta( MetaCons )
24import GHC.TypeLits ( KnownSymbol, Symbol, symbolVal )
25import Prelude hiding ( Enum )
28import qualified Hasql.Decoders as Hasql
31import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
34import Rel8.Type ( DBType, typeInformation )
35import Rel8.Type.Information ( TypeInformation(..) )
38import Data.Text ( unpack )
41-- | A deriving-via helper type for column types that store an \"enum\" type
42-- (in Haskell terms, a sum type where all constructors are nullary) using a
43-- Postgres @enum@ type.
45-- Note that this must map to a specific type in your database's schema
46-- (explicitly created with @CREATE TYPE ... AS ENUM@). The @name@ parameter
47-- of @Enum@ should be the name of this type. Note also the names of the
48-- Postgres @enum@'s values are assumed to match exactly the names of the
49-- constructors of the Haskell type (up to and including case sensitivity).
50type Enum :: Symbol -> Type -> Type
51newtype Enum name a = Enum
56instance (KnownSymbol name, Generic a, GEnum (Rep a)) => DBType (Enum name a)
58 typeInformation = TypeInformation
59 { decode = Hasql.enum ((`lookup` (fmap (Enum . to) <$> gdecode)) . unpack)
60 , encode = Opaleye.ConstExpr . Opaleye.StringLit . gencode . from . unEnum
61 , typeName = symbolVal (Proxy @name)
65type GEnum :: (Type -> Type) -> Constraint
67 gdecode :: [(String, rep x)]
68 gencode :: rep x -> String
71instance GEnum rep => GEnum (M1 D c rep) where
72 gdecode = fmap M1 <$> gdecode
73 gencode (M1 a) = gencode a
76instance (GEnum a, GEnum b) => GEnum (a :+: b) where
77 gdecode = (fmap L1 <$> gdecode) <> (fmap R1 <$> gdecode)
78 gencode (L1 a) = gencode a
79 gencode (R1 a) = gencode a
82instance (KnownSymbol name, a ~ U1) =>
83 GEnum (M1 C ('MetaCons name f r) a)
85 gdecode = [(symbolVal (Proxy @name), M1 U1)]
86 gencode _ = symbolVal (Proxy @name)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list