module Hasql.Decoders.Array where

import Hasql.Prelude
import PostgreSQL.Binary.Decoding qualified as A

newtype Array a
  = Array (ReaderT Bool A.Array a)
  deriving ((forall a b. (a -> b) -> Array a -> Array b)
-> (forall a b. a -> Array b -> Array a) -> Functor Array
forall a b. a -> Array b -> Array a
forall a b. (a -> b) -> Array a -> Array b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Array a -> Array b
fmap :: forall a b. (a -> b) -> Array a -> Array b
$c<$ :: forall a b. a -> Array b -> Array a
<$ :: forall a b. a -> Array b -> Array a
Functor)

{-# INLINE run #-}
run :: Array a -> Bool -> A.Value a
run :: forall a. Array a -> Bool -> Value a
run (Array ReaderT Bool Array a
imp) Bool
env =
  Array a -> Value a
forall a. Array a -> Value a
A.array (ReaderT Bool Array a -> Bool -> Array a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT Bool Array a
imp Bool
env)

{-# INLINE dimension #-}
dimension :: (forall m. (Monad m) => Int -> m a -> m b) -> Array a -> Array b
dimension :: forall a b.
(forall (m :: * -> *). Monad m => Int -> m a -> m b)
-> Array a -> Array b
dimension forall (m :: * -> *). Monad m => Int -> m a -> m b
replicateM (Array ReaderT Bool Array a
imp) =
  ReaderT Bool Array b -> Array b
forall a. ReaderT Bool Array a -> Array a
Array (ReaderT Bool Array b -> Array b)
-> ReaderT Bool Array b -> Array b
forall a b. (a -> b) -> a -> b
$ (Bool -> Array b) -> ReaderT Bool Array b
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Bool -> Array b) -> ReaderT Bool Array b)
-> (Bool -> Array b) -> ReaderT Bool Array b
forall a b. (a -> b) -> a -> b
$ \Bool
env -> (forall (m :: * -> *). Monad m => Int -> m a -> m b)
-> Array a -> Array b
forall a b.
(forall (m :: * -> *). Monad m => Int -> m a -> m b)
-> Array a -> Array b
A.dimensionArray Int -> m a -> m b
forall (m :: * -> *). Monad m => Int -> m a -> m b
replicateM (ReaderT Bool Array a -> Bool -> Array a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT Bool Array a
imp Bool
env)

{-# INLINE value #-}
value :: (Bool -> A.Value a) -> Array (Maybe a)
value :: forall a. (Bool -> Value a) -> Array (Maybe a)
value Bool -> Value a
decoder' =
  ReaderT Bool Array (Maybe a) -> Array (Maybe a)
forall a. ReaderT Bool Array a -> Array a
Array (ReaderT Bool Array (Maybe a) -> Array (Maybe a))
-> ReaderT Bool Array (Maybe a) -> Array (Maybe a)
forall a b. (a -> b) -> a -> b
$ (Bool -> Array (Maybe a)) -> ReaderT Bool Array (Maybe a)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Bool -> Array (Maybe a)) -> ReaderT Bool Array (Maybe a))
-> (Bool -> Array (Maybe a)) -> ReaderT Bool Array (Maybe a)
forall a b. (a -> b) -> a -> b
$ Value a -> Array (Maybe a)
forall a. Value a -> Array (Maybe a)
A.nullableValueArray (Value a -> Array (Maybe a))
-> (Bool -> Value a) -> Bool -> Array (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> Value a
decoder'

{-# INLINE nonNullValue #-}
nonNullValue :: (Bool -> A.Value a) -> Array a
nonNullValue :: forall a. (Bool -> Value a) -> Array a
nonNullValue Bool -> Value a
decoder' =
  ReaderT Bool Array a -> Array a
forall a. ReaderT Bool Array a -> Array a
Array (ReaderT Bool Array a -> Array a)
-> ReaderT Bool Array a -> Array a
forall a b. (a -> b) -> a -> b
$ (Bool -> Array a) -> ReaderT Bool Array a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Bool -> Array a) -> ReaderT Bool Array a)
-> (Bool -> Array a) -> ReaderT Bool Array a
forall a b. (a -> b) -> a -> b
$ Value a -> Array a
forall a. Value a -> Array a
A.valueArray (Value a -> Array a) -> (Bool -> Value a) -> Bool -> Array a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> Value a
decoder'