Skip to content

Commit 2563d5a

Browse files
Remove Enumable super-class from DBEnum (#374)
We still use `Enumable` in the signatures of the default implementation of the `DBEnum` class. We also add a method `enumerate` to that class to do away with the need for the superclass.
1 parent 5f77751 commit 2563d5a

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### Changed
2+
3+
- Some changes were made to the `DBEnum` type class:
4+
5+
* `Enumable` was removed as a superclass constraint. It is still used to provide the default implementation of the `DBEnum` class.
6+
* A new method, `enumerate`, was added to the `DBEnum` class (with the default implementation provided by `Enumable`).
7+
8+
This is unlikely to break any existing `DBEnum` instances, it just allows some instances that weren't possible before (e.g., for types that are not `Generic`).

src/Rel8/Type/Enum.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# language AllowAmbiguousTypes #-}
22
{-# language DataKinds #-}
3+
{-# language DefaultSignatures #-}
34
{-# language FlexibleContexts #-}
45
{-# language FlexibleInstances #-}
56
{-# language LambdaCase #-}
@@ -13,7 +14,7 @@
1314

1415
module Rel8.Type.Enum
1516
( Enum( Enum )
16-
, DBEnum( enumValue, enumTypeName )
17+
, DBEnum( enumValue, enumTypeName, enumerate )
1718
, Enumable
1819
)
1920
where
@@ -71,7 +72,7 @@ instance DBEnum a => DBType (Enum a) where
7172
typeInformation = TypeInformation
7273
{ decode =
7374
let
74-
mapping = (pack . enumValue &&& Enum) . to <$> genumerate @(Rep a)
75+
mapping = (pack . enumValue &&& Enum) <$> enumerate
7576
unrecognised = Left "enum: unrecognised value"
7677
in
7778
Decoder
@@ -107,16 +108,24 @@ instance DBEnum a => DBMin (Enum a)
107108

108109
-- | @DBEnum@ contains the necessary metadata to describe a PostgreSQL @enum@ type.
109110
type DBEnum :: Type -> Constraint
110-
class (DBType a, Enumable a) => DBEnum a where
111+
class DBType a => DBEnum a where
111112
-- | Map Haskell values to the corresponding element of the @enum@ type. The
112113
-- default implementation of this method will use the exact name of the
113114
-- Haskell constructors.
114115
enumValue :: a -> String
115-
enumValue = gshow @(Rep a) . from
116116

117117
-- | The name of the PostgreSQL @enum@ type that @a@ maps to.
118118
enumTypeName :: QualifiedName
119119

120+
-- | List of all possible values of the enum type.
121+
enumerate :: [a]
122+
123+
default enumValue :: Enumable a => a -> String
124+
enumValue = gshow @(Rep a) . from
125+
126+
default enumerate :: Enumable a => [a]
127+
enumerate = to <$> genumerate @(Rep a)
128+
120129

121130
-- | Types that are sum types, where each constructor is unary (that is, has no
122131
-- fields).

0 commit comments

Comments
 (0)