2{-# language LambdaCase #-}
3{-# language NamedFieldPuns #-}
4{-# language OverloadedStrings #-}
5{-# language ViewPatterns #-}
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 (wrap info) . toList
35{-# INLINABLE array #-}
38listTypeInformation :: ()
40 -> TypeInformation (Unnullify a)
41 -> TypeInformation [a]
42listTypeInformation nullity info@TypeInformation {encode, decode} =
44 { decode = case nullity of
45 Null -> Hasql.listArray (Hasql.nullable (unwrap info decode))
46 NotNull -> Hasql.listArray (Hasql.nonNullable (unwrap info decode))
47 , encode = case nullity of
48 Null -> Opaleye.ArrayExpr . fmap (wrap info . maybe null encode)
49 NotNull -> Opaleye.ArrayExpr . fmap (wrap info . encode)
50 , typeName = arrayType info <> "[]"
53 null = Opaleye.ConstExpr Opaleye.NullLit
56nonEmptyTypeInformation :: ()
58 -> TypeInformation (Unnullify a)
59 -> TypeInformation (NonEmpty a)
60nonEmptyTypeInformation nullity =
61 parseTypeInformation parse toList . listTypeInformation nullity
63 parse = maybe (Left message) Right . nonEmpty
64 message = "failed to decode NonEmptyList: got empty list"
67isArray :: TypeInformation a -> Bool
69 (reverse . typeName -> ']' : '[' : _) -> True
73arrayType :: TypeInformation a -> String
75 | isArray info = "record"
76 | otherwise = typeName info
79wrap :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
81 | isArray info = Opaleye.UnExpr (Opaleye.UnOpOther "ROW")
85unwrap :: TypeInformation a -> Hasql.Value x -> Hasql.Value x
87 | isArray info = Hasql.composite . Hasql.field . Hasql.nonNullable
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