@@ -7,29 +7,33 @@ module Database.PostgreSQL.Query.TH.Enum
7
7
, GeneratorOptions (.. )
8
8
) where
9
9
10
- import Prelude
10
+ import Prelude
11
11
12
- import Data.Default
13
- import Database.PostgreSQL.Simple.FromField
14
- import Database.PostgreSQL.Simple.ToField
15
- import Language.Haskell.TH
16
- import Text.Inflections
12
+ import Data.Default
13
+ import Data.FileEmbed
14
+ import Database.PostgreSQL.Simple.FromField
15
+ import Database.PostgreSQL.Simple.ToField
16
+ import GHC.Generics (Generic )
17
+ import Language.Haskell.TH
18
+ import Text.Inflections
17
19
18
- import qualified Data.ByteString.Char8 as C8
20
+ import qualified Data.Text.Encoding as T
21
+ import qualified Data.Text as T
19
22
20
23
-- | Function to transform constructor name into its PG enum conterpart.
21
24
-- It should have type 'String -> String'
22
- type InflectorFunc = Name
25
+ type InflectorFunc = String -> String
23
26
24
27
type Typ = Name
25
28
26
29
-- | Option record to pass to the TH generators.
27
30
data GeneratorOptions
28
31
= GeneratorOptions
29
- { inflectorFunc :: InflectorFunc }
32
+ { inflectorFunc :: InflectorFunc
33
+ } deriving (Generic )
30
34
31
35
instance Default GeneratorOptions where
32
- def = GeneratorOptions ' toUnderscore
36
+ def = GeneratorOptions toUnderscore
33
37
34
38
{-| derives 'FromField' and 'ToField' instances for a sum-type enum like
35
39
@@ -122,15 +126,15 @@ withEnumConstructor :: InflectorFunc
122
126
-> (Name -> ExpQ -> Q a )
123
127
-- ^ callback function from:
124
128
-- 1. haskell constructor name and
125
- -- 2. PG enum option
129
+ -- 2. PG enum option (ByteString)
126
130
-> Con
127
131
-- ^ constructor to decompose
128
132
-> Q a
129
133
withEnumConstructor i f = \ case
130
134
(NormalC _ (_: _)) ->
131
135
error " constructors with arguments are not supported in makeToFieldClause"
132
136
(NormalC nam [] ) -> f nam inflectedBs
133
- where inflected = appE (varE i) (litE (stringL ( nameBase nam)))
134
- inflectedBs = [ | C8. pack $ inflected | ]
137
+ where inflectedT = T. pack $ i $ nameBase nam
138
+ inflectedBs = bsToExp $ T. encodeUtf8 inflectedT
135
139
_ ->
136
140
error " unsupported constructor in makeFromFieldClause"
0 commit comments