on master help sign in
Remove `out` from `TypeInformation`

Fixes #22

commit0857ffbb2b1941aac5deb1e03ac7d9025a4bb4a3 authorOllie Charles <ollie@ocharles.org.uk> dateFri 2 Apr 2021, 19:06 (6 years ago) parent2a36dbe
6 files changed+19−44
src/Rel8/Expr/Serialize.hs+3−3 src/Rel8/Type.hs−17 src/Rel8/Type/Array.hs+3−4 src/Rel8/Type/Array1D.hs+3−4 src/Rel8/Type/Information.hs+10−15 src/Rel8/Type/JSONBEncoded.hs−1
src/Rel8/Expr/Serialize.hs+3−3
@@ -44,6 +44,6 @@ slitExpr nullability info@TypeInformation {encode} =
4444
4545
4646sparseValue :: Nullability a -> TypeInformation (Unnullify a) -> Hasql.Row a
47−sparseValue nullability TypeInformation {decode, out} = case nullability of
48− Nullable -> Hasql.column $ Hasql.nullable $ out <$> decode
49− NonNullable -> Hasql.column $ Hasql.nonNullable $ out <$> decode
47+sparseValue nullability TypeInformation {decode} = case nullability of
48+ Nullable -> Hasql.column $ Hasql.nullable decode
49+ NonNullable -> Hasql.column $ Hasql.nonNullable decode
src/Rel8/Type.hs−17
@@ -102,7 +102,6 @@ instance DBType Bool where
102102 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
103103 , decode = Hasql.bool
104104 , typeName = "bool"
105− , out = id
106105 }
107106
108107
@@ -112,7 +111,6 @@ instance DBType Char where
112111 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
113112 , decode = Hasql.char
114113 , typeName = "char"
115− , out = id
116114 }
117115
118116
@@ -122,7 +120,6 @@ instance DBType Int16 where
122120 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
123121 , decode = Hasql.int2
124122 , typeName = "int2"
125− , out = id
126123 }
127124
128125
@@ -132,7 +129,6 @@ instance DBType Int32 where
132129 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
133130 , decode = Hasql.int4
134131 , typeName = "int4"
135− , out = id
136132 }
137133
138134
@@ -142,7 +138,6 @@ instance DBType Int64 where
142138 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
143139 , decode = Hasql.int8
144140 , typeName = "int8"
145− , out = id
146141 }
147142
148143
@@ -152,7 +147,6 @@ instance DBType Float where
152147 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
153148 , decode = Hasql.float4
154149 , typeName = "float4"
155− , out = id
156150 }
157151
158152
@@ -162,7 +156,6 @@ instance DBType Double where
162156 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
163157 , decode = Hasql.float8
164158 , typeName = "float8"
165− , out = id
166159 }
167160
168161
@@ -172,7 +165,6 @@ instance DBType Scientific where
172165 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
173166 , decode = Hasql.numeric
174167 , typeName = "numeric"
175− , out = id
176168 }
177169
178170
@@ -184,7 +176,6 @@ instance DBType UTCTime where
184176 formatTime defaultTimeLocale "'%FT%T%QZ'"
185177 , decode = Hasql.timestamptz
186178 , typeName = "timestamptz"
187− , out = id
188179 }
189180
190181
@@ -196,7 +187,6 @@ instance DBType Day where
196187 formatTime defaultTimeLocale "'%F'"
197188 , decode = Hasql.date
198189 , typeName = "date"
199− , out = id
200190 }
201191
202192
@@ -208,7 +198,6 @@ instance DBType LocalTime where
208198 formatTime defaultTimeLocale "'%FT%T%Q'"
209199 , decode = Hasql.timestamp
210200 , typeName = "timestamp"
211− , out = id
212201 }
213202
214203
@@ -220,7 +209,6 @@ instance DBType TimeOfDay where
220209 formatTime defaultTimeLocale "'%T%Q'"
221210 , decode = Hasql.time
222211 , typeName = "time"
223− , out = id
224212 }
225213
226214
@@ -232,7 +220,6 @@ instance DBType CalendarDiffTime where
232220 formatTime defaultTimeLocale "'%bmon %0Es'"
233221 , decode = CalendarDiffTime 0 . realToFrac <$> Hasql.interval
234222 , typeName = "interval"
235− , out = id
236223 }
237224
238225
@@ -242,7 +229,6 @@ instance DBType Text where
242229 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
243230 , decode = Hasql.text
244231 , typeName = "text"
245− , out = id
246232 }
247233
248234
@@ -272,7 +258,6 @@ instance DBType ByteString where
272258 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
273259 , decode = Hasql.bytea
274260 , typeName = "bytea"
275− , out = id
276261 }
277262
278263
@@ -289,7 +274,6 @@ instance DBType UUID where
289274 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
290275 , decode = Hasql.uuid
291276 , typeName = "uuid"
292− , out = id
293277 }
294278
295279
@@ -302,7 +286,6 @@ instance DBType Value where
302286 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
303287 , decode = Hasql.jsonb
304288 , typeName = "jsonb"
305− , out = id
306289 }
307290
308291
src/Rel8/Type/Array.hs+3−4
@@ -40,15 +40,14 @@ listTypeInformation :: ()
4040 -> TypeInformation [a]
4141listTypeInformation nullability info =
4242 case info of
43− TypeInformation{ encode, decode, out } -> TypeInformation
43+ TypeInformation{ encode, decode } -> TypeInformation
4444 { decode = row $ case nullability of
45− Nullable -> Hasql.listArray (Hasql.nullable (out <$> decode))
46− NonNullable -> Hasql.listArray (Hasql.nonNullable (out <$> decode))
45+ Nullable -> Hasql.listArray (Hasql.nullable decode)
46+ NonNullable -> Hasql.listArray (Hasql.nonNullable decode)
4747 , encode = case nullability of
4848 Nullable -> array info . fmap (maybe null encode)
4949 NonNullable -> array info . fmap encode
5050 , typeName = "record"
51− , out = id
5251 }
5352 where
5453 row = Hasql.composite . Hasql.field . Hasql.nonNullable
src/Rel8/Type/Array1D.hs+3−4
@@ -98,15 +98,14 @@ array1DTypeInformation :: IsArray1D (Unnullify a) ~ 'False
9898 -> TypeInformation (Array1D a)
9999array1DTypeInformation nullability info =
100100 case info of
101− TypeInformation{ encode, decode, typeName, out } -> TypeInformation
101+ TypeInformation{ encode, decode, typeName } -> TypeInformation
102102 { decode = case nullability of
103− Nullable -> Array1D <$> Hasql.listArray (Hasql.nullable (out <$> decode))
104− NonNullable -> Array1D <$> Hasql.listArray (Hasql.nonNullable (out <$> decode))
103+ Nullable -> Array1D <$> Hasql.listArray (Hasql.nullable decode)
104+ NonNullable -> Array1D <$> Hasql.listArray (Hasql.nonNullable decode)
105105 , encode = case nullability of
106106 Nullable -> Opaleye.ArrayExpr . fmap (maybe null encode) . getArray1D
107107 NonNullable -> Opaleye.ArrayExpr . fmap encode . getArray1D
108108 , typeName = typeName <> "[]"
109− , out = id
110109 }
111110 where
112111 null = Opaleye.ConstExpr Opaleye.NullLit
src/Rel8/Type/Information.hs+10−15
@@ -28,18 +28,14 @@ import qualified Data.Text as Text
2828-- from database queries. The @typeName@ is the name of the type in the
2929-- database, which is used to accurately type literals.
3030type TypeInformation :: Type -> Type
31−data TypeInformation a where
32− TypeInformation ::
33− { encode :: a -> Opaleye.PrimExpr
34− -- ^ How to encode a single Haskell value as a SQL expression.
35− , decode :: Hasql.Value x
36− -- ^ How to deserialize a single result back to Haskell.
37− , typeName :: String
38− -- ^ The name of the SQL type.
39− , out :: x -> a
40− -- ^ A final output function - usually this will just be 'id'. This is
41− -- needed to allow @TypeInformation@s to be coerced.
42− } -> TypeInformation a
31+data TypeInformation a = TypeInformation
32+ { encode :: a -> Opaleye.PrimExpr
33+ -- ^ How to encode a single Haskell value as a SQL expression.
34+ , decode :: Hasql.Value a
35+ -- ^ How to deserialize a single result back to Haskell.
36+ , typeName :: String
37+ -- ^ The name of the SQL type.
38+ }
4339
4440
4541-- | Simultaneously map over how a type is both encoded and decoded, while
@@ -81,10 +77,9 @@ mapTypeInformation = parseTypeInformation . fmap pure
8177parseTypeInformation :: ()
8278 => (a -> Either String b) -> (b -> a)
8379 -> TypeInformation a -> TypeInformation b
84−parseTypeInformation to from TypeInformation {encode, decode, typeName, out} =
80+parseTypeInformation to from TypeInformation {encode, decode, typeName} =
8581 TypeInformation
8682 { encode = encode . from
87− , decode = Hasql.refine (first Text.pack . to) (out <$> decode)
83+ , decode = Hasql.refine (first Text.pack . to) decode
8884 , typeName
89− , out = id
9085 }
src/Rel8/Type/JSONBEncoded.hs−1
@@ -28,5 +28,4 @@ instance (FromJSON a, ToJSON a) => DBType (JSONBEncoded a) where
2828 { encode = encode typeInformation . toJSON . fromJSONBEncoded
2929 , decode = Hasql.refine (first pack . fmap JSONBEncoded . parseEither parseJSON) Hasql.jsonb
3030 , typeName = "jsonb"
31− , out = id
3231 }