2{-# language LambdaCase #-}
3{-# language NamedFieldPuns #-}
4{-# language OverloadedStrings #-}
5{-# language ViewPatterns #-}
8 ( array, encodeArrayElement
10 , nonEmptyTypeInformation
15import Data.Foldable ( toList )
16import Data.List.NonEmpty ( NonEmpty, nonEmpty )
17import Prelude hiding ( null, repeat, zipWith )
20import qualified Hasql.Decoders as Hasql
23import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
26import Rel8.Schema.Null ( Unnullify, Nullity( Null, NotNull ) )
27import Rel8.Type.Information ( TypeInformation(..), parseTypeInformation )
31 => TypeInformation a -> f Opaleye.PrimExpr -> Opaleye.PrimExpr
33 Opaleye.CastExpr (arrayType info <> "[]") .
34 Opaleye.ArrayExpr . map (encodeArrayElement info) . toList
35{-# INLINABLE array #-}
38listTypeInformation :: ()
40 -> TypeInformation (Unnullify a)
41 -> TypeInformation [a]
42listTypeInformation nullity info@TypeInformation {encode, decode} =
44 { decode = case nullity of
46 Hasql.listArray (decodeArrayElement info (Hasql.nullable decode))
48 Hasql.listArray (decodeArrayElement info (Hasql.nonNullable decode))
49 , encode = case nullity of
52 fmap (encodeArrayElement info . maybe null encode)
55 fmap (encodeArrayElement info . encode)
56 , typeName = arrayType info <> "[]"
59 null = Opaleye.ConstExpr Opaleye.NullLit
62nonEmptyTypeInformation :: ()
64 -> TypeInformation (Unnullify a)
65 -> TypeInformation (NonEmpty a)
66nonEmptyTypeInformation nullity =
67 parseTypeInformation parse toList . listTypeInformation nullity
69 parse = maybe (Left message) Right . nonEmpty
70 message = "failed to decode NonEmptyList: got empty list"
73isArray :: TypeInformation a -> Bool
75 (reverse . typeName -> ']' : '[' : _) -> True
79arrayType :: TypeInformation a -> String
81 | isArray info = "record"
82 | otherwise = typeName info
85decodeArrayElement :: TypeInformation a -> Hasql.NullableOrNot Hasql.Value x -> Hasql.NullableOrNot Hasql.Value x
86decodeArrayElement info
87 | isArray info = Hasql.nonNullable . Hasql.composite . Hasql.field
91encodeArrayElement :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
92encodeArrayElement info
93 | isArray info = Opaleye.UnExpr (Opaleye.UnOpOther "ROW")
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