|
1 | 1 | {-# language AllowAmbiguousTypes #-} |
2 | 2 | {-# language DataKinds #-} |
| 3 | +{-# language DefaultSignatures #-} |
3 | 4 | {-# language FlexibleContexts #-} |
4 | 5 | {-# language FlexibleInstances #-} |
5 | 6 | {-# language LambdaCase #-} |
|
13 | 14 |
|
14 | 15 | module Rel8.Type.Enum |
15 | 16 | ( Enum( Enum ) |
16 | | - , DBEnum( enumValue, enumTypeName ) |
| 17 | + , DBEnum( enumValue, enumTypeName, enumerate ) |
17 | 18 | , Enumable |
18 | 19 | ) |
19 | 20 | where |
@@ -71,7 +72,7 @@ instance DBEnum a => DBType (Enum a) where |
71 | 72 | typeInformation = TypeInformation |
72 | 73 | { decode = |
73 | 74 | let |
74 | | - mapping = (pack . enumValue &&& Enum) . to <$> genumerate @(Rep a) |
| 75 | + mapping = (pack . enumValue &&& Enum) <$> enumerate |
75 | 76 | unrecognised = Left "enum: unrecognised value" |
76 | 77 | in |
77 | 78 | Decoder |
@@ -107,16 +108,24 @@ instance DBEnum a => DBMin (Enum a) |
107 | 108 |
|
108 | 109 | -- | @DBEnum@ contains the necessary metadata to describe a PostgreSQL @enum@ type. |
109 | 110 | type DBEnum :: Type -> Constraint |
110 | | -class (DBType a, Enumable a) => DBEnum a where |
| 111 | +class DBType a => DBEnum a where |
111 | 112 | -- | Map Haskell values to the corresponding element of the @enum@ type. The |
112 | 113 | -- default implementation of this method will use the exact name of the |
113 | 114 | -- Haskell constructors. |
114 | 115 | enumValue :: a -> String |
115 | | - enumValue = gshow @(Rep a) . from |
116 | 116 |
|
117 | 117 | -- | The name of the PostgreSQL @enum@ type that @a@ maps to. |
118 | 118 | enumTypeName :: QualifiedName |
119 | 119 |
|
| 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 | + |
120 | 129 |
|
121 | 130 | -- | Types that are sum types, where each constructor is unary (that is, has no |
122 | 131 | -- fields). |
|
0 commit comments